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
This commit is contained in:
Mehdi Abaakouk 2013-10-22 16:49:52 +02:00
parent 7ba4986af1
commit 613887344d
11 changed files with 156 additions and 188 deletions

View File

@ -23,86 +23,85 @@ from sqlalchemy import String
from sqlalchemy import Table from sqlalchemy import Table
from sqlalchemy import UniqueConstraint 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): 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): for i in sorted(tables):
i.create() i.create()
def downgrade(migrate_engine): def downgrade(migrate_engine):
meta.bind = migrate_engine meta = MetaData(bind=migrate_engine)
for i in sorted(tables, reverse=True): for name in ['source', 'sourceassoc', 'project',
i.drop() 'user', 'resource', 'meter']:
t = Table(name, meta, autoload=True)
t.drop()

View File

@ -20,18 +20,15 @@ from sqlalchemy import MetaData
from sqlalchemy import Table from sqlalchemy import Table
meta = MetaData()
def upgrade(migrate_engine): def upgrade(migrate_engine):
meta.bind = migrate_engine meta = MetaData(bind=migrate_engine)
meter = Table('meter', meta, autoload=True) meter = Table('meter', meta, autoload=True)
duration = Column('counter_duration', Integer) duration = Column('counter_duration', Integer)
meter.drop_column(duration) meter.drop_column(duration)
def downgrade(migrate_engine): def downgrade(migrate_engine):
meta.bind = migrate_engine meta = MetaData(bind=migrate_engine)
meter = Table('meter', meta, autoload=True) meter = Table('meter', meta, autoload=True)
duration = Column('counter_duration', Integer) duration = Column('counter_duration', Integer)
meter.create_column(duration) meter.create_column(duration)

View File

@ -19,18 +19,16 @@ from sqlalchemy import MetaData
from sqlalchemy import String from sqlalchemy import String
from sqlalchemy import Table from sqlalchemy import Table
meta = MetaData()
def upgrade(migrate_engine): def upgrade(migrate_engine):
meta.bind = migrate_engine meta = MetaData(bind=migrate_engine)
meter = Table('meter', meta, autoload=True) meter = Table('meter', meta, autoload=True)
unit = Column('counter_unit', String(255)) unit = Column('counter_unit', String(255))
meter.create_column(unit) meter.create_column(unit)
def downgrade(migrate_engine): def downgrade(migrate_engine):
meta.bind = migrate_engine meta = MetaData(bind=migrate_engine)
meter = Table('meter', meta, autoload=True) meter = Table('meter', meta, autoload=True)
unit = Column('counter_unit', String(255)) unit = Column('counter_unit', String(255))
meter.drop_column(unit) meter.drop_column(unit)

View File

@ -20,11 +20,9 @@ from sqlalchemy import MetaData, Table, Column, DateTime
from ceilometer.openstack.common import timeutils from ceilometer.openstack.common import timeutils
meta = MetaData()
def upgrade(migrate_engine): def upgrade(migrate_engine):
meta.bind = migrate_engine meta = MetaData(bind=migrate_engine)
resource = Table('resource', meta, autoload=True) resource = Table('resource', meta, autoload=True)
timestamp = Column('timestamp', DateTime) timestamp = Column('timestamp', DateTime)
resource.drop_column(timestamp) resource.drop_column(timestamp)
@ -33,7 +31,7 @@ def upgrade(migrate_engine):
def downgrade(migrate_engine): def downgrade(migrate_engine):
meta.bind = migrate_engine meta = MetaData(bind=migrate_engine)
resource = Table('resource', meta, autoload=True) resource = Table('resource', meta, autoload=True)
timestamp = Column('timestamp', DateTime) timestamp = Column('timestamp', DateTime)
resource.create_column(timestamp) resource.create_column(timestamp)

View File

@ -21,16 +21,13 @@ from sqlalchemy import MetaData
from sqlalchemy import Table from sqlalchemy import Table
meta = MetaData()
def upgrade(migrate_engine): def upgrade(migrate_engine):
meta.bind = migrate_engine meta = MetaData(bind=migrate_engine)
meter = Table('meter', meta, autoload=True) meter = Table('meter', meta, autoload=True)
meter.c.counter_volume.alter(type=Float(53)) meter.c.counter_volume.alter(type=Float(53))
def downgrade(migrate_engine): def downgrade(migrate_engine):
meta.bind = migrate_engine meta = MetaData(bind=migrate_engine)
meter = Table('meter', meta, autoload=True) meter = Table('meter', meta, autoload=True)
meter.c.counter_volume.alter(type=Integer) meter.c.counter_volume.alter(type=Integer)

View File

@ -21,38 +21,36 @@
from sqlalchemy import MetaData, Table, Column, Text from sqlalchemy import MetaData, Table, Column, Text
from sqlalchemy import Boolean, Integer, String, DateTime, Float 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): 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() alarm.create()
def downgrade(migrate_engine): def downgrade(migrate_engine):
meta.bind = migrate_engine meta = MetaData(bind=migrate_engine)
alarm = Table('alarm', meta, autoload=True)
alarm.drop() alarm.drop()

View File

@ -21,54 +21,48 @@ from sqlalchemy import String
from sqlalchemy import Table 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): def upgrade(migrate_engine):
meta.bind = migrate_engine meta = MetaData(bind=migrate_engine)
for i in tables:
i.create() 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): def downgrade(migrate_engine):
meta.bind = migrate_engine meta = MetaData(bind=migrate_engine)
for i in reversed(tables): for name in ['trait', 'event', 'unique_name']:
i.drop() t = Table(name, meta, autoload=True)
t.drop()

View File

@ -16,11 +16,9 @@ from sqlalchemy import MetaData
from sqlalchemy import Table from sqlalchemy import Table
from sqlalchemy import VARCHAR from sqlalchemy import VARCHAR
meta = MetaData()
def upgrade(migrate_engine): def upgrade(migrate_engine):
meta.bind = migrate_engine meta = MetaData(bind=migrate_engine)
name = Table('unique_name', meta, autoload=True) name = Table('unique_name', meta, autoload=True)
name.c.key.alter(type=VARCHAR(length=255)) name.c.key.alter(type=VARCHAR(length=255))
trait = Table('trait', meta, autoload=True) trait = Table('trait', meta, autoload=True)
@ -28,7 +26,7 @@ def upgrade(migrate_engine):
def downgrade(migrate_engine): def downgrade(migrate_engine):
meta.bind = migrate_engine meta = MetaData(bind=migrate_engine)
name = Table('unique_name', meta, autoload=True) name = Table('unique_name', meta, autoload=True)
name.c.key.alter(type=VARCHAR(length=32)) name.c.key.alter(type=VARCHAR(length=32))
trait = Table('trait', meta, autoload=True) trait = Table('trait', meta, autoload=True)

View File

@ -13,22 +13,17 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from sqlalchemy import MetaData
from sqlalchemy import Index from sqlalchemy import Index
from ceilometer.storage.sqlalchemy.models import Meter from ceilometer.storage.sqlalchemy.models import Meter
meta = MetaData()
def upgrade(migrate_engine): def upgrade(migrate_engine):
meta.bind = migrate_engine
index = Index('idx_meter_rid_cname', Meter.resource_id, index = Index('idx_meter_rid_cname', Meter.resource_id,
Meter.counter_name) Meter.counter_name)
index.create(bind=migrate_engine) index.create(bind=migrate_engine)
def downgrade(migrate_engine): def downgrade(migrate_engine):
meta.bind = migrate_engine
index = Index('idx_meter_rid_cname', Meter.resource_id, index = Index('idx_meter_rid_cname', Meter.resource_id,
Meter.counter_name) Meter.counter_name)
index.drop(bind=migrate_engine) index.drop(bind=migrate_engine)

View File

@ -23,16 +23,13 @@ from sqlalchemy import MetaData
from sqlalchemy import Table from sqlalchemy import Table
meta = MetaData()
def upgrade(migrate_engine): def upgrade(migrate_engine):
meta.bind = migrate_engine meta = MetaData(bind=migrate_engine)
resource = Table('resource', meta, autoload=True) resource = Table('resource', meta, autoload=True)
resource.c.resource_metadata.alter(type=Text) resource.c.resource_metadata.alter(type=Text)
def downgrade(migrate_engine): def downgrade(migrate_engine):
meta.bind = migrate_engine meta = MetaData(bind=migrate_engine)
resource = Table('resource', meta, autoload=True) resource = Table('resource', meta, autoload=True)
resource.c.resource_metadata.alter(type=String(5000)) resource.c.resource_metadata.alter(type=String(5000))

View File

@ -23,16 +23,13 @@ from sqlalchemy import MetaData
from sqlalchemy import Table from sqlalchemy import Table
meta = MetaData()
def upgrade(migrate_engine): def upgrade(migrate_engine):
meta.bind = migrate_engine meta = MetaData(bind=migrate_engine)
alm_hist = Table('alarm_history', meta, autoload=True) alm_hist = Table('alarm_history', meta, autoload=True)
alm_hist.c.detail.alter(type=Text) alm_hist.c.detail.alter(type=Text)
def downgrade(migrate_engine): def downgrade(migrate_engine):
meta.bind = migrate_engine meta = MetaData(bind=migrate_engine)
alm_hist = Table('alarm_history', meta, autoload=True) alm_hist = Table('alarm_history', meta, autoload=True)
alm_hist.c.detail.alter(type=String(255)) alm_hist.c.detail.alter(type=String(255))