summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBoris Pavlovic <boris@pavlovic.me>2013-05-13 15:57:25 +0400
committerBoris Pavlovic <boris@pavlovic.me>2013-05-29 16:23:07 +0400
commit0c30c4ceb1b11a553be9d6652b7550ce0ef1fea9 (patch)
treedaa0bab5ecea995b7085e3d4a3ea37504133acab
parent09526751a85535b3c4ebf7e062b0da10d1d1a99f (diff)
downloadnova-0c30c4ceb1b11a553be9d6652b7550ce0ef1fea9.tar.gz
nova-0c30c4ceb1b11a553be9d6652b7550ce0ef1fea9.tar.xz
nova-0c30c4ceb1b11a553be9d6652b7550ce0ef1fea9.zip
Sync shadow table for 156 migration
To be able to use db archiving our table and shadow table should have the same columns. fixes bug 1179502 Change-Id: Ibb0d4708d949d53e17e91307f07bb90a11b9a990
-rw-r--r--nova/db/sqlalchemy/migrate_repo/versions/182_fix_156_migration_sync_shadow_table.py56
-rw-r--r--nova/tests/test_migrations.py69
2 files changed, 125 insertions, 0 deletions
diff --git a/nova/db/sqlalchemy/migrate_repo/versions/182_fix_156_migration_sync_shadow_table.py b/nova/db/sqlalchemy/migrate_repo/versions/182_fix_156_migration_sync_shadow_table.py
new file mode 100644
index 000000000..17ad64aac
--- /dev/null
+++ b/nova/db/sqlalchemy/migrate_repo/versions/182_fix_156_migration_sync_shadow_table.py
@@ -0,0 +1,56 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2013 IBM Corp.
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+from sqlalchemy import MetaData, String, Table
+from sqlalchemy.dialects import postgresql
+
+
+CIDR_TABLE_COLUMNS = [
+ # table name, column name
+ ('shadow_security_group_rules', 'cidr'),
+ ('shadow_provider_fw_rules', 'cidr'),
+ ('shadow_networks', 'cidr'),
+ ('shadow_networks', 'cidr_v6')]
+
+
+def upgrade(migrate_engine):
+ """Convert String columns holding IP addresses to INET for postgresql."""
+ meta = MetaData()
+ meta.bind = migrate_engine
+ dialect = migrate_engine.url.get_dialect()
+
+ if dialect is postgresql.dialect:
+ for table, column in CIDR_TABLE_COLUMNS:
+ # can't use migrate's alter() because it does not support
+ # explicit casting
+ migrate_engine.execute(
+ "ALTER TABLE %(table)s "
+ "ALTER COLUMN %(column)s TYPE INET USING %(column)s::INET"
+ % {'table': table, 'column': column})
+ else:
+ for table, column in CIDR_TABLE_COLUMNS:
+ t = Table(table, meta, autoload=True)
+ getattr(t.c, column).alter(type=String(43))
+
+
+def downgrade(migrate_engine):
+ """Convert columns back to the larger String(255)."""
+ meta = MetaData()
+ meta.bind = migrate_engine
+ for table, column in CIDR_TABLE_COLUMNS:
+ t = Table(table, meta, autoload=True)
+ getattr(t.c, column).alter(type=String(39))
diff --git a/nova/tests/test_migrations.py b/nova/tests/test_migrations.py
index da9e293a4..7ba3ebf6d 100644
--- a/nova/tests/test_migrations.py
+++ b/nova/tests/test_migrations.py
@@ -1335,6 +1335,75 @@ class TestNovaMigrations(BaseMigrationTestCase, CommonTestsMixIn):
def _check_181(self, engine, data):
self.assertTrue(db_utils.check_shadow_table(engine, 'cells'))
+ def _pre_upgrade_182(self, engine):
+ CIDR = '6666:1020:1000:2013:1000:6535:abcd:abcd'
+
+ security_group_rules = \
+ db_utils.get_table(engine, 'shadow_security_group_rules')
+ values = {
+ 'id': 182,
+ 'protocol': 'tcp',
+ 'from_port': 6666,
+ 'to_port': 9999,
+ 'cidr': CIDR,
+ 'deleted': 0
+ }
+ security_group_rules.insert().values(values).execute()
+
+ networks = db_utils.get_table(engine, 'shadow_networks')
+ values = {
+ 'id': 182,
+ 'vlan': 100500,
+ 'cidr': CIDR,
+ 'cidr_v6': CIDR,
+ 'deleted': 0
+ }
+ networks.insert().values(values).execute()
+
+ provider_fw_rules = db_utils.get_table(engine,
+ 'shadow_provider_fw_rules')
+ values = {
+ 'id': 182,
+ 'protocol': 'tcp',
+ 'from_port': 6666,
+ 'to_port': 9999,
+ 'cidr': CIDR,
+ 'deleted': 0
+ }
+ provider_fw_rules.insert().values(values).execute()
+ return {'cidr': CIDR}
+
+ def _check_182(self, engine, data):
+ self.assertTrue(db_utils.check_shadow_table(engine,
+ 'security_group_rules'))
+ self.assertTrue(db_utils.check_shadow_table(engine,
+ 'provider_fw_rules'))
+ self.assertTrue(db_utils.check_shadow_table(engine, 'networks'))
+
+ table_fields = {
+ 'shadow_security_group_rules': ['cidr'],
+ 'shadow_networks': ['cidr', 'cidr_v6'],
+ 'shadow_provider_fw_rules': ['cidr']
+ }
+
+ for table_name, fields in table_fields.iteritems():
+ table = db_utils.get_table(engine, table_name)
+ rows = table.\
+ select().\
+ where(table.c.id == 182).\
+ execute().\
+ fetchall()
+ self.assertEqual(len(rows), 1)
+ for field in fields:
+ self.assertEqual(rows[0][field], data['cidr'])
+
+ for field in fields:
+ # we should be able to store mask in cidr fields also
+ table.\
+ update().\
+ values({field: data['cidr'] + '/128'}).\
+ execute()
+
class TestBaremetalMigrations(BaseMigrationTestCase, CommonTestsMixIn):
"""Test sqlalchemy-migrate migrations."""