Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-backend / pithos / backends / migrate.py @ c4399e62

History | View | Annotate | Download (3.6 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 6a82f89f Sofia Papagiannaki
from pithos.backends.lib.sqlalchemy import node, groups, public, xfeatures
54 6a82f89f Sofia Papagiannaki
55 e95431e5 Georgios D. Tsoukalas
os.environ['DJANGO_SETTINGS_MODULE'] = 'synnefo.settings'
56 e95431e5 Georgios D. Tsoukalas
57 4051c82a Kostas Papadimitriou
try:
58 4051c82a Kostas Papadimitriou
    # pithos-app case
59 4051c82a Kostas Papadimitriou
    from synnefo.settings import PITHOS_BACKEND_DB_CONNECTION
60 4051c82a Kostas Papadimitriou
except ImportError:
61 4051c82a Kostas Papadimitriou
    try:
62 4051c82a Kostas Papadimitriou
        # plankton case
63 4051c82a Kostas Papadimitriou
        from synnefo.settings import BACKEND_DB_CONNECTION as \
64 4051c82a Kostas Papadimitriou
            PITHOS_BACKEND_DB_CONNECTION
65 4051c82a Kostas Papadimitriou
    except ImportError:
66 4051c82a Kostas Papadimitriou
        PITHOS_BACKEND_DB_CONNECTION = None
67 e3240ba0 Kostas Papadimitriou
68 6a82f89f Sofia Papagiannaki
import sqlalchemy as sa
69 0a47073c Kostas Papadimitriou
70 0a47073c Kostas Papadimitriou
DEFAULT_ALEMBIC_INI_PATH = os.path.join(
71 2715ade4 Sofia Papagiannaki
    os.path.abspath(os.path.dirname(sqlalchemy_backend.__file__)),
72 2715ade4 Sofia Papagiannaki
    'alembic.ini')
73 0a47073c Kostas Papadimitriou
74 e3240ba0 Kostas Papadimitriou
75 6a82f89f Sofia Papagiannaki
def initialize_db():
76 6a82f89f Sofia Papagiannaki
    alembic_cfg = Config(DEFAULT_ALEMBIC_INI_PATH)
77 e3240ba0 Kostas Papadimitriou
78 2715ade4 Sofia Papagiannaki
    db = alembic_cfg.get_main_option(
79 2715ade4 Sofia Papagiannaki
        "sqlalchemy.url", PITHOS_BACKEND_DB_CONNECTION)
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 e3240ba0 Kostas Papadimitriou
91 6a82f89f Sofia Papagiannaki
    # then, load the Alembic configuration and generate the
92 6a82f89f Sofia Papagiannaki
    # version table, "stamping" it with the most recent rev:
93 6a82f89f Sofia Papagiannaki
    command.stamp(alembic_cfg, "head")
94 6a82f89f Sofia Papagiannaki
95 6a82f89f Sofia Papagiannaki
96 0a47073c Kostas Papadimitriou
def main(argv=None, **kwargs):
97 0a47073c Kostas Papadimitriou
    if not argv:
98 0a47073c Kostas Papadimitriou
        argv = sys.argv
99 e3240ba0 Kostas Papadimitriou
100 0a47073c Kostas Papadimitriou
    # clean up args
101 0a47073c Kostas Papadimitriou
    argv.pop(0)
102 e3240ba0 Kostas Papadimitriou
103 e3240ba0 Kostas Papadimitriou
    if len(argv) >= 1 and argv[0] == 'initdb':
104 e3240ba0 Kostas Papadimitriou
        print "Initializing db."
105 6a82f89f Sofia Papagiannaki
        initialize_db()
106 e3240ba0 Kostas Papadimitriou
        print "DB initialized."
107 e3240ba0 Kostas Papadimitriou
        exit(1)
108 e3240ba0 Kostas Papadimitriou
109 0a47073c Kostas Papadimitriou
    # default config arg, if not already set
110 0a47073c Kostas Papadimitriou
    if not '-c' in argv:
111 0a47073c Kostas Papadimitriou
        argv.insert(0, DEFAULT_ALEMBIC_INI_PATH)
112 0a47073c Kostas Papadimitriou
        argv.insert(0, '-c')
113 0a47073c Kostas Papadimitriou
114 0a47073c Kostas Papadimitriou
    alembic_main(argv, **kwargs)
115 0a47073c Kostas Papadimitriou
if __name__ == '__main__':
116 0a47073c Kostas Papadimitriou
    import sys
117 0a47073c Kostas Papadimitriou
    main(sys.argv)