Fix sqlalchemy migration script 061 for DB2 backend

DB2 doesn't support alter column type from string to text,
need to add a new column and copy data from old column, and
later remove the old column.

Closes-Bug: #1443252
Change-Id: I7b3edf6bf7ea0efaf96398a83dad9ebe61caaa23
This commit is contained in:
Ethan Lynn 2015-04-13 18:43:53 +08:00
parent ebccde919e
commit 7b25a9d647
1 changed files with 28 additions and 2 deletions

View File

@ -20,7 +20,20 @@ def upgrade(migrate_engine):
for tab_name in ['stack', 'resource', 'software_deployment']:
table = sqlalchemy.Table(tab_name, meta, autoload=True)
table.c.status_reason.alter(type=sqlalchemy.Text)
if migrate_engine.name == 'ibm_db_sa':
status_reason = sqlalchemy.Column('new_status_reason',
sqlalchemy.Text)
table.create_column(status_reason)
qry = table.select().execute().fetchall()
for item in qry:
values = {'new_status_reason': item.status_reason}
update = table.update().where(
table.c.id == item.id).values(values)
migrate_engine.execute(update)
table.c.status_reason.drop()
table.c.new_status_reason.alter(name='status_reason')
else:
table.c.status_reason.alter(type=sqlalchemy.Text)
def downgrade(migrate_engine):
@ -29,4 +42,17 @@ def downgrade(migrate_engine):
for tab_name in ['stack', 'resource', 'software_deployment']:
table = sqlalchemy.Table(tab_name, meta, autoload=True)
table.c.status_reason.alter(type=sqlalchemy.String(255))
if migrate_engine.name == 'ibm_db_sa':
status_reason = sqlalchemy.Column('new_status_reason',
sqlalchemy.String(255))
table.create_column(status_reason)
qry = table.select().execute().fetchall()
for item in qry:
values = {'new_status_reason': item.status_reason}
update = table.update().where(
table.c.id == item.id).values(values)
migrate_engine.execute(update)
table.c.status_reason.drop()
table.c.new_status_reason.alter(name='status_reason')
else:
table.c.status_reason.alter(type=sqlalchemy.String(255))