##// END OF EJS Templates
dbmigrations:...
marcink -
r836:28a4bb11 beta
parent child Browse files
Show More
@@ -29,7 +29,7 b''
29
29
30 VERSION = (1, 1, 0, 'beta')
30 VERSION = (1, 1, 0, 'beta')
31 __version__ = '.'.join((str(each) for each in VERSION[:4]))
31 __version__ = '.'.join((str(each) for each in VERSION[:4]))
32 __dbversion__ = 1 #defines current db version for migrations
32 __dbversion__ = 2 #defines current db version for migrations
33
33
34 from rhodecode.lib.utils import get_current_revision
34 from rhodecode.lib.utils import get_current_revision
35 _rev = get_current_revision()
35 _rev = get_current_revision()
@@ -28,6 +28,8 b''
28 import logging
28 import logging
29 from sqlalchemy import engine_from_config
29 from sqlalchemy import engine_from_config
30
30
31 from rhodecode import __dbversion__
32 from rhodecode.lib.dbmigrate.migrate.versioning import api
31 from rhodecode.lib.dbmigrate.migrate.exceptions import \
33 from rhodecode.lib.dbmigrate.migrate.exceptions import \
32 DatabaseNotControlledError
34 DatabaseNotControlledError
33 from rhodecode.lib.utils import BasePasterCommand, Command, add_cache
35 from rhodecode.lib.utils import BasePasterCommand, Command, add_cache
@@ -49,25 +51,32 b' class UpgradeDb(BasePasterCommand):'
49
51
50 def command(self):
52 def command(self):
51 from pylons import config
53 from pylons import config
54
52 add_cache(config)
55 add_cache(config)
53 #engine = engine_from_config(config, 'sqlalchemy.db1.')
54 #rint engine
55
56
56 from rhodecode.lib.dbmigrate.migrate.versioning import api
57 #engine = engine_from_config(config, 'sqlalchemy.db1.')
57 path = 'rhodecode/lib/dbmigrate'
58
58
59 repository_path = 'rhodecode/lib/dbmigrate'
60 db_uri = config['sqlalchemy.db1.url']
59
61
60 try:
62 try:
61 curr_version = api.db_version(config['sqlalchemy.db1.url'], path)
63 curr_version = api.db_version(db_uri, repository_path)
62 msg = ('Found current database under version'
64 msg = ('Found current database under version'
63 ' control with version %s' % curr_version)
65 ' control with version %s' % curr_version)
64
66
65 except (RuntimeError, DatabaseNotControlledError), e:
67 except (RuntimeError, DatabaseNotControlledError), e:
66 curr_version = 0
68 curr_version = 1
67 msg = ('Current database is not under version control setting'
69 msg = ('Current database is not under version control setting'
68 ' as version %s' % curr_version)
70 ' as version %s' % curr_version)
71 api.version_control(db_uri, repository_path, curr_version)
72
69
73
70 print msg
74 print msg
75 #now we have our dbversion we can do upgrade
76
77 msg = 'attempting to do database upgrade to version %s' % __dbversion__
78 print msg
79 api.upgrade(db_uri, repository_path, __dbversion__)
71
80
72 def update_parser(self):
81 def update_parser(self):
73 self.parser.add_option('--sql',
82 self.parser.add_option('--sql',
@@ -119,7 +119,7 b' def alter_column(*p, **k):'
119
119
120
120
121 """
121 """
122
122
123 k.setdefault('alter_metadata', DEFAULT_ALTER_METADATA)
123 k.setdefault('alter_metadata', DEFAULT_ALTER_METADATA)
124
124
125 if 'table' not in k and isinstance(p[0], sqlalchemy.Column):
125 if 'table' not in k and isinstance(p[0], sqlalchemy.Column):
@@ -367,7 +367,7 b' class ColumnDelta(DictMixin, sqlalchemy.'
367 for_update=True))
367 for_update=True))
368 if toinit:
368 if toinit:
369 column._init_items(*toinit)
369 column._init_items(*toinit)
370
370
371 if not SQLA_06:
371 if not SQLA_06:
372 column.args = []
372 column.args = []
373
373
@@ -573,12 +573,12 b' populated with defaults'
573
573
574 def _col_name_in_constraint(self, cons, name):
574 def _col_name_in_constraint(self, cons, name):
575 return False
575 return False
576
576
577 def remove_from_table(self, table, unset_table=True):
577 def remove_from_table(self, table, unset_table=True):
578 # TODO: remove primary keys, constraints, etc
578 # TODO: remove primary keys, constraints, etc
579 if unset_table:
579 if unset_table:
580 self.table = None
580 self.table = None
581
581
582 to_drop = set()
582 to_drop = set()
583 for index in table.indexes:
583 for index in table.indexes:
584 columns = []
584 columns = []
@@ -590,7 +590,7 b' populated with defaults'
590 else:
590 else:
591 to_drop.add(index)
591 to_drop.add(index)
592 table.indexes = table.indexes - to_drop
592 table.indexes = table.indexes - to_drop
593
593
594 to_drop = set()
594 to_drop = set()
595 for cons in table.constraints:
595 for cons in table.constraints:
596 # TODO: deal with other types of constraint
596 # TODO: deal with other types of constraint
@@ -602,7 +602,7 b' populated with defaults'
602 if self.name == col_name:
602 if self.name == col_name:
603 to_drop.add(cons)
603 to_drop.add(cons)
604 table.constraints = table.constraints - to_drop
604 table.constraints = table.constraints - to_drop
605
605
606 if table.c.contains_column(self):
606 if table.c.contains_column(self):
607 table.c.remove(self)
607 table.c.remove(self)
608
608
@@ -30,8 +30,8 b' import inspect'
30 import logging
30 import logging
31
31
32 from rhodecode.lib.dbmigrate.migrate import exceptions
32 from rhodecode.lib.dbmigrate.migrate import exceptions
33 from rhodecode.lib.dbmigrate.migrate.versioning import (repository, schema, version,
33 from rhodecode.lib.dbmigrate.migrate.versioning import repository, schema, version, \
34 script as script_) # command name conflict
34 script as script_ # command name conflict
35 from rhodecode.lib.dbmigrate.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
@@ -18,7 +18,6 b' class SqlScript(base.BaseScript):'
18
18
19 :returns: :class:`SqlScript instance <migrate.versioning.script.sql.SqlScript>`"""
19 :returns: :class:`SqlScript instance <migrate.versioning.script.sql.SqlScript>`"""
20 cls.require_notfound(path)
20 cls.require_notfound(path)
21
22 src = Template(opts.pop('templates_path', None)).get_sql_script(theme=opts.pop('templates_theme', None))
21 src = Template(opts.pop('templates_path', None)).get_sql_script(theme=opts.pop('templates_theme', None))
23 shutil.copy(src, path)
22 shutil.copy(src, path)
24 return cls(path)
23 return cls(path)
@@ -37,7 +37,7 b' class Template(pathed.Pathed):'
37 :param path: Templates are loaded from rhodecode.lib.dbmigrate.migrate package
37 :param path: Templates are loaded from rhodecode.lib.dbmigrate.migrate package
38 if `path` is not provided.
38 if `path` is not provided.
39 """
39 """
40 pkg = 'migrate.versioning.templates'
40 pkg = 'rhodecode.lib.dbmigrate.migrate.versioning.templates'
41 _manage = 'manage.py_tmpl'
41 _manage = 'manage.py_tmpl'
42
42
43 def __new__(cls, path=None):
43 def __new__(cls, path=None):
@@ -80,7 +80,7 b' class Template(pathed.Pathed):'
80 def get_repository(self, *a, **kw):
80 def get_repository(self, *a, **kw):
81 """Calls self._get_item('repository', *a, **kw)"""
81 """Calls self._get_item('repository', *a, **kw)"""
82 return self._get_item('repository', *a, **kw)
82 return self._get_item('repository', *a, **kw)
83
83
84 def get_script(self, *a, **kw):
84 def get_script(self, *a, **kw):
85 """Calls self._get_item('script', *a, **kw)"""
85 """Calls self._get_item('script', *a, **kw)"""
86 return self._get_item('script', *a, **kw)
86 return self._get_item('script', *a, **kw)
@@ -1,5 +1,3 b''
1 from migrate import *
2
3 #==============================================================================
1 #==============================================================================
4 # DB INITIAL MODEL
2 # DB INITIAL MODEL
5 #==============================================================================
3 #==============================================================================
@@ -10,8 +8,9 b' from sqlalchemy import *'
10 from sqlalchemy.exc import DatabaseError
8 from sqlalchemy.exc import DatabaseError
11 from sqlalchemy.orm import relation, backref, class_mapper
9 from sqlalchemy.orm import relation, backref, class_mapper
12 from sqlalchemy.orm.session import Session
10 from sqlalchemy.orm.session import Session
11 from rhodecode.model.meta import Base
13
12
14 from rhodecode.model.meta import Base
13 from rhodecode.lib.dbmigrate.migrate import *
15
14
16 log = logging.getLogger(__name__)
15 log = logging.getLogger(__name__)
17
16
@@ -1,9 +1,16 b''
1 from sqlalchemy import *
1 import logging
2 from sqlalchemy.orm import relation
2 import datetime
3
3
4 from migrate import *
4 from sqlalchemy import *
5 from migrate.changeset import *
5 from sqlalchemy.exc import DatabaseError
6 from rhodecode.model.meta import Base, BaseModel
6 from sqlalchemy.orm import relation, backref, class_mapper
7 from sqlalchemy.orm.session import Session
8 from rhodecode.model.meta import Base
9 from rhodecode.model.db import BaseModel
10
11 from rhodecode.lib.dbmigrate.migrate import *
12
13 log = logging.getLogger(__name__)
7
14
8 def upgrade(migrate_engine):
15 def upgrade(migrate_engine):
9 """ Upgrade operations go here.
16 """ Upgrade operations go here.
@@ -18,10 +25,10 b' def upgrade(migrate_engine):'
18 autoload_with=migrate_engine)
25 autoload_with=migrate_engine)
19
26
20 #ADD is_ldap column
27 #ADD is_ldap column
21 is_ldap = Column("is_ldap", Boolean(), nullable=False,
28 is_ldap = Column("is_ldap", Boolean(), nullable=True,
22 unique=None, default=False)
29 unique=None, default=False)
23 is_ldap.create(tbl)
30 is_ldap.create(tbl, populate_default=True)
24
31 is_ldap.alter(nullable=False)
25
32
26 #==========================================================================
33 #==========================================================================
27 # Upgrade of `user_logs` table
34 # Upgrade of `user_logs` table
@@ -49,9 +56,10 b' def upgrade(migrate_engine):'
49 #ADD repo_type column
56 #ADD repo_type column
50 repo_type = Column("repo_type", String(length=None, convert_unicode=False,
57 repo_type = Column("repo_type", String(length=None, convert_unicode=False,
51 assert_unicode=None),
58 assert_unicode=None),
52 nullable=False, unique=False, default=None)
59 nullable=True, unique=False, default='hg')
53 repo_type.create(tbl)
54
60
61 repo_type.create(tbl, populate_default=True)
62 repo_type.alter(nullable=False)
55
63
56 #ADD statistics column
64 #ADD statistics column
57 enable_statistics = Column("statistics", Boolean(), nullable=True,
65 enable_statistics = Column("statistics", Boolean(), nullable=True,
@@ -64,6 +72,7 b' def upgrade(migrate_engine):'
64 # Add table `user_followings`
72 # Add table `user_followings`
65 #==========================================================================
73 #==========================================================================
66 tblname = 'user_followings'
74 tblname = 'user_followings'
75
67 class UserFollowing(Base, BaseModel):
76 class UserFollowing(Base, BaseModel):
68 __tablename__ = 'user_followings'
77 __tablename__ = 'user_followings'
69 __table_args__ = (UniqueConstraint('user_id', 'follows_repository_id'),
78 __table_args__ = (UniqueConstraint('user_id', 'follows_repository_id'),
@@ -85,6 +94,8 b' def upgrade(migrate_engine):'
85 #==========================================================================
94 #==========================================================================
86 # Add table `cache_invalidation`
95 # Add table `cache_invalidation`
87 #==========================================================================
96 #==========================================================================
97 tblname = 'cache_invalidation'
98
88 class CacheInvalidation(Base, BaseModel):
99 class CacheInvalidation(Base, BaseModel):
89 __tablename__ = 'cache_invalidation'
100 __tablename__ = 'cache_invalidation'
90 __table_args__ = (UniqueConstraint('cache_key'), {'useexisting':True})
101 __table_args__ = (UniqueConstraint('cache_key'), {'useexisting':True})
@@ -152,7 +152,7 b' class Repository(Base, BaseModel):'
152 __table_args__ = (UniqueConstraint('repo_name'), {'useexisting':True},)
152 __table_args__ = (UniqueConstraint('repo_name'), {'useexisting':True},)
153 repo_id = Column("repo_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
153 repo_id = Column("repo_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
154 repo_name = Column("repo_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None)
154 repo_name = Column("repo_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None)
155 repo_type = Column("repo_type", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=False, default=None)
155 repo_type = Column("repo_type", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=False, default='hg')
156 user_id = Column("user_id", Integer(), ForeignKey(u'users.user_id'), nullable=False, unique=False, default=None)
156 user_id = Column("user_id", Integer(), ForeignKey(u'users.user_id'), nullable=False, unique=False, default=None)
157 private = Column("private", Boolean(), nullable=True, unique=None, default=None)
157 private = Column("private", Boolean(), nullable=True, unique=None, default=None)
158 enable_statistics = Column("statistics", Boolean(), nullable=True, unique=None, default=True)
158 enable_statistics = Column("statistics", Boolean(), nullable=True, unique=None, default=True)
General Comments 0
You need to be logged in to leave comments. Login now