Nailgun database migrations

Nailgun database migrationsΒΆ

Nailgun uses Alembic (http://alembic.readthedocs.org/en/latest/) for database migrations, allowing access to all common Alembic commands through “python manage.py migrate”

This command creates DB tables for Nailgun service:

python manage.py syncdb

This is done by applying one by one a number of database migration files, which are located in nailgun/nailgun/db/migration/alembic_migrations/versions. This command does not create corresponding DB tables unless you created another migration file or updated an existing one, even if you’re making some changes in SQLAlchemy models or creating the new ones. A new migration file can be generated by running:

python manage.py migrate revision -m "Revision message" --autogenerate

There are two important points here:

  1. This command always creates a “diff” between the current database state and the one described by your SQLAlchemy models, so you should always run “python manage.py syncdb” before this command. This prevents running the migrate command with an empty database, which would cause it to create all tables from scratch.
  2. Some modifications may not be detected by “–autogenerate”, which require manual addition to the migration file. For example, adding a new value to ENUM field is not detected.

After creating a migration file, you can upgrade the database to a new state by using this command:

python manage.py migrate upgrade +1

To merge your migration with an existing migration file, you can just move lines of code from the “upgrade()” and “downgrade()” methods to the bottom of corresponding methods in previous migration file. As of this writing, the migration file is called “current.py”.

For all additional features and needs, you may refer to Alembic documentation: http://alembic.readthedocs.org/en/latest/tutorial.html

Creative Commons Attribution 3.0 License

Except where otherwise noted, this document is licensed under Creative Commons Attribution 3.0 License. See all OpenStack Legal Documents.

Contents