# HG changeset patch # User Marcin Kuzminski # Date 2010-12-11 02:41:27 # Node ID 28a4bb11bb6f35744a8f5442352c1b13f915eba3 # Parent 08d2dcd71666669f4677d638285b510834d1776f dbmigrations: added first working upgrade script fixed wrong versions fixed template path diff --git a/rhodecode/__init__.py b/rhodecode/__init__.py --- a/rhodecode/__init__.py +++ b/rhodecode/__init__.py @@ -29,7 +29,7 @@ VERSION = (1, 1, 0, 'beta') __version__ = '.'.join((str(each) for each in VERSION[:4])) -__dbversion__ = 1 #defines current db version for migrations +__dbversion__ = 2 #defines current db version for migrations from rhodecode.lib.utils import get_current_revision _rev = get_current_revision() diff --git a/rhodecode/lib/dbmigrate/__init__.py b/rhodecode/lib/dbmigrate/__init__.py --- a/rhodecode/lib/dbmigrate/__init__.py +++ b/rhodecode/lib/dbmigrate/__init__.py @@ -28,6 +28,8 @@ import logging from sqlalchemy import engine_from_config +from rhodecode import __dbversion__ +from rhodecode.lib.dbmigrate.migrate.versioning import api from rhodecode.lib.dbmigrate.migrate.exceptions import \ DatabaseNotControlledError from rhodecode.lib.utils import BasePasterCommand, Command, add_cache @@ -49,25 +51,32 @@ class UpgradeDb(BasePasterCommand): def command(self): from pylons import config + add_cache(config) - #engine = engine_from_config(config, 'sqlalchemy.db1.') - #rint engine - from rhodecode.lib.dbmigrate.migrate.versioning import api - path = 'rhodecode/lib/dbmigrate' + #engine = engine_from_config(config, 'sqlalchemy.db1.') + repository_path = 'rhodecode/lib/dbmigrate' + db_uri = config['sqlalchemy.db1.url'] try: - curr_version = api.db_version(config['sqlalchemy.db1.url'], path) + curr_version = api.db_version(db_uri, repository_path) msg = ('Found current database under version' ' control with version %s' % curr_version) except (RuntimeError, DatabaseNotControlledError), e: - curr_version = 0 + curr_version = 1 msg = ('Current database is not under version control setting' ' as version %s' % curr_version) + api.version_control(db_uri, repository_path, curr_version) + print msg + #now we have our dbversion we can do upgrade + + msg = 'attempting to do database upgrade to version %s' % __dbversion__ + print msg + api.upgrade(db_uri, repository_path, __dbversion__) def update_parser(self): self.parser.add_option('--sql', diff --git a/rhodecode/lib/dbmigrate/migrate/changeset/schema.py b/rhodecode/lib/dbmigrate/migrate/changeset/schema.py --- a/rhodecode/lib/dbmigrate/migrate/changeset/schema.py +++ b/rhodecode/lib/dbmigrate/migrate/changeset/schema.py @@ -119,7 +119,7 @@ def alter_column(*p, **k): """ - + k.setdefault('alter_metadata', DEFAULT_ALTER_METADATA) if 'table' not in k and isinstance(p[0], sqlalchemy.Column): @@ -367,7 +367,7 @@ class ColumnDelta(DictMixin, sqlalchemy. for_update=True)) if toinit: column._init_items(*toinit) - + if not SQLA_06: column.args = [] @@ -573,12 +573,12 @@ populated with defaults def _col_name_in_constraint(self, cons, name): return False - + def remove_from_table(self, table, unset_table=True): # TODO: remove primary keys, constraints, etc if unset_table: self.table = None - + to_drop = set() for index in table.indexes: columns = [] @@ -590,7 +590,7 @@ populated with defaults else: to_drop.add(index) table.indexes = table.indexes - to_drop - + to_drop = set() for cons in table.constraints: # TODO: deal with other types of constraint @@ -602,7 +602,7 @@ populated with defaults if self.name == col_name: to_drop.add(cons) table.constraints = table.constraints - to_drop - + if table.c.contains_column(self): table.c.remove(self) diff --git a/rhodecode/lib/dbmigrate/migrate/versioning/api.py b/rhodecode/lib/dbmigrate/migrate/versioning/api.py --- a/rhodecode/lib/dbmigrate/migrate/versioning/api.py +++ b/rhodecode/lib/dbmigrate/migrate/versioning/api.py @@ -30,8 +30,8 @@ import inspect import logging from rhodecode.lib.dbmigrate.migrate import exceptions -from rhodecode.lib.dbmigrate.migrate.versioning import (repository, schema, version, - script as script_) # command name conflict +from rhodecode.lib.dbmigrate.migrate.versioning import repository, schema, version, \ + script as script_ # command name conflict from rhodecode.lib.dbmigrate.migrate.versioning.util import catch_known_errors, with_engine diff --git a/rhodecode/lib/dbmigrate/migrate/versioning/script/sql.py b/rhodecode/lib/dbmigrate/migrate/versioning/script/sql.py --- a/rhodecode/lib/dbmigrate/migrate/versioning/script/sql.py +++ b/rhodecode/lib/dbmigrate/migrate/versioning/script/sql.py @@ -18,7 +18,6 @@ class SqlScript(base.BaseScript): :returns: :class:`SqlScript instance `""" cls.require_notfound(path) - src = Template(opts.pop('templates_path', None)).get_sql_script(theme=opts.pop('templates_theme', None)) shutil.copy(src, path) return cls(path) diff --git a/rhodecode/lib/dbmigrate/migrate/versioning/template.py b/rhodecode/lib/dbmigrate/migrate/versioning/template.py --- a/rhodecode/lib/dbmigrate/migrate/versioning/template.py +++ b/rhodecode/lib/dbmigrate/migrate/versioning/template.py @@ -37,7 +37,7 @@ class Template(pathed.Pathed): :param path: Templates are loaded from rhodecode.lib.dbmigrate.migrate package if `path` is not provided. """ - pkg = 'migrate.versioning.templates' + pkg = 'rhodecode.lib.dbmigrate.migrate.versioning.templates' _manage = 'manage.py_tmpl' def __new__(cls, path=None): @@ -80,7 +80,7 @@ class Template(pathed.Pathed): def get_repository(self, *a, **kw): """Calls self._get_item('repository', *a, **kw)""" return self._get_item('repository', *a, **kw) - + def get_script(self, *a, **kw): """Calls self._get_item('script', *a, **kw)""" return self._get_item('script', *a, **kw) diff --git a/rhodecode/lib/dbmigrate/versions/001_initial_release.py b/rhodecode/lib/dbmigrate/versions/001_initial_release.py --- a/rhodecode/lib/dbmigrate/versions/001_initial_release.py +++ b/rhodecode/lib/dbmigrate/versions/001_initial_release.py @@ -1,5 +1,3 @@ -from migrate import * - #============================================================================== # DB INITIAL MODEL #============================================================================== @@ -10,8 +8,9 @@ from sqlalchemy import * from sqlalchemy.exc import DatabaseError from sqlalchemy.orm import relation, backref, class_mapper from sqlalchemy.orm.session import Session +from rhodecode.model.meta import Base -from rhodecode.model.meta import Base +from rhodecode.lib.dbmigrate.migrate import * log = logging.getLogger(__name__) diff --git a/rhodecode/lib/dbmigrate/versions/002_version_1_1_0.py b/rhodecode/lib/dbmigrate/versions/002_version_1_1_0.py --- a/rhodecode/lib/dbmigrate/versions/002_version_1_1_0.py +++ b/rhodecode/lib/dbmigrate/versions/002_version_1_1_0.py @@ -1,9 +1,16 @@ -from sqlalchemy import * -from sqlalchemy.orm import relation +import logging +import datetime -from migrate import * -from migrate.changeset import * -from rhodecode.model.meta import Base, BaseModel +from sqlalchemy import * +from sqlalchemy.exc import DatabaseError +from sqlalchemy.orm import relation, backref, class_mapper +from sqlalchemy.orm.session import Session +from rhodecode.model.meta import Base +from rhodecode.model.db import BaseModel + +from rhodecode.lib.dbmigrate.migrate import * + +log = logging.getLogger(__name__) def upgrade(migrate_engine): """ Upgrade operations go here. @@ -18,10 +25,10 @@ def upgrade(migrate_engine): autoload_with=migrate_engine) #ADD is_ldap column - is_ldap = Column("is_ldap", Boolean(), nullable=False, + is_ldap = Column("is_ldap", Boolean(), nullable=True, unique=None, default=False) - is_ldap.create(tbl) - + is_ldap.create(tbl, populate_default=True) + is_ldap.alter(nullable=False) #========================================================================== # Upgrade of `user_logs` table @@ -49,9 +56,10 @@ def upgrade(migrate_engine): #ADD repo_type column repo_type = Column("repo_type", String(length=None, convert_unicode=False, assert_unicode=None), - nullable=False, unique=False, default=None) - repo_type.create(tbl) + nullable=True, unique=False, default='hg') + repo_type.create(tbl, populate_default=True) + repo_type.alter(nullable=False) #ADD statistics column enable_statistics = Column("statistics", Boolean(), nullable=True, @@ -64,6 +72,7 @@ def upgrade(migrate_engine): # Add table `user_followings` #========================================================================== tblname = 'user_followings' + class UserFollowing(Base, BaseModel): __tablename__ = 'user_followings' __table_args__ = (UniqueConstraint('user_id', 'follows_repository_id'), @@ -85,6 +94,8 @@ def upgrade(migrate_engine): #========================================================================== # Add table `cache_invalidation` #========================================================================== + tblname = 'cache_invalidation' + class CacheInvalidation(Base, BaseModel): __tablename__ = 'cache_invalidation' __table_args__ = (UniqueConstraint('cache_key'), {'useexisting':True}) diff --git a/rhodecode/model/db.py b/rhodecode/model/db.py --- a/rhodecode/model/db.py +++ b/rhodecode/model/db.py @@ -152,7 +152,7 @@ class Repository(Base, BaseModel): __table_args__ = (UniqueConstraint('repo_name'), {'useexisting':True},) repo_id = Column("repo_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) repo_name = Column("repo_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None) - repo_type = Column("repo_type", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=False, default=None) + repo_type = Column("repo_type", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=False, default='hg') user_id = Column("user_id", Integer(), ForeignKey(u'users.user_id'), nullable=False, unique=False, default=None) private = Column("private", Boolean(), nullable=True, unique=None, default=None) enable_statistics = Column("statistics", Boolean(), nullable=True, unique=None, default=True)