Mercurial > kallithea
comparison rhodecode/lib/db_manage.py @ 2919:29630805893d beta
Implemented proposed changes from pull request #77
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Thu, 11 Oct 2012 20:56:01 +0200 |
parents | ce8572f45c85 |
children | d3200c58764e |
comparison
equal
deleted
inserted
replaced
2918:ce8572f45c85 | 2919:29630805893d |
---|---|
55 ml = len(msg) + (4 * 2) | 55 ml = len(msg) + (4 * 2) |
56 print >> sys.stdout, ('*** %s ***\n%s' % (msg, '*' * ml)).upper() | 56 print >> sys.stdout, ('*** %s ***\n%s' % (msg, '*' * ml)).upper() |
57 | 57 |
58 | 58 |
59 class DbManage(object): | 59 class DbManage(object): |
60 def __init__(self, log_sql, dbconf, root, tests=False): | 60 def __init__(self, log_sql, dbconf, root, tests=False, cli_args={}): |
61 self.dbname = dbconf.split('/')[-1] | 61 self.dbname = dbconf.split('/')[-1] |
62 self.tests = tests | 62 self.tests = tests |
63 self.root = root | 63 self.root = root |
64 self.dburi = dbconf | 64 self.dburi = dbconf |
65 self.log_sql = log_sql | 65 self.log_sql = log_sql |
66 self.db_exists = False | 66 self.db_exists = False |
67 self.cli_args = cli_args | |
67 self.init_db() | 68 self.init_db() |
69 global ask_ok | |
70 | |
71 if self.cli_args.get('force_ask') is True: | |
72 ask_ok = lambda *args, **kwargs: True | |
73 elif self.cli_args.get('force_ask') is False: | |
74 ask_ok = lambda *args, **kwargs: False | |
68 | 75 |
69 def init_db(self): | 76 def init_db(self): |
70 engine = create_engine(self.dburi, echo=self.log_sql) | 77 engine = create_engine(self.dburi, echo=self.log_sql) |
71 init_model(engine) | 78 init_model(engine) |
72 self.sa = Session() | 79 self.sa = Session() |
73 | 80 |
74 def create_tables(self, override=False, defaults={}): | 81 def create_tables(self, override=False): |
75 """ | 82 """ |
76 Create a auth database | 83 Create a auth database |
77 """ | 84 """ |
78 quiet = defaults.get('quiet') | 85 |
79 log.info("Any existing database is going to be destroyed") | 86 log.info("Any existing database is going to be destroyed") |
80 if self.tests or quiet: | 87 if self.tests: |
81 destroy = True | 88 destroy = True |
82 else: | 89 else: |
83 destroy = ask_ok('Are you sure to destroy old database ? [y/n]') | 90 destroy = ask_ok('Are you sure to destroy old database ? [y/n]') |
84 if not destroy: | 91 if not destroy: |
85 sys.exit() | 92 sys.exit('Nothing done') |
86 if destroy: | 93 if destroy: |
87 Base.metadata.drop_all() | 94 Base.metadata.drop_all() |
88 | 95 |
89 checkfirst = not override | 96 checkfirst = not override |
90 Base.metadata.create_all(checkfirst=checkfirst) | 97 Base.metadata.create_all(checkfirst=checkfirst) |
326 self.sa.commit() | 333 self.sa.commit() |
327 except: | 334 except: |
328 self.sa.rollback() | 335 self.sa.rollback() |
329 raise | 336 raise |
330 | 337 |
331 def admin_prompt(self, second=False, defaults={}): | 338 def admin_prompt(self, second=False): |
332 if not self.tests: | 339 if not self.tests: |
333 import getpass | 340 import getpass |
334 | 341 |
335 # defaults | 342 # defaults |
343 defaults = self.cli_args | |
336 username = defaults.get('username') | 344 username = defaults.get('username') |
337 password = defaults.get('password') | 345 password = defaults.get('password') |
338 email = defaults.get('email') | 346 email = defaults.get('email') |
339 | 347 |
340 def get_password(): | 348 def get_password(): |
505 Session().delete(p) | 513 Session().delete(p) |
506 fixed = True | 514 fixed = True |
507 self.populate_default_permissions() | 515 self.populate_default_permissions() |
508 return fixed | 516 return fixed |
509 | 517 |
510 def config_prompt(self, test_repo_path='', retries=3, defaults={}): | 518 def config_prompt(self, test_repo_path='', retries=3): |
519 defaults = self.cli_args | |
511 _path = defaults.get('repos_location') | 520 _path = defaults.get('repos_location') |
512 if retries == 3: | 521 if retries == 3: |
513 log.info('Setting up repositories config') | 522 log.info('Setting up repositories config') |
514 | 523 |
515 if _path is not None: | 524 if _path is not None: |