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
1=== modified file 'openlp/plugins/bibles/lib/upgrade.py'
2--- openlp/plugins/bibles/lib/upgrade.py 2014-01-14 19:25:18 +0000
3+++ openlp/plugins/bibles/lib/upgrade.py 2014-03-18 19:36:20 +0000
4@@ -37,6 +37,7 @@
5 __version__ = 1
6 log = logging.getLogger(__name__)
7
8+
9 def upgrade_setup(metadata):
10 """
11 Set up the latest revision all tables, with reflection, needed for the
12@@ -61,31 +62,37 @@
13 metadata_table = metadata.tables[u'metadata']
14 # Copy "Version" to "name" ("version" used by upgrade system)
15 # TODO: Clean up in a subsequent release of OpenLP (like 2.0 final)
16- session.execute(insert(metadata_table).values(
17- key=u'name',
18- value=select(
19- [metadata_table.c.value],
20- metadata_table.c.key == u'Version'
21- ).as_scalar()
22- ))
23+ if select([metadata_table.c.value], metadata_table.c.key == u'Version')\
24+ .as_scalar().execute().fetchall():
25+ session.execute(insert(metadata_table).values(
26+ key=u'name',
27+ value=select(
28+ [metadata_table.c.value],
29+ metadata_table.c.key == u'Version'
30+ ).as_scalar()
31+ ))
32 # Copy "Copyright" to "copyright"
33 # TODO: Clean up in a subsequent release of OpenLP (like 2.0 final)
34- session.execute(insert(metadata_table).values(
35- key=u'copyright',
36- value=select(
37- [metadata_table.c.value],
38- metadata_table.c.key == u'Copyright'
39- ).as_scalar()
40- ))
41+ if select([metadata_table.c.value], metadata_table.c.key == u'Copyright')\
42+ .as_scalar().execute().fetchall():
43+ session.execute(insert(metadata_table).values(
44+ key=u'copyright',
45+ value=select(
46+ [metadata_table.c.value],
47+ metadata_table.c.key == u'Copyright'
48+ ).as_scalar()
49+ ))
50 # Copy "Permissions" to "permissions"
51 # TODO: Clean up in a subsequent release of OpenLP (like 2.0 final)
52- session.execute(insert(metadata_table).values(
53- key=u'permissions',
54- value=select(
55- [metadata_table.c.value],
56- metadata_table.c.key == u'Permissions'
57- ).as_scalar()
58- ))
59+ if select([metadata_table.c.value], metadata_table.c.key == u'Permissions')\
60+ .as_scalar().execute().fetchall():
61+ session.execute(insert(metadata_table).values(
62+ key=u'permissions',
63+ value=select(
64+ [metadata_table.c.value],
65+ metadata_table.c.key == u'Permissions'
66+ ).as_scalar()
67+ ))
68 # Copy "Bookname language" to "book_name_language"
69 # TODO: Clean up in a subsequent release of OpenLP (like 2.0 final)
70 value_count = session.execute(
71
72=== modified file 'openlp/plugins/songs/lib/upgrade.py'
73--- openlp/plugins/songs/lib/upgrade.py 2014-01-14 19:25:18 +0000
74+++ openlp/plugins/songs/lib/upgrade.py 2014-03-18 19:36:20 +0000
75@@ -30,18 +30,24 @@
76 The :mod:`upgrade` module provides a way for the database and schema that is the
77 backend for the Songs plugin
78 """
79+import logging
80
81 from sqlalchemy import Column, Table, types
82+from sqlalchemy.exc import NoSuchTableError, OperationalError
83 from sqlalchemy.sql.expression import func
84 from migrate.changeset.constraint import ForeignKeyConstraint
85
86+log = logging.getLogger(__name__)
87 __version__ = 3
88
89+
90 def upgrade_setup(metadata):
91 """
92 Set up the latest revision all tables, with reflection, needed for the
93 upgrade process. If you want to drop a table, you need to remove it from
94 here, and add it to your upgrade function.
95+
96+ :param metadata: The SQLAlchemy metadata object
97 """
98 tables = {
99 u'authors': Table(u'authors', metadata, autoload=True),
100@@ -66,16 +72,23 @@
101 In order to facilitate this one-to-many relationship, a song_id column is
102 added to the media_files table, and a weight column so that the media
103 files can be ordered.
104+
105+ :param session: An SQLAlchemy Session object
106+ :param metadata: An SQLAlchemy MetaData object
107+ :param tables: A dictionary of tables
108 """
109- Table(u'media_files_songs', metadata, autoload=True).drop(checkfirst=True)
110- Column(u'song_id', types.Integer(), default=None)\
111- .create(table=tables[u'media_files'])
112- Column(u'weight', types.Integer(), default=0)\
113- .create(table=tables[u'media_files'])
114- if metadata.bind.url.get_dialect().name != 'sqlite':
115- # SQLite doesn't support ALTER TABLE ADD CONSTRAINT
116- ForeignKeyConstraint([u'song_id'], [u'songs.id'],
117- table=tables[u'media_files']).create()
118+ try:
119+ Table(u'media_files_songs', metadata, autoload=True).drop(checkfirst=True)
120+ Column(u'song_id', types.Integer(), default=None)\
121+ .create(table=tables[u'media_files'])
122+ Column(u'weight', types.Integer(), default=0)\
123+ .create(table=tables[u'media_files'])
124+ if metadata.bind.url.get_dialect().name != 'sqlite':
125+ # SQLite doesn't support ALTER TABLE ADD CONSTRAINT
126+ ForeignKeyConstraint([u'song_id'], [u'songs.id'],
127+ table=tables[u'media_files']).create()
128+ except (NoSuchTableError, OperationalError):
129+ log.info(u'Upgrade 1 has already been run, continue with upgrade')
130
131
132 def upgrade_2(session, metadata, tables):
133@@ -83,11 +96,18 @@
134 Version 2 upgrade.
135
136 This upgrade adds a create_date and last_modified date to the songs table
137+
138+ :param session: An SQLAlchemy Session object
139+ :param metadata: An SQLAlchemy MetaData object
140+ :param tables: A dictionary of tables
141 """
142- Column(u'create_date', types.DateTime(), default=func.now())\
143- .create(table=tables[u'songs'])
144- Column(u'last_modified', types.DateTime(), default=func.now())\
145- .create(table=tables[u'songs'])
146+ try:
147+ Column(u'create_date', types.DateTime(), default=func.now())\
148+ .create(table=tables[u'songs'])
149+ Column(u'last_modified', types.DateTime(), default=func.now())\
150+ .create(table=tables[u'songs'])
151+ except OperationalError:
152+ log.info(u'Upgrade 2 has already been run, continue with upgrade')
153
154
155 def upgrade_3(session, metadata, tables):
156@@ -95,7 +115,13 @@
157 Version 3 upgrade.
158
159 This upgrade adds a temporary song flag to the songs table
160+
161+ :param session: An SQLAlchemy Session object
162+ :param metadata: An SQLAlchemy MetaData object
163+ :param tables: A dictionary of tables
164 """
165- Column(u'temporary', types.Boolean(), default=False)\
166- .create(table=tables[u'songs'])
167-
168+ try:
169+ Column(u'temporary', types.Boolean(), default=False)\
170+ .create(table=tables[u'songs'])
171+ except OperationalError:
172+ log.info(u'Upgrade 3 has already been run, continue with upgrade')
173
174=== modified file 'openlp/plugins/songusage/lib/upgrade.py'
175--- openlp/plugins/songusage/lib/upgrade.py 2014-01-14 19:25:18 +0000
176+++ openlp/plugins/songusage/lib/upgrade.py 2014-03-18 19:36:20 +0000
177@@ -30,11 +30,15 @@
178 The :mod:`upgrade` module provides a way for the database and schema that is the
179 backend for the SongsUsage plugin
180 """
181+import logging
182
183 from sqlalchemy import Column, Table, types
184+from sqlalchemy.exc import OperationalError
185
186+log = logging.getLogger(__name__)
187 __version__ = 1
188
189+
190 def upgrade_setup(metadata):
191 """
192 Set up the latest revision all tables, with reflection, needed for the
193@@ -53,7 +57,10 @@
194
195 This upgrade adds two new fields to the songusage database
196 """
197- Column(u'plugin_name', types.Unicode(20), default=u'') \
198- .create(table=tables[u'songusage_data'], populate_default=True)
199- Column(u'source', types.Unicode(10), default=u'') \
200- .create(table=tables[u'songusage_data'], populate_default=True)
201+ try:
202+ Column(u'plugin_name', types.Unicode(20), default=u'') \
203+ .create(table=tables[u'songusage_data'], populate_default=True)
204+ Column(u'source', types.Unicode(10), default=u'') \
205+ .create(table=tables[u'songusage_data'], populate_default=True)
206+ except OperationalError:
207+ log.info(u'Upgrade 1 has already been run, continue with upgrade')

Subscribers

People subscribed via source and target branches