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
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
SchemaMigratorand the second is an instance ofStateExample:
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
Stateand a corresponding table in the database to match it.
- class miggy.operations.RemoveModel(*args, **kwargs)[source]
Deletes the model from the
Stateand 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.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.
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
RunPythonoperation.
- sql(sql: str, params: tuple[Any, ...] | None = None) None[source]
A shortcut for adding a
RunSqloperation.
- 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
CreateModeloperation.
- add_field(model_name: str, name: str, field: Field) None[source]
A shortcut for adding a
AddFieldoperation.
- alter_field(model_name: str, name: str, field: Field) None[source]
A shortcut for adding a
AlterFieldoperation.
- remove_field(model_name: str, name: str) None[source]
A shortcut for adding a
RemoveFieldoperation.
- rename_field(model_name: str, old_name: str, new_name: str) None[source]
A shortcut for adding a
RenameFieldoperation.
- rename_table(model_name: str, new_table_name: str) None[source]
A shortcut for adding a
RenameTableoperation.
- 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
AddIndexoperation.