Package: peewee-migrations
Peewee MigrationsA simple and flexible migration manager for Peewee ORM.Version:0.3.32Status:Development/AlphaAuthor:Churin AndreyRequirementspython >= 3.5latest peeweeNoteSQLite is not supported.Quick startThis package can be installed using pip:$pipinstallpeewee-migrationsRunpem initin the project root.$peminit
Configurationfile'migrations.json'wascreated.Suppose we haveFoomodel inmodels.pyclassFoo(db.Model):bar=peewee.CharField(max_length=50)baz=peewee.IntegerField()quux=peewee.IntegerField()Add this model to the watch list and create migration.$pemaddmodels.Foo
Model'models.Foo'wasaddedtothewatchlist.
$pemwatch
Migration`0001_migration_201807191008`hasbeencreated.Now you can list available migrations:$pemlist[]0001_migration_201807191008Or view SQL that will be executed during migration:$pemshow[]0001_migration_201807191008:SQL>CREATETABLE"foo"("id"SERIALNOTNULLPRIMARYKEY,"bar"VARCHAR(50)NOTNULL,"baz"INTEGERNOTNULL,"quux"INTEGERNOTNULL)[]PY>set_done('0001_migration_201807191008')Usemigrateto run migrations:$pemmigrate[X]0001_migration_201807191008Change modelFooclassFoo(db.Model):bar=peewee.CharField(max_length=20)baz=peewee.IntegerField()quux=peewee.IntegerField()xyzzy=peewee.IntegerField()and runwatchto create new migration:$pemwatch
Migration`0002_migration_201807191036`hasbeencreated.
$pemshow[]0002_migration_201807191036:SQL>ALTERTABLE"foo"ADDCOLUMN"xyzzy"INTEGER[]SQL>ALTERTABLE"foo"RENAMECOLUMN"bar"TO"old__bar"[]SQL>ALTERTABLE"foo"ADDCOLUMN"bar"VARCHAR(20)[]SQL>UPDATE"foo"SET"xyzzy"=%sWHERE("xyzzy"IS%s)[0,None]SQL>UPDATE"foo"SET"bar"=SUBSTRING("old__bar",%s,%s)WHERE("old__bar"ISNOT%s)[1,20,None]SQL>ALTERTABLE"foo"DROPCOLUMN"old__bar"[]SQL>ALTERTABLE"foo"ALTERCOLUMN"xyzzy"SETNOTNULL[]SQL>ALTERTABLE"foo"ALTERCOLUMN"bar"SETNOTNULL[]PY>set_done('0002_migration_201807191036')It's possible to create "serialized" migrations, runpem watch --serialize. In this case, explicit migration functions will be additionally created.$pemwatch--serialize
Migration`0001_migration_202112161523`hasbeencreated.Additional code will be generated...defmigrate_forward(op,old_orm,new_orm):op.create_table(new_orm.foo)defmigrate_backward(op,old_orm,new_orm):op.drop_table(old_orm.foo)And after changing the model$pemwatch--serialize
Migration`0002_migration_202112161527`hasbeencreated....defmigrate_forward(op,old_orm,new_orm):op.add_column(new_orm.foo.xyzzy)op.rename_column(old_orm.foo.bar,'old__bar')op.add_column(new_orm.foo.bar)op.run_data_migration()op.drop_column(old_orm.foo.bar)op.add_not_null(new_orm.foo.xyzzy)op.add_not_null(new_orm.foo.bar)...defmigrate_backward(op,old_orm,new_orm):op.rename_column(old_orm.foo.bar,'old__bar')op.add_column(new_orm.foo.bar)op.run_data_migration()op.drop_column(old_orm.foo.xyzzy)op.drop_column(old_orm.foo.bar)op.add_not_null(new_orm.foo.bar)Serialized migrations are performed only according to the operations specified in the migrate functions.To run migrations without a transaction, usepem migrate --autocommit. To view a list of operations that will be performed in this mode, usepem show --autocommit(some operations may differ).For more information on using the commands see --help.migrations.json{
  "prerun": "some code here",  // some code to run before executing any command
  "directory": "migrations",   // folder to hold migrations
  "history": "migratehistory", // table to hold migration history
  "models": [                  // list of models to watch
    "module1.Model1",
    "module2.Model2"
  ]
}