本文整理汇总了Python中migrate.changeset.UniqueConstraint类的典型用法代码示例。如果您正苦于以下问题:Python UniqueConstraint类的具体用法?Python UniqueConstraint怎么用?Python UniqueConstraint使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了UniqueConstraint类的16个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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)
开发者ID:faye89,项目名称:nova,代码行数:30,代码来源:utils.py
示例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()
开发者ID:Haomeng,项目名称:ironic,代码行数:7,代码来源:011_add_chassis_uc.py
示例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()
开发者ID:n-nishida,项目名称:rack,代码行数:35,代码来源:utils.py
示例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()
开发者ID:674009287,项目名称:nova,代码行数:7,代码来源:207_fix_uniqname_cells.py
示例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()
开发者ID:aantonov,项目名称:nova,代码行数:33,代码来源:111_general_aggregates.py
示例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()
开发者ID:nitishb,项目名称:nova,代码行数:33,代码来源:111_general_aggregates.py
示例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()
开发者ID:674009287,项目名称:nova,代码行数:8,代码来源:177_add_floating_ip_uc.py
示例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()
开发者ID:674009287,项目名称:nova,代码行数:10,代码来源:200_add_transport_url_to_cell.py
示例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()
开发者ID:674009287,项目名称:nova,代码行数:10,代码来源:192_change_virtual_interface_uc.py
示例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()
开发者ID:674009287,项目名称:nova,代码行数:11,代码来源:192_change_virtual_interface_uc.py
示例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)
开发者ID:Hopebaytech,项目名称:trove,代码行数:18,代码来源:026_datastore_versions_unique_fix.py
示例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()
开发者ID:Cerberus98,项目名称:nova,代码行数:18,代码来源:133_aggregate_delete_fix.py
示例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()
开发者ID:liuliuxiaoge,项目名称:XDRS,代码行数:21,代码来源:utils.py
示例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()
开发者ID:AnyBucket,项目名称:nova,代码行数:22,代码来源:173_add_unique_constraint_to_key_pairs.py
示例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()
开发者ID:home-dog,项目名称:ironic,代码行数:24,代码来源:012_add_conductor_table.py
示例16: upgrade
def upgrade(migrate_engine):
meta = MetaData(bind=migrate_engine)
domain_quota = Table('domain_quotas', meta,
Column('id', Integer, primary_key=True, nullable=False),
Column('created_at', DateTime),
Column('updated_at', DateTime),
Column('deleted_at', DateTime),
Column('deleted', Integer),
Column('domain_id', String(255)),
Column('resource', String(255), nullable=False),
Column('hard_limit', Integer()),
mysql_engine='InnoDB',
mysql_charset='utf8')
domain_quota_usage = Table('domain_quota_usages', meta,
Column('id', Integer, primary_key=True, nullable=False),
Column('created_at', DateTime),
Column('updated_at', DateTime),
Column('deleted_at', DateTime),
Column('deleted', Integer),
Column('domain_id', String(255)),
Column('resource', String(255), nullable=False),
Column('in_use', Integer, nullable=False),
Column('reserved', Integer, nullable=False),
Column('until_refresh', Integer),
mysql_engine='InnoDB',
mysql_charset='utf8')
domain_reservation = Table('domain_reservations', meta,
Column('id', Integer, primary_key=True, nullable=False),
Column('created_at', DateTime),
Column('updated_at', DateTime),
Column('deleted_at', DateTime),
Column('deleted', Integer),
Column('uuid', String(length=36), nullable=False),
Column('domain_id', String(255)),
Column('usage_id', Integer, nullable=False),
Column('resource', String(length=255)),
Column('delta', Integer, nullable=False),
Column('expire', DateTime),
mysql_engine='InnoDB',
mysql_charset='utf8'
)
tables = [domain_quota, domain_quota_usage, domain_reservation]
for table in tables:
try:
table.create()
utils.create_shadow_table(migrate_engine, table=table)
except Exception:
LOG.info(repr(table))
LOG.exception(_('Exception while creating table.'))
raise
indexes = [
Index('domain_quotas_domain_id_deleted_idx',
'domain_id', 'deleted'),
#DomainQuotaUsages
Index('ix_domain_quota_usages_domain_id', 'domain_id'),
#DomainReservation
Index('ix_domain_reservations_id', 'domain_id'),
Index('domain_reservations_uuid_idx', 'uuid'),
]
# Common indexes
# for index in indexes:
# print "<<<<<<<<<<<<<<<INDEXES>>>>>>>>>>>>>>>>>>>"
# print index
# index.create(migrate_engine)
fkeys = [
[[domain_reservation.c.usage_id],
[domain_quota_usage.c.id],
'domain_reservations_ibfk_1']
]
for fkey_pair in fkeys:
if migrate_engine.name == 'mysql':
# For MySQL we name our fkeys explicitly so they match Folsom
fkey = ForeignKeyConstraint(columns=fkey_pair[0],
refcolumns=fkey_pair[1],
name=fkey_pair[2])
fkey.create()
elif migrate_engine.name == 'postgresql':
# PostgreSQL names things like it wants (correct and compatible!)
fkey = ForeignKeyConstraint(columns=fkey_pair[0],
refcolumns=fkey_pair[1])
fkey.create()
uniq_name = "uniq_domain_quotas0domain_id0resource0deleted"
uc_domain_quota = UniqueConstraint("domain_id", "resource", "deleted",
table=domain_quota, name=uniq_name)
uc_domain_quota.create()
开发者ID:carriercomm,项目名称:DomainQuotaAPIs,代码行数:97,代码来源:230_create_domain_quotas_tables.py
注:本文中的migrate.changeset.UniqueConstraint类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论