From 97ac9d15c36a65fca85ca7fbf8f2d239c9a3e5a3 Mon Sep 17 00:00:00 2001 From: Brent Moran Date: Tue, 30 Jan 2024 23:32:09 +0800 Subject: [PATCH] add custom migration to copy credentials --- .../0006_mathesar_databases_to_model.py | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 mathesar/migrations/0006_mathesar_databases_to_model.py diff --git a/mathesar/migrations/0006_mathesar_databases_to_model.py b/mathesar/migrations/0006_mathesar_databases_to_model.py new file mode 100644 index 0000000000..741796cb72 --- /dev/null +++ b/mathesar/migrations/0006_mathesar_databases_to_model.py @@ -0,0 +1,34 @@ +from decouple import config as decouple_config +from django.conf import settings +from django.db import migrations + + +def update_conn_info(apps, _): + """Add info from MATHESAR_DATABASES to new model fields.""" + Database = apps.get_model('mathesar', 'Database') + django_db_key = decouple_config('DJANGO_DATABASE_KEY', default="default") + user_databases = [key for key in settings.DATABASES if key != django_db_key] + for database_key in user_databases: + try: + db = Database.current_objects.get(name=database_key) + except Database.DoesNotExist: + continue + db_info = settings.DATABASES[database_key] + if 'postgres' in db_info['ENGINE']: + db.name = database_key + db.db_name = db_info['NAME'] + db.username = db_info['USER'] + db.password = db_info['PASSWORD'] + db.host = db_info['HOST'] + db.port = db_info['PORT'] + db.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('mathesar', '0005_release_0_1_4'), + ] + operations = [ + migrations.RunPython(update_conn_info), + ]