April 20, 2014 19:34 / 0 comments / peewee python

I'm pleased to announce that peewee now has more robust support for schema migrations. Basic migration support for Postgres has existed for some time, but peewee now supports SQLite and MySQL as well. In addition, there were a couple of issues with the original migrations API. Schema migrations have been one of the most-requested features, so I'm hopeful that with the addition of this feature, peewee users will have one more reason to enjoy using the library!

What the new migrations library is:

What the new migrations library is not:

The new migrations module could form the basis of a more sophisticated tool, but at the moment it is pretty simple. I'm really excited about the support for SQLite migrations, which are kind of tricky due to limited out-of-the-box support for ALTER TABLE queries.

Basic usage

While the documentation provides the authoritative guide for using the migrations module, I figured I'd show off a little of the new APIs so you can see how it works.

The first step, after importing the migration helpers, is to instantiate a migrator for your Database class.

from peewee import *
from playhouse.migrate import *

my_db = PostgresqlDatabase('my_database')
migrator = PostgresqlMigrator(my_db)

You can then run one or more operations using the migrate helper:

migrate(
    migrator.add_column('story', 'title', CharField(default='')),
    migrator.add_column('story', 'pub_date', DateTimeField(null=True)),
    migrator.add_index('story', ('pub_date',)),
)

The following operations are currently available:

Reading more

I hope this new feature proves useful to you. Thanks for reading!

Comments (0)


Commenting has been closed, but please feel free to contact me