You can download this code by clicking the button below.
This code is now available for download.
This function uses the Alembic library to create a new migration file. It first initializes the Alembic configuration object, then generates a random migration filename, and finally initializes a new migration, auto-generates the migration script, and applies the migration.
Technology Stack : Python, Alembic
Code Type : Python Function
Code Difficulty : Intermediate
from alembic import command
from alembic.config import Config
import random
def generate_migration_name():
adjectives = ["Quick", "Fast", "Efficient", "Sleek", "Agile"]
nouns = ["Migration", "Script", "Change", "Update", "Revision"]
return f"{random.choice(adjectives)}{random.choice(nouns)}"
def create_new_migration(directory, filename):
# Get the Alembic configuration object
config = Config(directory)
# Generate a random migration name
migration_name = generate_migration_name()
# Create a new migration file
command.init(config, filename)
command.autogenerate(config, filename, "head")
command.migrate(config, "head")
# Example usage:
# create_new_migration("migrations", "versions")