Custom Search

Thursday, July 14, 2016

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

a)
First add new column to "Vim" model.

$tacker/db/nfvo/nfvo_db.py

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)

b)
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/d4f265e8eb9d_add_default_to_vim.py

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

c)
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
2f2e337fc6c4

* 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
2f2e337fc6c4


1 comment: