From c1a648900f2ac60e3ddf33826191160f009864bc Mon Sep 17 00:00:00 2001 From: Tim Bentley Date: Sat, 27 Aug 2011 16:02:45 +0100 Subject: [PATCH 1/2] Fix initial setup bug --- openlp/core/lib/db.py | 2 +- openlp/plugins/songs/lib/upgrade.py | 21 ++++++++++++--------- openlp/plugins/songusage/lib/upgrade.py | 9 ++++++--- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/openlp/core/lib/db.py b/openlp/core/lib/db.py index 7ad743235..176262b22 100644 --- a/openlp/core/lib/db.py +++ b/openlp/core/lib/db.py @@ -90,7 +90,7 @@ 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 + version = 0 if tables else upgrade.__version__; else: version = int(version_meta.value) if version > upgrade.__version__: diff --git a/openlp/plugins/songs/lib/upgrade.py b/openlp/plugins/songs/lib/upgrade.py index a7aa1bd6c..4f3c3d0dc 100644 --- a/openlp/plugins/songs/lib/upgrade.py +++ b/openlp/plugins/songs/lib/upgrade.py @@ -41,15 +41,18 @@ 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. """ - tables = { - u'authors': Table(u'authors', metadata, autoload=True), - u'media_files': Table(u'media_files', metadata, autoload=True), - u'song_books': Table(u'song_books', metadata, autoload=True), - u'songs': Table(u'songs', metadata, autoload=True), - u'topics': Table(u'topics', metadata, autoload=True), - u'authors_songs': Table(u'authors_songs', metadata, autoload=True), - u'songs_topics': Table(u'songs_topics', metadata, autoload=True) - } + try: + tables = { + u'authors': Table(u'authors', metadata, autoload=True), + u'media_files': Table(u'media_files', metadata, autoload=True), + u'song_books': Table(u'song_books', metadata, autoload=True), + u'songs': Table(u'songs', metadata, autoload=True), + u'topics': Table(u'topics', metadata, autoload=True), + 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 diff --git a/openlp/plugins/songusage/lib/upgrade.py b/openlp/plugins/songusage/lib/upgrade.py index 50ca32fcd..d4211a038 100644 --- a/openlp/plugins/songusage/lib/upgrade.py +++ b/openlp/plugins/songusage/lib/upgrade.py @@ -40,9 +40,12 @@ 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. """ - tables = { - u'songusage_data': Table(u'songusage_data', metadata, autoload=True) - } + try: + tables = { + u'songusage_data': Table(u'songusage_data', metadata, autoload=True) + } + except: + tables = None return tables From 67894aa8d91210a8f85d08d02e7e656497795c24 Mon Sep 17 00:00:00 2001 From: Tim Bentley Date: Sat, 27 Aug 2011 19:43:05 +0100 Subject: [PATCH 2/2] Error Handling improvements --- openlp/core/lib/db.py | 34 +++++++++++++++---------- openlp/plugins/songs/lib/upgrade.py | 21 +++++++-------- openlp/plugins/songusage/lib/upgrade.py | 9 +++---- 3 files changed, 33 insertions(+), 31 deletions(-) diff --git a/openlp/core/lib/db.py b/openlp/core/lib/db.py index 176262b22..2e5d011cf 100644 --- a/openlp/core/lib/db.py +++ b/openlp/core/lib/db.py @@ -79,8 +79,11 @@ def upgrade_db(url, upgrade): Provides a class for the metadata table. """ pass - - tables = upgrade.upgrade_setup(metadata) + 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 - while hasattr(upgrade, u'upgrade_%d' % version): - log.debug(u'Running upgrade_%d', version) - try: - 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) - break - 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) + 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) + 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__ diff --git a/openlp/plugins/songs/lib/upgrade.py b/openlp/plugins/songs/lib/upgrade.py index b28d1764d..fae3400c2 100644 --- a/openlp/plugins/songs/lib/upgrade.py +++ b/openlp/plugins/songs/lib/upgrade.py @@ -42,18 +42,15 @@ 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), - u'song_books': Table(u'song_books', metadata, autoload=True), - u'songs': Table(u'songs', metadata, autoload=True), - u'topics': Table(u'topics', metadata, autoload=True), - u'authors_songs': Table(u'authors_songs', metadata, autoload=True), - u'songs_topics': Table(u'songs_topics', metadata, autoload=True) - } - except: - tables = None + tables = { + u'authors': Table(u'authors', metadata, autoload=True), + u'media_files': Table(u'media_files', metadata, autoload=True), + u'song_books': Table(u'song_books', metadata, autoload=True), + u'songs': Table(u'songs', metadata, autoload=True), + u'topics': Table(u'topics', metadata, autoload=True), + u'authors_songs': Table(u'authors_songs', metadata, autoload=True), + u'songs_topics': Table(u'songs_topics', metadata, autoload=True) + } return tables diff --git a/openlp/plugins/songusage/lib/upgrade.py b/openlp/plugins/songusage/lib/upgrade.py index d4211a038..50ca32fcd 100644 --- a/openlp/plugins/songusage/lib/upgrade.py +++ b/openlp/plugins/songusage/lib/upgrade.py @@ -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 + tables = { + u'songusage_data': Table(u'songusage_data', metadata, autoload=True) + } return tables