def downgrade():
    '''
    Downgrade the database to the previous revision
    '''

    schema = context.get_context().version_table_schema + '.' if context.get_context().version_table_schema else ''

    if context.get_context().dialect.name == 'oracle':
        try_drop_constraint('REPLICAS_STATE_CHK', 'replicas')
        create_check_constraint(constraint_name='REPLICAS_STATE_CHK', table_name='replicas',
                                condition="state in ('A', 'U', 'C', 'B', 'D', 'S', 'T')")
        try_drop_constraint('COLLECTION_REPLICAS_STATE_CHK', 'collection_replicas')
        create_check_constraint(constraint_name='COLLECTION_REPLICAS_STATE_CHK', table_name='collection_replicas',
                                condition="state in ('A', 'U', 'C', 'B', 'D', 'S', 'T')")

    elif context.get_context().dialect.name == 'postgresql':
        op.execute('ALTER TABLE ' + schema + 'replicas DROP CONSTRAINT IF EXISTS "REPLICAS_STATE_CHK", ALTER COLUMN state TYPE CHAR')
        op.execute('DROP TYPE "REPLICAS_STATE_CHK"')
        op.execute("CREATE TYPE \"REPLICAS_STATE_CHK\" AS ENUM('A', 'U', 'C', 'B', 'D', 'S', 'T')")
        op.execute("ALTER TABLE %sreplicas ALTER COLUMN state TYPE \"REPLICAS_STATE_CHK\" USING state::\"REPLICAS_STATE_CHK\"" % schema)

        op.execute('ALTER TABLE ' + schema + 'collection_replicas DROP CONSTRAINT IF EXISTS "COLLECTION_REPLICAS_STATE_CHK", ALTER COLUMN state TYPE CHAR')
        op.execute('DROP TYPE "COLLECTION_REPLICAS_STATE_CHK"')
        op.execute("CREATE TYPE \"COLLECTION_REPLICAS_STATE_CHK\" AS ENUM('A', 'U', 'C', 'B', 'D', 'S', 'T')")
        op.execute("ALTER TABLE %scollection_replicas ALTER COLUMN state TYPE \"COLLECTION_REPLICAS_STATE_CHK\" USING state::\"COLLECTION_REPLICAS_STATE_CHK\"" % schema)

    elif context.get_context().dialect.name == 'mysql':
        create_check_constraint(constraint_name='REPLICAS_STATE_CHK', table_name='replicas',
                                condition="state in ('A', 'U', 'C', 'B', 'D', 'S', 'T')")
        create_check_constraint(constraint_name='COLLECTION_REPLICAS_STATE_CHK', table_name='collection_replicas',
                                condition="state in ('A', 'U', 'C', 'B', 'D', 'S', 'T')")
def downgrade():
    '''
    Downgrade the database to the previous revision
    '''

    schema = context.get_context(
    ).version_table_schema + '.' if context.get_context(
    ).version_table_schema else ''

    if context.get_context().dialect.name == 'oracle':
        try_drop_constraint('RSE_STAGING_AREA_CHK', 'rses')
        try_drop_constraint('REQUESTS_TYPE_CHK', 'requests')
        create_check_constraint(constraint_name='REQUESTS_TYPE_CHK',
                                table_name='requests',
                                condition="request_type in ('U', 'D', 'T')")
        drop_column('rses', 'staging_area')

    elif context.get_context().dialect.name == 'postgresql':
        op.execute(
            'ALTER TABLE ' + schema +
            'requests DROP CONSTRAINT IF EXISTS "REQUESTS_TYPE_CHK", ALTER COLUMN request_type TYPE CHAR'
        )  # pylint: disable=no-member
        create_check_constraint(constraint_name='REQUESTS_TYPE_CHK',
                                table_name='requests',
                                condition="request_type in ('U', 'D', 'T')")
        drop_column('rses', 'staging_area', schema=schema[:-1])

    elif context.get_context().dialect.name == 'mysql':
        create_check_constraint(constraint_name='REQUESTS_TYPE_CHK',
                                table_name='requests',
                                condition="request_type in ('U', 'D', 'T')")
        drop_column('rses', 'staging_area', schema=schema[:-1])
Exemple #3
0
def downgrade():
    '''
    Downgrade the database to the previous revision
    '''

    schema = context.get_context(
    ).version_table_schema + '.' if context.get_context(
    ).version_table_schema else ''

    if context.get_context().dialect.name == 'oracle':
        drop_column('rules', 'ignore_account_limit')
        try_drop_constraint('RULES_STATE_CHK', 'rules')
        create_check_constraint('RULES_STATE_CHK', 'rules',
                                "state IN ('S', 'R', 'U', 'O')")

    elif context.get_context().dialect.name == 'postgresql':
        drop_column('rules', 'ignore_account_limit', schema=schema[:-1])
        op.execute(
            'ALTER TABLE ' + schema +
            'rules DROP CONSTRAINT IF EXISTS "RULES_STATE_CHK", ALTER COLUMN state TYPE CHAR'
        )
        op.execute("DROP TYPE \"RULES_STATE_CHK\"")
        op.execute(
            "CREATE TYPE \"RULES_STATE_CHK\" AS ENUM('S', 'R', 'U', 'O')")
        op.execute(
            "ALTER TABLE %srules ALTER COLUMN state TYPE \"RULES_STATE_CHK\" USING state::\"RULES_STATE_CHK\""
            % schema)

    elif context.get_context().dialect.name == 'mysql':
        drop_column('rules', 'ignore_account_limit', schema=schema[:-1])
        create_check_constraint('RULES_STATE_CHK', 'rules',
                                "state IN ('S', 'R', 'U', 'O')")
Exemple #4
0
def downgrade():
    '''
    Downgrade the database to the previous revision
    '''

    schema = context.get_context(
    ).version_table_schema + '.' if context.get_context(
    ).version_table_schema else ''

    if context.get_context().dialect.name == 'oracle':
        try_drop_constraint('RULES_NOTIFICATION_CHK', 'rules')
        create_check_constraint(constraint_name='RULES_NOTIFICATION_CHK',
                                table_name='rules',
                                condition="notification in ('Y', 'N', 'C')")

    elif context.get_context().dialect.name == 'postgresql':
        op.execute(
            'ALTER TABLE ' + schema +
            'rules DROP CONSTRAINT IF EXISTS "RULES_NOTIFICATION_CHK", ALTER COLUMN notification TYPE CHAR'
        )
        op.execute('DROP TYPE "RULES_NOTIFICATION_CHK"')
        op.execute(
            "CREATE TYPE \"RULES_NOTIFICATION_CHK\" AS ENUM('Y', 'N', 'C')")
        op.execute(
            "ALTER TABLE %srules ALTER COLUMN notification TYPE \"RULES_NOTIFICATION_CHK\" USING notification::\"RULES_NOTIFICATION_CHK\""
            % schema)

    elif context.get_context().dialect.name == 'mysql':
        create_check_constraint(constraint_name='RULES_NOTIFICATION_CHK',
                                table_name='rules',
                                condition="notification in ('Y', 'N', 'C')")
Exemple #5
0
def upgrade():
    '''
    Upgrade the database to this revision
    '''

    schema = context.get_context().version_table_schema + '.' if context.get_context().version_table_schema else ''
    if context.get_context().dialect.name in ['oracle', 'postgresql']:
        try_drop_constraint('IDENTITIES_TYPE_CHK', 'identities')
        create_check_constraint(constraint_name='IDENTITIES_TYPE_CHK',
                                table_name='identities',
                                condition="identity_type in ('X509', 'GSS', 'USERPASS', 'SSH', 'SAML')")
        try_drop_constraint('ACCOUNT_MAP_ID_TYPE_CHK', 'account_map')
        create_check_constraint(constraint_name='ACCOUNT_MAP_ID_TYPE_CHK',
                                table_name='account_map',
                                condition="identity_type in ('X509', 'GSS', 'USERPASS', 'SSH', 'SAML')")

    elif context.get_context().dialect.name == 'mysql':
        execute('ALTER TABLE ' + schema + 'identities DROP CHECK IDENTITIES_TYPE_CHK')  # pylint: disable=no-member
        create_check_constraint(constraint_name='IDENTITIES_TYPE_CHK',
                                table_name='identities',
                                condition="identity_type in ('X509', 'GSS', 'USERPASS', 'SSH', 'SAML')")
        execute('ALTER TABLE ' + schema + 'account_map DROP CHECK ACCOUNT_MAP_ID_TYPE_CHK')  # pylint: disable=no-member
        create_check_constraint(constraint_name='ACCOUNT_MAP_ID_TYPE_CHK',
                                table_name='account_map',
                                condition="identity_type in ('X509', 'GSS', 'USERPASS', 'SSH', 'SAML')")
Exemple #6
0
def upgrade():
    '''
    Upgrade the database to this revision
    '''

    schema = context.get_context(
    ).version_table_schema + '.' if context.get_context(
    ).version_table_schema else ''

    if context.get_context().dialect.name in ['oracle', 'postgresql']:
        try_drop_constraint('REQUESTS_STATE_CHK', 'requests')
        create_check_constraint(
            constraint_name='REQUESTS_STATE_CHK',
            table_name='requests',
            condition=
            "state in ('Q', 'G', 'S', 'D', 'F', 'L', 'N', 'O', 'A', 'U', 'W', 'M')"
        )

    elif context.get_context().dialect.name == 'mysql':
        op.execute('ALTER TABLE ' + schema +
                   'requests DROP CHECK REQUESTS_STATE_CHK')  # pylint: disable=no-member
        create_check_constraint(
            constraint_name='REQUESTS_STATE_CHK',
            table_name='requests',
            condition=
            "state in ('Q', 'G', 'S', 'D', 'F', 'L', 'N', 'O', 'A', 'U', 'W', 'M')"
        )
Exemple #7
0
def downgrade():
    '''
    Downgrade the database to the previous revision
    '''

    schema = context.get_context(
    ).version_table_schema + '.' if context.get_context(
    ).version_table_schema else ''

    if context.get_context().dialect.name == 'oracle':
        try_drop_constraint('REQUESTS_STATE_CHK', 'requests')
        create_check_constraint(
            constraint_name='REQUESTS_STATE_CHK',
            table_name='requests',
            condition=
            "state in ('Q', 'G', 'S', 'D', 'F', 'L', 'N', 'O', 'A', 'U', 'W')")

    elif context.get_context().dialect.name == 'postgresql':
        op.execute(
            'ALTER TABLE ' + schema +
            'requests DROP CONSTRAINT IF EXISTS "REQUESTS_STATE_CHK", ALTER COLUMN state TYPE CHAR'
        )  # pylint: disable=no-member
        create_check_constraint(
            constraint_name='REQUESTS_STATE_CHK',
            table_name='requests',
            condition=
            "state in ('Q', 'G', 'S', 'D', 'F', 'L', 'N', 'O', 'A', 'U', 'W')")

    elif context.get_context().dialect.name == 'mysql':
        create_check_constraint(
            constraint_name='REQUESTS_STATE_CHK',
            table_name='requests',
            condition=
            "state in ('Q', 'G', 'S', 'D', 'F', 'L', 'N', 'O', 'A', 'U', 'W')")
Exemple #8
0
def downgrade():

    '''
    Downgrade the database to the previous revision
    '''

    if context.get_context().dialect.name in ['oracle', 'postgresql']:
        try_drop_constraint('BAD_REPLICAS_STATE_CHK', 'bad_replicas')
Exemple #9
0
def downgrade():
    '''
    Downgrade the database to the previous revision
    '''

    schema = context.get_context(
    ).version_table_schema + '.' if context.get_context(
    ).version_table_schema else ''

    if context.get_context().dialect.name == 'oracle':
        drop_table('bad_pfns')
        drop_index('BAD_REPLICAS_EXPIRES_AT_IDX', 'bad_replicas')

        try_drop_constraint('BAD_REPLICAS_STATE_CHK', 'bad_replicas')
        create_check_constraint(constraint_name='BAD_REPLICAS_STATE_CHK',
                                table_name='bad_replicas',
                                condition="state in ('B', 'D', 'L', 'R', 'S')")

        drop_column('bad_replicas', 'expires_at')
        drop_constraint('BAD_REPLICAS_STATE_PK',
                        'bad_replicas',
                        type_='primary')
        create_primary_key('BAD_REPLICAS_STATE_PK', 'bad_replicas',
                           ['scope', 'name', 'rse_id', 'created_at'])

    elif context.get_context().dialect.name == 'postgresql':
        drop_table('bad_pfns')
        drop_index('BAD_REPLICAS_EXPIRES_AT_IDX', 'bad_replicas')

        op.execute(
            'ALTER TABLE ' + schema +
            'bad_replicas DROP CONSTRAINT IF EXISTS "BAD_REPLICAS_STATE_CHK", ALTER COLUMN state TYPE CHAR'
        )  # pylint: disable=no-member
        create_check_constraint(constraint_name='BAD_REPLICAS_STATE_CHK',
                                table_name='bad_replicas',
                                condition="state in ('B', 'D', 'L', 'R', 'S')")

        drop_column('bad_replicas', 'expires_at', schema=schema[:-1])
        drop_constraint('BAD_REPLICAS_STATE_PK',
                        'bad_replicas',
                        type_='primary')
        create_primary_key('BAD_REPLICAS_STATE_PK', 'bad_replicas',
                           ['scope', 'name', 'rse_id', 'created_at'])

    elif context.get_context().dialect.name == 'mysql':
        drop_table('bad_pfns')
        drop_index('BAD_REPLICAS_EXPIRES_AT_IDX', 'bad_replicas')

        create_check_constraint(constraint_name='BAD_REPLICAS_STATE_CHK',
                                table_name='bad_replicas',
                                condition="state in ('B', 'D', 'L', 'R', 'S')")

        drop_column('bad_replicas', 'expires_at', schema=schema[:-1])
        drop_constraint('BAD_REPLICAS_STATE_PK',
                        'bad_replicas',
                        type_='primary')
        create_primary_key('BAD_REPLICAS_STATE_PK', 'bad_replicas',
                           ['scope', 'name', 'rse_id', 'created_at'])
Exemple #10
0
def upgrade():
    """
    Upgrade the database to this revision
    """

    new_enum_values = [
        'Q', 'G', 'S', 'D', 'F', 'L', 'N', 'O', 'A', 'U', 'W', 'M', 'P'
    ]

    schema = context.get_context(
    ).version_table_schema + '.' if context.get_context(
    ).version_table_schema else ''
    dialect = context.get_context().dialect.name

    if dialect == 'oracle':
        try_drop_constraint('REQUESTS_STATE_CHK', 'requests')
        op.create_check_constraint(
            constraint_name='REQUESTS_STATE_CHK',
            table_name='requests',
            condition=f'state in ({enum_values_str(new_enum_values)})',
        )
    elif dialect == 'postgresql':
        op.execute(
            'ALTER TABLE %srequests_history DROP CONSTRAINT IF EXISTS "REQUESTS_HISTORY_STATE_CHK", ALTER COLUMN state TYPE CHAR'
            % schema)
        op.execute('DROP TYPE "REQUESTS_HISTORY_STATE_CHK"')
        op.execute(
            f'CREATE TYPE "REQUESTS_HISTORY_STATE_CHK" AS ENUM({enum_values_str(new_enum_values)})'
        )
        op.execute(
            'ALTER TABLE %srequests_history ALTER COLUMN state TYPE "REQUESTS_HISTORY_STATE_CHK" USING state::"REQUESTS_HISTORY_STATE_CHK"'
            % schema)
        op.execute(
            'ALTER TABLE %srequests DROP CONSTRAINT IF EXISTS "REQUESTS_STATE_CHK", ALTER COLUMN state TYPE CHAR'
            % schema)
        op.execute('DROP TYPE "REQUESTS_STATE_CHK"')
        op.execute(
            f'CREATE TYPE "REQUESTS_STATE_CHK" AS ENUM({enum_values_str(new_enum_values)})'
        )
        op.execute(
            'ALTER TABLE %srequests ALTER COLUMN state TYPE "REQUESTS_STATE_CHK" USING state::"REQUESTS_STATE_CHK"'
            % schema)

    elif dialect == 'mysql':
        if context.get_context().dialect.server_version_info[0] == 8:
            op.drop_constraint('REQUESTS_STATE_CHK', 'requests', type_='check')

        op.create_check_constraint(
            constraint_name='REQUESTS_STATE_CHK',
            table_name='requests',
            condition=f'state in ({enum_values_str(new_enum_values)})',
        )
Exemple #11
0
def downgrade():
    '''
    Downgrade the database to the previous revision
    '''

    if context.get_context().dialect.name == 'oracle':
        try_drop_constraint('COLLECTION_REPLICAS_STATE_CHK', 'collection_replicas')
        drop_table('collection_replicas')

    elif context.get_context().dialect.name == 'postgresql':
        drop_table('collection_replicas')

    elif context.get_context().dialect.name == 'mysql':
        drop_table('collection_replicas')
Exemple #12
0
def upgrade():
    '''
    Upgrade the database to this revision
    '''

    schema = context.get_context(
    ).version_table_schema + '.' if context.get_context(
    ).version_table_schema else ''

    if context.get_context().dialect.name == 'oracle':
        add_column(
            'rules',
            sa.Column('ignore_account_limit',
                      sa.Boolean(name='RULES_IGNORE_ACCOUNT_LIMIT_CHK',
                                 create_constraint=True),
                      default=False))
        try_drop_constraint('RULES_STATE_CHK', 'rules')
        create_check_constraint('RULES_STATE_CHK', 'rules',
                                "state IN ('S', 'R', 'U', 'O', 'W', 'I')")

    elif context.get_context().dialect.name == 'postgresql':
        add_column('rules',
                   sa.Column('ignore_account_limit',
                             sa.Boolean(name='RULES_IGNORE_ACCOUNT_LIMIT_CHK',
                                        create_constraint=True),
                             default=False),
                   schema=schema[:-1])
        op.execute(
            'ALTER TABLE ' + schema +
            'rules DROP CONSTRAINT IF EXISTS "RULES_STATE_CHK", ALTER COLUMN state TYPE CHAR'
        )
        op.execute("DROP TYPE \"RULES_STATE_CHK\"")
        op.execute(
            "CREATE TYPE \"RULES_STATE_CHK\" AS ENUM('S', 'R', 'U', 'O', 'W', 'I')"
        )
        op.execute(
            "ALTER TABLE %srules ALTER COLUMN state TYPE \"RULES_STATE_CHK\" USING state::\"RULES_STATE_CHK\""
            % schema)

    elif context.get_context().dialect.name == 'mysql':
        add_column('rules',
                   sa.Column('ignore_account_limit',
                             sa.Boolean(name='RULES_IGNORE_ACCOUNT_LIMIT_CHK',
                                        create_constraint=True),
                             default=False),
                   schema=schema[:-1])
        op.execute('ALTER TABLE ' + schema +
                   'rules DROP CHECK RULES_STATE_CHK')  # pylint: disable=no-member
        create_check_constraint('RULES_STATE_CHK', 'rules',
                                "state IN ('S', 'R', 'U', 'O', 'W', 'I')")
def upgrade():
    '''
    Upgrade the database to this revision
    '''

    schema = context.get_context(
    ).version_table_schema + '.' if context.get_context(
    ).version_table_schema else ''

    if context.get_context().dialect.name == 'oracle':
        add_column(
            'rses',
            sa.Column('staging_area',
                      sa.Boolean(name='RSE_STAGING_AREA_CHK',
                                 create_constraint=True),
                      default=False))
        try_drop_constraint('REQUESTS_TYPE_CHK', 'requests')
        create_check_constraint(
            constraint_name='REQUESTS_TYPE_CHK',
            table_name='requests',
            condition="request_type in ('U', 'D', 'T', 'I', '0')")

    elif context.get_context().dialect.name == 'postgresql':
        add_column('rses',
                   sa.Column('staging_area',
                             sa.Boolean(name='RSE_STAGING_AREA_CHK',
                                        create_constraint=True),
                             default=False),
                   schema=schema[:-1])
        drop_constraint('REQUESTS_TYPE_CHK', 'requests', type_='check')
        create_check_constraint(
            constraint_name='REQUESTS_TYPE_CHK',
            table_name='requests',
            condition="request_type in ('U', 'D', 'T', 'I', '0')")

    elif context.get_context().dialect.name == 'mysql':
        add_column('rses',
                   sa.Column('staging_area',
                             sa.Boolean(name='RSE_STAGING_AREA_CHK',
                                        create_constraint=True),
                             default=False),
                   schema=schema[:-1])
        op.execute('ALTER TABLE ' + schema +
                   'requests DROP CHECK REQUESTS_TYPE_CHK')  # pylint: disable=no-member
        create_check_constraint(
            constraint_name='REQUESTS_TYPE_CHK',
            table_name='requests',
            condition="request_type in ('U', 'D', 'T', 'I', '0')")
Exemple #14
0
def downgrade():
    '''
    Downgrade the database to the previous revision
    '''

    schema = context.get_context().version_table_schema + '.' if context.get_context().version_table_schema else ''

    if context.get_context().dialect.name == 'oracle':
        try_drop_constraint('RULES_NOTIFICATION_CHK', 'rules')
        drop_column('rules', 'notification', schema=schema[:-1])

    elif context.get_context().dialect.name == 'postgresql':
        op.execute('ALTER TABLE %srules DROP CONSTRAINT IF EXISTS "RULES_NOTIFICATION_CHK", ALTER COLUMN notification TYPE CHAR' % schema)
        op.execute('ALTER TABLE %srules DROP COLUMN notification' % schema)
        op.execute('DROP TYPE \"RULES_NOTIFICATION_CHK\"')

    elif context.get_context().dialect.name == 'mysql':
        drop_column('rules', 'notification', schema=schema[:-1])
def downgrade():
    '''
    Downgrade the database to the previous revision
    '''

    drop_index('DID_META_DID_TYPE_IDX', 'did_meta')
    schema = get_context().version_table_schema + '.' if get_context(
    ).version_table_schema else ''
    if get_context().dialect.name == 'oracle':
        try_drop_constraint('DID_META_DID_TYPE_CHK', 'did_meta')
        drop_column('did_meta', 'did_type', schema=schema[:-1])

    elif get_context().dialect.name == 'postgresql':
        execute(
            'ALTER TABLE %sdid_meta DROP CONSTRAINT IF EXISTS "DID_META_DID_TYPE_CHK", ALTER COLUMN did_type TYPE CHAR'
            % schema)
        execute('ALTER TABLE %sdid_meta DROP COLUMN did_type' % schema)
        execute('DROP TYPE \"DID_META_DID_TYPE_CHK\"')

    elif get_context().dialect.name == 'mysql':
        drop_column('did_meta', 'did_type', schema=schema[:-1])
Exemple #16
0
def upgrade():
    '''
    Upgrade the database to this revision
    '''

    schema = context.get_context(
    ).version_table_schema + '.' if context.get_context(
    ).version_table_schema else ''

    if context.get_context().dialect.name in ['oracle', 'postgresql']:
        try_drop_constraint('REQUESTS_STATE_CHK', 'requests')
        create_check_constraint(
            constraint_name='REQUESTS_STATE_CHK',
            table_name='requests',
            condition=
            "state in ('Q', 'G', 'S', 'D', 'F', 'L', 'N', 'O', 'A', 'U')")
        add_column('requests',
                   sa.Column('submitter_id', sa.Integer()),
                   schema=schema[:-1])
        add_column('sources',
                   sa.Column('is_using', sa.Boolean()),
                   schema=schema[:-1])

    elif context.get_context().dialect.name == 'mysql':
        op.execute('ALTER TABLE ' + schema +
                   'requests DROP CHECK REQUESTS_STATE_CHK')  # pylint: disable=no-member
        create_check_constraint(
            constraint_name='REQUESTS_STATE_CHK',
            table_name='requests',
            condition=
            "state in ('Q', 'G', 'S', 'D', 'F', 'L', 'N', 'O', 'A', 'U')")
        add_column('requests',
                   sa.Column('submitter_id', sa.Integer()),
                   schema=schema[:-1])
        add_column('sources',
                   sa.Column('is_using', sa.Boolean()),
                   schema=schema[:-1])
Exemple #17
0
def downgrade():
    '''
    Downgrade the database to the previous revision
    '''

    schema = context.get_context(
    ).version_table_schema + '.' if context.get_context(
    ).version_table_schema else ''

    if context.get_context().dialect.name == 'oracle':
        try_drop_constraint('REQUESTS_STATE_CHK', 'requests')
        create_check_constraint(
            constraint_name='REQUESTS_STATE_CHK',
            table_name='requests',
            condition="state in ('Q', 'G', 'S', 'D', 'F', 'L')")
        drop_column('requests', 'submitter_id')
        drop_column('sources', 'is_using')

    elif context.get_context().dialect.name == 'postgresql':
        drop_constraint('REQUESTS_STATE_CHK', 'requests', type_='check')
        create_check_constraint(
            constraint_name='REQUESTS_STATE_CHK',
            table_name='requests',
            condition="state in ('Q', 'G', 'S', 'D', 'F', 'L')")
        drop_column('requests', 'submitter_id', schema=schema[:-1])
        drop_column('sources', 'is_using', schema=schema[:-1])

    elif context.get_context().dialect.name == 'mysql':
        op.execute('ALTER TABLE ' + schema +
                   'requests DROP CHECK REQUESTS_STATE_CHK')  # pylint: disable=no-member
        create_check_constraint(
            constraint_name='REQUESTS_STATE_CHK',
            table_name='requests',
            condition="state in ('Q', 'G', 'S', 'D', 'F', 'L')")
        drop_column('requests', 'submitter_id', schema=schema[:-1])
        drop_column('sources', 'is_using', schema=schema[:-1])
def upgrade():
    '''
    Upgrade the database to this revision
    '''

    schema = context.get_context(
    ).version_table_schema + '.' if context.get_context(
    ).version_table_schema else ''

    if context.get_context().dialect.name in ['oracle', 'postgresql']:

        alter_column('tokens',
                     'identity',
                     existing_type=sa.String(255),
                     type_=sa.String(2048),
                     schema=schema[:-1])
        alter_column('identities',
                     'identity',
                     existing_type=sa.String(255),
                     type_=sa.String(2048),
                     schema=schema[:-1])
        alter_column('account_map',
                     'identity',
                     existing_type=sa.String(255),
                     type_=sa.String(2048),
                     schema=schema[:-1])

        try_drop_constraint('IDENTITIES_TYPE_CHK', 'identities')
        create_check_constraint(
            constraint_name='IDENTITIES_TYPE_CHK',
            table_name='identities',
            condition="identity_type in ('X509', 'GSS', 'USERPASS', 'SSH')")
        try_drop_constraint('ACCOUNT_MAP_ID_TYPE_CHK', 'account_map')
        create_check_constraint(
            constraint_name='ACCOUNT_MAP_ID_TYPE_CHK',
            table_name='account_map',
            condition="identity_type in ('X509', 'GSS', 'USERPASS', 'SSH')")

    elif context.get_context().dialect.name == 'mysql':
        alter_column('tokens',
                     'identity',
                     existing_type=sa.String(255),
                     type_=sa.String(2048),
                     schema=schema[:-1])

        # MySQL does not allow altering a column referenced by a ForeignKey
        # so we need to drop that one first
        drop_constraint('ACCOUNT_MAP_ID_TYPE_FK',
                        'account_map',
                        type_='foreignkey')
        alter_column('identities',
                     'identity',
                     existing_type=sa.String(255),
                     type_=sa.String(2048),
                     nullable=False,
                     schema=schema[:-1])
        alter_column('account_map',
                     'identity',
                     existing_type=sa.String(255),
                     type_=sa.String(2048),
                     nullable=False,
                     schema=schema[:-1])
        create_foreign_key('ACCOUNT_MAP_ID_TYPE_FK', 'account_map',
                           'identities', ['identity', 'identity_type'],
                           ['identity', 'identity_type'])

        op.execute('ALTER TABLE ' + schema +
                   'identities DROP CHECK IDENTITIES_TYPE_CHK')  # pylint: disable=no-member
        create_check_constraint(
            constraint_name='IDENTITIES_TYPE_CHK',
            table_name='identities',
            condition="identity_type in ('X509', 'GSS', 'USERPASS', 'SSH')")
        op.execute('ALTER TABLE ' + schema +
                   'account_map DROP CHECK ACCOUNT_MAP_ID_TYPE_CHK')  # pylint: disable=no-member
        create_check_constraint(
            constraint_name='ACCOUNT_MAP_ID_TYPE_CHK',
            table_name='account_map',
            condition="identity_type in ('X509', 'GSS', 'USERPASS', 'SSH')")
Exemple #19
0
def upgrade():
    '''
    Upgrade the database to this revision
    '''
    schema = context.get_context(
    ).version_table_schema + '.' if context.get_context(
    ).version_table_schema else ''  # pylint: disable=no-member
    if context.get_context().dialect.name in ['oracle', 'postgresql']:  # pylint: disable=no-member
        try_drop_constraint('IDENTITIES_TYPE_CHK', 'identities')
        create_check_constraint(
            constraint_name='IDENTITIES_TYPE_CHK',
            table_name='identities',
            condition=
            "identity_type in ('X509', 'GSS', 'USERPASS', 'SSH', 'SAML', 'OIDC')"
        )
        try_drop_constraint('ACCOUNT_MAP_ID_TYPE_CHK', 'account_map')
        create_check_constraint(
            constraint_name='ACCOUNT_MAP_ID_TYPE_CHK',
            table_name='account_map',
            condition=
            "identity_type in ('X509', 'GSS', 'USERPASS', 'SSH', 'SAML', 'OIDC')"
        )
    elif context.get_context().dialect.name == 'mysql':  # pylint: disable=no-member
        execute('ALTER TABLE ' + schema +
                'identities DROP CHECK IDENTITIES_TYPE_CHK')  # pylint: disable=no-member
        create_check_constraint(
            constraint_name='IDENTITIES_TYPE_CHK',
            table_name='identities',
            condition=
            "identity_type in ('X509', 'GSS', 'USERPASS', 'SSH', 'SAML', 'OIDC')"
        )
        execute('ALTER TABLE ' + schema +
                'account_map DROP CHECK ACCOUNT_MAP_ID_TYPE_CHK')  # pylint: disable=no-member
        create_check_constraint(
            constraint_name='ACCOUNT_MAP_ID_TYPE_CHK',
            table_name='account_map',
            condition=
            "identity_type in ('X509', 'GSS', 'USERPASS', 'SSH', 'SAML', 'OIDC')"
        )

    if context.get_context().dialect.name in ['oracle', 'mysql', 'postgresql']:  # pylint: disable=no-member
        add_column('tokens',
                   sa.Column('oidc_scope',
                             sa.String(2048),
                             nullable=True,
                             default=None),
                   schema=schema[:-1])
        add_column('tokens',
                   sa.Column('audience',
                             sa.String(315),
                             nullable=True,
                             default=None),
                   schema=schema[:-1])
        add_column('tokens',
                   sa.Column('refresh_token',
                             sa.String(315),
                             nullable=True,
                             default=None),
                   schema=schema[:-1])
        add_column('tokens',
                   sa.Column('refresh',
                             sa.Boolean(name='TOKENS_REFRESH_CHK',
                                        create_constraint=True),
                             default=False),
                   schema=schema[:-1])
        add_column('tokens',
                   sa.Column('refresh_start',
                             sa.DateTime(),
                             nullable=True,
                             default=None),
                   schema=schema[:-1])
        add_column('tokens',
                   sa.Column('refresh_expired_at',
                             sa.DateTime(),
                             nullable=True,
                             default=None),
                   schema=schema[:-1])
        add_column('tokens',
                   sa.Column('refresh_lifetime',
                             sa.Integer(),
                             nullable=True,
                             default=None),
                   schema=schema[:-1])

        create_table(
            'oauth_requests', sa.Column('account', InternalAccountString(25)),
            sa.Column('state', sa.String(50)),
            sa.Column('nonce', sa.String(50)),
            sa.Column('access_msg', sa.String(2048)),
            sa.Column('redirect_msg', sa.String(2048)),
            sa.Column('refresh_lifetime', sa.Integer(), nullable=True),
            sa.Column('ip', sa.String(39), nullable=True),
            sa.Column('expired_at',
                      sa.DateTime(),
                      default=datetime.datetime.utcnow() +
                      datetime.timedelta(seconds=600)),
            sa.Column('created_at',
                      sa.DateTime,
                      default=datetime.datetime.utcnow),
            sa.Column('updated_at',
                      sa.DateTime,
                      default=datetime.datetime.utcnow,
                      onupdate=datetime.datetime.utcnow))
        create_primary_key('OAUTH_REQUESTS_STATE_PK', 'oauth_requests',
                           ['state'])
        create_check_constraint('OAUTH_REQUESTS_EXPIRED_AT_NN',
                                'oauth_requests', 'expired_at is not null')
        create_index('OAUTH_REQUESTS_ACC_EXP_AT_IDX', 'oauth_requests',
                     ['account', 'expired_at'])
        create_index('OAUTH_REQUESTS_ACCESS_MSG_IDX', 'oauth_requests',
                     ['access_msg'])

    if context.get_context().dialect.name in ['oracle', 'postgresql']:  # pylint: disable=no-member
        alter_column('tokens',
                     'token',
                     existing_type=sa.String(length=352),
                     type_=sa.String(length=3072),
                     schema=schema[:-1])
    if context.get_context().dialect.name in ['mysql']:  # pylint: disable=no-member
        alter_column('tokens',
                     'token',
                     existing_type=sa.String(length=352),
                     type_=sa.String(length=3072),
                     existing_nullable=False,
                     nullable=False,
                     schema=schema[:-1])
Exemple #20
0
def downgrade():
    '''
    Downgrade the database to the previous revision
    '''
    schema = context.get_context(
    ).version_table_schema + '.' if context.get_context(
    ).version_table_schema else ''  # pylint: disable=no-member
    if context.get_context().dialect.name in ['oracle']:  # pylint: disable=no-member
        try_drop_constraint('IDENTITIES_TYPE_CHK', 'identities')
        create_check_constraint(
            constraint_name='IDENTITIES_TYPE_CHK',
            table_name='identities',
            condition=
            "identity_type in ('X509', 'GSS', 'USERPASS', 'SSH', 'SAML')")

        try_drop_constraint('ACCOUNT_MAP_ID_TYPE_CHK', 'account_map')
        create_check_constraint(
            constraint_name='ACCOUNT_MAP_ID_TYPE_CHK',
            table_name='account_map',
            condition=
            "identity_type in ('X509', 'GSS', 'USERPASS', 'SSH', 'SAML')")
        drop_column('tokens', 'oidc_scope', schema=schema[:-1])
        drop_column('tokens', 'audience', schema=schema[:-1])
        drop_column('tokens', 'refresh_token', schema=schema[:-1])
        drop_column('tokens', 'refresh', schema=schema[:-1])
        drop_column('tokens', 'refresh_start', schema=schema[:-1])
        drop_column('tokens', 'refresh_expired_at', schema=schema[:-1])
        drop_column('tokens', 'refresh_lifetime', schema=schema[:-1])
        drop_table('oauth_requests')
        alter_column('tokens',
                     'token',
                     existing_type=sa.String(length=3072),
                     type_=sa.String(length=352),
                     schema=schema[:-1])

    elif context.get_context().dialect.name == 'mysql':  # pylint: disable=no-member
        create_check_constraint(
            constraint_name='IDENTITIES_TYPE_CHK',
            table_name='identities',
            condition=
            "identity_type in ('X509', 'GSS', 'USERPASS', 'SSH', 'SAML')")

        create_check_constraint(
            constraint_name='ACCOUNT_MAP_ID_TYPE_CHK',
            table_name='account_map',
            condition=
            "identity_type in ('X509', 'GSS', 'USERPASS', 'SSH', 'SAML')")
        drop_column('tokens', 'oidc_scope', schema=schema[:-1])
        drop_column('tokens', 'audience', schema=schema[:-1])
        drop_column('tokens', 'refresh_token', schema=schema[:-1])
        drop_column('tokens', 'refresh', schema=schema[:-1])
        drop_column('tokens', 'refresh_start', schema=schema[:-1])
        drop_column('tokens', 'refresh_expired_at', schema=schema[:-1])
        drop_column('tokens', 'refresh_lifetime', schema=schema[:-1])
        alter_column('tokens',
                     'token',
                     existing_type=sa.String(length=3072),
                     type_=sa.String(length=352),
                     existing_nullable=False,
                     nullable=False,
                     schema=schema[:-1])
        drop_table('oauth_requests')

    elif context.get_context().dialect.name == 'postgresql':  # pylint: disable=no-member

        create_check_constraint(
            constraint_name='IDENTITIES_TYPE_CHK',
            table_name='identities',
            condition=
            "identity_type in ('X509', 'GSS', 'USERPASS', 'SSH', 'SAML')")

        create_check_constraint(
            constraint_name='ACCOUNT_MAP_ID_TYPE_CHK',
            table_name='account_map',
            condition=
            "identity_type in ('X509', 'GSS', 'USERPASS', 'SSH', 'SAML')")
        drop_column('tokens', 'oidc_scope', schema=schema[:-1])
        drop_column('tokens', 'audience', schema=schema[:-1])
        drop_column('tokens', 'refresh_token', schema=schema[:-1])
        drop_column('tokens', 'refresh', schema=schema[:-1])
        drop_column('tokens', 'refresh_start', schema=schema[:-1])
        drop_column('tokens', 'refresh_expired_at', schema=schema[:-1])
        drop_column('tokens', 'refresh_lifetime', schema=schema[:-1])
        alter_column('tokens',
                     'token',
                     existing_type=sa.String(length=3072),
                     type_=sa.String(length=352),
                     schema=schema[:-1])
        drop_table('oauth_requests')
Exemple #21
0
def downgrade():
    '''
    Downgrade the database to the previous revision
    '''

    schema = context.get_context(
    ).version_table_schema + '.' if context.get_context(
    ).version_table_schema else ''

    if context.get_context().dialect.name == 'oracle':
        try_drop_constraint('MESSAGES_EVENT_TYPE_NN', 'messages')
        try_drop_constraint('MESSAGES_PAYLOAD_NN', 'messages')
        try_drop_constraint('MESSAGES_CREATED_NN', 'messages')
        try_drop_constraint('MESSAGES_UPDATED_NN', 'messages')
        drop_constraint('MESSAGES_PK', 'messages', type_='primary')
        rename_table('messages', 'callbacks')
        create_primary_key('CALLBACKS_PK', 'callbacks', ['id'])
        create_check_constraint('CALLBACKS_EVENT_TYPE_NN', 'callbacks',
                                'event_type is not null')
        create_check_constraint('CALLBACKS_PAYLOAD_NN', 'callbacks',
                                'payload is not null')
        create_check_constraint('CALLBACKS_CREATED_NN', 'callbacks',
                                'created_at is not null')
        create_check_constraint('CALLBACKS_UPDATED_NN', 'callbacks',
                                'updated_at is not null')

    elif context.get_context().dialect.name == 'postgresql':
        drop_constraint('MESSAGES_EVENT_TYPE_NN', 'messages', type_='check')
        drop_constraint('MESSAGES_PAYLOAD_NN', 'messages', type_='check')
        drop_constraint('MESSAGES_CREATED_NN', 'messages', type_='check')
        drop_constraint('MESSAGES_UPDATED_NN', 'messages', type_='check')
        drop_constraint('MESSAGES_PK', 'messages', type_='primary')
        rename_table('messages', 'callbacks', schema=schema[:-1])
        create_primary_key('CALLBACKS_PK', 'callbacks', ['id'])
        create_check_constraint('CALLBACKS_EVENT_TYPE_NN', 'callbacks',
                                'event_type is not null')
        create_check_constraint('CALLBACKS_PAYLOAD_NN', 'callbacks',
                                'payload is not null')
        create_check_constraint('CALLBACKS_CREATED_NN', 'callbacks',
                                'created_at is not null')
        create_check_constraint('CALLBACKS_UPDATED_NN', 'callbacks',
                                'updated_at is not null')

    elif context.get_context().dialect.name == 'mysql':
        op.execute('ALTER TABLE ' + schema +
                   'messages DROP CHECK MESSAGES_EVENT_TYPE_NN')  # pylint: disable=no-member
        op.execute('ALTER TABLE ' + schema +
                   'messages DROP CHECK MESSAGES_PAYLOAD_NN')  # pylint: disable=no-member
        op.execute('ALTER TABLE ' + schema +
                   'messages DROP CHECK MESSAGES_CREATED_NN')  # pylint: disable=no-member
        op.execute('ALTER TABLE ' + schema +
                   'messages DROP CHECK MESSAGES_UPDATED_NN')  # pylint: disable=no-member
        drop_constraint('messages_pk', 'messages', type_='primary')
        rename_table('messages', 'callbacks', schema=schema[:-1])
        create_primary_key('callbacks_pk', 'callbacks', ['id'])
        create_check_constraint('callbacks_event_type_nn', 'callbacks',
                                'event_type is not null')
        create_check_constraint('callbacks_payload_nn', 'callbacks',
                                'payload is not null')
        create_check_constraint('callbacks_created_nn', 'callbacks',
                                'created_at is not null')
        create_check_constraint('callbacks_updated_nn', 'callbacks',
                                'updated_at is not null')
def downgrade():
    '''
    Downgrade the database to the previous revision
    '''

    schema = context.get_context(
    ).version_table_schema + '.' if context.get_context(
    ).version_table_schema else ''

    # Attention!
    # This automatically removes all SSH keys to accommodate the column size and check constraint.

    if context.get_context().dialect.name == 'oracle':
        execute("DELETE FROM account_map WHERE identity_type='SSH'")  # pylint: disable=no-member
        execute("DELETE FROM identities WHERE identity_type='SSH'")  # pylint: disable=no-member

        try_drop_constraint('IDENTITIES_TYPE_CHK', 'identities')
        create_check_constraint(
            constraint_name='IDENTITIES_TYPE_CHK',
            table_name='identities',
            condition="identity_type in ('X509', 'GSS', 'USERPASS')")

        try_drop_constraint('ACCOUNT_MAP_ID_TYPE_CHK', 'account_map')

        create_check_constraint(
            constraint_name='ACCOUNT_MAP_ID_TYPE_CHK',
            table_name='account_map',
            condition="identity_type in ('X509', 'GSS', 'USERPASS')")

        alter_column('tokens',
                     'identity',
                     existing_type=sa.String(2048),
                     type_=sa.String(255))
        alter_column('account_map',
                     'identity',
                     existing_type=sa.String(2048),
                     type_=sa.String(255))
        alter_column('identities',
                     'identity',
                     existing_type=sa.String(2048),
                     type_=sa.String(255))

    elif context.get_context().dialect.name == 'postgresql':
        execute("DELETE FROM " + schema +
                "account_map WHERE identity_type='SSH'")  # pylint: disable=no-member
        execute("DELETE FROM " + schema +
                "identities WHERE identity_type='SSH'")  # pylint: disable=no-member

        drop_constraint('ACCOUNT_MAP_ID_TYPE_FK',
                        'account_map',
                        type_='foreignkey')
        op.execute(
            'ALTER TABLE ' + schema +
            'identities DROP CONSTRAINT IF EXISTS "IDENTITIES_TYPE_CHK", ALTER COLUMN identity_type TYPE VARCHAR'
        )  # pylint: disable=no-member
        create_check_constraint(
            constraint_name='IDENTITIES_TYPE_CHK',
            table_name='identities',
            condition="identity_type in ('X509', 'GSS', 'USERPASS')")

        op.execute(
            'ALTER TABLE ' + schema +
            'account_map DROP CONSTRAINT IF EXISTS "ACCOUNT_MAP_ID_TYPE_CHK", ALTER COLUMN identity_type TYPE VARCHAR'
        )  # pylint: disable=no-member
        create_check_constraint(
            constraint_name='ACCOUNT_MAP_ID_TYPE_CHK',
            table_name='account_map',
            condition="identity_type in ('X509', 'GSS', 'USERPASS')")
        create_foreign_key('ACCOUNT_MAP_ID_TYPE_FK', 'account_map',
                           'identities', ['identity', 'identity_type'],
                           ['identity', 'identity_type'])

        alter_column('tokens',
                     'identity',
                     existing_type=sa.String(2048),
                     type_=sa.String(255),
                     schema=schema[:-1])
        alter_column('account_map',
                     'identity',
                     existing_type=sa.String(2048),
                     type_=sa.String(255),
                     schema=schema[:-1])
        alter_column('identities',
                     'identity',
                     existing_type=sa.String(2048),
                     type_=sa.String(255),
                     schema=schema[:-1])

    elif context.get_context().dialect.name == 'mysql':
        execute("DELETE FROM " + schema +
                "account_map WHERE identity_type='SSH'")  # pylint: disable=no-member
        execute("DELETE FROM " + schema +
                "identities WHERE identity_type='SSH'")  # pylint: disable=no-member

        op.execute('ALTER TABLE ' + schema +
                   'identities DROP CHECK IDENTITIES_TYPE_CHK')  # pylint: disable=no-member
        create_check_constraint(
            constraint_name='IDENTITIES_TYPE_CHK',
            table_name='identities',
            condition="identity_type in ('X509', 'GSS', 'USERPASS')")

        op.execute('ALTER TABLE ' + schema +
                   'account_map DROP CHECK ACCOUNT_MAP_ID_TYPE_CHK')  # pylint: disable=no-member
        create_check_constraint(
            constraint_name='ACCOUNT_MAP_ID_TYPE_CHK',
            table_name='account_map',
            condition="identity_type in ('X509', 'GSS', 'USERPASS')")

        alter_column('tokens',
                     'identity',
                     existing_type=sa.String(2048),
                     type_=sa.String(255),
                     schema=schema[:-1])

        # MySQL does not allow altering a column referenced by a ForeignKey
        # so we need to drop that one first
        drop_constraint('ACCOUNT_MAP_ID_TYPE_FK',
                        'account_map',
                        type_='foreignkey')
        alter_column('account_map',
                     'identity',
                     existing_type=sa.String(2048),
                     type_=sa.String(255),
                     nullable=False,
                     schema=schema[:-1])
        alter_column('identities',
                     'identity',
                     existing_type=sa.String(2048),
                     type_=sa.String(255),
                     nullable=False,
                     schema=schema[:-1])
        create_foreign_key('ACCOUNT_MAP_ID_TYPE_FK', 'account_map',
                           'identities', ['identity', 'identity_type'],
                           ['identity', 'identity_type'])
Exemple #23
0
def upgrade():
    '''
    Upgrade the database to this revision
    '''

    schema = context.get_context(
    ).version_table_schema + '.' if context.get_context(
    ).version_table_schema else ''

    if context.get_context().dialect.name in ['oracle', 'postgresql']:
        # Create new bad_pfns table
        create_table(
            'bad_pfns', sa.Column('path', sa.String(2048)),
            sa.Column('state',
                      sa.Enum(
                          BadPFNStatus,
                          name='BAD_PFNS_STATE_CHK',
                          values_callable=lambda obj: [e.value for e in obj]),
                      default=BadPFNStatus.SUSPICIOUS),
            sa.Column('reason', sa.String(255)),
            sa.Column('account', sa.String(25)),
            sa.Column('expires_at', sa.DateTime),
            sa.Column('created_at',
                      sa.DateTime,
                      default=datetime.datetime.utcnow),
            sa.Column('updated_at',
                      sa.DateTime,
                      default=datetime.datetime.utcnow,
                      onupdate=datetime.datetime.utcnow))

        create_primary_key('BAD_PFNS_PK', 'bad_pfns', ['path', 'state'])
        create_foreign_key('BAD_PFNS_ACCOUNT_FK', 'bad_pfns', 'accounts',
                           ['account'], ['account'])

        try_drop_constraint('BAD_REPLICAS_STATE_CHK', 'bad_replicas')
        create_check_constraint(
            constraint_name='BAD_REPLICAS_STATE_CHK',
            table_name='bad_replicas',
            condition="state in ('B', 'D', 'L', 'R', 'S', 'T')")

        # Add new column to bad_replicas table
        add_column('bad_replicas',
                   sa.Column('expires_at', sa.DateTime()),
                   schema=schema[:-1])

        # Change PK
        drop_constraint('BAD_REPLICAS_STATE_PK',
                        'bad_replicas',
                        type_='primary')
        create_primary_key('BAD_REPLICAS_STATE_PK', 'bad_replicas',
                           ['scope', 'name', 'rse_id', 'state', 'created_at'])

        # Add new Index to Table
        create_index('BAD_REPLICAS_EXPIRES_AT_IDX', 'bad_replicas',
                     ['expires_at'])

    elif context.get_context().dialect.name == 'mysql':
        # Create new bad_pfns table
        create_table(
            'bad_pfns', sa.Column('path', sa.String(2048)),
            sa.Column('state',
                      sa.Enum(
                          BadPFNStatus,
                          name='BAD_PFNS_STATE_CHK',
                          values_callable=lambda obj: [e.value for e in obj]),
                      default=BadPFNStatus.SUSPICIOUS),
            sa.Column('reason', sa.String(255)),
            sa.Column('account', sa.String(25)),
            sa.Column('expires_at', sa.DateTime),
            sa.Column('created_at',
                      sa.DateTime,
                      default=datetime.datetime.utcnow),
            sa.Column('updated_at',
                      sa.DateTime,
                      default=datetime.datetime.utcnow,
                      onupdate=datetime.datetime.utcnow))

        create_primary_key('BAD_PFNS_PK', 'bad_pfns', ['path', 'state'])
        create_foreign_key('BAD_PFNS_ACCOUNT_FK', 'bad_pfns', 'accounts',
                           ['account'], ['account'])

        op.execute('ALTER TABLE ' + schema +
                   'bad_replicas DROP CHECK BAD_REPLICAS_STATE_CHK')  # pylint: disable=no-member
        create_check_constraint(
            constraint_name='BAD_REPLICAS_STATE_CHK',
            table_name='bad_replicas',
            condition="state in ('B', 'D', 'L', 'R', 'S', 'T')")

        # Add new column to bad_replicas table
        add_column('bad_replicas',
                   sa.Column('expires_at', sa.DateTime()),
                   schema=schema[:-1])

        # Change PK
        drop_constraint('BAD_REPLICAS_STATE_PK',
                        'bad_replicas',
                        type_='primary')
        create_primary_key('BAD_REPLICAS_STATE_PK', 'bad_replicas',
                           ['scope', 'name', 'rse_id', 'state', 'created_at'])

        # Add new Index to Table
        create_index('BAD_REPLICAS_EXPIRES_AT_IDX', 'bad_replicas',
                     ['expires_at'])