Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-backend / pithos / backends / migrate.py @ 8ee4ec50

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