Changeset - 72c525a7e7ad
rhodecode/__init__.py
Show inline comments
 
# -*- coding: utf-8 -*-
 
"""
 
    rhodecode.__init__
 
    ~~~~~~~~~~~~~~~~~~
 

	
 
    RhodeCode, a web based repository management based on pylons
 
    versioning implementation: http://semver.org/
 

	
 
    :created_on: Apr 9, 2010
 
    :author: marcink
 
    :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com>
 
    :license: GPLv3, see COPYING for more details.
 
"""
 
# This program is free software: you can redistribute it and/or modify
 
# it under the terms of the GNU General Public License as published by
 
# the Free Software Foundation, either version 3 of the License, or
 
# (at your option) any later version.
 
#
 
# This program is distributed in the hope that it will be useful,
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
# GNU General Public License for more details.
 
#
 
# You should have received a copy of the GNU General Public License
 
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
import sys
 
import platform
 

	
 
VERSION = (1, 3, 0, 'beta')
 
__version__ = '.'.join((str(each) for each in VERSION[:4]))
 
__dbversion__ = 4  # defines current db version for migrations
 
__dbversion__ = 5  # defines current db version for migrations
 
__platform__ = platform.system()
 
__license__ = 'GPLv3'
 
__py_version__ = sys.version_info
 

	
 
PLATFORM_WIN = ('Windows')
 
PLATFORM_OTHERS = ('Linux', 'Darwin', 'FreeBSD', 'OpenBSD', 'SunOS')
 

	
 
requirements = [
 
    "Pylons==1.0.0",
 
    "Beaker==1.6.2",
 
    "WebHelpers>=1.2",
 
    "formencode==1.2.4",
 
    "SQLAlchemy==0.7.4",
 
    "Mako==0.5.0",
 
    "pygments>=1.4",
 
    "whoosh>=2.3.0,<2.4",
 
    "celery>=2.2.5,<2.3",
 
    "babel",
 
    "python-dateutil>=1.5.0,<2.0.0",
 
    "dulwich>=0.8.0,<0.9.0",
 
    "vcs>=0.2.3.dev",
 
    "webob==1.0.8",
 
    "markdown==2.0.3",
 
    "docutils==0.8.1",
 
]
 

	
 
if __py_version__ < (2, 6):
 
    requirements.append("simplejson")
 
    requirements.append("pysqlite")
 

	
 
if __platform__ in PLATFORM_WIN:
 
    requirements.append("mercurial>=2.1,<2.2")
 
else:
 
    requirements.append("py-bcrypt")
 
    requirements.append("mercurial>=2.1,<2.2")
 

	
 

	
 
try:
 
    from rhodecode.lib import get_current_revision
 
    _rev = get_current_revision()
 
except ImportError:
 
    # this is needed when doing some setup.py operations
 
    _rev = False
 

	
 
if len(VERSION) > 3 and _rev:
 
    __version__ += ' [rev:%s]' % _rev[0]
 

	
 

	
 
def get_version():
 
    """Returns shorter version (digit parts only) as string."""
 

	
 
    return '.'.join((str(each) for each in VERSION[:3]))
 

	
 
BACKENDS = {
 
    'hg': 'Mercurial repository',
 
    'git': 'Git repository',
 
}
 

	
 
CELERY_ON = False
 

	
 
# link to config for pylons
 
CONFIG = None
rhodecode/lib/db_manage.py
Show inline comments
 
@@ -78,196 +78,199 @@ class DbManage(object):
 

	
 
        checkfirst = not override
 
        meta.Base.metadata.create_all(checkfirst=checkfirst)
 
        log.info('Created tables for %s' % self.dbname)
 

	
 
    def set_db_version(self):
 
        ver = DbMigrateVersion()
 
        ver.version = __dbversion__
 
        ver.repository_id = 'rhodecode_db_migrations'
 
        ver.repository_path = 'versions'
 
        self.sa.add(ver)
 
        log.info('db version set to: %s' % __dbversion__)
 

	
 
    def upgrade(self):
 
        """
 
        Upgrades given database schema to given revision following
 
        all needed steps, to perform the upgrade
 

	
 
        """
 

	
 
        from rhodecode.lib.dbmigrate.migrate.versioning import api
 
        from rhodecode.lib.dbmigrate.migrate.exceptions import \
 
            DatabaseNotControlledError
 

	
 
        if 'sqlite' in self.dburi:
 
            print (
 
               '********************** WARNING **********************\n'
 
               'Make sure your version of sqlite is at least 3.7.X.  \n'
 
               'Earlier versions are known to fail on some migrations\n'
 
               '*****************************************************\n'
 
            )
 
        upgrade = ask_ok('You are about to perform database upgrade, make '
 
                         'sure You backed up your database before. '
 
                         'Continue ? [y/n]')
 
        if not upgrade:
 
            sys.exit('Nothing done')
 

	
 
        repository_path = jn(dn(dn(dn(os.path.realpath(__file__)))),
 
                             'rhodecode/lib/dbmigrate')
 
        db_uri = self.dburi
 

	
 
        try:
 
            curr_version = api.db_version(db_uri, repository_path)
 
            msg = ('Found current database under version'
 
                 ' control with version %s' % curr_version)
 

	
 
        except (RuntimeError, DatabaseNotControlledError):
 
            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)
 

	
 
        if curr_version == __dbversion__:
 
            sys.exit('This database is already at the newest version')
 

	
 
        #======================================================================
 
        # UPGRADE STEPS
 
        #======================================================================
 
        class UpgradeSteps(object):
 
            """
 
            Those steps follow schema versions so for example schema
 
            for example schema with seq 002 == step_2 and so on.
 
            """
 

	
 
            def __init__(self, klass):
 
                self.klass = klass
 

	
 
            def step_0(self):
 
                # step 0 is the schema upgrade, and than follow proper upgrades
 
                print ('attempting to do database upgrade to version %s' \
 
                                % __dbversion__)
 
                api.upgrade(db_uri, repository_path, __dbversion__)
 
                print ('Schema upgrade completed')
 

	
 
            def step_1(self):
 
                pass
 

	
 
            def step_2(self):
 
                print ('Patching repo paths for newer version of RhodeCode')
 
                self.klass.fix_repo_paths()
 

	
 
                print ('Patching default user of RhodeCode')
 
                self.klass.fix_default_user()
 

	
 
                log.info('Changing ui settings')
 
                self.klass.create_ui_settings()
 

	
 
            def step_3(self):
 
                print ('Adding additional settings into RhodeCode db')
 
                self.klass.fix_settings()
 
                print ('Adding ldap defaults')
 
                self.klass.create_ldap_options(skip_existing=True)
 

	
 
            def step_4(self):
 
                print ('TODO:')
 
                print ('create permissions and fix groups')
 
                self.klass.create_permissions()
 
                self.klass.fixup_groups()
 

	
 
            def step_5(self):
 
                pass
 
            
 
        upgrade_steps = [0] + range(curr_version + 1, __dbversion__ + 1)
 

	
 
        # CALL THE PROPER ORDER OF STEPS TO PERFORM FULL UPGRADE
 
        for step in upgrade_steps:
 
            print ('performing upgrade step %s' % step)
 
            getattr(UpgradeSteps(self), 'step_%s' % step)()
 
            self.sa.commit()
 
            
 
    def fix_repo_paths(self):
 
        """
 
        Fixes a old rhodecode version path into new one without a '*'
 
        """
 

	
 
        paths = self.sa.query(RhodeCodeUi)\
 
                .filter(RhodeCodeUi.ui_key == '/')\
 
                .scalar()
 

	
 
        paths.ui_value = paths.ui_value.replace('*', '')
 

	
 
        try:
 
            self.sa.add(paths)
 
            self.sa.commit()
 
        except:
 
            self.sa.rollback()
 
            raise
 

	
 
    def fix_default_user(self):
 
        """
 
        Fixes a old default user with some 'nicer' default values,
 
        used mostly for anonymous access
 
        """
 
        def_user = self.sa.query(User)\
 
                .filter(User.username == 'default')\
 
                .one()
 

	
 
        def_user.name = 'Anonymous'
 
        def_user.lastname = 'User'
 
        def_user.email = 'anonymous@rhodecode.org'
 

	
 
        try:
 
            self.sa.add(def_user)
 
            self.sa.commit()
 
        except:
 
            self.sa.rollback()
 
            raise
 

	
 
    def fix_settings(self):
 
        """
 
        Fixes rhodecode settings adds ga_code key for google analytics
 
        """
 

	
 
        hgsettings3 = RhodeCodeSetting('ga_code', '')
 

	
 
        try:
 
            self.sa.add(hgsettings3)
 
            self.sa.commit()
 
        except:
 
            self.sa.rollback()
 
            raise
 

	
 
    def admin_prompt(self, second=False):
 
        if not self.tests:
 
            import getpass
 

	
 
            def get_password():
 
                password = getpass.getpass('Specify admin password '
 
                                           '(min 6 chars):')
 
                confirm = getpass.getpass('Confirm password:')
 

	
 
                if password != confirm:
 
                    log.error('passwords mismatch')
 
                    return False
 
                if len(password) < 6:
 
                    log.error('password is to short use at least 6 characters')
 
                    return False
 

	
 
                return password
 

	
 
            username = raw_input('Specify admin username:')
 

	
 
            password = get_password()
 
            if not password:
 
                #second try
 
                password = get_password()
 
                if not password:
 
                    sys.exit()
 

	
 
            email = raw_input('Specify admin email:')
 
            self.create_user(username, password, email, True)
 
        else:
 
            log.info('creating admin and regular test users')
 
            from rhodecode.tests import TEST_USER_ADMIN_LOGIN,\
 
            TEST_USER_ADMIN_PASS, TEST_USER_ADMIN_EMAIL,\
 
            TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS,\
 
            TEST_USER_REGULAR_EMAIL, TEST_USER_REGULAR2_LOGIN, \
 
            TEST_USER_REGULAR2_PASS, TEST_USER_REGULAR2_EMAIL
rhodecode/lib/dbmigrate/migrate/changeset/ansisql.py
Show inline comments
 
"""
 
   Extensions to SQLAlchemy for altering existing tables.
 

	
 
   At the moment, this isn't so much based off of ANSI as much as
 
   things that just happen to work with multiple databases.
 
"""
 
import StringIO
 

	
 
import sqlalchemy as sa
 
from sqlalchemy.schema import SchemaVisitor
 
from sqlalchemy.engine.default import DefaultDialect
 
from sqlalchemy.sql import ClauseElement
 
from sqlalchemy.schema import (ForeignKeyConstraint,
 
                               PrimaryKeyConstraint,
 
                               CheckConstraint,
 
                               UniqueConstraint,
 
                               Index)
 

	
 
from rhodecode.lib.dbmigrate.migrate import exceptions
 
from rhodecode.lib.dbmigrate.migrate.changeset import constraint, SQLA_06
 
from rhodecode.lib.dbmigrate.migrate.changeset import constraint
 

	
 
if not SQLA_06:
 
    from sqlalchemy.sql.compiler import SchemaGenerator, SchemaDropper
 
else:
 
    from sqlalchemy.schema import AddConstraint, DropConstraint
 
    from sqlalchemy.sql.compiler import DDLCompiler
 
    SchemaGenerator = SchemaDropper = DDLCompiler
 

	
 

	
 
class AlterTableVisitor(SchemaVisitor):
 
    """Common operations for ``ALTER TABLE`` statements."""
 

	
 
    if SQLA_06:
 
        # engine.Compiler looks for .statement
 
        # when it spawns off a new compiler
 
        statement = ClauseElement()
 

	
 
    def append(self, s):
 
        """Append content to the SchemaIterator's query buffer."""
 

	
 
        self.buffer.write(s)
 

	
 
    def execute(self):
 
        """Execute the contents of the SchemaIterator's buffer."""
 
        try:
 
            return self.connection.execute(self.buffer.getvalue())
 
        finally:
 
            self.buffer.truncate(0)
 

	
 
    def __init__(self, dialect, connection, **kw):
 
        self.connection = connection
 
        self.buffer = StringIO.StringIO()
 
        self.preparer = dialect.identifier_preparer
 
        self.dialect = dialect
 

	
 
    def traverse_single(self, elem):
 
        ret = super(AlterTableVisitor, self).traverse_single(elem)
 
        if ret:
 
            # adapt to 0.6 which uses a string-returning
 
            # object
 
            self.append(" %s" % ret)
 

	
 
    def _to_table(self, param):
 
        """Returns the table object for the given param object."""
 
        if isinstance(param, (sa.Column, sa.Index, sa.schema.Constraint)):
 
            ret = param.table
 
        else:
 
            ret = param
 
        return ret
 

	
 
    def start_alter_table(self, param):
 
        """Returns the start of an ``ALTER TABLE`` SQL-Statement.
 

	
 
        Use the param object to determine the table name and use it
 
        for building the SQL statement.
 

	
 
        :param param: object to determine the table from
 
        :type param: :class:`sqlalchemy.Column`, :class:`sqlalchemy.Index`,
 
          :class:`sqlalchemy.schema.Constraint`, :class:`sqlalchemy.Table`,
 
          or string (table name)
 
        """
 
        table = self._to_table(param)
 
        self.append('\nALTER TABLE %s ' % self.preparer.format_table(table))
 
        return table
 

	
 

	
 
class ANSIColumnGenerator(AlterTableVisitor, SchemaGenerator):
 
    """Extends ansisql generator for column creation (alter table add col)"""
 

	
 
    def visit_column(self, column):
 
        """Create a column (table already exists).
 

	
 
        :param column: column object
 
        :type column: :class:`sqlalchemy.Column` instance
 
        """
 
        if column.default is not None:
 
            self.traverse_single(column.default)
 

	
 
        table = self.start_alter_table(column)
 
        self.append("ADD ")
 
        self.append(self.get_column_specification(column))
 

	
 
        for cons in column.constraints:
 
            self.traverse_single(cons)
 
        self.execute()
 

	
 
        # ALTER TABLE STATEMENTS
 

	
 
        # add indexes and unique constraints
 
        if column.index_name:
 
            Index(column.index_name,column).create()
 
        elif column.unique_name:
 
            constraint.UniqueConstraint(column,
 
                                        name=column.unique_name).create()
 

	
 
        # SA bounds FK constraints to table, add manually
 
        for fk in column.foreign_keys:
 
            self.add_foreignkey(fk.constraint)
 

	
 
        # add primary key constraint if needed
 
        if column.primary_key_name:
 
            cons = constraint.PrimaryKeyConstraint(column,
 
                                                   name=column.primary_key_name)
 
            cons.create()
 

	
 
    if SQLA_06:
 
        def add_foreignkey(self, fk):
 
            self.connection.execute(AddConstraint(fk))
 

	
 
class ANSIColumnDropper(AlterTableVisitor, SchemaDropper):
 
    """Extends ANSI SQL dropper for column dropping (``ALTER TABLE
 
    DROP COLUMN``).
 
    """
 

	
 
    def visit_column(self, column):
 
        """Drop a column from its table.
 

	
 
        :param column: the column object
 
        :type column: :class:`sqlalchemy.Column`
 
        """
 
        table = self.start_alter_table(column)
 
        self.append('DROP COLUMN %s' % self.preparer.format_column(column))
 
        self.execute()
 

	
 

	
 
class ANSISchemaChanger(AlterTableVisitor, SchemaGenerator):
 
    """Manages changes to existing schema elements.
 

	
 
    Note that columns are schema elements; ``ALTER TABLE ADD COLUMN``
 
    is in SchemaGenerator.
 

	
 
    All items may be renamed. Columns can also have many of their properties -
 
    type, for example - changed.
 

	
 
    Each function is passed a tuple, containing (object, name); where
 
    object is a type of object you'd expect for that function
 
    (ie. table for visit_table) and name is the object's new
 
    name. NONE means the name is unchanged.
 
    """
 

	
 
    def visit_table(self, table):
 
        """Rename a table. Other ops aren't supported."""
 
        self.start_alter_table(table)
 
        self.append("RENAME TO %s" % self.preparer.quote(table.new_name,
 
                                                         table.quote))
 
        self.execute()
 

	
 
    def visit_index(self, index):
 
        """Rename an index"""
 
        if hasattr(self, '_validate_identifier'):
 
            # SA <= 0.6.3
 
            self.append("ALTER INDEX %s RENAME TO %s" % (
 
                    self.preparer.quote(
 
                        self._validate_identifier(
 
                            index.name, True), index.quote),
 
                    self.preparer.quote(
 
                        self._validate_identifier(
 
                            index.new_name, True), index.quote)))
 
        else:
 
            # SA >= 0.6.5
 
            self.append("ALTER INDEX %s RENAME TO %s" % (
 
                    self.preparer.quote(
 
                        self._index_identifier(
 
                            index.name), index.quote),
 
                    self.preparer.quote(
 
                        self._index_identifier(
 
                            index.new_name), index.quote)))
 
        self.execute()
 

	
 
    def visit_column(self, delta):
 
        """Rename/change a column."""
 
        # ALTER COLUMN is implemented as several ALTER statements
 
        keys = delta.keys()
 
        if 'type' in keys:
 
            self._run_subvisit(delta, self._visit_column_type)
 
        if 'nullable' in keys:
 
            self._run_subvisit(delta, self._visit_column_nullable)
 
        if 'server_default' in keys:
 
            # Skip 'default': only handle server-side defaults, others
 
            # are managed by the app, not the db.
 
            self._run_subvisit(delta, self._visit_column_default)
 
        if 'name' in keys:
 
            self._run_subvisit(delta, self._visit_column_name, start_alter=False)
 

	
 
    def _run_subvisit(self, delta, func, start_alter=True):
 
        """Runs visit method based on what needs to be changed on column"""
 
        table = self._to_table(delta.table)
 
        col_name = delta.current_name
 
        if start_alter:
 
            self.start_alter_column(table, col_name)
 
        ret = func(table, delta.result_column, delta)
 
        self.execute()
 

	
 
    def start_alter_column(self, table, col_name):
 
        """Starts ALTER COLUMN"""
 
        self.start_alter_table(table)
 
        self.append("ALTER COLUMN %s " % self.preparer.quote(col_name, table.quote))
 

	
 
    def _visit_column_nullable(self, table, column, delta):
 
        nullable = delta['nullable']
 
        if nullable:
 
            self.append("DROP NOT NULL")
 
        else:
 
            self.append("SET NOT NULL")
 

	
 
    def _visit_column_default(self, table, column, delta):
 
        default_text = self.get_column_default_string(column)
 
        if default_text is not None:
 
            self.append("SET DEFAULT %s" % default_text)
 
        else:
 
            self.append("DROP DEFAULT")
 

	
 
    def _visit_column_type(self, table, column, delta):
 
        type_ = delta['type']
 
        if SQLA_06:
 
            type_text = str(type_.compile(dialect=self.dialect))
 
        else:
 
            type_text = type_.dialect_impl(self.dialect).get_col_spec()
 
        self.append("TYPE %s" % type_text)
 

	
 
    def _visit_column_name(self, table, column, delta):
 
        self.start_alter_table(table)
 
        col_name = self.preparer.quote(delta.current_name, table.quote)
 
        new_name = self.preparer.format_column(delta.result_column)
 
        self.append('RENAME COLUMN %s TO %s' % (col_name, new_name))
 

	
 

	
 
class ANSIConstraintCommon(AlterTableVisitor):
 
    """
 
    Migrate's constraints require a separate creation function from
 
    SA's: Migrate's constraints are created independently of a table;
 
    SA's are created at the same time as the table.
 
    """
 

	
 
    def get_constraint_name(self, cons):
 
        """Gets a name for the given constraint.
 

	
 
        If the name is already set it will be used otherwise the
 
        constraint's :meth:`autoname <migrate.changeset.constraint.ConstraintChangeset.autoname>`
 
        method is used.
 

	
 
        :param cons: constraint object
 
        """
 
        if cons.name is not None:
 
            ret = cons.name
 
        else:
 
            ret = cons.name = cons.autoname()
 
        return self.preparer.quote(ret, cons.quote)
 

	
 
    def visit_migrate_primary_key_constraint(self, *p, **k):
 
        self._visit_constraint(*p, **k)
 

	
 
    def visit_migrate_foreign_key_constraint(self, *p, **k):
 
        self._visit_constraint(*p, **k)
 

	
 
    def visit_migrate_check_constraint(self, *p, **k):
 
        self._visit_constraint(*p, **k)
 

	
 
    def visit_migrate_unique_constraint(self, *p, **k):
 
        self._visit_constraint(*p, **k)
 

	
 
if SQLA_06:
 
    class ANSIConstraintGenerator(ANSIConstraintCommon, SchemaGenerator):
 
        def _visit_constraint(self, constraint):
 
            constraint.name = self.get_constraint_name(constraint)
 
            self.append(self.process(AddConstraint(constraint)))
 
            self.execute()
 

	
 
    class ANSIConstraintDropper(ANSIConstraintCommon, SchemaDropper):
 
        def _visit_constraint(self, constraint):
 
            constraint.name = self.get_constraint_name(constraint)
 
            self.append(self.process(DropConstraint(constraint, cascade=constraint.cascade)))
 
            self.execute()
 

	
 
else:
 
    class ANSIConstraintGenerator(ANSIConstraintCommon, SchemaGenerator):
 

	
 
        def get_constraint_specification(self, cons, **kwargs):
 
            """Constaint SQL generators.
 

	
 
            We cannot use SA visitors because they append comma.
 
            """
 

	
 
            if isinstance(cons, PrimaryKeyConstraint):
 
                if cons.name is not None:
 
                    self.append("CONSTRAINT %s " % self.preparer.format_constraint(cons))
 
                self.append("PRIMARY KEY ")
 
                self.append("(%s)" % ', '.join(self.preparer.quote(c.name, c.quote)
 
                                               for c in cons))
 
                self.define_constraint_deferrability(cons)
 
            elif isinstance(cons, ForeignKeyConstraint):
 
                self.define_foreign_key(cons)
 
            elif isinstance(cons, CheckConstraint):
 
                if cons.name is not None:
 
                    self.append("CONSTRAINT %s " %
 
                                self.preparer.format_constraint(cons))
 
                self.append("CHECK (%s)" % cons.sqltext)
 
                self.define_constraint_deferrability(cons)
 
            elif isinstance(cons, UniqueConstraint):
 
                if cons.name is not None:
 
                    self.append("CONSTRAINT %s " %
 
                                self.preparer.format_constraint(cons))
 
                self.append("UNIQUE (%s)" % \
 
                    (', '.join(self.preparer.quote(c.name, c.quote) for c in cons)))
 
                self.define_constraint_deferrability(cons)
 
            else:
 
                raise exceptions.InvalidConstraintError(cons)
 

	
 
        def _visit_constraint(self, constraint):
 

	
 
            table = self.start_alter_table(constraint)
 
            constraint.name = self.get_constraint_name(constraint)
 
            self.append("ADD ")
 
            self.get_constraint_specification(constraint)
 
            self.execute()
 

	
 

	
 
    class ANSIConstraintDropper(ANSIConstraintCommon, SchemaDropper):
 

	
 
        def _visit_constraint(self, constraint):
 
            self.start_alter_table(constraint)
 
            self.append("DROP CONSTRAINT ")
 
            constraint.name = self.get_constraint_name(constraint)
 
            self.append(self.preparer.format_constraint(constraint))
 
            if constraint.cascade:
 
                self.cascade_constraint(constraint)
 
            self.execute()
 

	
 
        def cascade_constraint(self, constraint):
 
            self.append(" CASCADE")
 

	
 

	
 
class ANSIDialect(DefaultDialect):
 
    columngenerator = ANSIColumnGenerator
 
    columndropper = ANSIColumnDropper
 
    schemachanger = ANSISchemaChanger
 
    constraintgenerator = ANSIConstraintGenerator
 
    constraintdropper = ANSIConstraintDropper
rhodecode/lib/dbmigrate/migrate/changeset/constraint.py
Show inline comments
 
"""
 
   This module defines standalone schema constraint classes.
 
"""
 
from sqlalchemy import schema
 

	
 
from rhodecode.lib.dbmigrate.migrate.exceptions import *
 
from rhodecode.lib.dbmigrate.migrate.changeset import SQLA_06
 

	
 

	
 
class ConstraintChangeset(object):
 
    """Base class for Constraint classes."""
 

	
 
    def _normalize_columns(self, cols, table_name=False):
 
        """Given: column objects or names; return col names and
 
        (maybe) a table"""
 
        colnames = []
 
        table = None
 
        for col in cols:
 
            if isinstance(col, schema.Column):
 
                if col.table is not None and table is None:
 
                    table = col.table
 
                if table_name:
 
                    col = '.'.join((col.table.name, col.name))
 
                else:
 
                    col = col.name
 
            colnames.append(col)
 
        return colnames, table
 

	
 
    def __do_imports(self, visitor_name, *a, **kw):
 
        engine = kw.pop('engine', self.table.bind)
 
        from rhodecode.lib.dbmigrate.migrate.changeset.databases.visitor import (get_engine_visitor,
 
                                                                                 run_single_visitor)
 
        visitorcallable = get_engine_visitor(engine, visitor_name)
 
        run_single_visitor(engine, visitorcallable, self, *a, **kw)
 

	
 
    def create(self, *a, **kw):
 
        """Create the constraint in the database.
 

	
 
        :param engine: the database engine to use. If this is \
 
        :keyword:`None` the instance's engine will be used
 
        :type engine: :class:`sqlalchemy.engine.base.Engine`
 
        :param connection: reuse connection istead of creating new one.
 
        :type connection: :class:`sqlalchemy.engine.base.Connection` instance
 
        """
 
        # TODO: set the parent here instead of in __init__
 
        self.__do_imports('constraintgenerator', *a, **kw)
 

	
 
    def drop(self, *a, **kw):
 
        """Drop the constraint from the database.
 

	
 
        :param engine: the database engine to use. If this is
 
          :keyword:`None` the instance's engine will be used
 
        :param cascade: Issue CASCADE drop if database supports it
 
        :type engine: :class:`sqlalchemy.engine.base.Engine`
 
        :type cascade: bool
 
        :param connection: reuse connection istead of creating new one.
 
        :type connection: :class:`sqlalchemy.engine.base.Connection` instance
 
        :returns: Instance with cleared columns
 
        """
 
        self.cascade = kw.pop('cascade', False)
 
        self.__do_imports('constraintdropper', *a, **kw)
 
        # the spirit of Constraint objects is that they
 
        # are immutable (just like in a DB.  they're only ADDed
 
        # or DROPped).
 
        #self.columns.clear()
 
        return self
 

	
 

	
 
class PrimaryKeyConstraint(ConstraintChangeset, schema.PrimaryKeyConstraint):
 
    """Construct PrimaryKeyConstraint
 

	
 
    Migrate's additional parameters:
 

	
 
    :param cols: Columns in constraint.
 
    :param table: If columns are passed as strings, this kw is required
 
    :type table: Table instance
 
    :type cols: strings or Column instances
 
    """
 

	
 
    __migrate_visit_name__ = 'migrate_primary_key_constraint'
 

	
 
    def __init__(self, *cols, **kwargs):
 
        colnames, table = self._normalize_columns(cols)
 
        table = kwargs.pop('table', table)
 
        super(PrimaryKeyConstraint, self).__init__(*colnames, **kwargs)
 
        if table is not None:
 
            self._set_parent(table)
 

	
 

	
 
    def autoname(self):
 
        """Mimic the database's automatic constraint names"""
 
        return "%s_pkey" % self.table.name
 

	
 

	
 
class ForeignKeyConstraint(ConstraintChangeset, schema.ForeignKeyConstraint):
 
    """Construct ForeignKeyConstraint
 

	
 
    Migrate's additional parameters:
 

	
 
    :param columns: Columns in constraint
 
    :param refcolumns: Columns that this FK reffers to in another table.
 
    :param table: If columns are passed as strings, this kw is required
 
    :type table: Table instance
 
    :type columns: list of strings or Column instances
 
    :type refcolumns: list of strings or Column instances
 
    """
 

	
 
    __migrate_visit_name__ = 'migrate_foreign_key_constraint'
 

	
 
    def __init__(self, columns, refcolumns, *args, **kwargs):
 
        colnames, table = self._normalize_columns(columns)
 
        table = kwargs.pop('table', table)
 
        refcolnames, reftable = self._normalize_columns(refcolumns,
 
                                                        table_name=True)
 
        super(ForeignKeyConstraint, self).__init__(colnames, refcolnames, *args,
 
                                                   **kwargs)
 
        super(ForeignKeyConstraint, self).__init__(
 
            colnames, refcolnames, *args,**kwargs
 
        )
 
        if table is not None:
 
            self._set_parent(table)
 

	
 
    @property
 
    def referenced(self):
 
        return [e.column for e in self.elements]
 

	
 
    @property
 
    def reftable(self):
 
        return self.referenced[0].table
 

	
 
    def autoname(self):
 
        """Mimic the database's automatic constraint names"""
 
        if hasattr(self.columns, 'keys'):
 
            # SA <= 0.5
 
            firstcol = self.columns[self.columns.keys()[0]]
 
            ret = "%(table)s_%(firstcolumn)s_fkey" % dict(
 
                table=firstcol.table.name,
 
                firstcolumn=firstcol.name,)
 
        else:
 
            # SA >= 0.6
 
            ret = "%(table)s_%(firstcolumn)s_fkey" % dict(
 
                table=self.table.name,
 
                firstcolumn=self.columns[0],)
 
        return ret
 

	
 

	
 
class CheckConstraint(ConstraintChangeset, schema.CheckConstraint):
 
    """Construct CheckConstraint
 

	
 
    Migrate's additional parameters:
 

	
 
    :param sqltext: Plain SQL text to check condition
 
    :param columns: If not name is applied, you must supply this kw\
 
    to autoname constraint
 
    :param table: If columns are passed as strings, this kw is required
 
    :type table: Table instance
 
    :type columns: list of Columns instances
 
    :type sqltext: string
 
    """
 

	
 
    __migrate_visit_name__ = 'migrate_check_constraint'
 

	
 
    def __init__(self, sqltext, *args, **kwargs):
 
        cols = kwargs.pop('columns', [])
 
        if not cols and not kwargs.get('name', False):
 
            raise InvalidConstraintError('You must either set "name"'
 
                'parameter or "columns" to autogenarate it.')
 
        colnames, table = self._normalize_columns(cols)
 
        table = kwargs.pop('table', table)
 
        schema.CheckConstraint.__init__(self, sqltext, *args, **kwargs)
 
        if table is not None:
 
            if not SQLA_06:
 
                self.table = table
 
            self._set_parent(table)
 
        self.colnames = colnames
 

	
 
    def autoname(self):
 
        return "%(table)s_%(cols)s_check" % \
 
            dict(table=self.table.name, cols="_".join(self.colnames))
 

	
 

	
 
class UniqueConstraint(ConstraintChangeset, schema.UniqueConstraint):
 
    """Construct UniqueConstraint
 

	
 
    Migrate's additional parameters:
 

	
 
    :param cols: Columns in constraint.
 
    :param table: If columns are passed as strings, this kw is required
 
    :type table: Table instance
 
    :type cols: strings or Column instances
 

	
 
    .. versionadded:: 0.6.0
 
    """
 

	
 
    __migrate_visit_name__ = 'migrate_unique_constraint'
 

	
 
    def __init__(self, *cols, **kwargs):
 
        self.colnames, table = self._normalize_columns(cols)
 
        table = kwargs.pop('table', table)
 
        super(UniqueConstraint, self).__init__(*self.colnames, **kwargs)
 
        if table is not None:
 
            self._set_parent(table)
 

	
 
    def autoname(self):
 
        """Mimic the database's automatic constraint names"""
 
        return "%s_%s_key" % (self.table.name, self.colnames[0])
 
        return "%s_%s_key" % (self.table.name, '_'.join(self.colnames))
rhodecode/lib/dbmigrate/migrate/changeset/databases/firebird.py
Show inline comments
 
"""
 
   Firebird database specific implementations of changeset classes.
 
"""
 
from sqlalchemy.databases import firebird as sa_base
 
from sqlalchemy.schema import PrimaryKeyConstraint
 
from rhodecode.lib.dbmigrate.migrate import exceptions
 
from rhodecode.lib.dbmigrate.migrate.changeset import ansisql, SQLA_06
 
from rhodecode.lib.dbmigrate.migrate.changeset import ansisql
 

	
 

	
 
if SQLA_06:
 
    FBSchemaGenerator = sa_base.FBDDLCompiler
 
else:
 
    FBSchemaGenerator = sa_base.FBSchemaGenerator
 

	
 
class FBColumnGenerator(FBSchemaGenerator, ansisql.ANSIColumnGenerator):
 
    """Firebird column generator implementation."""
 

	
 

	
 
class FBColumnDropper(ansisql.ANSIColumnDropper):
 
    """Firebird column dropper implementation."""
 

	
 
    def visit_column(self, column):
 
        """Firebird supports 'DROP col' instead of 'DROP COLUMN col' syntax
 

	
 
        Drop primary key and unique constraints if dropped column is referencing it."""
 
        if column.primary_key:
 
            if column.table.primary_key.columns.contains_column(column):
 
                column.table.primary_key.drop()
 
                # TODO: recreate primary key if it references more than this column
 

	
 
        for index in column.table.indexes:
 
            # "column in index.columns" causes problems as all
 
            # column objects compare equal and return a SQL expression
 
            if column.name in [col.name for col in index.columns]:
 
                index.drop()
 
                # TODO: recreate index if it references more than this column
 

	
 
        for cons in column.table.constraints:
 
            if isinstance(cons,PrimaryKeyConstraint):
 
                # will be deleted only when the column its on
 
                # is deleted!
 
                continue
 

	
 
            if SQLA_06:
 
                should_drop = column.name in cons.columns
 
            else:
 
                should_drop = cons.contains_column(column) and cons.name
 
            if should_drop:
 
                self.start_alter_table(column)
 
                self.append("DROP CONSTRAINT ")
 
                self.append(self.preparer.format_constraint(cons))
 
                self.execute()
 
            # TODO: recreate unique constraint if it refenrences more than this column
 

	
 
        self.start_alter_table(column)
 
        self.append('DROP %s' % self.preparer.format_column(column))
 
        self.execute()
 

	
 

	
 
class FBSchemaChanger(ansisql.ANSISchemaChanger):
 
    """Firebird schema changer implementation."""
 

	
 
    def visit_table(self, table):
 
        """Rename table not supported"""
 
        raise exceptions.NotSupportedError(
 
            "Firebird does not support renaming tables.")
 

	
 
    def _visit_column_name(self, table, column, delta):
 
        self.start_alter_table(table)
 
        col_name = self.preparer.quote(delta.current_name, table.quote)
 
        new_name = self.preparer.format_column(delta.result_column)
 
        self.append('ALTER COLUMN %s TO %s' % (col_name, new_name))
 

	
 
    def _visit_column_nullable(self, table, column, delta):
 
        """Changing NULL is not supported"""
 
        # TODO: http://www.firebirdfaq.org/faq103/
 
        raise exceptions.NotSupportedError(
 
            "Firebird does not support altering NULL bevahior.")
 

	
 

	
 
class FBConstraintGenerator(ansisql.ANSIConstraintGenerator):
 
    """Firebird constraint generator implementation."""
 

	
 

	
 
class FBConstraintDropper(ansisql.ANSIConstraintDropper):
 
    """Firebird constaint dropper implementation."""
 

	
 
    def cascade_constraint(self, constraint):
 
        """Cascading constraints is not supported"""
 
        raise exceptions.NotSupportedError(
 
            "Firebird does not support cascading constraints")
 

	
 

	
 
class FBDialect(ansisql.ANSIDialect):
 
    columngenerator = FBColumnGenerator
 
    columndropper = FBColumnDropper
 
    schemachanger = FBSchemaChanger
 
    constraintgenerator = FBConstraintGenerator
 
    constraintdropper = FBConstraintDropper
rhodecode/lib/dbmigrate/migrate/changeset/databases/mysql.py
Show inline comments
 
"""
 
   MySQL database specific implementations of changeset classes.
 
"""
 

	
 
from sqlalchemy.databases import mysql as sa_base
 
from sqlalchemy import types as sqltypes
 

	
 
from rhodecode.lib.dbmigrate.migrate import exceptions
 
from rhodecode.lib.dbmigrate.migrate.changeset import ansisql, SQLA_06
 
from rhodecode.lib.dbmigrate.migrate.changeset import ansisql
 

	
 

	
 
if not SQLA_06:
 
    MySQLSchemaGenerator = sa_base.MySQLSchemaGenerator
 
else:
 
    MySQLSchemaGenerator = sa_base.MySQLDDLCompiler
 

	
 
class MySQLColumnGenerator(MySQLSchemaGenerator, ansisql.ANSIColumnGenerator):
 
    pass
 

	
 

	
 
class MySQLColumnDropper(ansisql.ANSIColumnDropper):
 
    pass
 

	
 

	
 
class MySQLSchemaChanger(MySQLSchemaGenerator, ansisql.ANSISchemaChanger):
 

	
 
    def visit_column(self, delta):
 
        table = delta.table
 
        colspec = self.get_column_specification(delta.result_column)
 
        if delta.result_column.autoincrement:
 
            primary_keys = [c for c in table.primary_key.columns
 
                       if (c.autoincrement and
 
                            isinstance(c.type, sqltypes.Integer) and
 
                            not c.foreign_keys)]
 

	
 
            if primary_keys:
 
                first = primary_keys.pop(0)
 
                if first.name == delta.current_name:
 
                    colspec += " AUTO_INCREMENT"
 
        old_col_name = self.preparer.quote(delta.current_name, table.quote)
 

	
 
        self.start_alter_table(table)
 

	
 
        self.append("CHANGE COLUMN %s " % old_col_name)
 
        self.append(colspec)
 
        self.execute()
 

	
 
    def visit_index(self, param):
 
        # If MySQL can do this, I can't find how
 
        raise exceptions.NotSupportedError("MySQL cannot rename indexes")
 

	
 

	
 
class MySQLConstraintGenerator(ansisql.ANSIConstraintGenerator):
 
    pass
 

	
 
if SQLA_06:
 

	
 
    class MySQLConstraintDropper(MySQLSchemaGenerator, ansisql.ANSIConstraintDropper):
 
        def visit_migrate_check_constraint(self, *p, **k):
 
            raise exceptions.NotSupportedError("MySQL does not support CHECK"
 
                " constraints, use triggers instead.")
 

	
 
else:
 
    class MySQLConstraintDropper(ansisql.ANSIConstraintDropper):
 

	
 
        def visit_migrate_primary_key_constraint(self, constraint):
 
            self.start_alter_table(constraint)
 
            self.append("DROP PRIMARY KEY")
 
            self.execute()
 

	
 
        def visit_migrate_foreign_key_constraint(self, constraint):
 
            self.start_alter_table(constraint)
 
            self.append("DROP FOREIGN KEY ")
 
            constraint.name = self.get_constraint_name(constraint)
 
            self.append(self.preparer.format_constraint(constraint))
 
            self.execute()
 

	
 
        def visit_migrate_check_constraint(self, *p, **k):
 
            raise exceptions.NotSupportedError("MySQL does not support CHECK"
 
                " constraints, use triggers instead.")
 

	
 
        def visit_migrate_unique_constraint(self, constraint, *p, **k):
 
            self.start_alter_table(constraint)
 
            self.append('DROP INDEX ')
 
            constraint.name = self.get_constraint_name(constraint)
 
            self.append(self.preparer.format_constraint(constraint))
 
            self.execute()
 

	
 

	
 
class MySQLDialect(ansisql.ANSIDialect):
 
    columngenerator = MySQLColumnGenerator
 
    columndropper = MySQLColumnDropper
 
    schemachanger = MySQLSchemaChanger
 
    constraintgenerator = MySQLConstraintGenerator
 
    constraintdropper = MySQLConstraintDropper
rhodecode/lib/dbmigrate/migrate/changeset/databases/postgres.py
Show inline comments
 
"""
 
   `PostgreSQL`_ database specific implementations of changeset classes.
 

	
 
   .. _`PostgreSQL`: http://www.postgresql.org/
 
"""
 
from rhodecode.lib.dbmigrate.migrate.changeset import ansisql, SQLA_06
 
from rhodecode.lib.dbmigrate.migrate.changeset import ansisql
 

	
 
if not SQLA_06:
 
    from sqlalchemy.databases import postgres as sa_base
 
    PGSchemaGenerator = sa_base.PGSchemaGenerator
 
else:
 

	
 
    from sqlalchemy.databases import postgresql as sa_base
 
    PGSchemaGenerator = sa_base.PGDDLCompiler
 

	
 

	
 
class PGColumnGenerator(PGSchemaGenerator, ansisql.ANSIColumnGenerator):
 
    """PostgreSQL column generator implementation."""
 
    pass
 

	
 

	
 
class PGColumnDropper(ansisql.ANSIColumnDropper):
 
    """PostgreSQL column dropper implementation."""
 
    pass
 

	
 

	
 
class PGSchemaChanger(ansisql.ANSISchemaChanger):
 
    """PostgreSQL schema changer implementation."""
 
    pass
 

	
 

	
 
class PGConstraintGenerator(ansisql.ANSIConstraintGenerator):
 
    """PostgreSQL constraint generator implementation."""
 
    pass
 

	
 

	
 
class PGConstraintDropper(ansisql.ANSIConstraintDropper):
 
    """PostgreSQL constaint dropper implementation."""
 
    pass
 

	
 

	
 
class PGDialect(ansisql.ANSIDialect):
 
    columngenerator = PGColumnGenerator
 
    columndropper = PGColumnDropper
 
    schemachanger = PGSchemaChanger
 
    constraintgenerator = PGConstraintGenerator
 
    constraintdropper = PGConstraintDropper
rhodecode/lib/dbmigrate/migrate/changeset/databases/sqlite.py
Show inline comments
 
"""
 
   `SQLite`_ database specific implementations of changeset classes.
 

	
 
   .. _`SQLite`: http://www.sqlite.org/
 
"""
 
from UserDict import DictMixin
 
from copy import copy
 

	
 
from sqlalchemy.databases import sqlite as sa_base
 

	
 
from rhodecode.lib.dbmigrate.migrate import exceptions
 
from rhodecode.lib.dbmigrate.migrate.changeset import ansisql, SQLA_06
 

	
 
SQLiteSchemaGenerator = sa_base.SQLiteDDLCompiler
 

	
 
if not SQLA_06:
 
    SQLiteSchemaGenerator = sa_base.SQLiteSchemaGenerator
 
else:
 
    SQLiteSchemaGenerator = sa_base.SQLiteDDLCompiler
 

	
 
class SQLiteCommon(object):
 

	
 
    def _not_supported(self, op):
 
        raise exceptions.NotSupportedError("SQLite does not support "
 
            "%s; see http://www.sqlite.org/lang_altertable.html" % op)
 

	
 

	
 
class SQLiteHelper(SQLiteCommon):
 

	
 
    def recreate_table(self,table,column=None,delta=None):
 
        table_name = self.preparer.format_table(table)
 

	
 
        # we remove all indexes so as not to have
 
        # problems during copy and re-create
 
        for index in table.indexes:
 
            index.drop()
 

	
 
        self.append('ALTER TABLE %s RENAME TO migration_tmp' % table_name)
 
        self.execute()
 

	
 
        insertion_string = self._modify_table(table, column, delta)
 

	
 
        table.create(bind=self.connection)
 
        self.append(insertion_string % {'table_name': table_name})
 
        self.execute()
 
        self.append('DROP TABLE migration_tmp')
 
        self.execute()
 

	
 
    def visit_column(self, delta):
 
        if isinstance(delta, DictMixin):
 
            column = delta.result_column
 
            table = self._to_table(delta.table)
 
        else:
 
            column = delta
 
            table = self._to_table(column.table)
 
        self.recreate_table(table,column,delta)
 

	
 
class SQLiteColumnGenerator(SQLiteSchemaGenerator,
 
                            ansisql.ANSIColumnGenerator,
 
                            # at the end so we get the normal
 
                            # visit_column by default
 
                            SQLiteHelper,
 
                            SQLiteCommon
 
                            ):
 
    """SQLite ColumnGenerator"""
 

	
 
    def _modify_table(self, table, column, delta):
 
        columns = ' ,'.join(map(
 
                self.preparer.format_column,
 
                [c for c in table.columns if c.name!=column.name]))
 
        return ('INSERT INTO %%(table_name)s (%(cols)s) '
 
                'SELECT %(cols)s from migration_tmp')%{'cols':columns}
 

	
 
    def visit_column(self,column):
 
        if column.foreign_keys:
 
            SQLiteHelper.visit_column(self,column)
 
        else:
 
            super(SQLiteColumnGenerator,self).visit_column(column)
 

	
 
class SQLiteColumnDropper(SQLiteHelper, ansisql.ANSIColumnDropper):
 
    """SQLite ColumnDropper"""
 

	
 
    def _modify_table(self, table, column, delta):
 

	
 
        columns = ' ,'.join(map(self.preparer.format_column, table.columns))
 
        return 'INSERT INTO %(table_name)s SELECT ' + columns + \
 
            ' from migration_tmp'
 

	
 
    def visit_column(self,column):
 
        # For SQLite, we *have* to remove the column here so the table
 
        # is re-created properly.
 
        column.remove_from_table(column.table,unset_table=False)
 
        super(SQLiteColumnDropper,self).visit_column(column)
 

	
 

	
 
class SQLiteSchemaChanger(SQLiteHelper, ansisql.ANSISchemaChanger):
 
    """SQLite SchemaChanger"""
 

	
 
    def _modify_table(self, table, column, delta):
 
        return 'INSERT INTO %(table_name)s SELECT * from migration_tmp'
 

	
 
    def visit_index(self, index):
 
        """Does not support ALTER INDEX"""
 
        self._not_supported('ALTER INDEX')
 

	
 

	
 
class SQLiteConstraintGenerator(ansisql.ANSIConstraintGenerator, SQLiteHelper, SQLiteCommon):
 

	
 
    def visit_migrate_primary_key_constraint(self, constraint):
 
        tmpl = "CREATE UNIQUE INDEX %s ON %s ( %s )"
 
        cols = ', '.join(map(self.preparer.format_column, constraint.columns))
 
        tname = self.preparer.format_table(constraint.table)
 
        name = self.get_constraint_name(constraint)
 
        msg = tmpl % (name, tname, cols)
 
        self.append(msg)
rhodecode/lib/dbmigrate/migrate/changeset/schema.py
Show inline comments
 
@@ -256,316 +256,314 @@ class ColumnDelta(DictMixin, sqlalchemy.
 
    def keys(self):
 
        return self.diffs.keys()
 

	
 
    def compare_parameters(self, current_name, *p, **k):
 
        """Compares Column objects with reflection"""
 
        self.table = k.pop('table')
 
        self.result_column = self._table.c.get(current_name)
 
        if len(p):
 
            k = self._extract_parameters(p, k, self.result_column)
 
        return k
 

	
 
    def compare_1_column(self, col, *p, **k):
 
        """Compares one Column object"""
 
        self.table = k.pop('table', None)
 
        if self.table is None:
 
            self.table = col.table
 
        self.result_column = col
 
        if len(p):
 
            k = self._extract_parameters(p, k, self.result_column)
 
        return k
 

	
 
    def compare_2_columns(self, old_col, new_col, *p, **k):
 
        """Compares two Column objects"""
 
        self.process_column(new_col)
 
        self.table = k.pop('table', None)
 
        # we cannot use bool() on table in SA06
 
        if self.table is None:
 
            self.table = old_col.table
 
        if self.table is None:
 
            new_col.table
 
        self.result_column = old_col
 

	
 
        # set differences
 
        # leave out some stuff for later comp
 
        for key in (set(self.diff_keys) - set(('type',))):
 
            val = getattr(new_col, key, None)
 
            if getattr(self.result_column, key, None) != val:
 
                k.setdefault(key, val)
 

	
 
        # inspect types
 
        if not self.are_column_types_eq(self.result_column.type, new_col.type):
 
            k.setdefault('type', new_col.type)
 

	
 
        if len(p):
 
            k = self._extract_parameters(p, k, self.result_column)
 
        return k
 

	
 
    def apply_diffs(self, diffs):
 
        """Populate dict and column object with new values"""
 
        self.diffs = diffs
 
        for key in self.diff_keys:
 
            if key in diffs:
 
                setattr(self.result_column, key, diffs[key])
 

	
 
        self.process_column(self.result_column)
 

	
 
        # create an instance of class type if not yet
 
        if 'type' in diffs and callable(self.result_column.type):
 
            self.result_column.type = self.result_column.type()
 

	
 
        # add column to the table
 
        if self.table is not None and self.alter_metadata:
 
            self.result_column.add_to_table(self.table)
 

	
 
    def are_column_types_eq(self, old_type, new_type):
 
        """Compares two types to be equal"""
 
        ret = old_type.__class__ == new_type.__class__
 

	
 
        # String length is a special case
 
        if ret and isinstance(new_type, sqlalchemy.types.String):
 
            ret = (getattr(old_type, 'length', None) == \
 
                       getattr(new_type, 'length', None))
 
        return ret
 

	
 
    def _extract_parameters(self, p, k, column):
 
        """Extracts data from p and modifies diffs"""
 
        p = list(p)
 
        while len(p):
 
            if isinstance(p[0], basestring):
 
                k.setdefault('name', p.pop(0))
 
            elif isinstance(p[0], sqlalchemy.types.AbstractType):
 
                k.setdefault('type', p.pop(0))
 
            elif callable(p[0]):
 
                p[0] = p[0]()
 
            else:
 
                break
 

	
 
        if len(p):
 
            new_col = column.copy_fixed()
 
            new_col._init_items(*p)
 
            k = self.compare_2_columns(column, new_col, **k)
 
        return k
 

	
 
    def process_column(self, column):
 
        """Processes default values for column"""
 
        # XXX: this is a snippet from SA processing of positional parameters
 
        if not SQLA_06 and column.args:
 
            toinit = list(column.args)
 
        else:
 
            toinit = list()
 

	
 
        if column.server_default is not None:
 
            if isinstance(column.server_default, sqlalchemy.FetchedValue):
 
                toinit.append(column.server_default)
 
            else:
 
                toinit.append(sqlalchemy.DefaultClause(column.server_default))
 
        if column.server_onupdate is not None:
 
            if isinstance(column.server_onupdate, FetchedValue):
 
                toinit.append(column.server_default)
 
            else:
 
                toinit.append(sqlalchemy.DefaultClause(column.server_onupdate,
 
                                            for_update=True))
 
        if toinit:
 
            column._init_items(*toinit)
 

	
 
        if not SQLA_06:
 
            column.args = []
 

	
 
    def _get_table(self):
 
        return getattr(self, '_table', None)
 

	
 
    def _set_table(self, table):
 
        if isinstance(table, basestring):
 
            if self.alter_metadata:
 
                if not self.meta:
 
                    raise ValueError("metadata must be specified for table"
 
                        " reflection when using alter_metadata")
 
                meta = self.meta
 
                if self.engine:
 
                    meta.bind = self.engine
 
            else:
 
                if not self.engine and not self.meta:
 
                    raise ValueError("engine or metadata must be specified"
 
                        " to reflect tables")
 
                if not self.engine:
 
                    self.engine = self.meta.bind
 
                meta = sqlalchemy.MetaData(bind=self.engine)
 
            self._table = sqlalchemy.Table(table, meta, autoload=True)
 
        elif isinstance(table, sqlalchemy.Table):
 
            self._table = table
 
            if not self.alter_metadata:
 
                self._table.meta = sqlalchemy.MetaData(bind=self._table.bind)
 
    def _get_result_column(self):
 
        return getattr(self, '_result_column', None)
 

	
 
    def _set_result_column(self, column):
 
        """Set Column to Table based on alter_metadata evaluation."""
 
        self.process_column(column)
 
        if not hasattr(self, 'current_name'):
 
            self.current_name = column.name
 
        if self.alter_metadata:
 
            self._result_column = column
 
        else:
 
            self._result_column = column.copy_fixed()
 

	
 
    table = property(_get_table, _set_table)
 
    result_column = property(_get_result_column, _set_result_column)
 

	
 

	
 
class ChangesetTable(object):
 
    """Changeset extensions to SQLAlchemy tables."""
 

	
 
    def create_column(self, column, *p, **kw):
 
        """Creates a column.
 

	
 
        The column parameter may be a column definition or the name of
 
        a column in this table.
 

	
 
        API to :meth:`ChangesetColumn.create`
 

	
 
        :param column: Column to be created
 
        :type column: Column instance or string
 
        """
 
        if not isinstance(column, sqlalchemy.Column):
 
            # It's a column name
 
            column = getattr(self.c, str(column))
 
        column.create(table=self, *p, **kw)
 

	
 
    def drop_column(self, column, *p, **kw):
 
        """Drop a column, given its name or definition.
 

	
 
        API to :meth:`ChangesetColumn.drop`
 

	
 
        :param column: Column to be droped
 
        :type column: Column instance or string
 
        """
 
        if not isinstance(column, sqlalchemy.Column):
 
            # It's a column name
 
            try:
 
                column = getattr(self.c, str(column))
 
            except AttributeError:
 
                # That column isn't part of the table. We don't need
 
                # its entire definition to drop the column, just its
 
                # name, so create a dummy column with the same name.
 
                column = sqlalchemy.Column(str(column), sqlalchemy.Integer())
 
        column.drop(table=self, *p, **kw)
 

	
 
    def rename(self, name, connection=None, **kwargs):
 
        """Rename this table.
 

	
 
        :param name: New name of the table.
 
        :type name: string
 
        :param connection: reuse connection istead of creating new one.
 
        :type connection: :class:`sqlalchemy.engine.base.Connection` instance
 
        """
 
        engine = self.bind
 
        self.new_name = name
 
        visitorcallable = get_engine_visitor(engine, 'schemachanger')
 
        run_single_visitor(engine, visitorcallable, self, connection, **kwargs)
 

	
 
        # Fix metadata registration
 
        self.name = name
 
        self.deregister()
 
        self._set_parent(self.metadata)
 

	
 
    def _meta_key(self):
 
        """Get the meta key for this table."""
 
        return sqlalchemy.schema._get_table_key(self.name, self.schema)
 

	
 
    def deregister(self):
 
        """Remove this table from its metadata"""
 
        if SQLA_07:
 
            self.metadata._remove_table(self.name, self.schema)
 
        else:
 
        key = self._meta_key()
 
        meta = self.metadata
 
        if key in meta.tables:
 
            del meta.tables[key]
 

	
 

	
 
class ChangesetColumn(object):
 
    """Changeset extensions to SQLAlchemy columns."""
 

	
 
    def alter(self, *p, **k):
 
        """Makes a call to :func:`alter_column` for the column this
 
        method is called on.
 
        """
 
        if 'table' not in k:
 
            k['table'] = self.table
 
        if 'engine' not in k:
 
            k['engine'] = k['table'].bind
 
        return alter_column(self, *p, **k)
 

	
 
    def create(self, table=None, index_name=None, unique_name=None,
 
               primary_key_name=None, populate_default=True, connection=None, **kwargs):
 
        """Create this column in the database.
 

	
 
        Assumes the given table exists. ``ALTER TABLE ADD COLUMN``,
 
        for most databases.
 

	
 
        :param table: Table instance to create on.
 
        :param index_name: Creates :class:`ChangesetIndex` on this column.
 
        :param unique_name: Creates :class:\
 
`~migrate.changeset.constraint.UniqueConstraint` on this column.
 
        :param primary_key_name: Creates :class:\
 
`~migrate.changeset.constraint.PrimaryKeyConstraint` on this column.
 
        :param populate_default: If True, created column will be \
 
populated with defaults
 
        :param connection: reuse connection istead of creating new one.
 
        :type table: Table instance
 
        :type index_name: string
 
        :type unique_name: string
 
        :type primary_key_name: string
 
        :type populate_default: bool
 
        :type connection: :class:`sqlalchemy.engine.base.Connection` instance
 

	
 
        :returns: self
 
        """
 
        self.populate_default = populate_default
 
        self.index_name = index_name
 
        self.unique_name = unique_name
 
        self.primary_key_name = primary_key_name
 
        for cons in ('index_name', 'unique_name', 'primary_key_name'):
 
            self._check_sanity_constraints(cons)
 

	
 
        self.add_to_table(table)
 
        engine = self.table.bind
 
        visitorcallable = get_engine_visitor(engine, 'columngenerator')
 
        engine._run_visitor(visitorcallable, self, connection, **kwargs)
 

	
 
        # TODO: reuse existing connection
 
        if self.populate_default and self.default is not None:
 
            stmt = table.update().values({self: engine._execute_default(self.default)})
 
            engine.execute(stmt)
 

	
 
        return self
 

	
 
    def drop(self, table=None, connection=None, **kwargs):
 
        """Drop this column from the database, leaving its table intact.
 

	
 
        ``ALTER TABLE DROP COLUMN``, for most databases.
 

	
 
        :param connection: reuse connection istead of creating new one.
 
        :type connection: :class:`sqlalchemy.engine.base.Connection` instance
 
        """
 
        if table is not None:
 
            self.table = table
 
        engine = self.table.bind
 
        visitorcallable = get_engine_visitor(engine, 'columndropper')
 
        engine._run_visitor(visitorcallable, self, connection, **kwargs)
 
        self.remove_from_table(self.table, unset_table=False)
 
        self.table = None
 
        return self
 

	
 
    def add_to_table(self, table):
 
        if table is not None  and self.table is None:
 
            if SQLA_07:
 
                table.append_column(self)
 
            else:
 
                self._set_parent(table)
 

	
 
    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()
rhodecode/lib/dbmigrate/migrate/versioning/schemadiff.py
Show inline comments
 
"""
 
   Schema differencing support.
 
"""
 

	
 
import logging
 
import sqlalchemy
 

	
 
from rhodecode.lib.dbmigrate.migrate.changeset import SQLA_06
 
from sqlalchemy.types import Float
 

	
 
log = logging.getLogger(__name__)
 

	
 
def getDiffOfModelAgainstDatabase(metadata, engine, excludeTables=None):
 
    """
 
    Return differences of model against database.
 

	
 
    :return: object which will evaluate to :keyword:`True` if there \
 
      are differences else :keyword:`False`.
 
    """
 
    db_metadata = sqlalchemy.MetaData(engine, reflect=True)
 

	
 
    # sqlite will include a dynamically generated 'sqlite_sequence' table if
 
    # there are autoincrement sequences in the database; this should not be
 
    # compared.
 
    if engine.dialect.name == 'sqlite':
 
        if 'sqlite_sequence' in db_metadata.tables:
 
            db_metadata.remove(db_metadata.tables['sqlite_sequence'])
 

	
 
    return SchemaDiff(metadata, db_metadata,
 
                      labelA='model',
 
                      labelB='database',
 
                      excludeTables=excludeTables)
 

	
 

	
 
def getDiffOfModelAgainstModel(metadataA, metadataB, excludeTables=None):
 
    """
 
    Return differences of model against another model.
 

	
 
    :return: object which will evaluate to :keyword:`True` if there \
 
      are differences else :keyword:`False`.
 
    """
 
    return SchemaDiff(metadataA, metadataB, excludeTables)
 
    return SchemaDiff(metadataA, metadataB, excludeTables=excludeTables)
 

	
 

	
 
class ColDiff(object):
 
    """
 
    Container for differences in one :class:`~sqlalchemy.schema.Column`
 
    between two :class:`~sqlalchemy.schema.Table` instances, ``A``
 
    and ``B``.
 

	
 
    .. attribute:: col_A
 

	
 
      The :class:`~sqlalchemy.schema.Column` object for A.
 

	
 
    .. attribute:: col_B
 

	
 
      The :class:`~sqlalchemy.schema.Column` object for B.
 

	
 
    .. attribute:: type_A
 

	
 
      The most generic type of the :class:`~sqlalchemy.schema.Column`
 
      object in A.
 

	
 
    .. attribute:: type_B
 

	
 
      The most generic type of the :class:`~sqlalchemy.schema.Column`
 
      object in A.
 

	
 
    """
 

	
 
    diff = False
 

	
 
    def __init__(self,col_A,col_B):
 
        self.col_A = col_A
 
        self.col_B = col_B
 

	
 
        self.type_A = col_A.type
 
        self.type_B = col_B.type
 

	
 
        self.affinity_A = self.type_A._type_affinity
 
        self.affinity_B = self.type_B._type_affinity
 

	
 
        if self.affinity_A is not self.affinity_B:
 
            self.diff = True
 
            return
 

	
 
        if isinstance(self.type_A,Float) or isinstance(self.type_B,Float):
 
            if not (isinstance(self.type_A,Float) and isinstance(self.type_B,Float)):
 
                self.diff=True
 
                return
 

	
 
        for attr in ('precision','scale','length'):
 
            A = getattr(self.type_A,attr,None)
 
            B = getattr(self.type_B,attr,None)
 
            if not (A is None or B is None) and A!=B:
 
                self.diff=True
 
                return
 

	
 
    def __nonzero__(self):
 
        return self.diff
 

	
 
class TableDiff(object):
 
    """
 
    Container for differences in one :class:`~sqlalchemy.schema.Table`
 
    between two :class:`~sqlalchemy.schema.MetaData` instances, ``A``
 
    and ``B``.
 

	
 
    .. attribute:: columns_missing_from_A
 

	
 
      A sequence of column names that were found in B but weren't in
 
      A.
 

	
 
    .. attribute:: columns_missing_from_B
 

	
 
      A sequence of column names that were found in A but weren't in
 
      B.
 

	
 
    .. attribute:: columns_different
 

	
 
      A dictionary containing information about columns that were
 
      found to be different.
 
      It maps column names to a :class:`ColDiff` objects describing the
 
      differences found.
 
    """
 
    __slots__ = (
 
        'columns_missing_from_A',
 
        'columns_missing_from_B',
 
        'columns_different',
 
        )
 

	
 
    def __nonzero__(self):
 
        return bool(
 
            self.columns_missing_from_A or
 
            self.columns_missing_from_B or
 
            self.columns_different
 
            )
 

	
 
class SchemaDiff(object):
rhodecode/lib/dbmigrate/migrate/versioning/util/__init__.py
Show inline comments
 
@@ -65,115 +65,115 @@ def guess_obj_type(obj):
 
    try:
 
        result = int(obj)
 
    except:
 
        pass
 

	
 
    if result is None:
 
        try:
 
            result = asbool(obj)
 
        except:
 
            pass
 

	
 
    if result is not None:
 
        return result
 
    else:
 
        return obj
 

	
 
@decorator
 
def catch_known_errors(f, *a, **kw):
 
    """Decorator that catches known api errors
 

	
 
    .. versionadded: 0.5.4
 
    """
 

	
 
    try:
 
        return f(*a, **kw)
 
    except exceptions.PathFoundError, e:
 
        raise exceptions.KnownError("The path %s already exists" % e.args[0])
 

	
 
def construct_engine(engine, **opts):
 
    """.. versionadded:: 0.5.4
 

	
 
    Constructs and returns SQLAlchemy engine.
 

	
 
    Currently, there are 2 ways to pass create_engine options to :mod:`migrate.versioning.api` functions:
 

	
 
    :param engine: connection string or a existing engine
 
    :param engine_dict: python dictionary of options to pass to `create_engine`
 
    :param engine_arg_*: keyword parameters to pass to `create_engine` (evaluated with :func:`migrate.versioning.util.guess_obj_type`)
 
    :type engine_dict: dict
 
    :type engine: string or Engine instance
 
    :type engine_arg_*: string
 
    :returns: SQLAlchemy Engine
 

	
 
    .. note::
 

	
 
        keyword parameters override ``engine_dict`` values.
 

	
 
    """
 
    if isinstance(engine, Engine):
 
        return engine
 
    elif not isinstance(engine, basestring):
 
        raise ValueError("you need to pass either an existing engine or a database uri")
 

	
 
    # get options for create_engine
 
    if opts.get('engine_dict') and isinstance(opts['engine_dict'], dict):
 
        kwargs = opts['engine_dict']
 
    else:
 
        kwargs = dict()
 

	
 
    # DEPRECATED: handle echo the old way
 
    echo = asbool(opts.get('echo', False))
 
    if echo:
 
        warnings.warn('echo=True parameter is deprecated, pass '
 
            'engine_arg_echo=True or engine_dict={"echo": True}',
 
            exceptions.MigrateDeprecationWarning)
 
        kwargs['echo'] = echo
 

	
 
    # parse keyword arguments
 
    for key, value in opts.iteritems():
 
        if key.startswith('engine_arg_'):
 
            kwargs[key[11:]] = guess_obj_type(value)
 

	
 
    log.debug('Constructing engine')
 
    # TODO: return create_engine(engine, poolclass=StaticPool, **kwargs)
 
    # seems like 0.5.x branch does not work with engine.dispose and staticpool
 
    return create_engine(engine, **kwargs)
 

	
 
@decorator
 
def with_engine(f, *a, **kw):
 
    """Decorator for :mod:`migrate.versioning.api` functions
 
    to safely close resources after function usage.
 

	
 
    Passes engine parameters to :func:`construct_engine` and
 
    resulting parameter is available as kw['engine'].
 

	
 
    Engine is disposed after wrapped function is executed.
 

	
 
    .. versionadded: 0.6.0
 
    """
 
    url = a[0]
 
    engine = construct_engine(url, **kw)
 

	
 
    try:
 
        kw['engine'] = engine
 
        return f(*a, **kw)
 
    finally:
 
        if isinstance(engine, Engine):
 
            log.debug('Disposing SQLAlchemy engine %s' % engine)
 
        if isinstance(engine, Engine) and engine is not url:
 
            log.debug('Disposing SQLAlchemy engine %s', engine)
 
            engine.dispose()
 

	
 

	
 
class Memoize:
 
    """Memoize(fn) - an instance which acts like fn but memoizes its arguments
 
       Will only work on functions with non-mutable arguments
 

	
 
       ActiveState Code 52201
 
    """
 
    def __init__(self, fn):
 
        self.fn = fn
 
        self.memo = {}
 

	
 
    def __call__(self, *args):
 
        if not self.memo.has_key(args):
 
            self.memo[args] = self.fn(*args)
 
        return self.memo[args]
rhodecode/lib/dbmigrate/schema/db_1_2_0.py
Show inline comments
 
@@ -834,192 +834,193 @@ class RepoGroup(Base, BaseModel):
 
        :param group_name:
 
        """
 
        path_prefix = (self.parent_group.full_path_splitted if
 
                       self.parent_group else [])
 
        return Group.url_sep().join(path_prefix + [group_name])
 

	
 

	
 
class Permission(Base, BaseModel):
 
    __tablename__ = 'permissions'
 
    __table_args__ = {'extend_existing':True}
 
    permission_id = Column("permission_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
 
    permission_name = Column("permission_name", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
 
    permission_longname = Column("permission_longname", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
 

	
 
    def __repr__(self):
 
        return "<%s('%s:%s')>" % (self.__class__.__name__,
 
                                  self.permission_id, self.permission_name)
 

	
 
    @classmethod
 
    def get_by_key(cls, key):
 
        return cls.query().filter(cls.permission_name == key).scalar()
 

	
 
class UserRepoToPerm(Base, BaseModel):
 
    __tablename__ = 'repo_to_perm'
 
    __table_args__ = (UniqueConstraint('user_id', 'repository_id'), {'extend_existing':True})
 
    repo_to_perm_id = Column("repo_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
 
    user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
 
    permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
 
    repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
 

	
 
    user = relationship('User')
 
    permission = relationship('Permission')
 
    repository = relationship('Repository')
 

	
 
class UserToPerm(Base, BaseModel):
 
    __tablename__ = 'user_to_perm'
 
    __table_args__ = (UniqueConstraint('user_id', 'permission_id'), {'extend_existing':True})
 
    user_to_perm_id = Column("user_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
 
    user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
 
    permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
 

	
 
    user = relationship('User')
 
    permission = relationship('Permission')
 

	
 
    @classmethod
 
    def has_perm(cls, user_id, perm):
 
        if not isinstance(perm, Permission):
 
            raise Exception('perm needs to be an instance of Permission class')
 

	
 
        return cls.query().filter(cls.user_id == user_id)\
 
            .filter(cls.permission == perm).scalar() is not None
 

	
 
    @classmethod
 
    def grant_perm(cls, user_id, perm):
 
        if not isinstance(perm, Permission):
 
            raise Exception('perm needs to be an instance of Permission class')
 

	
 
        new = cls()
 
        new.user_id = user_id
 
        new.permission = perm
 
        try:
 
            Session.add(new)
 
            Session.commit()
 
        except:
 
            Session.rollback()
 

	
 

	
 
    @classmethod
 
    def revoke_perm(cls, user_id, perm):
 
        if not isinstance(perm, Permission):
 
            raise Exception('perm needs to be an instance of Permission class')
 

	
 
        try:
 
            cls.query().filter(cls.user_id == user_id)\
 
                .filter(cls.permission == perm).delete()
 
            Session.commit()
 
        except:
 
            Session.rollback()
 

	
 
class UsersGroupRepoToPerm(Base, BaseModel):
 
    __tablename__ = 'users_group_repo_to_perm'
 
    __table_args__ = (UniqueConstraint('repository_id', 'users_group_id', 'permission_id'), {'extend_existing':True})
 
    users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
 
    users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
 
    permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
 
    repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
 

	
 
    users_group = relationship('UsersGroup')
 
    permission = relationship('Permission')
 
    repository = relationship('Repository')
 

	
 
    def __repr__(self):
 
        return '<userGroup:%s => %s >' % (self.users_group, self.repository)
 

	
 
class UsersGroupToPerm(Base, BaseModel):
 
    __tablename__ = 'users_group_to_perm'
 
    __table_args__ = {'extend_existing':True}
 
    users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
 
    users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
 
    permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
 

	
 
    users_group = relationship('UsersGroup')
 
    permission = relationship('Permission')
 

	
 

	
 
    @classmethod
 
    def has_perm(cls, users_group_id, perm):
 
        if not isinstance(perm, Permission):
 
            raise Exception('perm needs to be an instance of Permission class')
 

	
 
        return cls.query().filter(cls.users_group_id ==
 
                                         users_group_id)\
 
                                         .filter(cls.permission == perm)\
 
                                         .scalar() is not None
 

	
 
    @classmethod
 
    def grant_perm(cls, users_group_id, perm):
 
        if not isinstance(perm, Permission):
 
            raise Exception('perm needs to be an instance of Permission class')
 

	
 
        new = cls()
 
        new.users_group_id = users_group_id
 
        new.permission = perm
 
        try:
 
            Session.add(new)
 
            Session.commit()
 
        except:
 
            Session.rollback()
 

	
 

	
 
    @classmethod
 
    def revoke_perm(cls, users_group_id, perm):
 
        if not isinstance(perm, Permission):
 
            raise Exception('perm needs to be an instance of Permission class')
 

	
 
        try:
 
            cls.query().filter(cls.users_group_id == users_group_id)\
 
                .filter(cls.permission == perm).delete()
 
            Session.commit()
 
        except:
 
            Session.rollback()
 

	
 

	
 
class UserRepoGroupToPerm(Base, BaseModel):
 
    __tablename__ = 'group_to_perm'
 
    __table_args__ = (UniqueConstraint('group_id', 'permission_id'), {'extend_existing':True})
 

	
 
    group_to_perm_id = Column("group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
 
    user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
 
    permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
 
    group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=False, unique=None, default=None)
 

	
 
    user = relationship('User')
 
    permission = relationship('Permission')
 
    group = relationship('RepoGroup')
 

	
 
class Statistics(Base, BaseModel):
 
    __tablename__ = 'statistics'
 
    __table_args__ = (UniqueConstraint('repository_id'), {'extend_existing':True})
 
    stat_id = Column("stat_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
 
    repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=True, default=None)
 
    stat_on_revision = Column("stat_on_revision", Integer(), nullable=False)
 
    commit_activity = Column("commit_activity", LargeBinary(1000000), nullable=False)#JSON data
 
    commit_activity_combined = Column("commit_activity_combined", LargeBinary(), nullable=False)#JSON data
 
    languages = Column("languages", LargeBinary(1000000), nullable=False)#JSON data
 

	
 
    repository = relationship('Repository', single_parent=True)
 

	
 
class UserFollowing(Base, BaseModel):
 
    __tablename__ = 'user_followings'
 
    __table_args__ = (UniqueConstraint('user_id', 'follows_repository_id'),
 
                      UniqueConstraint('user_id', 'follows_user_id')
 
                      , {'extend_existing':True})
 

	
 
    user_following_id = Column("user_following_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
 
    user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
 
    follows_repo_id = Column("follows_repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=True, unique=None, default=None)
 
    follows_user_id = Column("follows_user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
 
    follows_from = Column('follows_from', DateTime(timezone=False), nullable=True, unique=None, default=datetime.datetime.now)
 

	
 
    user = relationship('User', primaryjoin='User.user_id==UserFollowing.user_id')
 

	
 
    follows_user = relationship('User', primaryjoin='User.user_id==UserFollowing.follows_user_id')
 
    follows_repository = relationship('Repository', order_by='Repository.repo_name')
 

	
 

	
 
    @classmethod
 
    def get_repo_followers(cls, repo_id):
 
        return cls.query().filter(cls.follows_repo_id == repo_id)
 

	
 
class CacheInvalidation(Base, BaseModel):
 
    __tablename__ = 'cache_invalidation'
 
    __table_args__ = (UniqueConstraint('cache_key'), {'extend_existing':True})
rhodecode/lib/dbmigrate/schema/db_1_3_0.py
Show inline comments
 
# -*- coding: utf-8 -*-
 
"""
 
    rhodecode.model.db
 
    ~~~~~~~~~~~~~~~~~~
 

	
 
    Database Models for RhodeCode
 

	
 
    :created_on: Apr 08, 2010
 
    :author: marcink
 
    :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com>
 
    :license: GPLv3, see COPYING for more details.
 
"""
 
# This program is free software: you can redistribute it and/or modify
 
# it under the terms of the GNU General Public License as published by
 
# the Free Software Foundation, either version 3 of the License, or
 
# (at your option) any later version.
 
#
 
# This program is distributed in the hope that it will be useful,
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
# GNU General Public License for more details.
 
#
 
# You should have received a copy of the GNU General Public License
 
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 

	
 
#TODO: when branch 1.3 is finished replacem with db.py content
 

	
 
from rhodecode.model.db import *
rhodecode/lib/dbmigrate/versions/002_version_1_1_0.py
Show inline comments
 
import logging
 
import datetime
 

	
 
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.lib.dbmigrate.migrate import *
 
from rhodecode.lib.dbmigrate.migrate.changeset import *
 

	
 
log = logging.getLogger(__name__)
 

	
 

	
 
def upgrade(migrate_engine):
 
    """ Upgrade operations go here.
 
    Don't create your own engine; bind migrate_engine to your metadata
 
    """
 

	
 
    #==========================================================================
 
    # Upgrade of `users` table
 
    #==========================================================================
 
    tblname = 'users'
 
    tbl = Table(tblname, MetaData(bind=migrate_engine), autoload=True,
 
                    autoload_with=migrate_engine)
 

	
 
    #ADD is_ldap column
 
    is_ldap = Column("is_ldap", Boolean(), nullable=True,
 
                     unique=None, default=False)
 
    is_ldap.create(tbl, populate_default=True)
 
    is_ldap.alter(nullable=False)
 

	
 
    #==========================================================================
 
    # Upgrade of `user_logs` table
 
    #==========================================================================
 

	
 
    tblname = 'users'
 
    tbl = Table(tblname, MetaData(bind=migrate_engine), autoload=True,
 
                    autoload_with=migrate_engine)
 

	
 
    #ADD revision column
 
    revision = Column('revision', TEXT(length=None, convert_unicode=False,
 
                                       assert_unicode=None),
 
                      nullable=True, unique=None, default=None)
 
    revision.create(tbl)
 

	
 

	
 

	
 
    #==========================================================================
 
    # Upgrade of `repositories` table
 
    #==========================================================================
 
    tblname = 'repositories'
 
    tbl = Table(tblname, MetaData(bind=migrate_engine), autoload=True,
 
                    autoload_with=migrate_engine)
 

	
 
    #ADD repo_type column#
 
    repo_type = Column("repo_type", String(length=None, convert_unicode=False,
 
                                           assert_unicode=None),
 
                       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,
 
                               unique=None, default=True)
 
    enable_statistics.create(tbl)
 

	
 
    #==========================================================================
 
    # Add table `user_followings`
 
    #==========================================================================
 
    from rhodecode.lib.dbmigrate.schema.db_1_1_0 import UserFollowing
 
    UserFollowing().__table__.create()
 

	
 
    #==========================================================================
 
    # Add table `cache_invalidation`
 
    #==========================================================================
 
    from rhodecode.lib.dbmigrate.schema.db_1_1_0 import CacheInvalidation
 
    CacheInvalidation().__table__.create()
 

	
 
    return
 

	
 

	
 
def downgrade(migrate_engine):
 
    meta = MetaData()
 
    meta.bind = migrate_engine
rhodecode/lib/dbmigrate/versions/003_version_1_2_0.py
Show inline comments
 
import logging
 
import datetime
 

	
 
from sqlalchemy import *
 
from sqlalchemy.exc import DatabaseError
 
from sqlalchemy.orm import relation, backref, class_mapper
 
from sqlalchemy.orm.session import Session
 

	
 
from rhodecode.lib.dbmigrate.migrate import *
 
from rhodecode.lib.dbmigrate.migrate.changeset import *
 

	
 
from rhodecode.model.meta import Base
 

	
 
log = logging.getLogger(__name__)
 

	
 

	
 
def upgrade(migrate_engine):
 
    """ Upgrade operations go here.
 
    Don't create your own engine; bind migrate_engine to your metadata
 
    """
 

	
 
    #==========================================================================
 
    # Add table `groups``
 
    #==========================================================================
 
    from rhodecode.lib.dbmigrate.schema.db_1_2_0 import RepoGroup as Group
 
    Group().__table__.create()
 

	
 
    #==========================================================================
 
    # Add table `group_to_perm`
 
    #==========================================================================
 
    from rhodecode.lib.dbmigrate.schema.db_1_2_0 import UserRepoGroupToPerm
 
    UserRepoGroupToPerm().__table__.create()
 

	
 
    #==========================================================================
 
    # Add table `users_groups`
 
    #==========================================================================
 
    from rhodecode.lib.dbmigrate.schema.db_1_2_0 import UsersGroup
 
    UsersGroup().__table__.create()
 

	
 
    #==========================================================================
 
    # Add table `users_groups_members`
 
    #==========================================================================
 
    from rhodecode.lib.dbmigrate.schema.db_1_2_0 import UsersGroupMember
 
    UsersGroupMember().__table__.create()
 

	
 
    #==========================================================================
 
    # Add table `users_group_repo_to_perm`
 
    #==========================================================================
 
    from rhodecode.lib.dbmigrate.schema.db_1_2_0 import UsersGroupRepoToPerm
 
    UsersGroupRepoToPerm().__table__.create()
 

	
 
    #==========================================================================
 
    # Add table `users_group_to_perm`
 
    #==========================================================================
 
    from rhodecode.lib.dbmigrate.schema.db_1_2_0 import UsersGroupToPerm
 
    UsersGroupToPerm().__table__.create()
 

	
 
    #==========================================================================
 
    # Upgrade of `users` table
 
    #==========================================================================
 
    from rhodecode.lib.dbmigrate.schema.db_1_2_0 import User
 

	
 
    #add column
 
    ldap_dn = Column("ldap_dn", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
 
    ldap_dn.create(User().__table__)
 

	
 
    api_key = Column("api_key", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
 
    api_key.create(User().__table__)
 

	
 
    #remove old column
 
    is_ldap = Column("is_ldap", Boolean(), nullable=False, unique=None, default=False)
 
    is_ldap.drop(User().__table__)
 

	
 

	
 
    #==========================================================================
 
    # Upgrade of `repositories` table
 
    #==========================================================================
 
    from rhodecode.lib.dbmigrate.schema.db_1_2_0 import Repository
 

	
 
    #ADD clone_uri column#
 

	
 
    clone_uri = Column("clone_uri", String(length=255, convert_unicode=False,
 
                                           assert_unicode=None),
 
                        nullable=True, unique=False, default=None)
 

	
 
    clone_uri.create(Repository().__table__)
 

	
 
    #ADD downloads column#
 
    enable_downloads = Column("downloads", Boolean(), nullable=True, unique=None, default=True)
 
    enable_downloads.create(Repository().__table__)
 

	
 
    #ADD column created_on
 
    created_on = Column('created_on', DateTime(timezone=False), nullable=True,
 
                        unique=None, default=datetime.datetime.now)
 
    created_on.create(Repository().__table__)
 

	
 
    #ADD group_id column#
 
    group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'),
 
                  nullable=True, unique=False, default=None)
 

	
 
    group_id.create(Repository().__table__)
 

	
 

	
 
    #==========================================================================
 
    # Upgrade of `user_followings` table
 
    #==========================================================================
 

	
 
    from rhodecode.lib.dbmigrate.schema.db_1_2_0 import UserFollowing
 

	
 
    follows_from = Column('follows_from', DateTime(timezone=False),
 
                          nullable=True, unique=None,
 
                          default=datetime.datetime.now)
rhodecode/lib/dbmigrate/versions/004_version_1_3_0.py
Show inline comments
 
import logging
 
import datetime
 

	
 
from sqlalchemy import *
 
from sqlalchemy.exc import DatabaseError
 
from sqlalchemy.orm import relation, backref, class_mapper
 
from sqlalchemy.orm.session import Session
 

	
 
from rhodecode.lib.dbmigrate.migrate import *
 
from rhodecode.lib.dbmigrate.migrate.changeset import *
 

	
 
from rhodecode.model.meta import Base
 

	
 
log = logging.getLogger(__name__)
 

	
 

	
 
def upgrade(migrate_engine):
 
    """ Upgrade operations go here.
 
    Don't create your own engine; bind migrate_engine to your metadata
 
    """
 
    #==========================================================================
 
    # Add table `users_group_repo_group_to_perm`
 
    #==========================================================================
 
    from rhodecode.lib.dbmigrate.schema.db_1_3_0 import UsersGroupRepoGroupToPerm
 
    UsersGroupRepoGroupToPerm().__table__.create()
 

	
 
    #==========================================================================
 
    # Add table `changeset_comments`
 
    #==========================================================================
 
    from rhodecode.lib.dbmigrate.schema.db_1_3_0 import  ChangesetComment
 
    ChangesetComment().__table__.create()
 

	
 
    #==========================================================================
 
    # Add table `notifications`
 
    #==========================================================================
 
    from rhodecode.lib.dbmigrate.schema.db_1_3_0 import  Notification
 
    Notification().__table__.create()
 

	
 
    #==========================================================================
 
    # Add table `user_to_notification`
 
    #==========================================================================
 
    from rhodecode.lib.dbmigrate.schema.db_1_3_0 import  UserNotification
 
    UserNotification().__table__.create()
 

	
 
    #==========================================================================
 
    # Add unique to table `users_group_to_perm`
 
    #==========================================================================
 
    from rhodecode.lib.dbmigrate.schema.db_1_3_0 import UsersGroupToPerm
 
    tbl = UsersGroupToPerm().__table__
 
    cons = UniqueConstraint('users_group_id', 'permission_id', table=tbl)
 
    cons.create()
 

	
 
    #==========================================================================
 
    # Fix unique constrain on table `user_logs`
 
    #==========================================================================
 
    from rhodecode.lib.dbmigrate.schema.db_1_3_0 import UserLog
 
    tbl = UserLog().__table__
 
    col = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'),
 
                 nullable=False, unique=None, default=None)
 
    col.alter(nullable=True, table=tbl)
 

	
 
    #==========================================================================
 
    # Rename table `group_to_perm` to `user_repo_group_to_perm`
 
    #==========================================================================
 
    tbl = Table('group_to_perm', MetaData(bind=migrate_engine), autoload=True,
 
                    autoload_with=migrate_engine)
 
    tbl.rename('user_repo_group_to_perm')
 

	
 
    return
 

	
 

	
 
def downgrade(migrate_engine):
 
    meta = MetaData()
 
    meta.bind = migrate_engine
rhodecode/lib/dbmigrate/versions/005_version_1_3_0.py
Show inline comments
 
new file 100644
 
import logging
 
import datetime
 

	
 
from sqlalchemy import *
 
from sqlalchemy.exc import DatabaseError
 
from sqlalchemy.orm import relation, backref, class_mapper
 
from sqlalchemy.orm.session import Session
 

	
 
from rhodecode.lib.dbmigrate.migrate import *
 
from rhodecode.lib.dbmigrate.migrate.changeset import *
 

	
 
from rhodecode.model.meta import Base
 

	
 
log = logging.getLogger(__name__)
 

	
 

	
 
def upgrade(migrate_engine):
 
    """ Upgrade operations go here.
 
    Don't create your own engine; bind migrate_engine to your metadata
 
    """
 

	
 
    #==========================================================================
 
    # Change unique constraints of table `repo_to_perm`
 
    #==========================================================================
 
    from rhodecode.lib.dbmigrate.schema.db_1_3_0 import UserRepoToPerm
 
    tbl = UserRepoToPerm().__table__
 
    new_cons = UniqueConstraint('user_id', 'repository_id', 'permission_id', table=tbl)
 
    new_cons.create()
 

	
 
    if migrate_engine.name in ['mysql']:
 
        old_cons = UniqueConstraint('user_id', 'repository_id', table=tbl, name="user_id")
 
        old_cons.drop()
 
    elif migrate_engine.name in ['postgresql']:
 
        old_cons = UniqueConstraint('user_id', 'repository_id', table=tbl)
 
        old_cons.drop()
 
    else:
 
        # sqlite doesn't support dropping constraints...
 
        print """Please manually drop UniqueConstraint('user_id', 'repository_id')"""
 

	
 
    #==========================================================================
 
    # fix uniques of table `user_repo_group_to_perm`
 
    #==========================================================================
 
    from rhodecode.lib.dbmigrate.schema.db_1_3_0 import UserRepoGroupToPerm
 
    tbl = UserRepoGroupToPerm().__table__
 
    new_cons = UniqueConstraint('group_id', 'permission_id', 'user_id', table=tbl)
 
    new_cons.create()
 

	
 
    # fix uniqueConstraints
 
    if migrate_engine.name in ['mysql']:
 
        #mysql is givinig troubles here...
 
        old_cons = UniqueConstraint('group_id', 'permission_id', table=tbl, name="group_id")
 
        old_cons.drop()
 
    elif migrate_engine.name in ['postgresql']:
 
        old_cons = UniqueConstraint('group_id', 'permission_id', table=tbl, name='group_to_perm_group_id_permission_id_key')
 
        old_cons.drop()
 
    else:
 
        # sqlite doesn't support dropping constraints...
 
        print """Please manually drop UniqueConstraint('group_id', 'permission_id')"""
 

	
 
    return
 

	
 

	
 
def downgrade(migrate_engine):
 
    meta = MetaData()
 
    meta.bind = migrate_engine
rhodecode/model/db.py
Show inline comments
 
# -*- coding: utf-8 -*-
 
"""
 
    rhodecode.model.db
 
    ~~~~~~~~~~~~~~~~~~
 

	
 
    Database Models for RhodeCode
 

	
 
    :created_on: Apr 08, 2010
 
    :author: marcink
 
    :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com>
 
    :license: GPLv3, see COPYING for more details.
 
"""
 
# This program is free software: you can redistribute it and/or modify
 
# it under the terms of the GNU General Public License as published by
 
# the Free Software Foundation, either version 3 of the License, or
 
# (at your option) any later version.
 
#
 
# This program is distributed in the hope that it will be useful,
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
# GNU General Public License for more details.
 
#
 
# You should have received a copy of the GNU General Public License
 
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 

	
 
import os
 
import logging
 
import datetime
 
import traceback
 
from collections import defaultdict
 

	
 
from sqlalchemy import *
 
from sqlalchemy.ext.hybrid import hybrid_property
 
from sqlalchemy.orm import relationship, joinedload, class_mapper, validates
 
from beaker.cache import cache_region, region_invalidate
 

	
 
from vcs import get_backend
 
from vcs.utils.helpers import get_scm
 
from vcs.exceptions import VCSError
 
from vcs.utils.lazy import LazyProperty
 

	
 
from rhodecode.lib import str2bool, safe_str, get_changeset_safe, safe_unicode
 
from rhodecode.lib.exceptions import UsersGroupsAssignedException
 
from rhodecode.lib.compat import json
 
from rhodecode.lib.caching_query import FromCache
 

	
 
from rhodecode.model.meta import Base, Session
 

	
 

	
 
log = logging.getLogger(__name__)
 

	
 
#==============================================================================
 
# BASE CLASSES
 
#==============================================================================
 

	
 

	
 
class ModelSerializer(json.JSONEncoder):
 
    """
 
    Simple Serializer for JSON,
 

	
 
    usage::
 

	
 
        to make object customized for serialization implement a __json__
 
        method that will return a dict for serialization into json
 

	
 
    example::
 

	
 
        class Task(object):
 

	
 
            def __init__(self, name, value):
 
                self.name = name
 
                self.value = value
 

	
 
            def __json__(self):
 
                return dict(name=self.name,
 
                            value=self.value)
 

	
 
    """
 

	
 
    def default(self, obj):
 

	
 
        if hasattr(obj, '__json__'):
 
            return obj.__json__()
 
        else:
 
            return json.JSONEncoder.default(self, obj)
 

	
 

	
 
class BaseModel(object):
 
    """
 
    Base Model for all classess
 
    """
 

	
 
    @classmethod
 
    def _get_keys(cls):
 
        """return column names for this model """
 
        return class_mapper(cls).c.keys()
 

	
 
    def get_dict(self):
 
        """
 
        return dict with keys and values corresponding
 
        to this model data """
 

	
 
        d = {}
 
        for k in self._get_keys():
 
            d[k] = getattr(self, k)
 

	
 
        # also use __json__() if present to get additional fields
 
        for k, val in getattr(self, '__json__', lambda: {})().iteritems():
 
            d[k] = val
 
        return d
 

	
 
    def get_appstruct(self):
 
        """return list with keys and values tupples corresponding
 
        to this model data """
 

	
 
        l = []
 
        for k in self._get_keys():
 
            l.append((k, getattr(self, k),))
 
        return l
 

	
 
    def populate_obj(self, populate_dict):
 
        """populate model with data from given populate_dict"""
 

	
 
        for k in self._get_keys():
 
            if k in populate_dict:
 
                setattr(self, k, populate_dict[k])
 

	
 
    @classmethod
 
    def query(cls):
 
        return Session.query(cls)
 

	
 
    @classmethod
 
    def get(cls, id_):
 
        if id_:
 
            return cls.query().get(id_)
 

	
 
    @classmethod
 
    def getAll(cls):
 
        return cls.query().all()
 

	
 
    @classmethod
 
    def delete(cls, id_):
 
        obj = cls.query().get(id_)
 
        Session.delete(obj)
 

	
 
@@ -589,462 +589,463 @@ class Repository(Base, BaseModel):
 
                baseui.setconfig(ui_.ui_section, ui_.ui_key, ui_.ui_value)
 

	
 
        return baseui
 

	
 
    @classmethod
 
    def is_valid(cls, repo_name):
 
        """
 
        returns True if given repo name is a valid filesystem repository
 

	
 
        :param cls:
 
        :param repo_name:
 
        """
 
        from rhodecode.lib.utils import is_valid_repo
 

	
 
        return is_valid_repo(repo_name, cls.base_path())
 

	
 
    #==========================================================================
 
    # SCM PROPERTIES
 
    #==========================================================================
 

	
 
    def get_changeset(self, rev):
 
        return get_changeset_safe(self.scm_instance, rev)
 

	
 
    @property
 
    def tip(self):
 
        return self.get_changeset('tip')
 

	
 
    @property
 
    def author(self):
 
        return self.tip.author
 

	
 
    @property
 
    def last_change(self):
 
        return self.scm_instance.last_change
 

	
 
    def comments(self, revisions=None):
 
        """
 
        Returns comments for this repository grouped by revisions
 

	
 
        :param revisions: filter query by revisions only
 
        """
 
        cmts = ChangesetComment.query()\
 
            .filter(ChangesetComment.repo == self)
 
        if revisions:
 
            cmts = cmts.filter(ChangesetComment.revision.in_(revisions))
 
        grouped = defaultdict(list)
 
        for cmt in cmts.all():
 
            grouped[cmt.revision].append(cmt)
 
        return grouped
 

	
 
    #==========================================================================
 
    # SCM CACHE INSTANCE
 
    #==========================================================================
 

	
 
    @property
 
    def invalidate(self):
 
        return CacheInvalidation.invalidate(self.repo_name)
 

	
 
    def set_invalidate(self):
 
        """
 
        set a cache for invalidation for this instance
 
        """
 
        CacheInvalidation.set_invalidate(self.repo_name)
 

	
 
    @LazyProperty
 
    def scm_instance(self):
 
        return self.__get_instance()
 

	
 
    @property
 
    def scm_instance_cached(self):
 
        @cache_region('long_term')
 
        def _c(repo_name):
 
            return self.__get_instance()
 
        rn = self.repo_name
 
        log.debug('Getting cached instance of repo')
 
        inv = self.invalidate
 
        if inv is not None:
 
            region_invalidate(_c, None, rn)
 
            # update our cache
 
            CacheInvalidation.set_valid(inv.cache_key)
 
        return _c(rn)
 

	
 
    def __get_instance(self):
 
        repo_full_path = self.repo_full_path
 
        try:
 
            alias = get_scm(repo_full_path)[0]
 
            log.debug('Creating instance of %s repository' % alias)
 
            backend = get_backend(alias)
 
        except VCSError:
 
            log.error(traceback.format_exc())
 
            log.error('Perhaps this repository is in db and not in '
 
                      'filesystem run rescan repositories with '
 
                      '"destroy old data " option from admin panel')
 
            return
 

	
 
        if alias == 'hg':
 

	
 
            repo = backend(safe_str(repo_full_path), create=False,
 
                           baseui=self._ui)
 
            # skip hidden web repository
 
            if repo._get_hidden():
 
                return
 
        else:
 
            repo = backend(repo_full_path, create=False)
 

	
 
        return repo
 

	
 

	
 
class RepoGroup(Base, BaseModel):
 
    __tablename__ = 'groups'
 
    __table_args__ = (
 
        UniqueConstraint('group_name', 'group_parent_id'),
 
        CheckConstraint('group_id != group_parent_id'),
 
        {'extend_existing': True},
 
    )
 
    __mapper_args__ = {'order_by': 'group_name'}
 

	
 
    group_id = Column("group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
 
    group_name = Column("group_name", String(length=255, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None)
 
    group_parent_id = Column("group_parent_id", Integer(), ForeignKey('groups.group_id'), nullable=True, unique=None, default=None)
 
    group_description = Column("group_description", String(length=10000, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
 

	
 
    repo_group_to_perm = relationship('UserRepoGroupToPerm', cascade='all', order_by='UserRepoGroupToPerm.group_to_perm_id')
 
    users_group_to_perm = relationship('UsersGroupRepoGroupToPerm', cascade='all')
 

	
 
    parent_group = relationship('RepoGroup', remote_side=group_id)
 

	
 
    def __init__(self, group_name='', parent_group=None):
 
        self.group_name = group_name
 
        self.parent_group = parent_group
 

	
 
    def __repr__(self):
 
        return "<%s('%s:%s')>" % (self.__class__.__name__, self.group_id,
 
                                  self.group_name)
 

	
 
    @classmethod
 
    def groups_choices(cls):
 
        from webhelpers.html import literal as _literal
 
        repo_groups = [('', '')]
 
        sep = ' &raquo; '
 
        _name = lambda k: _literal(sep.join(k))
 

	
 
        repo_groups.extend([(x.group_id, _name(x.full_path_splitted))
 
                              for x in cls.query().all()])
 

	
 
        repo_groups = sorted(repo_groups, key=lambda t: t[1].split(sep)[0])
 
        return repo_groups
 

	
 
    @classmethod
 
    def url_sep(cls):
 
        return '/'
 

	
 
    @classmethod
 
    def get_by_group_name(cls, group_name, cache=False, case_insensitive=False):
 
        if case_insensitive:
 
            gr = cls.query()\
 
                .filter(cls.group_name.ilike(group_name))
 
        else:
 
            gr = cls.query()\
 
                .filter(cls.group_name == group_name)
 
        if cache:
 
            gr = gr.options(FromCache("sql_cache_short",
 
                                          "get_group_%s" % group_name))
 
        return gr.scalar()
 

	
 
    @property
 
    def parents(self):
 
        parents_recursion_limit = 5
 
        groups = []
 
        if self.parent_group is None:
 
            return groups
 
        cur_gr = self.parent_group
 
        groups.insert(0, cur_gr)
 
        cnt = 0
 
        while 1:
 
            cnt += 1
 
            gr = getattr(cur_gr, 'parent_group', None)
 
            cur_gr = cur_gr.parent_group
 
            if gr is None:
 
                break
 
            if cnt == parents_recursion_limit:
 
                # this will prevent accidental infinit loops
 
                log.error('group nested more than %s' %
 
                          parents_recursion_limit)
 
                break
 

	
 
            groups.insert(0, gr)
 
        return groups
 

	
 
    @property
 
    def children(self):
 
        return RepoGroup.query().filter(RepoGroup.parent_group == self)
 

	
 
    @property
 
    def name(self):
 
        return self.group_name.split(RepoGroup.url_sep())[-1]
 

	
 
    @property
 
    def full_path(self):
 
        return self.group_name
 

	
 
    @property
 
    def full_path_splitted(self):
 
        return self.group_name.split(RepoGroup.url_sep())
 

	
 
    @property
 
    def repositories(self):
 
        return Repository.query().filter(Repository.group == self)
 

	
 
    @property
 
    def repositories_recursive_count(self):
 
        cnt = self.repositories.count()
 

	
 
        def children_count(group):
 
            cnt = 0
 
            for child in group.children:
 
                cnt += child.repositories.count()
 
                cnt += children_count(child)
 
            return cnt
 

	
 
        return cnt + children_count(self)
 

	
 
    def get_new_name(self, group_name):
 
        """
 
        returns new full group name based on parent and new name
 

	
 
        :param group_name:
 
        """
 
        path_prefix = (self.parent_group.full_path_splitted if
 
                       self.parent_group else [])
 
        return RepoGroup.url_sep().join(path_prefix + [group_name])
 

	
 

	
 
class Permission(Base, BaseModel):
 
    __tablename__ = 'permissions'
 
    __table_args__ = {'extend_existing': True}
 
    permission_id = Column("permission_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
 
    permission_name = Column("permission_name", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
 
    permission_longname = Column("permission_longname", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
 

	
 
    def __repr__(self):
 
        return "<%s('%s:%s')>" % (
 
            self.__class__.__name__, self.permission_id, self.permission_name
 
        )
 

	
 
    @classmethod
 
    def get_by_key(cls, key):
 
        return cls.query().filter(cls.permission_name == key).scalar()
 

	
 
    @classmethod
 
    def get_default_perms(cls, default_user_id):
 
        q = Session.query(UserRepoToPerm, Repository, cls)\
 
         .join((Repository, UserRepoToPerm.repository_id == Repository.repo_id))\
 
         .join((cls, UserRepoToPerm.permission_id == cls.permission_id))\
 
         .filter(UserRepoToPerm.user_id == default_user_id)
 

	
 
        return q.all()
 

	
 
    @classmethod
 
    def get_default_group_perms(cls, default_user_id):
 
        q = Session.query(UserRepoGroupToPerm, RepoGroup, cls)\
 
         .join((RepoGroup, UserRepoGroupToPerm.group_id == RepoGroup.group_id))\
 
         .join((cls, UserRepoGroupToPerm.permission_id == cls.permission_id))\
 
         .filter(UserRepoGroupToPerm.user_id == default_user_id)
 

	
 
        return q.all()
 

	
 

	
 
class UserRepoToPerm(Base, BaseModel):
 
    __tablename__ = 'repo_to_perm'
 
    __table_args__ = (
 
        UniqueConstraint('user_id', 'repository_id'),
 
        UniqueConstraint('user_id', 'repository_id', 'permission_id'),
 
        {'extend_existing': True}
 
    )
 
    repo_to_perm_id = Column("repo_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
 
    user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
 
    permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
 
    repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
 

	
 
    user = relationship('User')
 
    repository = relationship('Repository')
 
    permission = relationship('Permission')
 
    repository = relationship('Repository')
 

	
 
    @classmethod
 
    def create(cls, user, repository, permission):
 
        n = cls()
 
        n.user = user
 
        n.repository = repository
 
        n.permission = permission
 
        Session.add(n)
 
        return n
 

	
 
    def __repr__(self):
 
        return '<user:%s => %s >' % (self.user, self.repository)
 

	
 

	
 
class UserToPerm(Base, BaseModel):
 
    __tablename__ = 'user_to_perm'
 
    __table_args__ = (
 
        UniqueConstraint('user_id', 'permission_id'),
 
        {'extend_existing': True}
 
    )
 
    user_to_perm_id = Column("user_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
 
    user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
 
    permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
 

	
 
    user = relationship('User')
 
    permission = relationship('Permission', lazy='joined')
 

	
 

	
 
class UsersGroupRepoToPerm(Base, BaseModel):
 
    __tablename__ = 'users_group_repo_to_perm'
 
    __table_args__ = (
 
        UniqueConstraint('repository_id', 'users_group_id',),
 
        UniqueConstraint('repository_id', 'users_group_id', 'permission_id'),
 
        {'extend_existing': True}
 
    )
 
    users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
 
    users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
 
    permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
 
    repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
 

	
 
    users_group = relationship('UsersGroup')
 
    permission = relationship('Permission')
 
    repository = relationship('Repository')
 

	
 
    @classmethod
 
    def create(cls, users_group, repository, permission):
 
        n = cls()
 
        n.users_group = users_group
 
        n.repository = repository
 
        n.permission = permission
 
        Session.add(n)
 
        return n
 

	
 
    def __repr__(self):
 
        return '<userGroup:%s => %s >' % (self.users_group, self.repository)
 

	
 

	
 
class UsersGroupToPerm(Base, BaseModel):
 
    __tablename__ = 'users_group_to_perm'
 
    __table_args__ = (
 
        UniqueConstraint('users_group_id', 'permission_id',),
 
        {'extend_existing': True}
 
    )
 
    users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
 
    users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
 
    permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
 

	
 
    users_group = relationship('UsersGroup')
 
    permission = relationship('Permission')
 

	
 

	
 
class UserRepoGroupToPerm(Base, BaseModel):
 
    __tablename__ = 'user_repo_group_to_perm'
 
    __table_args__ = (
 
        UniqueConstraint('user_id', 'group_id'),
 
        UniqueConstraint('user_id', 'group_id', 'permission_id'),
 
        {'extend_existing': True}
 
    )
 

	
 
    group_to_perm_id = Column("group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
 
    user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
 
    group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=False, unique=None, default=None)
 
    permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
 

	
 
    user = relationship('User')
 
    group = relationship('RepoGroup')
 
    permission = relationship('Permission')
 
    group = relationship('RepoGroup')
 

	
 

	
 
class UsersGroupRepoGroupToPerm(Base, BaseModel):
 
    __tablename__ = 'users_group_repo_group_to_perm'
 
    __table_args__ = (
 
        UniqueConstraint('users_group_id', 'group_id'),
 
        {'extend_existing': True}
 
    )
 

	
 
    users_group_repo_group_to_perm_id = Column("users_group_repo_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
 
    users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
 
    group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=False, unique=None, default=None)
 
    permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
 

	
 
    users_group = relationship('UsersGroup')
 
    permission = relationship('Permission')
 
    group = relationship('RepoGroup')
 

	
 

	
 
class Statistics(Base, BaseModel):
 
    __tablename__ = 'statistics'
 
    __table_args__ = (UniqueConstraint('repository_id'), {'extend_existing': True})
 
    stat_id = Column("stat_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
 
    repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=True, default=None)
 
    stat_on_revision = Column("stat_on_revision", Integer(), nullable=False)
 
    commit_activity = Column("commit_activity", LargeBinary(1000000), nullable=False)#JSON data
 
    commit_activity_combined = Column("commit_activity_combined", LargeBinary(), nullable=False)#JSON data
 
    languages = Column("languages", LargeBinary(1000000), nullable=False)#JSON data
 

	
 
    repository = relationship('Repository', single_parent=True)
 

	
 

	
 
class UserFollowing(Base, BaseModel):
 
    __tablename__ = 'user_followings'
 
    __table_args__ = (
 
        UniqueConstraint('user_id', 'follows_repository_id'),
 
        UniqueConstraint('user_id', 'follows_user_id'),
 
        {'extend_existing': True}
 
    )
 

	
 
    user_following_id = Column("user_following_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
 
    user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
 
    follows_repo_id = Column("follows_repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=True, unique=None, default=None)
 
    follows_user_id = Column("follows_user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
 
    follows_from = Column('follows_from', DateTime(timezone=False), nullable=True, unique=None, default=datetime.datetime.now)
 

	
 
    user = relationship('User', primaryjoin='User.user_id==UserFollowing.user_id')
 

	
 
    follows_user = relationship('User', primaryjoin='User.user_id==UserFollowing.follows_user_id')
 
    follows_repository = relationship('Repository', order_by='Repository.repo_name')
 

	
 
    @classmethod
 
    def get_repo_followers(cls, repo_id):
 
        return cls.query().filter(cls.follows_repo_id == repo_id)
 

	
 

	
 
class CacheInvalidation(Base, BaseModel):
 
    __tablename__ = 'cache_invalidation'
 
    __table_args__ = (UniqueConstraint('cache_key'), {'extend_existing': True})
 
    cache_id = Column("cache_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
 
    cache_key = Column("cache_key", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
 
    cache_args = Column("cache_args", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
 
    cache_active = Column("cache_active", Boolean(), nullable=True, unique=None, default=False)
 

	
 
    def __init__(self, cache_key, cache_args=''):
 
        self.cache_key = cache_key
 
        self.cache_args = cache_args
 
        self.cache_active = False
 

	
 
    def __repr__(self):
 
        return "<%s('%s:%s')>" % (self.__class__.__name__,
 
                                  self.cache_id, self.cache_key)
 

	
 
    @classmethod
 
    def invalidate(cls, key):
 
        """
 
        Returns Invalidation object if this given key should be invalidated
 
        None otherwise. `cache_active = False` means that this cache
 
        state is not valid and needs to be invalidated
 

	
 
        :param key:
 
        """
 
        return cls.query()\
 
                .filter(CacheInvalidation.cache_key == key)\
 
                .filter(CacheInvalidation.cache_active == False)\
 
                .scalar()
 

	
 
    @classmethod
 
    def set_invalidate(cls, key):
 
        """
 
        Mark this Cache key for invalidation
 

	
 
        :param key:
 
        """
 

	
 
        log.debug('marking %s for invalidation' % key)
0 comments (0 inline, 0 general)