# HG changeset patch # User Mads Kiilerich # Date 1593043561 -7200 # Node ID 8759238d73562f0ce73d4cd5dc428873926ea294 # Parent e1f233b069b044b5eff1ec931ed864aa73f689c8 db: refactor create_tables to try to make it more clear when existing databases are destroyed and created diff -r e1f233b069b0 -r 8759238d7356 kallithea/lib/db_manage.py --- a/kallithea/lib/db_manage.py Fri Jun 19 17:24:36 2020 +0200 +++ b/kallithea/lib/db_manage.py Thu Jun 25 02:06:01 2020 +0200 @@ -74,24 +74,18 @@ def create_tables(self): """ - Create a auth database + Create database (optional) and tables. + The database will be dropped (if it exists) and a new one created. """ + url = sqlalchemy.engine.url.make_url(self.dburi) + database = url.database + log.info("The existing database %r will be destroyed and created." % database) + if not self.tests: + if not self._ask_ok('Are you sure to destroy old database? [y/n]'): + print('Nothing done.') + sys.exit(0) - log.info("Any existing database is going to be destroyed") - if self.tests: - destroy = True - else: - destroy = self._ask_ok('Are you sure to destroy old database ? [y/n]') - if not destroy: - print('Nothing done.') - sys.exit(0) - if destroy: - # drop and re-create old schemas - - url = sqlalchemy.engine.url.make_url(self.dburi) - database = url.database - - # Some databases enforce foreign key constraints and Base.metadata.drop_all() doesn't work + if True: if url.drivername == 'mysql': url.database = None # don't connect to the database (it might not exist) engine = sqlalchemy.create_engine(url) @@ -107,6 +101,7 @@ conn.execute('DROP DATABASE IF EXISTS "%s"' % database) conn.execute('CREATE DATABASE "%s"' % database) else: + # Some databases enforce foreign key constraints and Base.metadata.drop_all() doesn't work, but this is # known to work on SQLite - possibly not on other databases with strong referential integrity Base.metadata.drop_all()