Error Handling improvements

This commit is contained in:
Tim Bentley 2011-08-27 19:43:05 +01:00
parent 3f078a553f
commit 67894aa8d9
3 changed files with 33 additions and 31 deletions

View File

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

View File

@ -42,7 +42,6 @@ def upgrade_setup(metadata):
upgrade process. If you want to drop a table, you need to remove it from
here, and add it to your upgrade function.
"""
try:
tables = {
u'authors': Table(u'authors', metadata, autoload=True),
u'media_files': Table(u'media_files', metadata, autoload=True),
@ -52,8 +51,6 @@ def upgrade_setup(metadata):
u'authors_songs': Table(u'authors_songs', metadata, autoload=True),
u'songs_topics': Table(u'songs_topics', metadata, autoload=True)
}
except:
tables = None
return tables

View File

@ -40,12 +40,9 @@ def upgrade_setup(metadata):
upgrade process. If you want to drop a table, you need to remove it from
here, and add it to your upgrade function.
"""
try:
tables = {
u'songusage_data': Table(u'songusage_data', metadata, autoload=True)
}
except:
tables = None
return tables