##// END OF EJS Templates
fixed imports on migrate, added getting current version from database
marcink -
r835:08d2dcd7 beta
parent child Browse files
Show More
@@ -0,0 +1,20 b''
1 [db_settings]
2 # Used to identify which repository this database is versioned under.
3 # You can use the name of your project.
4 repository_id=rhodecode_db_migrations
5
6 # The name of the database table used to track the schema version.
7 # This name shouldn't already be used by your project.
8 # If this is changed once a database is under version control, you'll need to
9 # change the table name in each database too.
10 version_table=db_migrate_version
11
12 # When committing a change script, Migrate will attempt to generate the
13 # sql for all supported databases; normally, if one of them fails - probably
14 # because you don't have that database installed - it is ignored and the
15 # commit continues, perhaps ending successfully.
16 # Databases in this list MUST compile successfully during a commit, or the
17 # entire commit will fail. List the databases your application will actually
18 # be using to ensure your updates to that database work properly.
19 # This must be a list; example: ['postgres','sqlite']
20 required_dbs=['sqlite']
@@ -1,8 +1,15 b''
1 #!/usr/bin/env python
2 # encoding: utf-8
3 # database management for RhodeCode
4 # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
5 #
1 # -*- coding: utf-8 -*-
2 """
3 rhodecode.lib.db_manage
4 ~~~~~~~~~~~~~~~~~~~~~~~
5
6 Database creation, and setup module for RhodeCode
7
8 :created_on: Apr 10, 2010
9 :author: marcink
10 :copyright: (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
11 :license: GPLv3, see COPYING for more details.
12 """
6 13 # This program is free software; you can redistribute it and/or
7 14 # modify it under the terms of the GNU General Public License
8 15 # as published by the Free Software Foundation; version 2
@@ -18,25 +25,24 b''
18 25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 26 # MA 02110-1301, USA.
20 27
21 """
22 Created on April 10, 2010
23 database management and creation for RhodeCode
24 @author: marcink
25 """
26
27 from os.path import dirname as dn, join as jn
28 28 import os
29 29 import sys
30 30 import uuid
31 import logging
32 from os.path import dirname as dn, join as jn
33
34 from rhodecode import __dbversion__
35 from rhodecode.model.db import
36 from rhodecode.model import meta
31 37
32 38 from rhodecode.lib.auth import get_crypt_password
33 39 from rhodecode.lib.utils import ask_ok
34 40 from rhodecode.model import init_model
35 41 from rhodecode.model.db import User, Permission, RhodeCodeUi, RhodeCodeSettings, \
36 UserToPerm
37 from rhodecode.model import meta
42 UserToPerm, DbMigrateVersion
43
38 44 from sqlalchemy.engine import create_engine
39 import logging
45
40 46
41 47 log = logging.getLogger(__name__)
42 48
@@ -83,8 +89,6 b' class DbManage(object):'
83 89
84 90
85 91 def set_db_version(self):
86 from rhodecode import __dbversion__
87 from rhodecode.model.db import DbMigrateVersion
88 92 try:
89 93 ver = DbMigrateVersion()
90 94 ver.version = __dbversion__
@@ -25,10 +25,14 b''
25 25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26 26 # MA 02110-1301, USA.
27 27
28 from rhodecode.lib.utils import BasePasterCommand
28 import logging
29 from sqlalchemy import engine_from_config
30
31 from rhodecode.lib.dbmigrate.migrate.exceptions import \
32 DatabaseNotControlledError
29 33 from rhodecode.lib.utils import BasePasterCommand, Command, add_cache
30 34
31 from sqlalchemy import engine_from_config
35 log = logging.getLogger(__name__)
32 36
33 37 class UpgradeDb(BasePasterCommand):
34 38 """Command used for paster to upgrade our database to newer version
@@ -46,10 +50,24 b' class UpgradeDb(BasePasterCommand):'
46 50 def command(self):
47 51 from pylons import config
48 52 add_cache(config)
49 engine = engine_from_config(config, 'sqlalchemy.db1.')
50 print engine
51 raise NotImplementedError('Not implemented yet')
53 #engine = engine_from_config(config, 'sqlalchemy.db1.')
54 #rint engine
55
56 from rhodecode.lib.dbmigrate.migrate.versioning import api
57 path = 'rhodecode/lib/dbmigrate'
58
52 59
60 try:
61 curr_version = api.db_version(config['sqlalchemy.db1.url'], path)
62 msg = ('Found current database under version'
63 ' control with version %s' % curr_version)
64
65 except (RuntimeError, DatabaseNotControlledError), e:
66 curr_version = 0
67 msg = ('Current database is not under version control setting'
68 ' as version %s' % curr_version)
69
70 print msg
53 71
54 72 def update_parser(self):
55 73 self.parser.add_option('--sql',
@@ -5,5 +5,5 b''
5 5 using Python.
6 6 """
7 7
8 from migrate.versioning import *
9 from migrate.changeset import *
8 from rhodecode.lib.dbmigrate.migrate.versioning import *
9 from rhodecode.lib.dbmigrate.migrate.changeset import *
@@ -18,8 +18,8 b' SQLA_06 = _sa_version >= (0, 6)'
18 18 del re
19 19 del _sa_version
20 20
21 from migrate.changeset.schema import *
22 from migrate.changeset.constraint import *
21 from rhodecode.lib.dbmigrate.migrate.changeset.schema import *
22 from rhodecode.lib.dbmigrate.migrate.changeset.constraint import *
23 23
24 24 sqlalchemy.schema.Table.__bases__ += (ChangesetTable, )
25 25 sqlalchemy.schema.Column.__bases__ += (ChangesetColumn, )
@@ -16,8 +16,8 b' from sqlalchemy.schema import (ForeignKe'
16 16 UniqueConstraint,
17 17 Index)
18 18
19 from migrate import exceptions
20 from migrate.changeset import constraint, SQLA_06
19 from rhodecode.lib.dbmigrate.migrate import exceptions
20 from rhodecode.lib.dbmigrate.migrate.changeset import constraint, SQLA_06
21 21
22 22 if not SQLA_06:
23 23 from sqlalchemy.sql.compiler import SchemaGenerator, SchemaDropper
@@ -3,8 +3,8 b''
3 3 """
4 4 from sqlalchemy import schema
5 5
6 from migrate.exceptions import *
7 from migrate.changeset import SQLA_06
6 from rhodecode.lib.dbmigrate.migrate.exceptions import *
7 from rhodecode.lib.dbmigrate.migrate.changeset import SQLA_06
8 8
9 9 class ConstraintChangeset(object):
10 10 """Base class for Constraint classes."""
@@ -27,7 +27,7 b' class ConstraintChangeset(object):'
27 27
28 28 def __do_imports(self, visitor_name, *a, **kw):
29 29 engine = kw.pop('engine', self.table.bind)
30 from migrate.changeset.databases.visitor import (get_engine_visitor,
30 from rhodecode.lib.dbmigrate.migrate.changeset.databases.visitor import (get_engine_visitor,
31 31 run_single_visitor)
32 32 visitorcallable = get_engine_visitor(engine, visitor_name)
33 33 run_single_visitor(engine, visitorcallable, self, *a, **kw)
@@ -3,8 +3,8 b''
3 3 """
4 4 from sqlalchemy.databases import firebird as sa_base
5 5
6 from migrate import exceptions
7 from migrate.changeset import ansisql, SQLA_06
6 from rhodecode.lib.dbmigrate.migrate import exceptions
7 from rhodecode.lib.dbmigrate.migrate.changeset import ansisql, SQLA_06
8 8
9 9
10 10 if SQLA_06:
@@ -5,8 +5,8 b''
5 5 from sqlalchemy.databases import mysql as sa_base
6 6 from sqlalchemy import types as sqltypes
7 7
8 from migrate import exceptions
9 from migrate.changeset import ansisql, SQLA_06
8 from rhodecode.lib.dbmigrate.migrate import exceptions
9 from rhodecode.lib.dbmigrate.migrate.changeset import ansisql, SQLA_06
10 10
11 11
12 12 if not SQLA_06:
@@ -4,8 +4,8 b''
4 4 import sqlalchemy as sa
5 5 from sqlalchemy.databases import oracle as sa_base
6 6
7 from migrate import exceptions
8 from migrate.changeset import ansisql, SQLA_06
7 from rhodecode.lib.dbmigrate.migrate import exceptions
8 from rhodecode.lib.dbmigrate.migrate.changeset import ansisql, SQLA_06
9 9
10 10
11 11 if not SQLA_06:
@@ -3,7 +3,7 b''
3 3
4 4 .. _`PostgreSQL`: http://www.postgresql.org/
5 5 """
6 from migrate.changeset import ansisql, SQLA_06
6 from rhodecode.lib.dbmigrate.migrate.changeset import ansisql, SQLA_06
7 7
8 8 if not SQLA_06:
9 9 from sqlalchemy.databases import postgres as sa_base
@@ -8,8 +8,8 b' from copy import copy'
8 8
9 9 from sqlalchemy.databases import sqlite as sa_base
10 10
11 from migrate import exceptions
12 from migrate.changeset import ansisql, SQLA_06
11 from rhodecode.lib.dbmigrate.migrate import exceptions
12 from rhodecode.lib.dbmigrate.migrate.changeset import ansisql, SQLA_06
13 13
14 14
15 15 if not SQLA_06:
@@ -3,8 +3,8 b''
3 3 """
4 4 import sqlalchemy as sa
5 5
6 from migrate.changeset import ansisql
7 from migrate.changeset.databases import (sqlite,
6 from rhodecode.lib.dbmigrate.migrate.changeset import ansisql
7 from rhodecode.lib.dbmigrate.migrate.changeset.databases import (sqlite,
8 8 postgres,
9 9 mysql,
10 10 oracle,
@@ -10,9 +10,9 b' import sqlalchemy'
10 10 from sqlalchemy.schema import ForeignKeyConstraint
11 11 from sqlalchemy.schema import UniqueConstraint
12 12
13 from migrate.exceptions import *
14 from migrate.changeset import SQLA_06
15 from migrate.changeset.databases.visitor import (get_engine_visitor,
13 from rhodecode.lib.dbmigrate.migrate.exceptions import *
14 from rhodecode.lib.dbmigrate.migrate.changeset import SQLA_06
15 from rhodecode.lib.dbmigrate.migrate.changeset.databases.visitor import (get_engine_visitor,
16 16 run_single_visitor)
17 17
18 18
@@ -29,10 +29,10 b' import sys'
29 29 import inspect
30 30 import logging
31 31
32 from migrate import exceptions
33 from migrate.versioning import (repository, schema, version,
32 from rhodecode.lib.dbmigrate.migrate import exceptions
33 from rhodecode.lib.dbmigrate.migrate.versioning import (repository, schema, version,
34 34 script as script_) # command name conflict
35 from migrate.versioning.util import catch_known_errors, with_engine
35 from rhodecode.lib.dbmigrate.migrate.versioning.util import catch_known_errors, with_engine
36 36
37 37
38 38 log = logging.getLogger(__name__)
@@ -4,8 +4,8 b''
4 4
5 5 from ConfigParser import ConfigParser
6 6
7 from migrate.versioning.config import *
8 from migrate.versioning import pathed
7 from rhodecode.lib.dbmigrate.migrate.versioning.config import *
8 from rhodecode.lib.dbmigrate.migrate.versioning import pathed
9 9
10 10
11 11 class Parser(ConfigParser):
@@ -11,9 +11,8 b' import logging'
11 11
12 12 import sqlalchemy
13 13
14 import migrate
15 import migrate.changeset
16
14 from rhodecode.lib.dbmigrate import migrate
15 from rhodecode.lib.dbmigrate.migrate import changeset
17 16
18 17 log = logging.getLogger(__name__)
19 18 HEADER = """
@@ -112,17 +111,17 b' class ModelGenerator(object):'
112 111 out.append(")")
113 112 return out
114 113
115 def _get_tables(self,missingA=False,missingB=False,modified=False):
114 def _get_tables(self, missingA=False, missingB=False, modified=False):
116 115 to_process = []
117 for bool_,names,metadata in (
118 (missingA,self.diff.tables_missing_from_A,self.diff.metadataB),
119 (missingB,self.diff.tables_missing_from_B,self.diff.metadataA),
120 (modified,self.diff.tables_different,self.diff.metadataA),
116 for bool_, names, metadata in (
117 (missingA, self.diff.tables_missing_from_A, self.diff.metadataB),
118 (missingB, self.diff.tables_missing_from_B, self.diff.metadataA),
119 (modified, self.diff.tables_different, self.diff.metadataA),
121 120 ):
122 121 if bool_:
123 122 for name in names:
124 123 yield metadata.tables.get(name)
125
124
126 125 def toPython(self):
127 126 """Assume database is current and model is empty."""
128 127 out = []
@@ -138,10 +137,10 b' class ModelGenerator(object):'
138 137
139 138 def toUpgradeDowngradePython(self, indent=' '):
140 139 ''' Assume model is most current and database is out-of-date. '''
141 decls = ['from migrate.changeset import schema',
140 decls = ['from rhodecode.lib.dbmigrate.migrate.changeset import schema',
142 141 'meta = MetaData()']
143 142 for table in self._get_tables(
144 missingA=True,missingB=True,modified=True
143 missingA=True, missingB=True, modified=True
145 144 ):
146 145 decls.extend(self.getTableDefn(table))
147 146
@@ -182,7 +181,7 b' class ModelGenerator(object):'
182 181 '\n'.join([pre_command] + ['%s%s' % (indent, line) for line in upgradeCommands]),
183 182 '\n'.join([pre_command] + ['%s%s' % (indent, line) for line in downgradeCommands]))
184 183
185 def _db_can_handle_this_change(self,td):
184 def _db_can_handle_this_change(self, td):
186 185 if (td.columns_missing_from_B
187 186 and not td.columns_missing_from_A
188 187 and not td.columns_different):
@@ -208,9 +207,9 b' class ModelGenerator(object):'
208 207 dbTable = self.diff.metadataB.tables[tableName]
209 208
210 209 td = self.diff.tables_different[tableName]
211
210
212 211 if self._db_can_handle_this_change(td):
213
212
214 213 for col in td.columns_missing_from_B:
215 214 modelTable.columns[col].create()
216 215 for col in td.columns_missing_from_A:
@@ -6,9 +6,9 b' import os'
6 6 import shutil
7 7 import logging
8 8
9 from migrate import exceptions
10 from migrate.versioning.config import *
11 from migrate.versioning.util import KeyedInstance
9 from rhodecode.lib.dbmigrate.migrate import exceptions
10 from rhodecode.lib.dbmigrate.migrate.versioning.config import *
11 from rhodecode.lib.dbmigrate.migrate.versioning.util import KeyedInstance
12 12
13 13
14 14 log = logging.getLogger(__name__)
@@ -9,10 +9,10 b' import logging'
9 9 from pkg_resources import resource_filename
10 10 from tempita import Template as TempitaTemplate
11 11
12 from migrate import exceptions
13 from migrate.versioning import version, pathed, cfgparse
14 from migrate.versioning.template import Template
15 from migrate.versioning.config import *
12 from rhodecode.lib.dbmigrate.migrate import exceptions
13 from rhodecode.lib.dbmigrate.migrate.versioning import version, pathed, cfgparse
14 from rhodecode.lib.dbmigrate.migrate.versioning.template import Template
15 from rhodecode.lib.dbmigrate.migrate.versioning.config import *
16 16
17 17
18 18 log = logging.getLogger(__name__)
@@ -10,11 +10,11 b' from sqlalchemy.sql import and_'
10 10 from sqlalchemy import exceptions as sa_exceptions
11 11 from sqlalchemy.sql import bindparam
12 12
13 from migrate import exceptions
14 from migrate.versioning import genmodel, schemadiff
15 from migrate.versioning.repository import Repository
16 from migrate.versioning.util import load_model
17 from migrate.versioning.version import VerNum
13 from rhodecode.lib.dbmigrate.migrate import exceptions
14 from rhodecode.lib.dbmigrate.migrate.versioning import genmodel, schemadiff
15 from rhodecode.lib.dbmigrate.migrate.versioning.repository import Repository
16 from rhodecode.lib.dbmigrate.migrate.versioning.util import load_model
17 from rhodecode.lib.dbmigrate.migrate.versioning.version import VerNum
18 18
19 19
20 20 log = logging.getLogger(__name__)
@@ -5,7 +5,7 b''
5 5 import logging
6 6 import sqlalchemy
7 7
8 from migrate.changeset import SQLA_06
8 from rhodecode.lib.dbmigrate.migrate.changeset import SQLA_06
9 9 from sqlalchemy.types import Float
10 10
11 11 log = logging.getLogger(__name__)
@@ -1,6 +1,6 b''
1 1 #!/usr/bin/env python
2 2 # -*- coding: utf-8 -*-
3 3
4 from migrate.versioning.script.base import BaseScript
5 from migrate.versioning.script.py import PythonScript
6 from migrate.versioning.script.sql import SqlScript
4 from rhodecode.lib.dbmigrate.migrate.versioning.script.base import BaseScript
5 from rhodecode.lib.dbmigrate.migrate.versioning.script.py import PythonScript
6 from rhodecode.lib.dbmigrate.migrate.versioning.script.sql import SqlScript
@@ -2,9 +2,9 b''
2 2 # -*- coding: utf-8 -*-
3 3 import logging
4 4
5 from migrate import exceptions
6 from migrate.versioning.config import operations
7 from migrate.versioning import pathed
5 from rhodecode.lib.dbmigrate.migrate import exceptions
6 from rhodecode.lib.dbmigrate.migrate.versioning.config import operations
7 from rhodecode.lib.dbmigrate.migrate.versioning import pathed
8 8
9 9
10 10 log = logging.getLogger(__name__)
@@ -6,13 +6,13 b' import warnings'
6 6 import logging
7 7 from StringIO import StringIO
8 8
9 import migrate
10 from migrate.versioning import genmodel, schemadiff
11 from migrate.versioning.config import operations
12 from migrate.versioning.template import Template
13 from migrate.versioning.script import base
14 from migrate.versioning.util import import_path, load_model, with_engine
15 from migrate.exceptions import MigrateDeprecationWarning, InvalidScriptError, ScriptError
9 from rhodecode.lib.dbmigrate import migrate
10 from rhodecode.lib.dbmigrate.migrate.versioning import genmodel, schemadiff
11 from rhodecode.lib.dbmigrate.migrate.versioning.config import operations
12 from rhodecode.lib.dbmigrate.migrate.versioning.template import Template
13 from rhodecode.lib.dbmigrate.migrate.versioning.script import base
14 from rhodecode.lib.dbmigrate.migrate.versioning.util import import_path, load_model, with_engine
15 from rhodecode.lib.dbmigrate.migrate.exceptions import MigrateDeprecationWarning, InvalidScriptError, ScriptError
16 16
17 17 log = logging.getLogger(__name__)
18 18 __all__ = ['PythonScript']
@@ -49,10 +49,10 b' class PythonScript(base.BaseScript):'
49 49 :returns: Upgrade / Downgrade script
50 50 :rtype: string
51 51 """
52
52
53 53 if isinstance(repository, basestring):
54 54 # oh dear, an import cycle!
55 from migrate.versioning.repository import Repository
55 from rhodecode.lib.dbmigrate.migrate.versioning.repository import Repository
56 56 repository = Repository(repository)
57 57
58 58 oldmodel = load_model(oldmodel)
@@ -65,7 +65,7 b' class PythonScript(base.BaseScript):'
65 65 excludeTables=[repository.version_table])
66 66 # TODO: diff can be False (there is no difference?)
67 67 decls, upgradeCommands, downgradeCommands = \
68 genmodel.ModelGenerator(diff,engine).toUpgradeDowngradePython()
68 genmodel.ModelGenerator(diff, engine).toUpgradeDowngradePython()
69 69
70 70 # Store differences into file.
71 71 src = Template(opts.pop('templates_path', None)).get_script(opts.pop('templates_theme', None))
@@ -3,8 +3,8 b''
3 3 import logging
4 4 import shutil
5 5
6 from migrate.versioning.script import base
7 from migrate.versioning.template import Template
6 from rhodecode.lib.dbmigrate.migrate.versioning.script import base
7 from rhodecode.lib.dbmigrate.migrate.versioning.template import Template
8 8
9 9
10 10 log = logging.getLogger(__name__)
@@ -8,10 +8,10 b' import inspect'
8 8 import logging
9 9 from optparse import OptionParser, BadOptionError
10 10
11 from migrate import exceptions
12 from migrate.versioning import api
13 from migrate.versioning.config import *
14 from migrate.versioning.util import asbool
11 from rhodecode.lib.dbmigrate.migrate import exceptions
12 from rhodecode.lib.dbmigrate.migrate.versioning import api
13 from rhodecode.lib.dbmigrate.migrate.versioning.config import *
14 from rhodecode.lib.dbmigrate.migrate.versioning.util import asbool
15 15
16 16
17 17 alias = dict(
@@ -7,8 +7,8 b' import sys'
7 7
8 8 from pkg_resources import resource_filename
9 9
10 from migrate.versioning.config import *
11 from migrate.versioning import pathed
10 from rhodecode.lib.dbmigrate.migrate.versioning.config import *
11 from rhodecode.lib.dbmigrate.migrate.versioning import pathed
12 12
13 13
14 14 class Collection(pathed.Pathed):
@@ -34,7 +34,7 b' class SQLScriptCollection(Collection):'
34 34 class Template(pathed.Pathed):
35 35 """Finds the paths/packages of various Migrate templates.
36 36
37 :param path: Templates are loaded from migrate package
37 :param path: Templates are loaded from rhodecode.lib.dbmigrate.migrate package
38 38 if `path` is not provided.
39 39 """
40 40 pkg = 'migrate.versioning.templates'
@@ -11,9 +11,9 b' from sqlalchemy import create_engine'
11 11 from sqlalchemy.engine import Engine
12 12 from sqlalchemy.pool import StaticPool
13 13
14 from migrate import exceptions
15 from migrate.versioning.util.keyedinstance import KeyedInstance
16 from migrate.versioning.util.importpath import import_path
14 from rhodecode.lib.dbmigrate.migrate import exceptions
15 from rhodecode.lib.dbmigrate.migrate.versioning.util.keyedinstance import KeyedInstance
16 from rhodecode.lib.dbmigrate.migrate.versioning.util.importpath import import_path
17 17
18 18
19 19 log = logging.getLogger(__name__)
@@ -6,8 +6,8 b' import re'
6 6 import shutil
7 7 import logging
8 8
9 from migrate import exceptions
10 from migrate.versioning import pathed, script
9 from rhodecode.lib.dbmigrate.migrate import exceptions
10 from rhodecode.lib.dbmigrate.migrate.versioning import pathed, script
11 11
12 12
13 13 log = logging.getLogger(__name__)
General Comments 0
You need to be logged in to leave comments. Login now