0

I have an application where I need to create a new database and run migrations to create tables. Here is my current implementation:

def create_database(db_name):
    with connection.cursor() as cursor:
        try:
            cursor.execute(f"CREATE DATABASE IF NOT EXISTS {db_name}")
            return True
        except Exception as e:
            print(f"Error creating database {db_name}: {str(e)}")
            return False

The code above successfully creates the database.

def run_migrations(db_name, app_names):
  
    new_db_settings = copy.deepcopy(settings.DATABASES['default'])
    new_db_settings.update({
        'NAME': db_name,
    })

    settings.DATABASES['default'] = new_db_settings

    try:
 

        with TemporaryOverrideSettings({'default': new_db_settings}):
            call_command('makemigrations', app_names) 
            call_command('migrate', app_names, database=db_name)
    except Exception as e:  # Handle potential errors gracefully
        print(f"Error encountered while checking migrations: {e}")

The run_migrations function is intended to run the migrations. The makemigrations command runs successfully, but the migrate command does not work for the new database.

My requirement is to always create the database and run migrations on each request. However, since there might not be new changes, the migrations might not need to run every time.

Issue:

The migrate command does not work as expected for the new database after running makemigrations.

Questions:

How can I ensure that the migrate command runs correctly on the new database? Is there a better approach to handle dynamic database creation and migrations in Django? Any help or suggestions would be greatly appreciated. Thanks!

The migrate command does not work as expected for the new database after running makemigrations.

6
  • 1
    My question to you would be why do you need dynamic databases? Oftentimes this is an XY problem where people think they need dynamic databases whereas there problem can be solved by just designing their database schema differently. Commented Jul 5, 2024 at 6:42
  • @AbdulAzizBarkat this is requirment of project. if you know about then describe. Thanks Commented Jul 5, 2024 at 6:45
  • 1
    "this is requirment of project" that's a bit vague, can you be more specific? Are you looking for some sort of multi-tenancy? Commented Jul 5, 2024 at 6:47
  • 2
    @Zeshan: I agree with Abdul, just making dynamic databases because it is a "requirement of the project" does not seem to make much sense. It appears you (or the designer of the project) seems to think that such dynamic database will solve a certain problem, but you don't explain what that problem is, and very likely we don't need this. Commented Jul 5, 2024 at 7:38
  • What does "the command does not work" mean exactly? What is or isn't happening when you run the command? Where are you calling these two methods - in a view or a script? Why are you overriding the settings twice in run_migrations? What does TemporaryOverrideSettings do? Commented Jul 5, 2024 at 8:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.