From 3d86095b766158b5effe1bf7547857b876958517 Mon Sep 17 00:00:00 2001 From: Kanagaraj Manickam Date: Wed, 1 Apr 2015 15:28:33 +0530 Subject: [PATCH] Fixes migrate_data utils to avoid created_at filed migrate_data utils does not migrate table data if table does not have column created_at field. It fixes this issue. Change-Id: Id179b295d8f0e0644420fb589baa5341d3020c7f Closes-bug: #1439094 --- heat/db/sqlalchemy/utils.py | 4 +-- heat/tests/db/test_utils.py | 54 +++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/heat/db/sqlalchemy/utils.py b/heat/db/sqlalchemy/utils.py index 128e5a96d..049af6101 100644 --- a/heat/db/sqlalchemy/utils.py +++ b/heat/db/sqlalchemy/utils.py @@ -66,9 +66,7 @@ def migrate_data(migrate_engine, table_name = table.name - list_of_rows = list(table.select().order_by( - sqlalchemy.sql.expression.asc(table.c.created_at)) - .execute()) + list_of_rows = list(table.select().execute()) colnames = [c.name for c in table.columns] diff --git a/heat/tests/db/test_utils.py b/heat/tests/db/test_utils.py index f46f883f8..b7dd5c880 100644 --- a/heat/tests/db/test_utils.py +++ b/heat/tests/db/test_utils.py @@ -156,3 +156,57 @@ class DBMigrationUtilsTest(common.HeatTestCase): meta, ignorecons=ignorecons) self.assertFalse(_has_constraint(new_table.constraints, UniqueConstraint, 'uix_1')) + + def test_migrate_data(self): + meta = MetaData(bind=self.engine) + + # create TableA + table_a = Table('TableA', + meta, + Column('id', Integer, primary_key=True), + Column('first', String(8), nullable=False), + Column('second', Integer)) + table_a.create() + + # update it with sample data + values = [ + {'id': 1, 'first': 'a'}, + {'id': 2, 'first': 'b'}, + {'id': 3, 'first': 'c'} + ] + + for value in values: + self.engine.execute(table_a.insert(values=value)) + + # create TableB similar to TableA, except column 'second' + table_b = Table('TableB', + meta, + Column('id', Integer, primary_key=True), + Column('first', String(8), nullable=False)) + table_b.create() + + # migrate data + migrate_utils.migrate_data(self.engine, + table_a, + table_b, + ['second']) + + # validate table_a is dropped + self.assertTrue(self.engine.dialect.has_table( + self.engine.connect(), + 'TableA'), + 'Data migration failed to drop source table') + + # validate table_b is updated with data from table_a + table_b_rows = list(table_b.select().execute()) + self.assertEqual(3, + len(table_b_rows), + "Data migration is failed") + table_b_values = [] + for row in table_b_rows: + table_b_values.append({'id': row.id, + 'first': row.first}) + + self.assertEqual(values, + table_b_values, + "Data migration failed with invalid data copy")