Custom Search

Thursday, July 14, 2016

How to tacker-db-manage generate database migration script and test upgrade and downgrade

First add new column to "Vim" model.


class Vim(model_base.BASE, models_v1.HasId, models_v1.HasTenant):
    type = sa.Column(sa.String(64), nullable=False)
    name = sa.Column(sa.String(255), nullable=False)
    description = sa.Column(sa.Text, nullable=True)
    placement_attr = sa.Column(types.Json, nullable=True)
    shared = sa.Column(sa.Boolean, default=True, server_default=sql.true(
    ), nullable=False)
    default = sa.Column(sa.Boolean, default=False, server_default=sql.false(
    ), nullable=False)
    vim_auth = orm.relationship('VimAuth')
    status = sa.Column(sa.String(255), nullable=False)

Then Generate Migration script.
$ ls tacker/db/migration/alembic_migrations/versions/ | wc -l

* name of the migration script file taken from -m option
$ tacker-db-manage --config-file /etc/tacker/tacker.conf revision -m "add default to vim" --autogenerate

* Open auto generated  migration script.
$ vim tacker/db/migration/alembic_migrations/versions/

* Then delete unwanted statements from "upgrade" and "downgrade" methods

Test upgrade and downgrade:

$ tacker-db-manage --config-file /etc/tacker/tacker.conf check_migration

$ tacker-db-manage --config-file /etc/tacker/tacker.conf history
2f2e337fc6c4 -> d4f265e8eb9d (head), add default to vim

$ tacker-db-manage --config-file /etc/tacker/tacker.conf current

* Upgrade to head
$ tacker-db-manage --config-file /etc/tacker/tacker.conf upgrade head
INFO  [alembic.runtime.migration] Running upgrade 2f2e337fc6c4 -> d4f265e8eb9d, add default to vim

$ tacker-db-manage --config-file /etc/tacker/tacker.conf current
d4f265e8eb9d (head)

* Downgrade to 2f2e337fc6c4
$ tacker-db-manage --config-file /etc/tacker/tacker.conf downgrade 2f2e337fc6c4
INFO  [alembic.runtime.migration] Running downgrade d4f265e8eb9d -> 2f2e337fc6c4, add default to vim

$ tacker-db-manage --config-file /etc/tacker/tacker.conf current

No comments:

Post a Comment