From 613887344d3d3756ecedc8e0badb276ab1f67694 Mon Sep 17 00:00:00 2001 From: Mehdi Abaakouk Date: Tue, 22 Oct 2013 16:49:52 +0200 Subject: [PATCH] Don't use sqlachemy Metadata as global var In up>down>up mode migrations are failing due to using of common MetaData. It should be declared separatly for both of migration methods. Change-Id: I594c649def9a269033463558ba2aa9313f817ee5 Related-Bug: #1240898 --- .../versions/001_add_meter_table.py | 151 +++++++++--------- .../versions/002_remove_duration.py | 7 +- .../versions/004_add_counter_unit.py | 6 +- .../versions/005_remove_resource_timestamp.py | 6 +- .../versions/006_counter_volume_is_float.py | 7 +- .../versions/007_add_alarm_table.py | 54 +++---- .../migrate_repo/versions/008_add_events.py | 88 +++++----- .../versions/009_event_strings.py | 6 +- .../versions/010_add_index_to_meter.py | 5 - .../018_resource_resource_metadata_is_text.py | 7 +- .../019_alarm_history_detail_is_text.py | 7 +- 11 files changed, 156 insertions(+), 188 deletions(-) diff --git a/ceilometer/storage/sqlalchemy/migrate_repo/versions/001_add_meter_table.py b/ceilometer/storage/sqlalchemy/migrate_repo/versions/001_add_meter_table.py index 081b4e56e..c476edf83 100644 --- a/ceilometer/storage/sqlalchemy/migrate_repo/versions/001_add_meter_table.py +++ b/ceilometer/storage/sqlalchemy/migrate_repo/versions/001_add_meter_table.py @@ -23,86 +23,85 @@ from sqlalchemy import String from sqlalchemy import Table from sqlalchemy import UniqueConstraint -meta = MetaData() - -meter = Table( - 'meter', meta, - Column('id', Integer, primary_key=True, index=True), - Column('counter_name', String(255)), - Column('user_id', String(255), index=True), - Column('project_id', String(255), index=True), - Column('resource_id', String(255)), - Column('resource_metadata', String(5000)), - Column('counter_type', String(255)), - Column('counter_volume', Integer), - Column('counter_duration', Integer), - Column('timestamp', DateTime(timezone=False), index=True), - Column('message_signature', String(1000)), - Column('message_id', String(1000)), - mysql_engine='InnoDB', - mysql_charset='utf8', -) - -resource = Table( - 'resource', meta, - Column('id', String(255), primary_key=True, index=True), - Column('resource_metadata', String(5000)), - Column('project_id', String(255), index=True), - Column('received_timestamp', DateTime(timezone=False)), - Column('timestamp', DateTime(timezone=False), index=True), - Column('user_id', String(255), index=True), - mysql_engine='InnoDB', - mysql_charset='utf8', -) - -user = Table( - 'user', meta, - Column('id', String(255), primary_key=True, index=True), - mysql_engine='InnoDB', - mysql_charset='utf8', -) - -project = Table( - 'project', meta, - Column('id', String(255), primary_key=True, index=True), - mysql_engine='InnoDB', - mysql_charset='utf8', -) - -sourceassoc = Table( - 'sourceassoc', meta, - Column('source_id', String(255), index=True), - Column('user_id', String(255)), - Column('project_id', String(255)), - Column('resource_id', String(255)), - Column('meter_id', Integer), - Index('idx_su', 'source_id', 'user_id'), - Index('idx_sp', 'source_id', 'project_id'), - Index('idx_sr', 'source_id', 'resource_id'), - Index('idx_sm', 'source_id', 'meter_id'), - mysql_engine='InnoDB', - mysql_charset='utf8', -) - -source = Table( - 'source', meta, - Column('id', String(255), primary_key=True, index=True), - UniqueConstraint('id'), - mysql_engine='InnoDB', - mysql_charset='utf8', -) - - -tables = [meter, project, resource, user, source, sourceassoc] - def upgrade(migrate_engine): - meta.bind = migrate_engine + meta = MetaData(bind=migrate_engine) + + meter = Table( + 'meter', meta, + Column('id', Integer, primary_key=True, index=True), + Column('counter_name', String(255)), + Column('user_id', String(255), index=True), + Column('project_id', String(255), index=True), + Column('resource_id', String(255)), + Column('resource_metadata', String(5000)), + Column('counter_type', String(255)), + Column('counter_volume', Integer), + Column('counter_duration', Integer), + Column('timestamp', DateTime(timezone=False), index=True), + Column('message_signature', String(1000)), + Column('message_id', String(1000)), + mysql_engine='InnoDB', + mysql_charset='utf8', + ) + + resource = Table( + 'resource', meta, + Column('id', String(255), primary_key=True, index=True), + Column('resource_metadata', String(5000)), + Column('project_id', String(255), index=True), + Column('received_timestamp', DateTime(timezone=False)), + Column('timestamp', DateTime(timezone=False), index=True), + Column('user_id', String(255), index=True), + mysql_engine='InnoDB', + mysql_charset='utf8', + ) + + user = Table( + 'user', meta, + Column('id', String(255), primary_key=True, index=True), + mysql_engine='InnoDB', + mysql_charset='utf8', + ) + + project = Table( + 'project', meta, + Column('id', String(255), primary_key=True, index=True), + mysql_engine='InnoDB', + mysql_charset='utf8', + ) + + sourceassoc = Table( + 'sourceassoc', meta, + Column('source_id', String(255), index=True), + Column('user_id', String(255)), + Column('project_id', String(255)), + Column('resource_id', String(255)), + Column('meter_id', Integer), + Index('idx_su', 'source_id', 'user_id'), + Index('idx_sp', 'source_id', 'project_id'), + Index('idx_sr', 'source_id', 'resource_id'), + Index('idx_sm', 'source_id', 'meter_id'), + mysql_engine='InnoDB', + mysql_charset='utf8', + ) + + source = Table( + 'source', meta, + Column('id', String(255), primary_key=True, index=True), + UniqueConstraint('id'), + mysql_engine='InnoDB', + mysql_charset='utf8', + ) + + tables = [meter, project, resource, user, source, sourceassoc] for i in sorted(tables): i.create() def downgrade(migrate_engine): - meta.bind = migrate_engine - for i in sorted(tables, reverse=True): - i.drop() + meta = MetaData(bind=migrate_engine) + for name in ['source', 'sourceassoc', 'project', + 'user', 'resource', 'meter']: + t = Table(name, meta, autoload=True) + t.drop() diff --git a/ceilometer/storage/sqlalchemy/migrate_repo/versions/002_remove_duration.py b/ceilometer/storage/sqlalchemy/migrate_repo/versions/002_remove_duration.py index e22d94f28..4b200bacd 100644 --- a/ceilometer/storage/sqlalchemy/migrate_repo/versions/002_remove_duration.py +++ b/ceilometer/storage/sqlalchemy/migrate_repo/versions/002_remove_duration.py @@ -20,18 +20,15 @@ from sqlalchemy import MetaData from sqlalchemy import Table -meta = MetaData() - - def upgrade(migrate_engine): - meta.bind = migrate_engine + meta = MetaData(bind=migrate_engine) meter = Table('meter', meta, autoload=True) duration = Column('counter_duration', Integer) meter.drop_column(duration) def downgrade(migrate_engine): - meta.bind = migrate_engine + meta = MetaData(bind=migrate_engine) meter = Table('meter', meta, autoload=True) duration = Column('counter_duration', Integer) meter.create_column(duration) diff --git a/ceilometer/storage/sqlalchemy/migrate_repo/versions/004_add_counter_unit.py b/ceilometer/storage/sqlalchemy/migrate_repo/versions/004_add_counter_unit.py index e18e94d4f..c7b877907 100644 --- a/ceilometer/storage/sqlalchemy/migrate_repo/versions/004_add_counter_unit.py +++ b/ceilometer/storage/sqlalchemy/migrate_repo/versions/004_add_counter_unit.py @@ -19,18 +19,16 @@ from sqlalchemy import MetaData from sqlalchemy import String from sqlalchemy import Table -meta = MetaData() - def upgrade(migrate_engine): - meta.bind = migrate_engine + meta = MetaData(bind=migrate_engine) meter = Table('meter', meta, autoload=True) unit = Column('counter_unit', String(255)) meter.create_column(unit) def downgrade(migrate_engine): - meta.bind = migrate_engine + meta = MetaData(bind=migrate_engine) meter = Table('meter', meta, autoload=True) unit = Column('counter_unit', String(255)) meter.drop_column(unit) diff --git a/ceilometer/storage/sqlalchemy/migrate_repo/versions/005_remove_resource_timestamp.py b/ceilometer/storage/sqlalchemy/migrate_repo/versions/005_remove_resource_timestamp.py index 96d2f38ca..2914cfe33 100644 --- a/ceilometer/storage/sqlalchemy/migrate_repo/versions/005_remove_resource_timestamp.py +++ b/ceilometer/storage/sqlalchemy/migrate_repo/versions/005_remove_resource_timestamp.py @@ -20,11 +20,9 @@ from sqlalchemy import MetaData, Table, Column, DateTime from ceilometer.openstack.common import timeutils -meta = MetaData() - def upgrade(migrate_engine): - meta.bind = migrate_engine + meta = MetaData(bind=migrate_engine) resource = Table('resource', meta, autoload=True) timestamp = Column('timestamp', DateTime) resource.drop_column(timestamp) @@ -33,7 +31,7 @@ def upgrade(migrate_engine): def downgrade(migrate_engine): - meta.bind = migrate_engine + meta = MetaData(bind=migrate_engine) resource = Table('resource', meta, autoload=True) timestamp = Column('timestamp', DateTime) resource.create_column(timestamp) diff --git a/ceilometer/storage/sqlalchemy/migrate_repo/versions/006_counter_volume_is_float.py b/ceilometer/storage/sqlalchemy/migrate_repo/versions/006_counter_volume_is_float.py index 64aafa343..4c000bfb2 100644 --- a/ceilometer/storage/sqlalchemy/migrate_repo/versions/006_counter_volume_is_float.py +++ b/ceilometer/storage/sqlalchemy/migrate_repo/versions/006_counter_volume_is_float.py @@ -21,16 +21,13 @@ from sqlalchemy import MetaData from sqlalchemy import Table -meta = MetaData() - - def upgrade(migrate_engine): - meta.bind = migrate_engine + meta = MetaData(bind=migrate_engine) meter = Table('meter', meta, autoload=True) meter.c.counter_volume.alter(type=Float(53)) def downgrade(migrate_engine): - meta.bind = migrate_engine + meta = MetaData(bind=migrate_engine) meter = Table('meter', meta, autoload=True) meter.c.counter_volume.alter(type=Integer) diff --git a/ceilometer/storage/sqlalchemy/migrate_repo/versions/007_add_alarm_table.py b/ceilometer/storage/sqlalchemy/migrate_repo/versions/007_add_alarm_table.py index 68b6318fd..703ade55a 100644 --- a/ceilometer/storage/sqlalchemy/migrate_repo/versions/007_add_alarm_table.py +++ b/ceilometer/storage/sqlalchemy/migrate_repo/versions/007_add_alarm_table.py @@ -21,38 +21,36 @@ from sqlalchemy import MetaData, Table, Column, Text from sqlalchemy import Boolean, Integer, String, DateTime, Float -meta = MetaData() - -alarm = Table( - 'alarm', meta, - Column('id', String(255), primary_key=True, index=True), - Column('enabled', Boolean), - Column('name', Text()), - Column('description', Text()), - Column('timestamp', DateTime(timezone=False)), - Column('counter_name', String(255), index=True), - Column('user_id', String(255), index=True), - Column('project_id', String(255), index=True), - Column('comparison_operator', String(2)), - Column('threshold', Float), - Column('statistic', String(255)), - Column('evaluation_periods', Integer), - Column('period', Integer), - Column('state', String(255)), - Column('state_timestamp', DateTime(timezone=False)), - Column('ok_actions', Text()), - Column('alarm_actions', Text()), - Column('insufficient_data_actions', Text()), - Column('matching_metadata', Text()), - mysql_engine='InnoDB', - mysql_charset='utf8') - def upgrade(migrate_engine): - meta.bind = migrate_engine + meta = MetaData(bind=migrate_engine) + alarm = Table( + 'alarm', meta, + Column('id', String(255), primary_key=True, index=True), + Column('enabled', Boolean), + Column('name', Text()), + Column('description', Text()), + Column('timestamp', DateTime(timezone=False)), + Column('counter_name', String(255), index=True), + Column('user_id', String(255), index=True), + Column('project_id', String(255), index=True), + Column('comparison_operator', String(2)), + Column('threshold', Float), + Column('statistic', String(255)), + Column('evaluation_periods', Integer), + Column('period', Integer), + Column('state', String(255)), + Column('state_timestamp', DateTime(timezone=False)), + Column('ok_actions', Text()), + Column('alarm_actions', Text()), + Column('insufficient_data_actions', Text()), + Column('matching_metadata', Text()), + mysql_engine='InnoDB', + mysql_charset='utf8') alarm.create() def downgrade(migrate_engine): - meta.bind = migrate_engine + meta = MetaData(bind=migrate_engine) + alarm = Table('alarm', meta, autoload=True) alarm.drop() diff --git a/ceilometer/storage/sqlalchemy/migrate_repo/versions/008_add_events.py b/ceilometer/storage/sqlalchemy/migrate_repo/versions/008_add_events.py index c4600e755..1b1ee879e 100644 --- a/ceilometer/storage/sqlalchemy/migrate_repo/versions/008_add_events.py +++ b/ceilometer/storage/sqlalchemy/migrate_repo/versions/008_add_events.py @@ -21,54 +21,48 @@ from sqlalchemy import String from sqlalchemy import Table -meta = MetaData() - - -unique_name = Table( - 'unique_name', meta, - Column('id', Integer, primary_key=True), - Column('key', String(32), index=True), - mysql_engine='InnoDB', - mysql_charset='utf8', -) - - -event = Table( - 'event', meta, - Column('id', Integer, primary_key=True), - Column('generated', Float(asdecimal=True), index=True), - Column('unique_name_id', Integer, ForeignKey('unique_name.id')), - mysql_engine='InnoDB', - mysql_charset='utf8', -) - - -trait = Table( - 'trait', meta, - Column('id', Integer, primary_key=True), - Column('name_id', Integer, ForeignKey('unique_name.id')), - Column('t_type', Integer, index=True), - Column('t_string', String(32), nullable=True, default=None, index=True), - Column('t_float', Float, nullable=True, default=None, index=True), - Column('t_int', Integer, nullable=True, default=None, index=True), - Column('t_datetime', Float(asdecimal=True), nullable=True, default=None, - index=True), - Column('event_id', Integer, ForeignKey('event.id')), - mysql_engine='InnoDB', - mysql_charset='utf8', -) - - -tables = [unique_name, event, trait] - - def upgrade(migrate_engine): - meta.bind = migrate_engine - for i in tables: - i.create() + meta = MetaData(bind=migrate_engine) + + unique_name = Table( + 'unique_name', meta, + Column('id', Integer, primary_key=True), + Column('key', String(32), index=True), + mysql_engine='InnoDB', + mysql_charset='utf8', + ) + unique_name.create() + + event = Table( + 'event', meta, + Column('id', Integer, primary_key=True), + Column('generated', Float(asdecimal=True), index=True), + Column('unique_name_id', Integer, ForeignKey('unique_name.id')), + mysql_engine='InnoDB', + mysql_charset='utf8', + ) + event.create() + + trait = Table( + 'trait', meta, + Column('id', Integer, primary_key=True), + Column('name_id', Integer, ForeignKey('unique_name.id')), + Column('t_type', Integer, index=True), + Column('t_string', String(32), nullable=True, default=None, + index=True), + Column('t_float', Float, nullable=True, default=None, index=True), + Column('t_int', Integer, nullable=True, default=None, index=True), + Column('t_datetime', Float(asdecimal=True), nullable=True, + default=None, index=True), + Column('event_id', Integer, ForeignKey('event.id')), + mysql_engine='InnoDB', + mysql_charset='utf8', + ) + trait.create() def downgrade(migrate_engine): - meta.bind = migrate_engine - for i in reversed(tables): - i.drop() + meta = MetaData(bind=migrate_engine) + for name in ['trait', 'event', 'unique_name']: + t = Table(name, meta, autoload=True) + t.drop() diff --git a/ceilometer/storage/sqlalchemy/migrate_repo/versions/009_event_strings.py b/ceilometer/storage/sqlalchemy/migrate_repo/versions/009_event_strings.py index 6623c0544..daee15654 100644 --- a/ceilometer/storage/sqlalchemy/migrate_repo/versions/009_event_strings.py +++ b/ceilometer/storage/sqlalchemy/migrate_repo/versions/009_event_strings.py @@ -16,11 +16,9 @@ from sqlalchemy import MetaData from sqlalchemy import Table from sqlalchemy import VARCHAR -meta = MetaData() - def upgrade(migrate_engine): - meta.bind = migrate_engine + meta = MetaData(bind=migrate_engine) name = Table('unique_name', meta, autoload=True) name.c.key.alter(type=VARCHAR(length=255)) trait = Table('trait', meta, autoload=True) @@ -28,7 +26,7 @@ def upgrade(migrate_engine): def downgrade(migrate_engine): - meta.bind = migrate_engine + meta = MetaData(bind=migrate_engine) name = Table('unique_name', meta, autoload=True) name.c.key.alter(type=VARCHAR(length=32)) trait = Table('trait', meta, autoload=True) diff --git a/ceilometer/storage/sqlalchemy/migrate_repo/versions/010_add_index_to_meter.py b/ceilometer/storage/sqlalchemy/migrate_repo/versions/010_add_index_to_meter.py index 548bba7bd..ba5db2e48 100644 --- a/ceilometer/storage/sqlalchemy/migrate_repo/versions/010_add_index_to_meter.py +++ b/ceilometer/storage/sqlalchemy/migrate_repo/versions/010_add_index_to_meter.py @@ -13,22 +13,17 @@ # License for the specific language governing permissions and limitations # under the License. -from sqlalchemy import MetaData from sqlalchemy import Index from ceilometer.storage.sqlalchemy.models import Meter -meta = MetaData() - def upgrade(migrate_engine): - meta.bind = migrate_engine index = Index('idx_meter_rid_cname', Meter.resource_id, Meter.counter_name) index.create(bind=migrate_engine) def downgrade(migrate_engine): - meta.bind = migrate_engine index = Index('idx_meter_rid_cname', Meter.resource_id, Meter.counter_name) index.drop(bind=migrate_engine) diff --git a/ceilometer/storage/sqlalchemy/migrate_repo/versions/018_resource_resource_metadata_is_text.py b/ceilometer/storage/sqlalchemy/migrate_repo/versions/018_resource_resource_metadata_is_text.py index 100924e1d..4749e8d96 100644 --- a/ceilometer/storage/sqlalchemy/migrate_repo/versions/018_resource_resource_metadata_is_text.py +++ b/ceilometer/storage/sqlalchemy/migrate_repo/versions/018_resource_resource_metadata_is_text.py @@ -23,16 +23,13 @@ from sqlalchemy import MetaData from sqlalchemy import Table -meta = MetaData() - - def upgrade(migrate_engine): - meta.bind = migrate_engine + meta = MetaData(bind=migrate_engine) resource = Table('resource', meta, autoload=True) resource.c.resource_metadata.alter(type=Text) def downgrade(migrate_engine): - meta.bind = migrate_engine + meta = MetaData(bind=migrate_engine) resource = Table('resource', meta, autoload=True) resource.c.resource_metadata.alter(type=String(5000)) diff --git a/ceilometer/storage/sqlalchemy/migrate_repo/versions/019_alarm_history_detail_is_text.py b/ceilometer/storage/sqlalchemy/migrate_repo/versions/019_alarm_history_detail_is_text.py index bfb5d68b6..1f9233b34 100644 --- a/ceilometer/storage/sqlalchemy/migrate_repo/versions/019_alarm_history_detail_is_text.py +++ b/ceilometer/storage/sqlalchemy/migrate_repo/versions/019_alarm_history_detail_is_text.py @@ -23,16 +23,13 @@ from sqlalchemy import MetaData from sqlalchemy import Table -meta = MetaData() - - def upgrade(migrate_engine): - meta.bind = migrate_engine + meta = MetaData(bind=migrate_engine) alm_hist = Table('alarm_history', meta, autoload=True) alm_hist.c.detail.alter(type=Text) def downgrade(migrate_engine): - meta.bind = migrate_engine + meta = MetaData(bind=migrate_engine) alm_hist = Table('alarm_history', meta, autoload=True) alm_hist.c.detail.alter(type=String(255))