Пример #1
0
def alter_column(*p, **k):
    """Alter a column.

    Direct API to :class:`ColumnDelta`.

    :param table: Table or table name (will issue reflection).
    :param engine: Will be used for reflection.
    :param alter_metadata: Defaults to True. It will alter changes also to objects.
    :returns: :class:`Columndelta` instance
    """
    
    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:
        k['engine'] = k['table'].bind

    # deprecation
    if len(p) >= 2 and isinstance(p[1], sqlalchemy.Column):
        warnings.warn("Alter column with comparing columns is deprecated."
            " Just pass in parameters instead.", MigrateDeprecationWarning)
    engine = k['engine']
    delta = ColumnDelta(*p, **k)

    visitorcallable = get_engine_visitor(engine, 'schemachanger')
    engine._run_visitor(visitorcallable, delta)

    return delta
 def drop(self):
     from migrate.changeset.databases.visitor import get_engine_visitor
     visitorcallable = get_engine_visitor(self.table.bind,
                                          'constraintdropper')
     _engine_run_visitor(self.table.bind, visitorcallable, self)
     self.columns.clear()
     return self
Пример #3
0
def alter_column(*p,**k):
    """Alter a column
    Parameters: column name, table name, an engine, and the
    properties of that column to change
    """
    if len(p) and isinstance(p[0],sqlalchemy.Column):
        col = p[0]
    else:
        col = None
    if 'table' not in k:
        k['table'] = col.table
    if 'engine' not in k:
        k['engine'] = k['table'].bind
    engine = k['engine']
    delta = _ColumnDelta(*p,**k)
    visitorcallable = get_engine_visitor(engine,'schemachanger')
    _engine_run_visitor(engine,visitorcallable,delta)

    # Update column
    if col is not None:
        # Special case: change column key on rename, if key not explicit
        # Used by SA : table.c.[key]
        #
        # This fails if the key was explit AND equal to the column name. 
        # (It changes the key name when it shouldn't.) 
        # Not much we can do about it.
        if 'name' in delta.keys():
            if (col.name == col.key):
                newname = delta['name']
                del col.table.c[col.key]
                setattr(col,'key',newname)
                col.table.c[col.key] = col
        # Change all other attrs
        for key,val in delta.iteritems():
            setattr(col,key,val)
Пример #4
0
    def create(self, table=None, *args, **kwargs):
        """Create this column in the database.

        Assumes the given table exists. ``ALTER TABLE ADD COLUMN``,
        for most databases.
        """
        table = _normalize_table(self, table)
        engine = table.bind
        visitorcallable = get_engine_visitor(engine, 'columngenerator')
        engine._run_visitor(visitorcallable, self, *args, **kwargs)

        #add in foreign keys
        if self.foreign_keys:
            for fk in self.foreign_keys:
                visitorcallable = get_engine_visitor(engine,
                                                     'columnfkgenerator')
                engine._run_visitor(visitorcallable, self, fk=fk)
        return self
Пример #5
0
    def create(self, table=None, *args, **kwargs):
        """Create this column in the database.

        Assumes the given table exists. ``ALTER TABLE ADD COLUMN``,
        for most databases.
        """
        table = _normalize_table(self, table)
        engine = table.bind
        visitorcallable = get_engine_visitor(engine, 'columngenerator')
        engine._run_visitor(visitorcallable, self, *args, **kwargs)

        #add in foreign keys
        if self.foreign_keys:
            for fk in self.foreign_keys:
                visitorcallable = get_engine_visitor(engine,
                                                     'columnfkgenerator')
                engine._run_visitor(visitorcallable, self, fk=fk)
        return self
Пример #6
0
 def create(self,table=None,*args,**kwargs):
     """Create this column in the database. Assumes the given table exists.
     ALTER TABLE ADD COLUMN, for most databases. 
     """
     table = _normalize_table(self,table)
     engine = table.bind
     visitorcallable = get_engine_visitor(engine,'columngenerator')
     engine._run_visitor(visitorcallable,self,*args,**kwargs)
     return self
Пример #7
0
 def rename(self,name,*args,**kwargs):
     """Change the name of an index.
     This changes both the Python object name and the database name.
     """
     engine = self.table.bind
     visitorcallable = get_engine_visitor(engine,'schemachanger')
     param = _WrapRename(self,name)
     #engine._run_visitor(visitorcallable,param,*args,**kwargs)
     _engine_run_visitor(engine,visitorcallable,param,*args,**kwargs)
     self.name = name
Пример #8
0
    def rename(self, name, *args, **kwargs):
        """Change the name of an index.

        This changes both the Python object name and the database
        name.
        """
        engine = self.table.bind
        visitorcallable = get_engine_visitor(engine, 'schemachanger')
        param = _WrapRename(self, name)
        _engine_run_visitor(engine, visitorcallable, param, *args, **kwargs)
        self.name = name
Пример #9
0
    def drop(self, table=None, *args, **kwargs):
        """Drop this column from the database, leaving its table intact.

        ``ALTER TABLE DROP COLUMN``, for most databases.
        """
        table = _normalize_table(self, table)
        engine = table.bind
        visitorcallable = get_engine_visitor(engine, 'columndropper')
        engine._run_visitor(lambda dialect, conn: visitorcallable(conn), self,
                            *args, **kwargs)
        return self
Пример #10
0
    def drop(self, table=None, *args, **kwargs):
        """Drop this column from the database, leaving its table intact.

        ``ALTER TABLE DROP COLUMN``, for most databases.
        """
        table = _normalize_table(self, table)
        engine = table.bind
        visitorcallable = get_engine_visitor(engine, 'columndropper')
        engine._run_visitor(lambda dialect, conn: visitorcallable(conn),
                            self, *args, **kwargs)
        return self
Пример #11
0
    def rename(self, name, connection=None, **kwargs):
        """Change the name of an index.

        :param name: New name of the Index.
        :type name: string
        :param connection: reuse connection istead of creating new one.
        :type connection: :class:`sqlalchemy.engine.base.Connection` instance
        """
        engine = self.table.bind
        self.new_name = name
        visitorcallable = get_engine_visitor(engine, 'schemachanger')
        engine._run_visitor(visitorcallable, self, connection, **kwargs)
        self.name = name
Пример #12
0
    def rename(self, name, connection=None, **kwargs):
        """Change the name of an index.

        :param name: New name of the Index.
        :type name: string
        :param connection: reuse connection istead of creating new one.
        :type connection: :class:`sqlalchemy.engine.base.Connection` instance
        """
        engine = self.table.bind
        self.new_name = name
        visitorcallable = get_engine_visitor(engine, 'schemachanger')
        engine._run_visitor(visitorcallable, self, connection, **kwargs)
        self.name = name
Пример #13
0
 def drop(self,table=None,*args,**kwargs):
     """Drop this column from the database, leaving its table intact.
     ALTER TABLE DROP COLUMN, for most databases. 
     """
     table = _normalize_table(self,table)
     engine = table.bind
     visitorcallable = get_engine_visitor(engine,'columndropper')
     #engine._run_visitor(visitorcallable,self,*args,**kwargs)
     engine._run_visitor(lambda dialect, conn: visitorcallable(conn), self, *args, **kwargs)
     ## Remove col from table object, too
     #del table._columns[self.key]
     #if self in table.primary_key:
     #    table.primary_key.remove(self)
     return self
Пример #14
0
    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
Пример #15
0
    def rename(self,name,*args,**kwargs):
        """Rename this table
        This changes both the database name and the name of this Python object
        """
        engine = self.bind
        visitorcallable = get_engine_visitor(engine,'schemachanger')
        param = _WrapRename(self,name)
        #engine._run_visitor(visitorcallable,param,*args,**kwargs)
        _engine_run_visitor(engine,visitorcallable,param,*args,**kwargs)

        # Fix metadata registration
        meta = self.metadata
        self.deregister()
        self.name = name
        self._set_parent(meta)
Пример #16
0
def alter_column(*p, **k):
    """Alter a column.

    This is a helper function that creates a :class:`ColumnDelta` and
    runs it.

    :argument column:
      The name of the column to be altered or a
      :class:`ChangesetColumn` column representing it.

    :param table:
      A :class:`~sqlalchemy.schema.Table` or table name to
      for the table where the column will be changed.

    :param engine:
      The :class:`~sqlalchemy.engine.base.Engine` to use for table
      reflection and schema alterations.

    :returns: A :class:`ColumnDelta` instance representing the change.


    """

    if 'table' not in k and isinstance(p[0], sqlalchemy.Column):
        k['table'] = p[0].table
    if 'engine' not in k:
        k['engine'] = k['table'].bind

    # deprecation
    if len(p) >= 2 and isinstance(p[1], sqlalchemy.Column):
        warnings.warn(
            "Passing a Column object to alter_column is deprecated."
            " Just pass in keyword parameters instead.",
            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')
    engine._run_visitor(visitorcallable, delta)

    return delta
Пример #17
0
def alter_column(*p, **k):
    """Alter a column.

    This is a helper function that creates a :class:`ColumnDelta` and
    runs it.

    :argument column:
      The name of the column to be altered or a
      :class:`ChangesetColumn` column representing it.

    :param table:
      A :class:`~sqlalchemy.schema.Table` or table name to
      for the table where the column will be changed.

    :param engine:
      The :class:`~sqlalchemy.engine.base.Engine` to use for table
      reflection and schema alterations.

    :returns: A :class:`ColumnDelta` instance representing the change.


    """

    if 'table' not in k and isinstance(p[0], sqlalchemy.Column):
        k['table'] = p[0].table
    if 'engine' not in k:
        k['engine'] = k['table'].bind

    # deprecation
    if len(p) >= 2 and isinstance(p[1], sqlalchemy.Column):
        warnings.warn(
            "Passing a Column object to alter_column is deprecated."
            " Just pass in keyword parameters instead.",
            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')
    engine._run_visitor(visitorcallable, delta)

    return delta
Пример #18
0
    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
Пример #19
0
    def rename(self, name, *args, **kwargs):
        """Rename this table.

        This changes both the database name and the name of this
        Python object
        """
        engine = self.bind
        visitorcallable = get_engine_visitor(engine, 'schemachanger')
        param = _WrapRename(self, name)
        _engine_run_visitor(engine, visitorcallable, param, *args, **kwargs)

        # Fix metadata registration
        meta = self.metadata
        self.deregister()
        self.name = name
        self._set_parent(meta)
Пример #20
0
    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
Пример #21
0
    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 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.
        :type table: Table instance
        :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')
        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
Пример #22
0
    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)
Пример #23
0
    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)
Пример #24
0
    def rename(self, name, connection=None, **kwargs):
        """Change the name of an index.

        :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
Пример #25
0
    def rename(self, name, connection=None, **kwargs):
        """Rename this table.

        :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)
Пример #26
0
    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 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.table = None
        return self
Пример #27
0
def alter_column(*p, **k):
    """Alter a column.

    Parameters: column name, table name, an engine, and the properties
    of that column to change
    """
    if len(p) and isinstance(p[0], sqlalchemy.Column):
        col = p[0]
    else:
        col = None
    if 'table' not in k:
        k['table'] = col.table
    if 'engine' not in k:
        k['engine'] = k['table'].bind
    engine = k['engine']
    delta = _ColumnDelta(*p, **k)
    visitorcallable = get_engine_visitor(engine, 'schemachanger')
    _engine_run_visitor(engine, visitorcallable, delta)

    # Update column
    if col is not None:
        # Special case: change column key on rename, if key not
        # explicit
        #
        # Used by SA : table.c.[key]
        #
        # This fails if the key was explit AND equal to the column
        # name.  (It changes the key name when it shouldn't.)
        #
        # Not much we can do about it.
        if 'name' in delta.keys():
            if (col.name == col.key):
                newname = delta['name']
                del col.table.c[col.key]
                setattr(col, 'key', newname)
                col.table.c[col.key] = col
        # Change all other attrs
        for key, val in delta.iteritems():
            setattr(col, key, val)
Пример #28
0
 def __do_imports(self, visitor_name, *a, **kw):
     engine = kw.pop('engine', self.table.bind)
     from 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)
Пример #29
0
 def create(self):
     from migrate.changeset.databases.visitor import get_engine_visitor
     visitorcallable = get_engine_visitor(self.table.bind,
                                          'constraintgenerator')
     _engine_run_visitor(self.table.bind, visitorcallable, self)
Пример #30
0
 def create(self, *args, **kwargs):
     from migrate.changeset.databases.visitor import get_engine_visitor
     visitorcallable = get_engine_visitor(self.table.bind,'constraintgenerator')
     _engine_run_visitor (self.table.bind, visitorcallable, self)
Пример #31
0
 def __do_imports(self, visitor_name, *a, **kw):
     engine = kw.pop('engine', self.table.bind)
     from 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)