Handle missing tables and set the meta data to the correct value

bzr-revno: 1727
This commit is contained in:
Tim Bentley 2011-08-28 06:56:09 +01:00
commit b0584ccd09

View File

@ -79,8 +79,11 @@ def upgrade_db(url, upgrade):
Provides a class for the metadata table. Provides a class for the metadata table.
""" """
pass pass
load_changes = True
try:
tables = upgrade.upgrade_setup(metadata) tables = upgrade.upgrade_setup(metadata)
except SQLAlchemyError, DBAPIError:
load_changes = False
metadata_table = Table(u'metadata', metadata, metadata_table = Table(u'metadata', metadata,
Column(u'key', types.Unicode(64), primary_key=True), Column(u'key', types.Unicode(64), primary_key=True),
Column(u'value', types.UnicodeText(), default=None) Column(u'value', types.UnicodeText(), default=None)
@ -96,16 +99,21 @@ def upgrade_db(url, upgrade):
if version > upgrade.__version__: if version > upgrade.__version__:
return version, upgrade.__version__ return version, upgrade.__version__
version += 1 version += 1
if load_changes:
while hasattr(upgrade, u'upgrade_%d' % version): while hasattr(upgrade, u'upgrade_%d' % version):
log.debug(u'Running upgrade_%d', version) log.debug(u'Running upgrade_%d', version)
try: try:
getattr(upgrade, u'upgrade_%d' % version)(session, metadata, tables) getattr(upgrade, u'upgrade_%d' % version) \
(session, metadata, tables)
version_meta.value = unicode(version) version_meta.value = unicode(version)
except SQLAlchemyError, DBAPIError: except SQLAlchemyError, DBAPIError:
log.exception(u'Could not run database upgrade script "upgrade_%s"'\ log.exception(u'Could not run database upgrade script '
', upgrade process has been halted.', version) '"upgrade_%s", upgrade process has been halted.', version)
break break
version += 1 version += 1
else:
version_meta = Metadata.populate(key=u'version',
value=int(upgrade.__version__))
session.add(version_meta) session.add(version_meta)
session.commit() session.commit()
return int(version_meta.value), upgrade.__version__ return int(version_meta.value), upgrade.__version__