Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-backend / pithos / backends / migrate.py @ 6e0f3e65

History | View | Annotate | Download (3.7 kB)

1 0a47073c Kostas Papadimitriou
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2 0a47073c Kostas Papadimitriou
#
3 0a47073c Kostas Papadimitriou
# Redistribution and use in source and binary forms, with or
4 0a47073c Kostas Papadimitriou
# without modification, are permitted provided that the following
5 0a47073c Kostas Papadimitriou
# conditions are met:
6 0a47073c Kostas Papadimitriou
#
7 0a47073c Kostas Papadimitriou
#   1. Redistributions of source code must retain the above
8 0a47073c Kostas Papadimitriou
#      copyright notice, this list of conditions and the following
9 0a47073c Kostas Papadimitriou
#      disclaimer.
10 0a47073c Kostas Papadimitriou
#
11 0a47073c Kostas Papadimitriou
#   2. Redistributions in binary form must reproduce the above
12 0a47073c Kostas Papadimitriou
#      copyright notice, this list of conditions and the following
13 0a47073c Kostas Papadimitriou
#      disclaimer in the documentation and/or other materials
14 0a47073c Kostas Papadimitriou
#      provided with the distribution.
15 0a47073c Kostas Papadimitriou
#
16 0a47073c Kostas Papadimitriou
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 0a47073c Kostas Papadimitriou
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 0a47073c Kostas Papadimitriou
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 0a47073c Kostas Papadimitriou
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 0a47073c Kostas Papadimitriou
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 0a47073c Kostas Papadimitriou
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 0a47073c Kostas Papadimitriou
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 0a47073c Kostas Papadimitriou
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 0a47073c Kostas Papadimitriou
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 0a47073c Kostas Papadimitriou
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 0a47073c Kostas Papadimitriou
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 0a47073c Kostas Papadimitriou
# POSSIBILITY OF SUCH DAMAGE.
28 0a47073c Kostas Papadimitriou
#
29 0a47073c Kostas Papadimitriou
# The views and conclusions contained in the software and
30 0a47073c Kostas Papadimitriou
# documentation are those of the authors and should not be
31 0a47073c Kostas Papadimitriou
# interpreted as representing official policies, either expressed
32 0a47073c Kostas Papadimitriou
# or implied, of GRNET S.A.
33 0a47073c Kostas Papadimitriou
34 0a47073c Kostas Papadimitriou
"""
35 0a47073c Kostas Papadimitriou
Alembic migration wrapper for pithos backend database.
36 0a47073c Kostas Papadimitriou

37 0a47073c Kostas Papadimitriou
- Locate alembic.ini in backends/lib/sqlalchemy package and pass it
38 0a47073c Kostas Papadimitriou
  as parameter to alembic
39 0a47073c Kostas Papadimitriou

40 0a47073c Kostas Papadimitriou
e.g::
41 0a47073c Kostas Papadimitriou

42 0a47073c Kostas Papadimitriou
    $ pithos-migrate upgrade head
43 0a47073c Kostas Papadimitriou

44 0a47073c Kostas Papadimitriou
"""
45 0a47073c Kostas Papadimitriou
46 0a47073c Kostas Papadimitriou
import sys
47 0a47073c Kostas Papadimitriou
import os
48 0a47073c Kostas Papadimitriou
49 6a82f89f Sofia Papagiannaki
from alembic.config import main as alembic_main, Config
50 6a82f89f Sofia Papagiannaki
from alembic import context, command
51 6a82f89f Sofia Papagiannaki
52 0a47073c Kostas Papadimitriou
from pithos.backends.lib import sqlalchemy as sqlalchemy_backend
53 bc4f25c0 Sofia Papagiannaki
from pithos.backends.lib.sqlalchemy import (node, groups, public, xfeatures,
54 bc4f25c0 Sofia Papagiannaki
                                            quotaholder_serials)
55 6a82f89f Sofia Papagiannaki
56 e95431e5 Georgios D. Tsoukalas
os.environ['DJANGO_SETTINGS_MODULE'] = 'synnefo.settings'
57 e95431e5 Georgios D. Tsoukalas
58 4051c82a Kostas Papadimitriou
try:
59 4051c82a Kostas Papadimitriou
    # pithos-app case
60 4051c82a Kostas Papadimitriou
    from synnefo.settings import PITHOS_BACKEND_DB_CONNECTION
61 4051c82a Kostas Papadimitriou
except ImportError:
62 4051c82a Kostas Papadimitriou
    try:
63 4051c82a Kostas Papadimitriou
        # plankton case
64 4051c82a Kostas Papadimitriou
        from synnefo.settings import BACKEND_DB_CONNECTION as \
65 4051c82a Kostas Papadimitriou
            PITHOS_BACKEND_DB_CONNECTION
66 4051c82a Kostas Papadimitriou
    except ImportError:
67 4051c82a Kostas Papadimitriou
        PITHOS_BACKEND_DB_CONNECTION = None
68 e3240ba0 Kostas Papadimitriou
69 6a82f89f Sofia Papagiannaki
import sqlalchemy as sa
70 0a47073c Kostas Papadimitriou
71 0a47073c Kostas Papadimitriou
DEFAULT_ALEMBIC_INI_PATH = os.path.join(
72 2715ade4 Sofia Papagiannaki
    os.path.abspath(os.path.dirname(sqlalchemy_backend.__file__)),
73 2715ade4 Sofia Papagiannaki
    'alembic.ini')
74 0a47073c Kostas Papadimitriou
75 e3240ba0 Kostas Papadimitriou
76 bc4f25c0 Sofia Papagiannaki
def initialize_db(dbconnection):
77 6a82f89f Sofia Papagiannaki
    alembic_cfg = Config(DEFAULT_ALEMBIC_INI_PATH)
78 e3240ba0 Kostas Papadimitriou
79 bc4f25c0 Sofia Papagiannaki
    db = alembic_cfg.get_main_option("sqlalchemy.url", dbconnection)
80 e3240ba0 Kostas Papadimitriou
    alembic_cfg.set_main_option("sqlalchemy.url", db)
81 e3240ba0 Kostas Papadimitriou
82 6a82f89f Sofia Papagiannaki
    engine = sa.engine_from_config(
83 2715ade4 Sofia Papagiannaki
        alembic_cfg.get_section(alembic_cfg.config_ini_section),
84 2715ade4 Sofia Papagiannaki
        prefix='sqlalchemy.')
85 e3240ba0 Kostas Papadimitriou
86 6a82f89f Sofia Papagiannaki
    node.create_tables(engine)
87 6a82f89f Sofia Papagiannaki
    groups.create_tables(engine)
88 6a82f89f Sofia Papagiannaki
    public.create_tables(engine)
89 6a82f89f Sofia Papagiannaki
    xfeatures.create_tables(engine)
90 bc4f25c0 Sofia Papagiannaki
    quotaholder_serials.create_tables(engine)
91 e3240ba0 Kostas Papadimitriou
92 6a82f89f Sofia Papagiannaki
    # then, load the Alembic configuration and generate the
93 6a82f89f Sofia Papagiannaki
    # version table, "stamping" it with the most recent rev:
94 6a82f89f Sofia Papagiannaki
    command.stamp(alembic_cfg, "head")
95 6a82f89f Sofia Papagiannaki
96 6a82f89f Sofia Papagiannaki
97 0a47073c Kostas Papadimitriou
def main(argv=None, **kwargs):
98 0a47073c Kostas Papadimitriou
    if not argv:
99 0a47073c Kostas Papadimitriou
        argv = sys.argv
100 e3240ba0 Kostas Papadimitriou
101 0a47073c Kostas Papadimitriou
    # clean up args
102 0a47073c Kostas Papadimitriou
    argv.pop(0)
103 e3240ba0 Kostas Papadimitriou
104 e3240ba0 Kostas Papadimitriou
    if len(argv) >= 1 and argv[0] == 'initdb':
105 e3240ba0 Kostas Papadimitriou
        print "Initializing db."
106 bc4f25c0 Sofia Papagiannaki
        initialize_db(PITHOS_BACKEND_DB_CONNECTION)
107 e3240ba0 Kostas Papadimitriou
        print "DB initialized."
108 e3240ba0 Kostas Papadimitriou
        exit(1)
109 e3240ba0 Kostas Papadimitriou
110 0a47073c Kostas Papadimitriou
    # default config arg, if not already set
111 0a47073c Kostas Papadimitriou
    if not '-c' in argv:
112 0a47073c Kostas Papadimitriou
        argv.insert(0, DEFAULT_ALEMBIC_INI_PATH)
113 0a47073c Kostas Papadimitriou
        argv.insert(0, '-c')
114 0a47073c Kostas Papadimitriou
115 0a47073c Kostas Papadimitriou
    alembic_main(argv, **kwargs)
116 0a47073c Kostas Papadimitriou
if __name__ == '__main__':
117 0a47073c Kostas Papadimitriou
    import sys
118 0a47073c Kostas Papadimitriou
    main(sys.argv)