~suutari-olli/openlp/click-slide-to-go-live-from-blank

« back to all changes in this revision

Viewing changes to openlp/core/common/db.py

  • Committer: Simon Hanna
  • Date: 2016-05-17 08:48:19 UTC
  • mfrom: (2625.1.36 openlp)
  • mto: (2625.1.37 openlp)
  • mto: This revision was merged to the branch mainline in revision 2649.
  • Revision ID: simon.hanna@serve-me.info-20160517084819-lgup78nzyzjympuu
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
 
3
 
 
4
###############################################################################
 
5
# OpenLP - Open Source Lyrics Projection                                      #
 
6
# --------------------------------------------------------------------------- #
 
7
# Copyright (c) 2008-2016 OpenLP Developers                                   #
 
8
# --------------------------------------------------------------------------- #
 
9
# This program is free software; you can redistribute it and/or modify it     #
 
10
# under the terms of the GNU General Public License as published by the Free  #
 
11
# Software Foundation; version 2 of the License.                              #
 
12
#                                                                             #
 
13
# This program is distributed in the hope that it will be useful, but WITHOUT #
 
14
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
 
15
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for    #
 
16
# more details.                                                               #
 
17
#                                                                             #
 
18
# You should have received a copy of the GNU General Public License along     #
 
19
# with this program; if not, write to the Free Software Foundation, Inc., 59  #
 
20
# Temple Place, Suite 330, Boston, MA 02111-1307 USA                          #
 
21
###############################################################################
 
22
"""
 
23
The :mod:`db` module provides helper functions for database related methods.
 
24
"""
 
25
import sqlalchemy
 
26
import logging
 
27
 
 
28
from copy import deepcopy
 
29
 
 
30
log = logging.getLogger(__name__)
 
31
 
 
32
 
 
33
def drop_column(op, tablename, columnname):
 
34
    drop_columns(op, tablename, [columnname])
 
35
 
 
36
 
 
37
def drop_columns(op, tablename, columns):
 
38
    """
 
39
    Column dropping functionality for SQLite, as there is no DROP COLUMN support in SQLite
 
40
 
 
41
    From https://github.com/klugjohannes/alembic-sqlite
 
42
    """
 
43
 
 
44
    # get the db engine and reflect database tables
 
45
    engine = op.get_bind()
 
46
    meta = sqlalchemy.MetaData(bind=engine)
 
47
    meta.reflect()
 
48
 
 
49
    # create a select statement from the old table
 
50
    old_table = meta.tables[tablename]
 
51
    select = sqlalchemy.sql.select([c for c in old_table.c if c.name not in columns])
 
52
 
 
53
    # get remaining columns without table attribute attached
 
54
    remaining_columns = [deepcopy(c) for c in old_table.columns if c.name not in columns]
 
55
    for column in remaining_columns:
 
56
        column.table = None
 
57
 
 
58
    # create a temporary new table
 
59
    new_tablename = '{0}_new'.format(tablename)
 
60
    op.create_table(new_tablename, *remaining_columns)
 
61
    meta.reflect()
 
62
    new_table = meta.tables[new_tablename]
 
63
 
 
64
    # copy data from old table
 
65
    insert = sqlalchemy.sql.insert(new_table).from_select([c.name for c in remaining_columns], select)
 
66
    engine.execute(insert)
 
67
 
 
68
    # drop the old table and rename the new table to take the old tables
 
69
    # position
 
70
    op.drop_table(tablename)
 
71
    op.rename_table(new_tablename, tablename)