API Documentation

class miggy.state.State(data: dict[str, type[Model]] | None = None)[source]

Current state containing historical models that match the operation’s place in the project history. This is a dict-like class that stores data in the format model_name: model_class. The model_name is case-insensitive.

Example:

User = state["user"]
User.get(id=1)
class miggy.migrator.SchemaMigrator(database)[source]

Extended playhouse.migrate.SchemaMigrator from peewee

sql(sql, params: tuple[Any, ...] | None = None)[source]

Execute raw SQL.

rename_index(old_name: str, new_name: str)[source]

Change index name

create_table(model: type[Model], safe: bool = False) Callable[source]

Create table from model class

drop_table(model: type[Model], safe: bool = False) Callable[source]

Drop model table

Migrate operations

class miggy.operations.MigrateOperation(*args, **kwargs)[source]

Base class for a migrate operation

state_forwards(state: State) None[source]

Take the state from the previous migration, and mutate it so that it matches what this migration would perform.

database_forwards(schema_migrator: SchemaMigrator, from_state: State, to_state: State) list[Operation] | list[Callable][source]

Perform the mutation on the database schema in the normal (forwards) direction. The method MUST NOT mutate provided states.

class miggy.operations.RunPython(*args, **kwargs)[source]

Allows to run custom Python code. func should be callable object that accept two arguments; the first is an instance of SchemaMigrator and the second is an instance of State

Example:

def save_user(schema_migrator: SchemaMigrator, current_state: State):
    User = current_state["user"]
    User(
        first_name="First",
        last_name="Last",
    ).save()

migrator.add_operaion(RunPython(save_user))
class miggy.operations.RunSql(*args, **kwargs)[source]

Allows running of arbitrary SQL on the database - useful for more advanced features of database backends that Miggy doesn’t support directly.

Example:

migrator.add_operation(
    RunSql(
        'INSERT INTO "user" ("first_name", "last_name") VALUES (%s, %s)',
        (
            "First",
            "Last",
        ),
    )
)
class miggy.operations.CreateModel(*args, **kwargs)[source]

Creates a new model in the State and a corresponding table in the database to match it.

class miggy.operations.RemoveModel(*args, **kwargs)[source]

Deletes the model from the State and its table from the database.

class miggy.operations.AddIndex(*args, **kwargs)[source]

Creates an index in the database table for the model with model_name. The index will be saved in Model._meta.indexes_state dict

class miggy.operations.DropIndex(*args, **kwargs)[source]

Removes the index named name from the model with model_name.

class miggy.operations.RenameTable(*args, **kwargs)[source]

Renames the model from the old name to a new one. It also renames all single-column indexes, if they exist.

Warning:

This operation does not rename indexes created via the Meta class or the add_index() method. You should explicitly specify index names if you plan to use this operation. Otherwise, you will be prompted to recreate the indexes with a new name in the next migration.

class miggy.operations.AddField(*args, **kwargs)[source]

Add a field to a model.

class miggy.operations.AlterField(*args, **kwargs)[source]

Alter a field for a model.

class miggy.operations.RemoveField(*args, **kwargs)[source]

Remove a field from a model

class miggy.operations.RenameField(*args, **kwargs)[source]

Changes a field’s name (and, unless column_name is set, its column name). It also renames a single-column indexe, if it exists.

Warning:

This operation does not rename indexes created via the Meta class or the add_index() method. You should explicitly specify index names if you plan to use this operation. Otherwise, you will be prompted to recreate the index with a new name in the next migration.

class miggy.operations.AddPrimaryKeyConstraint(*args, **kwargs)[source]
class miggy.operations.RemovePrimaryKeyConstraint(*args, **kwargs)[source]

Migrator

class miggy.migrator.Migrator(database, schema=None)[source]

A class that provides shortcuts for adding migration operations.

add_operation(op: MigrateOperation) None[source]

Adds a migrate operation

python(func: Callable[[SchemaMigrator, State], None])[source]

A shortcut for adding a RunPython operation.

sql(sql: str, params: tuple[Any, ...] | None = None) None[source]

A shortcut for adding a RunSql operation.

create_model(name: type[Model] | str, fields: dict[str, Field] | None = None, meta: dict[str, Any] | None = None) type[Model] | None[source]

A shortcut for adding a CreateModel operation.

remove_model(model_name: str) None[source]

A shortcut for adding a RemoveModel operation.

add_field(model_name: str, name: str, field: Field) None[source]

A shortcut for adding a AddField operation.

alter_field(model_name: str, name: str, field: Field) None[source]

A shortcut for adding a AlterField operation.

remove_field(model_name: str, name: str) None[source]

A shortcut for adding a RemoveField operation.

rename_field(model_name: str, old_name: str, new_name: str) None[source]

A shortcut for adding a RenameField operation.

rename_table(model_name: str, new_table_name: str) None[source]

A shortcut for adding a RenameTable operation.

add_index(model_name: str, *fields: str, name: str, unique: bool = False, where: SQL | None = None, safe: bool = False, concurrently: bool = False) None[source]

A shortcut for adding a AddIndex operation.

drop_index(model_name: str, name: str) None[source]

A shortcut for adding a DropIndex operation.

add_primary_key_constraint(model_name: str, *fields: str) None[source]

A shortcut for adding a AddPrimaryKeyConstraint operation.

remove_primary_key_constraint(model_name: str) None[source]

A shortcut for adding a RemovePrimaryKeyConstraint operation.