~raoul-snyman/openlp/bug-1544263

« back to all changes in this revision

Viewing changes to openlp/plugins/songs/lib/upgrade.py

  • Committer: Tim Bentley
  • Author(s): s.mehrbrodt at gmail
  • Date: 2016-01-09 19:53:09 UTC
  • mfrom: (2587.1.20 multiple-songbooks)
  • Revision ID: tim.bentley@gmail.com-20160109195309-tabll90cajyh3bn8
lp:~sam92/openlp/multiple-songbooks (revision 2607)
[SUCCESS] https://ci.openlp.io/job/Branch-01-Pull/1226/
[SUCCESS] https://ci.openlp.io/job/Branch-02-Functional-Tests/1151/
[SUCCESS] https://ci.openlp.io/job/Branch-03-Interface-Tests/1090/
[FAILURE] https://ci.openlp.io/job/Branch-04a-Windows_Functional_Tests/928/
Stopping after failure

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
from sqlalchemy.sql.expression import func, false, null, text
30
30
 
31
31
from openlp.core.lib.db import get_upgrade_op
32
 
from openlp.core.common import trace_error_handler
 
32
from openlp.core.utils.db import drop_columns
33
33
 
34
34
log = logging.getLogger(__name__)
35
 
__version__ = 4
 
35
__version__ = 5
36
36
 
37
37
 
38
38
# TODO: When removing an upgrade path the ftw-data needs updating to the minimum supported version
117
117
        op.rename_table('authors_songs_tmp', 'authors_songs')
118
118
    else:
119
119
        log.warning('Skipping upgrade_4 step of upgrading the song db')
 
120
 
 
121
 
 
122
def upgrade_5(session, metadata):
 
123
    """
 
124
    Version 5 upgrade.
 
125
 
 
126
    This upgrade adds support for multiple songbooks
 
127
    """
 
128
    op = get_upgrade_op(session)
 
129
    songs_table = Table('songs', metadata)
 
130
    if 'song_book_id' in [col.name for col in songs_table.c.values()]:
 
131
        log.warning('Skipping upgrade_5 step of upgrading the song db')
 
132
        return
 
133
 
 
134
    # Create the mapping table (songs <-> songbooks)
 
135
    op.create_table('songs_songbooks',
 
136
                    Column('songbook_id', types.Integer(), ForeignKey('song_books.id'), primary_key=True),
 
137
                    Column('song_id', types.Integer(), ForeignKey('songs.id'), primary_key=True),
 
138
                    Column('entry', types.Unicode(255), primary_key=True, nullable=False))
 
139
 
 
140
    # Migrate old data
 
141
    op.execute('INSERT INTO songs_songbooks SELECT song_book_id, id, song_number FROM songs\
 
142
                WHERE song_book_id IS NOT NULL AND song_number IS NOT NULL')
 
143
 
 
144
    # Drop old columns
 
145
    if metadata.bind.url.get_dialect().name == 'sqlite':
 
146
        drop_columns(op, 'songs', ['song_book_id', 'song_number'])
 
147
    else:
 
148
        op.drop_constraint('songs_ibfk_1', 'songs', 'foreignkey')
 
149
        op.drop_column('songs', 'song_book_id')
 
150
        op.drop_column('songs', 'song_number')