diff options
author | Chris Behrens <cbehrens@codestud.com> | 2013-03-05 14:45:33 -0800 |
---|---|---|
committer | Chris Behrens <cbehrens@codestud.com> | 2013-03-05 18:48:18 -0800 |
commit | 12d362b4ca324c589918a36ba332ee54a62caf9f (patch) | |
tree | 4ade07026a776eedb08d381a434c88c748f6fdb1 | |
parent | e2d956ccaab96c7035fdda2c825ecc1e7bb5423a (diff) | |
download | nova-12d362b4ca324c589918a36ba332ee54a62caf9f.tar.gz nova-12d362b4ca324c589918a36ba332ee54a62caf9f.tar.xz nova-12d362b4ca324c589918a36ba332ee54a62caf9f.zip |
Fix instance_system_metadata deleted columns
Migration 153 adds instance_type data to sys_meta, but creates entries
with the default value of deleted.. which is NULL. This means these
entries do not get pulled in on a join when querying an instance, as
there is a condition of deleted=0.
This converts 'deleted' NULL entries to be 0.
Fixes bug 1147839
Change-Id: I3caf92b3992b22089a2653c2e7220585d616a102
-rw-r--r-- | nova/db/sqlalchemy/migrate_repo/versions/160_fix_system_metadata_deleted.py | 33 | ||||
-rw-r--r-- | nova/tests/test_migrations.py | 49 |
2 files changed, 81 insertions, 1 deletions
diff --git a/nova/db/sqlalchemy/migrate_repo/versions/160_fix_system_metadata_deleted.py b/nova/db/sqlalchemy/migrate_repo/versions/160_fix_system_metadata_deleted.py new file mode 100644 index 000000000..d7909f897 --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/160_fix_system_metadata_deleted.py @@ -0,0 +1,33 @@ +# Copyright 2013 Rackspace Hosting +# +# 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. + +import sqlalchemy + + +def upgrade(migrate_engine): + meta = sqlalchemy.MetaData() + meta.bind = migrate_engine + sys_meta = sqlalchemy.Table('instance_system_metadata', meta, + autoload=True) + # is None does not work here. + sys_meta.update().\ + where(sys_meta.c.deleted == None).\ + values(deleted=0).\ + execute() + + +def downgrade(migration_engine): + # This migration only corrects NULL to be 0. There's no action to + # revert this. + pass diff --git a/nova/tests/test_migrations.py b/nova/tests/test_migrations.py index 2cea6fe0d..cf5c2f509 100644 --- a/nova/tests/test_migrations.py +++ b/nova/tests/test_migrations.py @@ -849,7 +849,6 @@ class TestNovaMigrations(BaseMigrationTestCase, CommonTestsMixIn): meta.reflect(engine) table_names = set(meta.tables.keys()) for table_name in table_names: - print table_name if table_name.startswith("shadow_"): shadow_name = table_name base_name = table_name.replace("shadow_", "") @@ -985,6 +984,54 @@ class TestNovaMigrations(BaseMigrationTestCase, CommonTestsMixIn): console_pools = get_table(engine, 'console_pools') self.assertEqual(console_pools.columns['address'].type.length, 43) + # migration 160, fix system_metadata NULL deleted entries to be 0 + def _pre_upgrade_160(self, engine): + fake_instances = [ + dict(uuid='m160-uuid1'), + dict(uuid='m160-uuid2'), + dict(uuid='m160-uuid3'), + ] + fake_sys_meta = [ + dict(instance_uuid='m160-uuid1', key='foo', value='bar'), + dict(instance_uuid='m160-uuid2', key='foo2', value='bar2'), + dict(instance_uuid='m160-uuid3', key='foo3', value='bar3')] + + instances = get_table(engine, 'instances') + sys_meta = get_table(engine, 'instance_system_metadata') + engine.execute(instances.insert(), fake_instances) + + # Create the metadata entries + data = {} + for sm in fake_sys_meta: + result = sys_meta.insert().values(sm).execute() + sm['id'] = result.inserted_primary_key[0] + data[sm['id']] = sm + + # Make sure the entries in the DB for 'deleted' are None. + our_ids = data.keys() + results = sys_meta.select().where(sys_meta.c.id.in_(our_ids)).\ + execute() + results = list(results) + self.assertEqual(len(our_ids), len(results)) + for result in results: + self.assertEqual(result['deleted'], None) + return data + + def _check_160(self, engine, data): + our_ids = data.keys() + sys_meta = get_table(engine, 'instance_system_metadata') + results = sys_meta.select().where(sys_meta.c.id.in_(our_ids)).\ + execute() + results = list(results) + self.assertEqual(len(our_ids), len(results)) + for result in results: + the_id = result['id'] + # Make sure this is now 0. + self.assertEqual(result['deleted'], 0) + # Make sure nothing else changed. + for key, value in data[the_id].items(): + self.assertEqual(value, result[key]) + class TestBaremetalMigrations(BaseMigrationTestCase, CommonTestsMixIn): """Test sqlalchemy-migrate migrations.""" |