New Schema Migrations Module for Peewee ORM
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:
- Generates schema altering statements for your database.
- Easy-to-use API.
- Only depends on a
Databaseobject, so there should be less integration issues when using with your existing code.
- Does the right thing, such as populating a new column with a default value.
- Easily scriptable.
What the new migrations library is not:
- Introspection and automatic schema generation for your models.
- Version control for your database.
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.
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
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( 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:
- Adding a column.
- Dropping a column.
- Renaming a column.
- Make a column nullable or not nullable.
- Rename a table.
- Create an index.
- Drop an index.
I hope this new feature proves useful to you. Thanks for reading!
Commenting has been closed, but please feel free to contact me