So I had a task of adding a new table to the neutron database and at the same time not to use the neutron's migration script, as we wanted to keep the neutron code pure.
I tried to google for the alembic data migration but could not find anything useful. So I started to reverse engineer the migration scripts of the other Openstack projects.
I used the db code base from the following link.
https://github.com/stackforge/group-based-policy/tree/stable/juno/gbpservice/neutron/db
So I ll just mention the modifications we need to do to make it work. Please note that I am doing these changes in the Devstack environment.
1. Create folders for new project.
Lets say we are building a new project called test_db. Add a folder named test_db as shown in the below location
/opt/stack/test_db/test_db/ (Yes 2 folders just to keep in sync with Devstack Arch)
2. Copy migration folder from the below link to /opt/stack/test_db/test_db/
4. Create upgrade and downgrade script.
I tried to google for the alembic data migration but could not find anything useful. So I started to reverse engineer the migration scripts of the other Openstack projects.
I used the db code base from the following link.
https://github.com/stackforge/group-based-policy/tree/stable/juno/gbpservice/neutron/db
So I ll just mention the modifications we need to do to make it work. Please note that I am doing these changes in the Devstack environment.
1. Create folders for new project.
Lets say we are building a new project called test_db. Add a folder named test_db as shown in the below location
/opt/stack/test_db/test_db/ (Yes 2 folders just to keep in sync with Devstack Arch)
2. Copy migration folder from the below link to /opt/stack/test_db/test_db/
https://github.com/stackforge/group-based-policy/tree/stable/juno/gbpservice/neutron/db
Which basically uses the neutron's infrastructure to create the tables in nuetron DB.
Which basically uses the neutron's infrastructure to create the tables in nuetron DB.
3. Modify the file /opt/stack/test_db/test_db/migration/alembic_migrations/env.py.
Change from
gbpservice.neutron.db.migration.models import head # noqa
To
test_db.migration.models import head # noqa
and search for GBP/gbp and replace that with our porject name TEST_DB/test_db.
4. Create upgrade and downgrade script.
revision = '9d65abe72596'
down_revision = None
from alembic import op
import sqlalchemy as sa
def upgrade():
op.create_table(
'test_db_table',
sa.Column('tenant_id', sa.String(length=100)),
)
def downgrade():
op.drop_table('test_db_table')
This is to create the table with the name test_db_table with just one field tenant_id, and also to drop the table when we want to downgrade.
5. Edit HEAD file, /opt/stack/test_db/test_db/migration/alembic_migrations/versions/HEAD.
Delete all the content and add 9d65abe72596 (Note: same as the revision value of the above script)
6. Edit the file /opt/stack/test_db/test_db/migration/cli.py
Change from
gbpservice.neutron.db.migration:alembic_migrations
TO
test_db.migration:alembic_migrations
7. Comment off the following 2 lines in /opt/stack/test_db/test_db/migration/models/head.py
from gbpservice.neutron.db.grouppolicy import group_policy_db # noqa
from gbpservice.neutron.db.grouppolicy import group_policy_mapping_db # noqa
from gbpservice.neutron.db.grouppolicy import group_policy_mapping_db # noqa
8. Create a file called /usr/local/bin/test-db-manage
and add the following content.
#!/usr/bin/python
# PBR Generated from u'console_scripts'
import sys
sys.path.append('/opt/stack/test_db')
from test_db.migration.cli import main
if __name__ == "__main__":
sys.exit(main())
9. Sudo chmod +x /usr/local/bin/test-db-manage
10. To create the table.
test-db-manage --config-file /etc/neutron/neutron.conf upgrade head
Since we have neutron.conf as the config file, the migration script uses the neutron's db url and adds the table into the neutron database.
Since we have neutron.conf as the config file, the migration script uses the neutron's db url and adds the table into the neutron database.
11. To delete the table.
test-db-manage --config-file /etc/neutron/neutron.conf downgrade base
Please do let me know if you have any inputs :).
Comments
Post a Comment