peewee orm迁移引擎
peewee-migrations的Python项目详细描述
peewee迁移
一个简单灵活的PeeWee ORM迁移管理器。
- 版本:0.3.17
- 状态:开发/alpha
- 作者:churin andrey
要求
- python>;=3.5
- 最新小便
快速启动
此软件包可以使用pip安装:
$ pip install peewee-migrations
在项目根目录中运行pem init
。
$ pem init
Configuration file 'migrations.json' was created.
假设我们在models.py
中有Foo
模型
classFoo(db.Model):bar=peewee.CharField(max_length=50)baz=peewee.IntegerField()quux=peewee.IntegerField()
将此模型添加到监视列表并创建迁移。
$ pem add models.Foo Model 'models.Foo' was added to the watch list. $ pem watch Migration `0001_migration_201807191008` has been created.
现在您可以列出可用的迁移:
$ pem list [] 0001_migration_201807191008
或查看迁移期间将执行的SQL:
$ pem show [] 0001_migration_201807191008: SQL> CREATE TABLE "foo"("id" SERIAL NOT NULL PRIMARY KEY, "bar" VARCHAR(50) NOT NULL, "baz" INTEGER NOT NULL, "quux" INTEGER NOT NULL)[] PY> set_done('0001_migration_201807191008')
使用migrate
运行迁移:
$ pem migrate [X] 0001_migration_201807191008
更改模型Foo
classFoo(db.Model):bar=peewee.CharField(max_length=20)baz=peewee.IntegerField()quux=peewee.IntegerField()xyzzy=peewee.IntegerField()
并运行watch
以创建新的迁移:
$ pem watch Migration `0002_migration_201807191036` has been created. $ pem show [] 0002_migration_201807191036: SQL> ALTER TABLE "foo" ADD COLUMN "xyzzy" INTEGER [] SQL> ALTER TABLE "foo" RENAME COLUMN "bar" TO "old__bar"[] SQL> ALTER TABLE "foo" ADD COLUMN "bar" VARCHAR(20)[] SQL> UPDATE "foo" SET "xyzzy"= %s WHERE ("xyzzy" IS %s)[0, None] SQL> UPDATE "foo" SET "bar"= SUBSTRING("old__bar", %s, %s) WHERE ("old__bar" IS NOT %s)[1, 20, None] SQL> ALTER TABLE "foo" DROP COLUMN "old__bar"[] SQL> ALTER TABLE "foo" ALTER COLUMN "xyzzy" SET NOT NULL [] SQL> ALTER TABLE "foo" ALTER COLUMN "bar" SET NOT NULL [] PY> set_done('0002_migration_201807191036')
有关使用这些命令的详细信息,请参见--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"
]
}