Merge lp:~raoul-snyman/openlp/bug-1136278-2.0 into lp:openlp/2.0

Proposed by Raoul Snyman
Status: Merged
Merged at revision: 2191
Proposed branch: lp:~raoul-snyman/openlp/bug-1136278-2.0
Merge into: lp:openlp/2.0
Diff against target: 207 lines (+81/-41)
3 files modified
openlp/plugins/bibles/lib/upgrade.py (+28/-21)
openlp/plugins/songs/lib/upgrade.py (+42/-16)
openlp/plugins/songusage/lib/upgrade.py (+11/-4)
To merge this branch: bzr merge lp:~raoul-snyman/openlp/bug-1136278-2.0
Reviewer Review Type Date Requested Status
Tim Bentley Approve
Review via email: mp+211610@code.launchpad.net

Description of the change

Fix bug #1136278 by trying to detect when an upgrade has already been performed.

To post a comment you must log in.
Revision history for this message
Tim Bentley (trb143) wrote :

Looks horrid but approved.

There must be better ways!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'openlp/plugins/bibles/lib/upgrade.py'
--- openlp/plugins/bibles/lib/upgrade.py 2014-01-14 19:25:18 +0000
+++ openlp/plugins/bibles/lib/upgrade.py 2014-03-18 19:36:20 +0000
@@ -37,6 +37,7 @@
37__version__ = 137__version__ = 1
38log = logging.getLogger(__name__)38log = logging.getLogger(__name__)
3939
40
40def upgrade_setup(metadata):41def upgrade_setup(metadata):
41 """42 """
42 Set up the latest revision all tables, with reflection, needed for the43 Set up the latest revision all tables, with reflection, needed for the
@@ -61,31 +62,37 @@
61 metadata_table = metadata.tables[u'metadata']62 metadata_table = metadata.tables[u'metadata']
62 # Copy "Version" to "name" ("version" used by upgrade system)63 # Copy "Version" to "name" ("version" used by upgrade system)
63 # TODO: Clean up in a subsequent release of OpenLP (like 2.0 final)64 # TODO: Clean up in a subsequent release of OpenLP (like 2.0 final)
64 session.execute(insert(metadata_table).values(65 if select([metadata_table.c.value], metadata_table.c.key == u'Version')\
65 key=u'name',66 .as_scalar().execute().fetchall():
66 value=select(67 session.execute(insert(metadata_table).values(
67 [metadata_table.c.value],68 key=u'name',
68 metadata_table.c.key == u'Version'69 value=select(
69 ).as_scalar()70 [metadata_table.c.value],
70 ))71 metadata_table.c.key == u'Version'
72 ).as_scalar()
73 ))
71 # Copy "Copyright" to "copyright"74 # Copy "Copyright" to "copyright"
72 # TODO: Clean up in a subsequent release of OpenLP (like 2.0 final)75 # TODO: Clean up in a subsequent release of OpenLP (like 2.0 final)
73 session.execute(insert(metadata_table).values(76 if select([metadata_table.c.value], metadata_table.c.key == u'Copyright')\
74 key=u'copyright',77 .as_scalar().execute().fetchall():
75 value=select(78 session.execute(insert(metadata_table).values(
76 [metadata_table.c.value],79 key=u'copyright',
77 metadata_table.c.key == u'Copyright'80 value=select(
78 ).as_scalar()81 [metadata_table.c.value],
79 ))82 metadata_table.c.key == u'Copyright'
83 ).as_scalar()
84 ))
80 # Copy "Permissions" to "permissions"85 # Copy "Permissions" to "permissions"
81 # TODO: Clean up in a subsequent release of OpenLP (like 2.0 final)86 # TODO: Clean up in a subsequent release of OpenLP (like 2.0 final)
82 session.execute(insert(metadata_table).values(87 if select([metadata_table.c.value], metadata_table.c.key == u'Permissions')\
83 key=u'permissions',88 .as_scalar().execute().fetchall():
84 value=select(89 session.execute(insert(metadata_table).values(
85 [metadata_table.c.value],90 key=u'permissions',
86 metadata_table.c.key == u'Permissions'91 value=select(
87 ).as_scalar()92 [metadata_table.c.value],
88 ))93 metadata_table.c.key == u'Permissions'
94 ).as_scalar()
95 ))
89 # Copy "Bookname language" to "book_name_language"96 # Copy "Bookname language" to "book_name_language"
90 # TODO: Clean up in a subsequent release of OpenLP (like 2.0 final)97 # TODO: Clean up in a subsequent release of OpenLP (like 2.0 final)
91 value_count = session.execute(98 value_count = session.execute(
9299
=== modified file 'openlp/plugins/songs/lib/upgrade.py'
--- openlp/plugins/songs/lib/upgrade.py 2014-01-14 19:25:18 +0000
+++ openlp/plugins/songs/lib/upgrade.py 2014-03-18 19:36:20 +0000
@@ -30,18 +30,24 @@
30The :mod:`upgrade` module provides a way for the database and schema that is the30The :mod:`upgrade` module provides a way for the database and schema that is the
31backend for the Songs plugin31backend for the Songs plugin
32"""32"""
33import logging
3334
34from sqlalchemy import Column, Table, types35from sqlalchemy import Column, Table, types
36from sqlalchemy.exc import NoSuchTableError, OperationalError
35from sqlalchemy.sql.expression import func37from sqlalchemy.sql.expression import func
36from migrate.changeset.constraint import ForeignKeyConstraint38from migrate.changeset.constraint import ForeignKeyConstraint
3739
40log = logging.getLogger(__name__)
38__version__ = 341__version__ = 3
3942
43
40def upgrade_setup(metadata):44def upgrade_setup(metadata):
41 """45 """
42 Set up the latest revision all tables, with reflection, needed for the46 Set up the latest revision all tables, with reflection, needed for the
43 upgrade process. If you want to drop a table, you need to remove it from47 upgrade process. If you want to drop a table, you need to remove it from
44 here, and add it to your upgrade function.48 here, and add it to your upgrade function.
49
50 :param metadata: The SQLAlchemy metadata object
45 """51 """
46 tables = {52 tables = {
47 u'authors': Table(u'authors', metadata, autoload=True),53 u'authors': Table(u'authors', metadata, autoload=True),
@@ -66,16 +72,23 @@
66 In order to facilitate this one-to-many relationship, a song_id column is72 In order to facilitate this one-to-many relationship, a song_id column is
67 added to the media_files table, and a weight column so that the media73 added to the media_files table, and a weight column so that the media
68 files can be ordered.74 files can be ordered.
75
76 :param session: An SQLAlchemy Session object
77 :param metadata: An SQLAlchemy MetaData object
78 :param tables: A dictionary of tables
69 """79 """
70 Table(u'media_files_songs', metadata, autoload=True).drop(checkfirst=True)80 try:
71 Column(u'song_id', types.Integer(), default=None)\81 Table(u'media_files_songs', metadata, autoload=True).drop(checkfirst=True)
72 .create(table=tables[u'media_files'])82 Column(u'song_id', types.Integer(), default=None)\
73 Column(u'weight', types.Integer(), default=0)\83 .create(table=tables[u'media_files'])
74 .create(table=tables[u'media_files'])84 Column(u'weight', types.Integer(), default=0)\
75 if metadata.bind.url.get_dialect().name != 'sqlite':85 .create(table=tables[u'media_files'])
76 # SQLite doesn't support ALTER TABLE ADD CONSTRAINT86 if metadata.bind.url.get_dialect().name != 'sqlite':
77 ForeignKeyConstraint([u'song_id'], [u'songs.id'],87 # SQLite doesn't support ALTER TABLE ADD CONSTRAINT
78 table=tables[u'media_files']).create()88 ForeignKeyConstraint([u'song_id'], [u'songs.id'],
89 table=tables[u'media_files']).create()
90 except (NoSuchTableError, OperationalError):
91 log.info(u'Upgrade 1 has already been run, continue with upgrade')
7992
8093
81def upgrade_2(session, metadata, tables):94def upgrade_2(session, metadata, tables):
@@ -83,11 +96,18 @@
83 Version 2 upgrade.96 Version 2 upgrade.
8497
85 This upgrade adds a create_date and last_modified date to the songs table98 This upgrade adds a create_date and last_modified date to the songs table
99
100 :param session: An SQLAlchemy Session object
101 :param metadata: An SQLAlchemy MetaData object
102 :param tables: A dictionary of tables
86 """103 """
87 Column(u'create_date', types.DateTime(), default=func.now())\104 try:
88 .create(table=tables[u'songs'])105 Column(u'create_date', types.DateTime(), default=func.now())\
89 Column(u'last_modified', types.DateTime(), default=func.now())\106 .create(table=tables[u'songs'])
90 .create(table=tables[u'songs'])107 Column(u'last_modified', types.DateTime(), default=func.now())\
108 .create(table=tables[u'songs'])
109 except OperationalError:
110 log.info(u'Upgrade 2 has already been run, continue with upgrade')
91111
92112
93def upgrade_3(session, metadata, tables):113def upgrade_3(session, metadata, tables):
@@ -95,7 +115,13 @@
95 Version 3 upgrade.115 Version 3 upgrade.
96116
97 This upgrade adds a temporary song flag to the songs table117 This upgrade adds a temporary song flag to the songs table
118
119 :param session: An SQLAlchemy Session object
120 :param metadata: An SQLAlchemy MetaData object
121 :param tables: A dictionary of tables
98 """122 """
99 Column(u'temporary', types.Boolean(), default=False)\123 try:
100 .create(table=tables[u'songs'])124 Column(u'temporary', types.Boolean(), default=False)\
101125 .create(table=tables[u'songs'])
126 except OperationalError:
127 log.info(u'Upgrade 3 has already been run, continue with upgrade')
102128
=== modified file 'openlp/plugins/songusage/lib/upgrade.py'
--- openlp/plugins/songusage/lib/upgrade.py 2014-01-14 19:25:18 +0000
+++ openlp/plugins/songusage/lib/upgrade.py 2014-03-18 19:36:20 +0000
@@ -30,11 +30,15 @@
30The :mod:`upgrade` module provides a way for the database and schema that is the30The :mod:`upgrade` module provides a way for the database and schema that is the
31backend for the SongsUsage plugin31backend for the SongsUsage plugin
32"""32"""
33import logging
3334
34from sqlalchemy import Column, Table, types35from sqlalchemy import Column, Table, types
36from sqlalchemy.exc import OperationalError
3537
38log = logging.getLogger(__name__)
36__version__ = 139__version__ = 1
3740
41
38def upgrade_setup(metadata):42def upgrade_setup(metadata):
39 """43 """
40 Set up the latest revision all tables, with reflection, needed for the44 Set up the latest revision all tables, with reflection, needed for the
@@ -53,7 +57,10 @@
5357
54 This upgrade adds two new fields to the songusage database58 This upgrade adds two new fields to the songusage database
55 """59 """
56 Column(u'plugin_name', types.Unicode(20), default=u'') \60 try:
57 .create(table=tables[u'songusage_data'], populate_default=True)61 Column(u'plugin_name', types.Unicode(20), default=u'') \
58 Column(u'source', types.Unicode(10), default=u'') \62 .create(table=tables[u'songusage_data'], populate_default=True)
59 .create(table=tables[u'songusage_data'], populate_default=True)63 Column(u'source', types.Unicode(10), default=u'') \
64 .create(table=tables[u'songusage_data'], populate_default=True)
65 except OperationalError:
66 log.info(u'Upgrade 1 has already been run, continue with upgrade')

Subscribers

People subscribed via source and target branches