本文整理汇总了Python中migrate.changeset.UniqueConstraint类的典型用法代码示例。如果您正苦于以下问题:Python UniqueConstraint类的具体用法?Python UniqueConstraint怎么用?Python UniqueConstraint使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了UniqueConstraint类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: drop_unique_constraint
def drop_unique_constraint(migrate_engine, table_name, uc_name, *columns,
**col_name_col_instance):
"""
This method drops UC from table and works for mysql, postgresql and sqlite.
In mysql and postgresql we are able to use "alter table" constuction. In
sqlite is only one way to drop UC:
1) Create new table with same columns, indexes and constraints
(except one that we want to drop).
2) Copy data from old table to new.
3) Drop old table.
4) Rename new table to the name of old table.
:param migrate_engine: sqlalchemy engine
:param table_name: name of table that contains uniq constarint.
:param uc_name: name of uniq constraint that will be dropped.
:param columns: columns that are in uniq constarint.
:param col_name_col_instance: contains pair column_name=column_instance.
column_instance is instance of Column. These params
are required only for columns that have unsupported
types by sqlite. For example BigInteger.
"""
if migrate_engine.name in ["mysql", "postgresql"]:
meta = MetaData()
meta.bind = migrate_engine
t = Table(table_name, meta, autoload=True)
uc = UniqueConstraint(*columns, table=t, name=uc_name)
uc.drop()
else:
_drop_unique_constraint_in_sqlite(migrate_engine, table_name, uc_name,
**col_name_col_instance)
示例2: upgrade
def upgrade(migrate_engine):
meta = MetaData(bind=migrate_engine)
t = Table('chassis', meta, autoload=True)
# NOTE: new name convention for UC
uc = UniqueConstraint('uuid', table=t, name='uniq_chassis0uuid')
uc.create()
示例3: drop_unique_constraint
def drop_unique_constraint(migrate_engine, table_name, uc_name, *columns,
**col_name_col_instance):
"""Drop unique constraint from table.
This method drops UC from table and works for mysql, postgresql and sqlite.
In mysql and postgresql we are able to use "alter table" construction.
Sqlalchemy doesn't support some sqlite column types and replaces their
type with NullType in metadata. We process these columns and replace
NullType with the correct column type.
:param migrate_engine: sqlalchemy engine
:param table_name: name of table that contains uniq constraint.
:param uc_name: name of uniq constraint that will be dropped.
:param columns: columns that are in uniq constraint.
:param col_name_col_instance: contains pair column_name=column_instance.
column_instance is instance of Column. These params
are required only for columns that have unsupported
types by sqlite. For example BigInteger.
"""
meta = MetaData()
meta.bind = migrate_engine
t = Table(table_name, meta, autoload=True)
if migrate_engine.name == "sqlite":
override_cols = [
_get_not_supported_column(col_name_col_instance, col.name)
for col in t.columns
if isinstance(col.type, NullType)
]
for col in override_cols:
t.columns.replace(col)
uc = UniqueConstraint(*columns, table=t, name=uc_name)
uc.drop()
示例4: downgrade
def downgrade(migrate_engine):
meta = MetaData(bind=migrate_engine)
table = Table(TABLE_NAME, meta, autoload=True)
utils.drop_unique_constraint(migrate_engine, TABLE_NAME, NEW_NAME,
*COLUMNS)
uc_old = UniqueConstraint(*COLUMNS, table=table, name=OLD_NAME)
uc_old.create()
示例5: upgrade
def upgrade(migrate_engine):
meta = MetaData()
meta.bind = migrate_engine
dialect = migrate_engine.url.get_dialect().name
aggregates = Table('aggregates', meta, autoload=True)
aggregate_metadata = Table('aggregate_metadata', meta, autoload=True)
record_list = list(aggregates.select().execute())
for rec in record_list:
row = aggregate_metadata.insert()
row.execute({'created_at': rec['created_at'],
'updated_at': rec['updated_at'],
'deleted_at': rec['deleted_at'],
'deleted': rec['deleted'],
'key': 'operational_state',
'value': rec['operational_state'],
'aggregate_id': rec['id'],
})
aggregates.drop_column('operational_state')
aggregate_hosts = Table('aggregate_hosts', meta, autoload=True)
if dialect.startswith('sqlite'):
aggregate_hosts.drop_column('host')
aggregate_hosts.create_column(Column('host', String(255)))
elif dialect.startswith('postgres'):
ucon = UniqueConstraint('host',
name='aggregate_hosts_host_key',
table=aggregate_hosts)
ucon.drop()
else:
col = aggregate_hosts.c.host
UniqueConstraint(col, name='host').drop()
示例6: upgrade
def upgrade(migrate_engine):
meta = MetaData()
meta.bind = migrate_engine
dialect = migrate_engine.url.get_dialect().name
aggregates = Table("aggregates", meta, autoload=True)
aggregate_metadata = Table("aggregate_metadata", meta, autoload=True)
record_list = list(aggregates.select().execute())
for rec in record_list:
row = aggregate_metadata.insert()
row.execute(
{
"created_at": rec["created_at"],
"updated_at": rec["updated_at"],
"deleted_at": rec["deleted_at"],
"deleted": rec["deleted"],
"key": "operational_state",
"value": rec["operational_state"],
"aggregate_id": rec["id"],
}
)
aggregates.drop_column("operational_state")
aggregate_hosts = Table("aggregate_hosts", meta, autoload=True)
if dialect.startswith("sqlite"):
aggregate_hosts.c.host.alter(unique=False)
elif dialect.startswith("postgres"):
ucon = UniqueConstraint("host", name="aggregate_hosts_host_key", table=aggregate_hosts)
ucon.drop()
else:
col = aggregate_hosts.c.host
UniqueConstraint(col, name="host").drop()
示例7: upgrade
def upgrade(migrate_engine):
meta = MetaData(bind=migrate_engine)
t = Table(TABLE_NAME, meta, autoload=True)
utils.drop_old_duplicate_entries_from_table(migrate_engine, TABLE_NAME,
True, *COLUMNS)
uc = UniqueConstraint(*COLUMNS, table=t, name=UC_NAME)
uc.create()
示例8: restore_unique_constraint
def restore_unique_constraint(table):
# NOTE(Vek): So, sqlite doesn't really support dropping columns,
# and so it gets implemented by dropping and recreating
# the table...which of course means we completely lose
# the unique constraint. We re-create it here to work
# around this issue.
uc_name = 'uniq_cell_name0deleted'
columns = ('name', 'deleted')
uc = UniqueConstraint(*columns, table=table, name=uc_name)
uc.create()
示例9: upgrade
def upgrade(migrate_engine):
utils.drop_unique_constraint(migrate_engine, TABLE_NAME, OLD_UC_NAME,
OLD_COLUMN)
meta = MetaData(bind=migrate_engine)
t = Table(TABLE_NAME, meta, autoload=True)
if migrate_engine.name == "mysql":
index = Index(OLD_COLUMN, t.c[OLD_COLUMN], unique=True)
index.drop()
uc = UniqueConstraint(*COLUMNS, table=t, name=UC_NAME)
uc.create()
示例10: downgrade
def downgrade(migrate_engine):
utils.drop_unique_constraint(migrate_engine, TABLE_NAME, UC_NAME, *COLUMNS)
meta = MetaData(bind=migrate_engine)
t = Table(TABLE_NAME, meta, autoload=True)
delete_statement = t.delete().where(t.c.deleted != 0)
migrate_engine.execute(delete_statement)
uc = UniqueConstraint(OLD_COLUMN, table=t, name=OLD_UC_NAME)
uc.create()
if migrate_engine.name == "mysql":
index = Index(OLD_COLUMN, t.c[OLD_COLUMN], unique=True)
index.create()
示例11: upgrade
def upgrade(migrate_engine):
meta = MetaData()
meta.bind = migrate_engine
datastore_versions = Table('datastore_versions', meta, autoload=True)
# drop the unique index on the name column - unless we are
# using sqlite - it doesn't support dropping unique constraints
uc = None
if migrate_engine.name == "mysql":
uc = UniqueConstraint('name', table=datastore_versions, name='name')
elif migrate_engine.name == "postgresql":
uc = UniqueConstraint('name', table=datastore_versions,
name='datastore_versions_name_key')
if uc:
try:
uc.drop()
except (OperationalError, InternalError) as e:
logger.info(e)
示例12: upgrade
def upgrade(migrate_engine):
meta = MetaData()
meta.bind = migrate_engine
dialect = migrate_engine.url.get_dialect().name
aggregates = Table('aggregates', meta, autoload=True)
if dialect.startswith('sqlite'):
aggregates.c.name.alter(unique=False)
elif dialect.startswith('postgres'):
ucon = UniqueConstraint('name',
name='aggregates_name_key',
table=aggregates)
ucon.drop()
else:
col2 = aggregates.c.name
UniqueConstraint(col2, name='name').drop()
示例13: drop_unique_constraint
def drop_unique_constraint(migrate_engine, table_name, uc_name, *columns,
**col_name_col_instance):
"""
Drop unique constraint from table.
"""
meta = MetaData()
meta.bind = migrate_engine
t = Table(table_name, meta, autoload=True)
if migrate_engine.name == "sqlite":
override_cols = [
_get_not_supported_column(col_name_col_instance, col.name)
for col in t.columns
if isinstance(col.type, NullType)
]
for col in override_cols:
t.columns.replace(col)
uc = UniqueConstraint(*columns, table=t, name=uc_name)
uc.drop()
示例14: upgrade
def upgrade(migrate_engine):
meta = MetaData(bind=migrate_engine)
key_pairs = Table(TABLE_NAME, meta, autoload=True)
utils.drop_old_duplicate_entries_from_table(migrate_engine,
TABLE_NAME, True,
*UC_COLUMNS)
old_idx = None
#Drop old index because the new UniqueConstraint can be used instead.
for index in key_pairs.indexes:
if index.name == OLD_IDX_NAME:
index.drop()
old_idx = index
#index.drop() in SQLAlchemy-migrate will issue a DROP INDEX statement to
#the DB but WILL NOT update the table metadata to remove the `Index`
#object. This can cause subsequent calls like drop or create constraint
#on that table to fail.The solution is to update the table metadata to
#reflect the now dropped column.
if old_idx:
key_pairs.indexes.remove(old_idx)
uc = UniqueConstraint(*(UC_COLUMNS), table=key_pairs, name=UC_NAME)
uc.create()
示例15: upgrade
def upgrade(migrate_engine):
meta = MetaData(bind=migrate_engine)
conductor = Table(
"conductors",
meta,
Column("id", Integer, primary_key=True, nullable=False),
Column("hostname", String(length=255), nullable=False),
Column("drivers", Text),
Column("created_at", DateTime),
Column("updated_at", DateTime),
mysql_engine=ENGINE,
mysql_charset=CHARSET,
)
try:
conductor.create()
except Exception:
LOG.info(repr(conductor))
LOG.exception(_("Exception while creating table."))
raise
uc = UniqueConstraint("hostname", table=conductor, name="uniq_conductors0hostname")
uc.create()