Git Product home page Git Product logo

peewee_migrations's Introduction

Peewee Migrations

A simple and flexible migration manager for Peewee ORM.

  • Version: 0.3.22
  • 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(new_orm.foo.bar, 'old__bar')
    op.add_column(new_orm.foo.bar)
    op.run_data_migration()
    op.drop_column(new_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.rename_column(old_orm.foo.bar, 'old__old__bar')
    op.add_column(old_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(old_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"
  ]
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.