Changeset - 9bb609d164e4
[Not reviewed]
rhodecode/lib/dbmigrate/migrate/changeset/databases/firebird.py
Show inline comments
 
@@ -2,7 +2,7 @@
 
   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
 

	
 
@@ -27,13 +27,32 @@ class FBColumnDropper(ansisql.ANSIColumn
 
            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
 
        if column.unique or getattr(column, 'unique_name', None):
 

	
 
        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 cons.contains_column(column):
 
                    cons.drop()
 
            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
 

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

	
rhodecode/lib/dbmigrate/migrate/changeset/databases/sqlite.py
Show inline comments
 
@@ -80,10 +80,17 @@ class SQLiteColumnDropper(SQLiteHelper, 
 
    """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"""
rhodecode/lib/dbmigrate/migrate/changeset/schema.py
Show inline comments
 
@@ -29,9 +29,6 @@ __all__ = [
 
    'ColumnDelta',
 
]
 

	
 
DEFAULT_ALTER_METADATA = True
 

	
 

	
 
def create_column(column, table=None, *p, **kw):
 
    """Create a column, given the table.
 
    
 
@@ -109,19 +106,11 @@ def alter_column(*p, **k):
 
      The :class:`~sqlalchemy.engine.base.Engine` to use for table
 
      reflection and schema alterations.
 
    
 
    :param alter_metadata:
 
      If `True`, which is the default, the
 
      :class:`~sqlalchemy.schema.Column` will also modified.
 
      If `False`, the :class:`~sqlalchemy.schema.Column` will be left
 
      as it was.
 
    
 
    :returns: A :class:`ColumnDelta` instance representing the change.
 

	
 
    
 
    """
 

	
 
    k.setdefault('alter_metadata', DEFAULT_ALTER_METADATA)
 

	
 
    if 'table' not in k and isinstance(p[0], sqlalchemy.Column):
 
        k['table'] = p[0].table
 
    if 'engine' not in k:
 
@@ -135,6 +124,12 @@ def alter_column(*p, **k):
 
            MigrateDeprecationWarning
 
            )
 
    engine = k['engine']
 

	
 
    # enough tests seem to break when metadata is always altered
 
    # that this crutch has to be left in until they can be sorted
 
    # out
 
    k['alter_metadata']=True
 
    
 
    delta = ColumnDelta(*p, **k)
 

	
 
    visitorcallable = get_engine_visitor(engine, 'schemachanger')
 
@@ -188,11 +183,10 @@ class ColumnDelta(DictMixin, sqlalchemy.
 
        :param table: Table at which current Column should be bound to.\
 
        If table name is given, reflection will be used.
 
        :type table: string or Table instance
 
        :param alter_metadata: If True, it will apply changes to metadata.
 
        :type alter_metadata: bool
 
        :param metadata: If `alter_metadata` is true, \
 
        metadata is used to reflect table names into
 
        :type metadata: :class:`MetaData` instance
 
        
 
        :param metadata: A :class:`MetaData` instance to store
 
                         reflected table names
 
                         
 
        :param engine: When reflecting tables, either engine or metadata must \
 
        be specified to acquire engine object.
 
        :type engine: :class:`Engine` instance
 
@@ -213,7 +207,11 @@ class ColumnDelta(DictMixin, sqlalchemy.
 
    __visit_name__ = 'column'
 

	
 
    def __init__(self, *p, **kw):
 
        # 'alter_metadata' is not a public api. It exists purely
 
        # as a crutch until the tests that fail when 'alter_metadata'
 
        # behaviour always happens can be sorted out
 
        self.alter_metadata = kw.pop("alter_metadata", False)
 
        
 
        self.meta = kw.pop("metadata", None)
 
        self.engine = kw.pop("engine", None)
 

	
 
@@ -237,8 +235,10 @@ class ColumnDelta(DictMixin, sqlalchemy.
 
        self.apply_diffs(diffs)
 

	
 
    def __repr__(self):
 
        return '<ColumnDelta altermetadata=%r, %s>' % (self.alter_metadata,
 
            super(ColumnDelta, self).__repr__())
 
        return '<ColumnDelta altermetadata=%r, %s>' % (
 
            self.alter_metadata,
 
            super(ColumnDelta, self).__repr__()
 
            )
 

	
 
    def __getitem__(self, key):
 
        if key not in self.keys():
 
@@ -395,7 +395,6 @@ class ColumnDelta(DictMixin, sqlalchemy.
 
            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)
 

	
 
@@ -456,19 +455,15 @@ class ChangesetTable(object):
 

	
 
        :param name: New name of the table.
 
        :type name: string
 
        :param alter_metadata: If True, table will be removed from metadata
 
        :type alter_metadata: bool
 
        :param connection: reuse connection istead of creating new one.
 
        :type connection: :class:`sqlalchemy.engine.base.Connection` instance
 
        """
 
        self.alter_metadata = kwargs.pop('alter_metadata', DEFAULT_ALTER_METADATA)
 
        engine = self.bind
 
        self.new_name = name
 
        visitorcallable = get_engine_visitor(engine, 'schemachanger')
 
        run_single_visitor(engine, visitorcallable, self, connection, **kwargs)
 

	
 
        # Fix metadata registration
 
        if self.alter_metadata:
 
            self.name = name
 
            self.deregister()
 
            self._set_parent(self.metadata)
 
@@ -510,7 +505,6 @@ class ChangesetColumn(object):
 
`~migrate.changeset.constraint.UniqueConstraint` on this column.
 
        :param primary_key_name: Creates :class:\
 
`~migrate.changeset.constraint.PrimaryKeyConstraint` on this column.
 
        :param alter_metadata: If True, column will be added to table object.
 
        :param populate_default: If True, created column will be \
 
populated with defaults
 
        :param connection: reuse connection istead of creating new one.
 
@@ -518,21 +512,18 @@ populated with defaults
 
        :type index_name: string
 
        :type unique_name: string
 
        :type primary_key_name: string
 
        :type alter_metadata: bool
 
        :type populate_default: bool
 
        :type connection: :class:`sqlalchemy.engine.base.Connection` instance
 

	
 
        :returns: self
 
        """
 
        self.populate_default = populate_default
 
        self.alter_metadata = kwargs.pop('alter_metadata', DEFAULT_ALTER_METADATA)
 
        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)
 

	
 
        if self.alter_metadata:
 
            self.add_to_table(table)
 
        engine = self.table.bind
 
        visitorcallable = get_engine_visitor(engine, 'columngenerator')
 
@@ -550,20 +541,15 @@ populated with defaults
 

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

	
 
        :param alter_metadata: If True, column will be removed from table object.
 
        :type alter_metadata: bool
 
        :param connection: reuse connection istead of creating new one.
 
        :type connection: :class:`sqlalchemy.engine.base.Connection` instance
 
        """
 
        self.alter_metadata = kwargs.pop('alter_metadata', DEFAULT_ALTER_METADATA)
 
        if table is not None:
 
            self.table = table
 
        engine = self.table.bind
 
        if self.alter_metadata:
 
            self.remove_from_table(self.table, unset_table=False)
 
        visitorcallable = get_engine_visitor(engine, 'columndropper')
 
        engine._run_visitor(visitorcallable, self, connection, **kwargs)
 
        if self.alter_metadata:
 
        self.remove_from_table(self.table, unset_table=False)
 
            self.table = None
 
        return self
 

	
 
@@ -643,17 +629,13 @@ class ChangesetIndex(object):
 

	
 
        :param name: New name of the Index.
 
        :type name: string
 
        :param alter_metadata: If True, Index object will be altered.
 
        :type alter_metadata: bool
 
        :param connection: reuse connection istead of creating new one.
 
        :type connection: :class:`sqlalchemy.engine.base.Connection` instance
 
        """
 
        self.alter_metadata = kwargs.pop('alter_metadata', DEFAULT_ALTER_METADATA)
 
        engine = self.table.bind
 
        self.new_name = name
 
        visitorcallable = get_engine_visitor(engine, 'schemachanger')
 
        engine._run_visitor(visitorcallable, self, connection, **kwargs)
 
        if self.alter_metadata:
 
            self.name = name
 

	
 

	
rhodecode/lib/dbmigrate/migrate/versioning/genmodel.py
Show inline comments
 
@@ -169,11 +169,11 @@ class ModelGenerator(object):
 
                        modelTable, col.name))
 
            for modelCol, databaseCol, modelDecl, databaseDecl in diffDecl:
 
                upgradeCommands.append(
 
                    'assert False, "Can\'t alter columns: %s:%s=>%s"',
 
                    modelTable, modelCol.name, databaseCol.name)
 
                    'assert False, "Can\'t alter columns: %s:%s=>%s"' % (
 
                    modelTable, modelCol.name, databaseCol.name))
 
                downgradeCommands.append(
 
                    'assert False, "Can\'t alter columns: %s:%s=>%s"',
 
                    modelTable, modelCol.name, databaseCol.name)
 
                    'assert False, "Can\'t alter columns: %s:%s=>%s"' % (
 
                    modelTable, modelCol.name, databaseCol.name))
 
        pre_command = '    meta.bind = migrate_engine'
 

	
 
        return (
rhodecode/lib/dbmigrate/migrate/versioning/script/py.py
Show inline comments
 
@@ -4,6 +4,7 @@
 
import shutil
 
import warnings
 
import logging
 
import inspect
 
from StringIO import StringIO
 

	
 
from rhodecode.lib.dbmigrate import migrate
 
@@ -136,12 +137,12 @@ class PythonScript(base.BaseScript):
 
        funcname = base.operations[op]
 
        script_func = self._func(funcname)
 

	
 
        try:
 
        # check for old way of using engine
 
        if not inspect.getargspec(script_func)[0]:
 
            raise TypeError("upgrade/downgrade functions must accept engine"
 
                " parameter (since version 0.5.4)")
 

	
 
            script_func(engine)
 
        except TypeError:
 
            warnings.warn("upgrade/downgrade functions must accept engine"
 
                " parameter (since version > 0.5.4)", MigrateDeprecationWarning)
 
            raise
 

	
 
    @property
 
    def module(self):
rhodecode/lib/dbmigrate/migrate/versioning/script/sql.py
Show inline comments
 
@@ -18,6 +18,7 @@ class SqlScript(base.BaseScript):
 
        
 
        :returns: :class:`SqlScript instance <migrate.versioning.script.sql.SqlScript>`"""
 
        cls.require_notfound(path)
 

	
 
        src = Template(opts.pop('templates_path', None)).get_sql_script(theme=opts.pop('templates_theme', None))
 
        shutil.copy(src, path)
 
        return cls(path)
rhodecode/lib/dbmigrate/migrate/versioning/shell.py
Show inline comments
 
@@ -77,8 +77,7 @@ def main(argv=None, **kwargs):
 
        %s
 

	
 
    Enter "%%prog help COMMAND" for information on a particular command.
 
    """ % '\n\t'.join(["%s - %s" % (command.ljust(28),
 
                    api.command_desc.get(command)) for command in commands])
 
    """ % '\n\t'.join(["%s - %s" % (command.ljust(28), api.command_desc.get(command)) for command in commands])
 

	
 
    parser = PassiveOptionParser(usage=usage)
 
    parser.add_option("-d", "--debug",
0 comments (0 inline, 0 general)