Skip to content

Commit

Permalink
Merge pull request #3431 from mathesar-foundation/migrate_envvar_to_m…
Browse files Browse the repository at this point in the history
…odel

Migration to copy `MATHESAR_DATABASES` credentials
  • Loading branch information
pavish authored Jan 30, 2024
2 parents c2c644b + 97ac9d1 commit c67ba14
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions mathesar/migrations/0006_mathesar_databases_to_model.py
Original file line number Diff line number Diff line change
@@ -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),
]

0 comments on commit c67ba14

Please sign in to comment.