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
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)
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
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
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
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
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
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
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
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
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
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
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 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)
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
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
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 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)
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 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
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 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 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
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)
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
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)
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)
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)
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)
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)