From 68621a4a923ad9569cc53dc355079e4098956007 Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Thu, 25 Sep 2025 18:30:27 +0300 Subject: [PATCH] use AI to redo MariaDB reinit --- databaseReset.py | 46 +++++++++++++--------------------------------- 1 file changed, 13 insertions(+), 33 deletions(-) diff --git a/databaseReset.py b/databaseReset.py index 821ee61..53d9d64 100644 --- a/databaseReset.py +++ b/databaseReset.py @@ -119,39 +119,19 @@ def reinit_db(): else: print(" - No database file found: " + currentdbname + " ..continuing, will create it.\n") else: - print(f" - Attempting to nuke : {currentdbname}\n") - # this is now completely failing to nuke MariaDB adequately, and it crashes when creating Area objects with a no null parent message - # when null parents are explciitly allowed in the model. - cursor = django.db.connection.cursor() - print(f" - - Using {cursor}") - try: - cursor.execute(f"DROP DATABASE {currentdbname}") - except: - print(f" - - Exception when attempting to: DROP DATABASE {currentdbname} with {cursor}") - pass - try: - cursor.execute(f"CREATE DATABASE {currentdbname}") - except: - print(f" - - Exception when attempting to: CREATE DATABASE {currentdbname} with {cursor}") - pass - try: - cursor.execute(f"ALTER DATABASE {currentdbname} CHARACTER SET=utf8") - except: - print(f" - - Exception when attempting to: ALTER DATABASE {currentdbname} CHARACTER SET=utf8") - pass - try: - cursor.execute(f"USE {currentdbname}") - except: - print(f" - - Exception when attempting to: USE {currentdbname}") - pass - try: - cmd = flush.Command() - call_command(cmd, verbosity=0, interactive=False) - except: - print(f" - - Exception when attempting to: FLUSH") - pass - print(f" - Nuked : {currentdbname}\n") - + print(f" - Attempting to drop all tables in: {currentdbname}\n") + with django.db.connection.cursor() as cursor: + print(f" - - Using {cursor}") + # Disable foreign key checks so we can drop tables in any order + cursor.execute("SET FOREIGN_KEY_CHECKS = 0;") + # Get all table names + cursor.execute("SHOW TABLES;") + tables = [row[0] for row in cursor.fetchall()] + for table in tables: + print(f" - Dropping table: {table}") + cursor.execute(f"DROP TABLE IF EXISTS `{table}`;") + cursor.execute("SET FOREIGN_KEY_CHECKS = 1;") + print(f" - All tables dropped in: {currentdbname}\n") print(" - Migrating: " + django.db.connections.databases["default"]["NAME"]) if django.db.connections.databases["default"]["ENGINE"] == "django.db.backends.sqlite3":