forked from openlp/openlp
Fix upgrade on song db with lost version. Fixes bug 1391638
Fixes: https://launchpad.net/bugs/1391638
This commit is contained in:
parent
93706f8d99
commit
c6e4c07a24
@ -32,10 +32,11 @@ backend for the Songs plugin
|
||||
"""
|
||||
import logging
|
||||
|
||||
from sqlalchemy import Column, ForeignKey, types
|
||||
from sqlalchemy import Table, Column, ForeignKey, types
|
||||
from sqlalchemy.sql.expression import func, false, null, text
|
||||
|
||||
from openlp.core.lib.db import get_upgrade_op
|
||||
from openlp.core.common import trace_error_handler
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
__version__ = 4
|
||||
@ -57,12 +58,16 @@ def upgrade_1(session, metadata):
|
||||
:param metadata:
|
||||
"""
|
||||
op = get_upgrade_op(session)
|
||||
op.drop_table('media_files_songs')
|
||||
op.add_column('media_files', Column('song_id', types.Integer(), server_default=null()))
|
||||
op.add_column('media_files', Column('weight', types.Integer(), server_default=text('0')))
|
||||
if metadata.bind.url.get_dialect().name != 'sqlite':
|
||||
# SQLite doesn't support ALTER TABLE ADD CONSTRAINT
|
||||
op.create_foreign_key('fk_media_files_song_id', 'media_files', 'songs', ['song_id', 'id'])
|
||||
songs_table = Table('songs', metadata, autoload=True)
|
||||
if 'media_files_songs' in [t.name for t in metadata.tables.values()]:
|
||||
op.drop_table('media_files_songs')
|
||||
op.add_column('media_files', Column('song_id', types.Integer(), server_default=null()))
|
||||
op.add_column('media_files', Column('weight', types.Integer(), server_default=text('0')))
|
||||
if metadata.bind.url.get_dialect().name != 'sqlite':
|
||||
# SQLite doesn't support ALTER TABLE ADD CONSTRAINT
|
||||
op.create_foreign_key('fk_media_files_song_id', 'media_files', 'songs', ['song_id', 'id'])
|
||||
else:
|
||||
log.warning('Skipping upgrade_1 step of upgrading the song db')
|
||||
|
||||
|
||||
def upgrade_2(session, metadata):
|
||||
@ -72,8 +77,12 @@ def upgrade_2(session, metadata):
|
||||
This upgrade adds a create_date and last_modified date to the songs table
|
||||
"""
|
||||
op = get_upgrade_op(session)
|
||||
op.add_column('songs', Column('create_date', types.DateTime(), default=func.now()))
|
||||
op.add_column('songs', Column('last_modified', types.DateTime(), default=func.now()))
|
||||
songs_table = Table('songs', metadata, autoload=True)
|
||||
if 'create_date' not in [col.name for col in songs_table.c.values()]:
|
||||
op.add_column('songs', Column('create_date', types.DateTime(), default=func.now()))
|
||||
op.add_column('songs', Column('last_modified', types.DateTime(), default=func.now()))
|
||||
else:
|
||||
log.warning('Skipping upgrade_2 step of upgrading the song db')
|
||||
|
||||
|
||||
def upgrade_3(session, metadata):
|
||||
@ -83,10 +92,14 @@ def upgrade_3(session, metadata):
|
||||
This upgrade adds a temporary song flag to the songs table
|
||||
"""
|
||||
op = get_upgrade_op(session)
|
||||
if metadata.bind.url.get_dialect().name == 'sqlite':
|
||||
op.add_column('songs', Column('temporary', types.Boolean(create_constraint=False), server_default=false()))
|
||||
songs_table = Table('songs', metadata, autoload=True)
|
||||
if 'temporary' not in [col.name for col in songs_table.c.values()]:
|
||||
if metadata.bind.url.get_dialect().name == 'sqlite':
|
||||
op.add_column('songs', Column('temporary', types.Boolean(create_constraint=False), server_default=false()))
|
||||
else:
|
||||
op.add_column('songs', Column('temporary', types.Boolean(), server_default=false()))
|
||||
else:
|
||||
op.add_column('songs', Column('temporary', types.Boolean(), server_default=false()))
|
||||
log.warning('Skipping upgrade_3 step of upgrading the song db')
|
||||
|
||||
|
||||
def upgrade_4(session, metadata):
|
||||
@ -98,11 +111,15 @@ def upgrade_4(session, metadata):
|
||||
# Since SQLite doesn't support changing the primary key of a table, we need to recreate the table
|
||||
# and copy the old values
|
||||
op = get_upgrade_op(session)
|
||||
op.create_table('authors_songs_tmp',
|
||||
Column('author_id', types.Integer(), ForeignKey('authors.id'), primary_key=True),
|
||||
Column('song_id', types.Integer(), ForeignKey('songs.id'), primary_key=True),
|
||||
Column('author_type', types.String(), primary_key=True,
|
||||
nullable=False, server_default=text('""')))
|
||||
op.execute('INSERT INTO authors_songs_tmp SELECT author_id, song_id, "" FROM authors_songs')
|
||||
op.drop_table('authors_songs')
|
||||
op.rename_table('authors_songs_tmp', 'authors_songs')
|
||||
songs_table = Table('songs', metadata)
|
||||
if 'author_type' not in [col.name for col in songs_table.c.values()]:
|
||||
op.create_table('authors_songs_tmp',
|
||||
Column('author_id', types.Integer(), ForeignKey('authors.id'), primary_key=True),
|
||||
Column('song_id', types.Integer(), ForeignKey('songs.id'), primary_key=True),
|
||||
Column('author_type', types.String(), primary_key=True,
|
||||
nullable=False, server_default=text('""')))
|
||||
op.execute('INSERT INTO authors_songs_tmp SELECT author_id, song_id, "" FROM authors_songs')
|
||||
op.drop_table('authors_songs')
|
||||
op.rename_table('authors_songs_tmp', 'authors_songs')
|
||||
else:
|
||||
log.warning('Skipping upgrade_4 step of upgrading the song db')
|
||||
|
Loading…
Reference in New Issue
Block a user