Fix latest DB migration script

Unfortunately, as long as the user_creds_id column is not nullable the
script will still fail if there is data in the database. However with this
patch it is at least left in a recoverable state. Also, downgrades now
work.

Change-Id: Ibb7b7664ad2532154fab90a50bbb95ae9ccfbb91
Signed-off-by: Zane Bitter <zbitter@redhat.com>
This commit is contained in:
Zane Bitter 2012-06-15 16:19:08 +02:00
parent f8fbbd0726
commit be0d7bf844
1 changed files with 21 additions and 10 deletions

View File

@ -39,30 +39,41 @@ def upgrade(migrate_engine):
Column('aws_creds', Text())
)
try:
user_creds.create()
except Exception:
meta.drop_all(tables=tables)
raise
user_creds.create()
stack = Table('stack', meta, autoload=True)
try:
Column('user_creds_id', Integer, ForeignKey("user_creds.id"),
nullable=False).create(stack)
except sqlalchemy.exc.IntegrityError:
stack.c.user_creds_id.drop()
user_creds.drop()
raise
Column('username', String(length=256, convert_unicode=False,
assert_unicode=None,
unicode_error=None,
_warn_on_bytestring=False)).create(stack)
Column('user_creds_id', Integer, ForeignKey("user_creds.id"),
nullable=False).create(stack)
def downgrade(migrate_engine):
meta = MetaData()
meta.bind = migrate_engine
watch_rule = Table('user_creds', meta, autoload=True)
watch_rule.drop()
stack = Table('stack', meta, autoload=True)
user_creds = Table('user_creds', meta, autoload=True)
stack.c.username.drop()
def fk_name(table, ref_column):
for fk in table.foreign_keys:
if fk.column == ref_column:
return fk.name
fkc = ForeignKeyConstraint([stack.c.user_creds_id], [user_creds.c.id],
name=fk_name(stack, user_creds.c.id))
fkc.drop()
stack.c.user_creds_id.drop()
user_creds.drop()