changeset 8591:8759238d7356

db: refactor create_tables to try to make it more clear when existing databases are destroyed and created
author Mads Kiilerich <mads@kiilerich.com>
date Thu, 25 Jun 2020 02:06:01 +0200
parents e1f233b069b0
children 307c876a6e89
files kallithea/lib/db_manage.py
diffstat 1 files changed, 11 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- 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()