Migrations
mydborm.migrations.migrate(model_class, description='')
Apply migrations for a model class.
Returns a result dict: { "table" : "users", "sqls" : [...], "applied" : True | False, "message" : "...", }
mydborm.migrations.generate(model_class, output_dir='migrations', description='', apply=False)
Generate a versioned SQL migration file from model diff.
Compares model definition against live DB schema and writes a numbered SQL file to output_dir.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_class
|
BaseModel subclass to generate migration for |
required | |
output_dir
|
str
|
directory to write migration files |
'migrations'
|
description
|
str
|
optional description for the migration |
''
|
apply
|
bool
|
if True, also apply the migration immediately |
False
|
Returns:
| Type | Description |
|---|---|
dict
|
{ "file" : path to generated file (or None if up to date), "version" : migration version string, "sqls" : list of SQL statements, "applied" : True if migration was applied, "message" : status message, |
dict
|
} |
Usage
from mydborm.migrations import generate
result = generate(User, output_dir="migrations/") print(result["file"])
migrations/0001_create_users.sql
mydborm.migrations.migration_status()
Return all applied migrations from the tracking table.
Returns list of dicts with keys
id, version, description, checksum, applied_at, rolled_back
mydborm.migrations.rollback(model_class)
Roll back the last migration for a model by dropping its table. Marks the migration as rolled_back in the tracking table.
Use with caution — this drops the table and all its data.