API Documentation
- class miggy.migrator.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.migrator.MigrateOperation[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.migrator.RunPython(func: Callable[[SchemaMigrator, State], None])[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.migrator.RunSql(sql: str, params: tuple[Any, ...] | None = None)[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.migrator.CreateModel(model: type[Model])[source]
Creates a new model in the
Stateand a corresponding table in the database to match it.
- class miggy.migrator.RemoveModel(model_name: str)[source]
Deletes the model from the
Stateand its table from the database.
- class miggy.migrator.AddIndex(model_name: str, *fields: str, name: str, unique: bool = False, where: SQL | None = None, safe: bool = False, concurrently: bool = False)[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.migrator.DropIndex(model_name: str, name: str)[source]
Removes the index named name from the model with model_name.
- class miggy.migrator.RenameTable(model_name: str, new_table_name: str)[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.migrator.ChangeFields(model_name: str, **fields: Field)[source]
Change fields to a model.
- class miggy.migrator.RemoveFields(model_name: str, *names: str, cascade: bool = False)[source]
Removes fields from a model
- class miggy.migrator.RenameField(model_name: str, old_name: str, new_name: str)[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(model: type[Model]) type[Model][source]
A shortcut for adding a
CreateModeloperation.
- remove_model(model_name: str) None[source]
A shortcut for adding a
RemoveModeloperation.
- add_fields(model_name: str, **fields: Any) None[source]
A shortcut for adding a
AddFieldsoperation.
- change_fields(model_name: str, **fields: Field) None[source]
A shortcut for adding a
ChangeFieldsoperation.
- remove_fields(model_name: str, *names: str, cascade: bool = False) None[source]
A shortcut for adding a
RemoveFieldsoperation.
- 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.