Skip to main content

Migration engine for peewee orm

Project description

Peewee Migrations

A simple and flexible migration manager for Peewee ORM.

  • Version: 0.3.32
  • Status: Development/Alpha
  • Author: Churin Andrey

Requirements

  • python >= 3.5
  • latest peewee

Note

SQLite is not supported.

Quick start

This package can be installed using pip:

$ pip install peewee-migrations

Run pem init in the project root.

$ pem init
Configuration file 'migrations.json' was created.

Suppose we have Foo model in models.py

class Foo(db.Model):
    bar = peewee.CharField(max_length=50)
    baz = peewee.IntegerField()
    quux = peewee.IntegerField()

Add this model to the watch list and create migration.

$ pem add models.Foo
Model 'models.Foo' was added to the watch list.
$ pem watch
Migration `0001_migration_201807191008` has been created.

Now you can list available migrations:

$ pem list
[ ] 0001_migration_201807191008

Or view SQL that will be executed during migration:

$ 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')

Use migrate to run migrations:

$ pem migrate
[X] 0001_migration_201807191008

Change model Foo

class Foo(db.Model):
    bar = peewee.CharField(max_length=20)
    baz = peewee.IntegerField()
    quux = peewee.IntegerField()
    xyzzy = peewee.IntegerField()

and run watch to create new migration:

$ 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')

It's possible to create "serialized" migrations, run pem watch --serialize. In this case, explicit migration functions will be additionally created.

$ pem watch --serialize
Migration `0001_migration_202112161523` has been created.

Additional code will be generated

...

def migrate_forward(op, old_orm, new_orm):
    op.create_table(new_orm.foo)


def migrate_backward(op, old_orm, new_orm):
    op.drop_table(old_orm.foo)

And after changing the model

$ pem watch --serialize
Migration `0002_migration_202112161527` has been created.
...

def migrate_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)


...

def migrate_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, use pem migrate --autocommit. To view a list of operations that will be performed in this mode, use pem 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"
  ]
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

peewee-migrations-0.3.32.tar.gz (20.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

peewee_migrations-0.3.32-py3-none-any.whl (19.6 kB view details)

Uploaded Python 3

File details

Details for the file peewee-migrations-0.3.32.tar.gz.

File metadata

  • Download URL: peewee-migrations-0.3.32.tar.gz
  • Upload date:
  • Size: 20.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.4

File hashes

Hashes for peewee-migrations-0.3.32.tar.gz
Algorithm Hash digest
SHA256 813f7089946aa26228d1e40daa0c30746ca94b805a449cc930f499ca32f5929f
MD5 fb07c04dd0445dc1b14196ee23aaae56
BLAKE2b-256 3e0fe32c0078061a3454089da61e75addb29288aaa72fd1b5514f0d94ff3dd01

See more details on using hashes here.

File details

Details for the file peewee_migrations-0.3.32-py3-none-any.whl.

File metadata

  • Download URL: peewee_migrations-0.3.32-py3-none-any.whl
  • Upload date:
  • Size: 19.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.4

File hashes

Hashes for peewee_migrations-0.3.32-py3-none-any.whl
Algorithm Hash digest
SHA256 225ccff8535e9d8b4b8aa4697d1d93eca5f651477708f6dfeb60168256e976fb
MD5 012c5abfeaccfe7e11ee42a7f4449ff1
BLAKE2b-256 eeed4b13aa5aa7d82e2ee1918beaa99b040f505c9035dc5ff29626179197e21b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page