コード例 #1
0
ファイル: utils.py プロジェクト: yanyuge/rack
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()
コード例 #2
0
ファイル: utils.py プロジェクト: faye89/nova
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)
コード例 #3
0
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()
コード例 #4
0
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" construction. 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 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.
    """
    if migrate_engine.name == "sqlite":
        _drop_unique_constraint_in_sqlite(migrate_engine, table_name, uc_name,
                                          **col_name_col_instance)
    else:
        meta = MetaData()
        meta.bind = migrate_engine
        t = Table(table_name, meta, autoload=True)
        uc = UniqueConstraint(*columns, table=t, name=uc_name)
        uc.drop()
コード例 #5
0
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
0
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
0
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)
コード例 #8
0
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()
コード例 #9
0
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 as e:
            logger.info(e)
コード例 #10
0
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()
コード例 #11
0
ファイル: utils.py プロジェクト: liuliuxiaoge/XDRS
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()