Python alembic.op.create_primary_key() Examples

The following are 30 code examples of alembic.op.create_primary_key(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module alembic.op , or try the search function .
Example #1
Source File: fdf8821871d7_main_tables.py    From fastapi-realworld-example-app with MIT License 6 votes vote down vote up
def create_favorites_table() -> None:
    op.create_table(
        "favorites",
        sa.Column(
            "user_id",
            sa.Integer,
            sa.ForeignKey("users.id", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column(
            "article_id",
            sa.Integer,
            sa.ForeignKey("articles.id", ondelete="CASCADE"),
            nullable=False,
        ),
    )
    op.create_primary_key("pk_favorites", "favorites", ["user_id", "article_id"]) 
Example #2
Source File: 2019_04_13_69d10714ee45_rewrite_changes_and_tags.py    From sticker-finder with MIT License 6 votes vote down vote up
def downgrade():
    """Down migration."""
    # Drop all the new stuff
    op.drop_index(op.f('ix_change_removed_tags_tag_name'), table_name='change_removed_tags')
    op.drop_index(op.f('ix_change_removed_tags_change_id'), table_name='change_removed_tags')
    op.drop_table('change_removed_tags')
    op.drop_index(op.f('ix_change_added_tags_tag_name'), table_name='change_added_tags')
    op.drop_index(op.f('ix_change_added_tags_change_id'), table_name='change_added_tags')
    op.drop_table('change_added_tags')

    # Restore the old tag primary key constraint stuff
    op.drop_constraint('sticker_tag_tag_name_fkey', 'sticker_tag', type_='foreignkey')
    op.drop_column('sticker_tag', 'tag_is_default_language')
    op.drop_constraint('tag_pkey', 'tag')
    op.create_primary_key('tag_pkey', 'tag', ['name'])

    op.create_foreign_key(
        'sticker_tag_tag_name_fkey',
        'sticker_tag', 'tag',
        ['tag_name'], ['name'],
        onupdate='CASCADE', ondelete='CASCADE', deferrable=True
    )

    # ### end Alembic commands ### 
Example #3
Source File: 77a2637f243c_update_user_table.py    From GraphSpace with GNU General Public License v2.0 6 votes vote down vote up
def downgrade():
	# Drop New Index
	op.drop_index('ix_user_email', 'user')

	# Rename email column
	op.alter_column('user', 'email', new_column_name='user_id')

	# Rename admin column
	op.alter_column('user', 'is_admin', new_column_name='admin')

	# Drop date columns
	op.drop_column('user', 'created_at')
	op.drop_column('user', 'updated_at')

	# Drop new ID Column
	op.drop_column('user', 'id')

	# Reinstate OLD PKey
	op.create_primary_key("user_pkey", "user", ["user_id", ]) 
Example #4
Source File: bb85d8864dfa_update_group_table.py    From GraphSpace with GNU General Public License v2.0 6 votes vote down vote up
def downgrade():

	# Remove new foreign key reference
	op.drop_constraint('group_owner_email_fkey', 'group', type_='foreignkey')

	# Drop New Index
	op.drop_index('_group_uc_name_owner_email', 'group')

	# Rename email column
	op.alter_column('group', 'owner_email', new_column_name='owner_id')

	# Drop date columns
	op.drop_column('group', 'created_at')
	op.drop_column('group', 'updated_at')

	# Drop new ID Column
	op.drop_column('group', 'id')

	# Reinstate OLD PKey
	op.create_primary_key("group_pkey", "group", ["group_id", "owner_id" ])

	pass 
Example #5
Source File: 2ab3f5371bde_dsa_in_container_type_modelbase_to.py    From barbican with Apache License 2.0 6 votes vote down vote up
def upgrade():
    op.add_column('container_secret',
                  sa.Column('created_at', sa.DateTime(), nullable=False))
    op.add_column('container_secret',
                  sa.Column('deleted', sa.Boolean(), nullable=False))
    op.add_column('container_secret',
                  sa.Column('deleted_at', sa.DateTime(), nullable=True))
    op.add_column('container_secret',
                  sa.Column('id', sa.String(length=36), nullable=False))
    op.add_column('container_secret',
                  sa.Column('status', sa.String(length=20), nullable=False))
    op.add_column('container_secret',
                  sa.Column('updated_at', sa.DateTime(), nullable=False))

    op.create_primary_key('pk_container_secret', 'container_secret', ['id'])
    op.create_unique_constraint(
        '_container_secret_name_uc',
        'container_secret',
        ['container_id', 'secret_id', 'name']
    ) 
Example #6
Source File: 13b73f6d1082_restructuring_vulnerability_pk_and_.py    From vulncode-db with Apache License 2.0 6 votes vote down vote up
def downgrade():
    op.drop_constraint('fk_vuln', 'vulnerability_git_commits', type_='foreignkey')
    op.create_index('uk_ver_cve_id', 'vulnerability', ['version', 'cve_id'], unique=True)
    op.drop_constraint('uk_vcdb_id_version', 'vulnerability', type_='unique')
    op.drop_column('vulnerability', 'vcdb_id')
    op.drop_column('vulnerability', 'prev_version')
    op.alter_column('vulnerability', 'version',
                    existing_type=mysql.INTEGER(display_width=11),
                    nullable=False)
    # Drop all foreign keys on vulnerability.id as we intend to update it.
    op.drop_constraint('vulnerability_git_commits_ibfk_1', 'vulnerability_git_commits', type_='foreignkey')
    ##############
    # Update the vulnerability primary key.
    # Remove autoincrement from the PK as there can only be one auto key and it has to be the PK.
    op.alter_column('vulnerability', 'id', existing_type=sa.Integer(), autoincrement=False, nullable=False)
    op.drop_constraint('id', 'vulnerability', type_='primary')
    # Now we can define a new primary key.
    op.create_primary_key('pk', 'vulnerability', ['id', 'version'])
    # Re-enable auto incrementing for the id column, too.
    op.alter_column('vulnerability', 'id', existing_type=sa.Integer(), autoincrement=True, nullable=False)
    # ----------------------------------------------------------------------------------------------------
    op.add_column('vulnerability_git_commits', sa.Column('version', sa.Integer(), nullable=False))
    op.create_foreign_key('fk_vuln', 'vulnerability_git_commits', 'vulnerability', ['vulnerability_details_id', 'version'], ['id', 'version'])
    # ### end Alembic commands ### 
Example #7
Source File: ops.py    From alembic with MIT License 6 votes vote down vote up
def batch_create_primary_key(cls, operations, constraint_name, columns):
        """Issue a "create primary key" instruction using the
        current batch migration context.

        The batch form of this call omits the ``table_name`` and ``schema``
        arguments from the call.

        .. seealso::

            :meth:`.Operations.create_primary_key`

        """
        op = cls(
            constraint_name,
            operations.impl.table_name,
            columns,
            schema=operations.impl.schema,
        )
        return operations.invoke(op) 
Example #8
Source File: 854bd902b1bc_change_kernel_identification.py    From backend.ai-manager with GNU Lesser General Public License v3.0 6 votes vote down vote up
def upgrade():
    op.drop_constraint('fk_vfolder_attachment_vfolder_vfolders', 'vfolder_attachment', type_='foreignkey')
    op.drop_constraint('fk_vfolder_attachment_kernel_kernels', 'vfolder_attachment', type_='foreignkey')
    op.drop_constraint('pk_kernels', 'kernels', type_='primary')
    op.add_column('kernels',
                  sa.Column('id', GUID(),
                            server_default=sa.text('uuid_generate_v4()'),
                            nullable=False))
    op.add_column('kernels', sa.Column('role', sa.String(length=16), nullable=False, default='master'))
    op.create_primary_key('pk_kernels', 'kernels', ['id'])
    op.alter_column(
        'kernels', 'sess_id',
        existing_type=postgresql.UUID(),
        type_=sa.String(length=64),
        nullable=True,
        existing_server_default=sa.text('uuid_generate_v4()'))
    op.create_index(op.f('ix_kernels_sess_id'), 'kernels', ['sess_id'], unique=False)
    op.create_index(op.f('ix_kernels_sess_id_role'), 'kernels', ['sess_id', 'role'], unique=False)
    op.create_foreign_key('fk_vfolder_attachment_vfolder_vfolders',
                          'vfolder_attachment', 'vfolders',
                          ['vfolder'], ['id'], onupdate='CASCADE', ondelete='CASCADE')
    op.create_foreign_key('fk_vfolder_attachment_kernel_kernels',
                          'vfolder_attachment', 'kernels',
                          ['kernel'], ['id'], onupdate='CASCADE', ondelete='CASCADE') 
Example #9
Source File: fdf8821871d7_main_tables.py    From fastapi-realworld-example-app with MIT License 6 votes vote down vote up
def create_articles_to_tags_table() -> None:
    op.create_table(
        "articles_to_tags",
        sa.Column(
            "article_id",
            sa.Integer,
            sa.ForeignKey("articles.id", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column(
            "tag",
            sa.Text,
            sa.ForeignKey("tags.tag", ondelete="CASCADE"),
            nullable=False,
        ),
    )
    op.create_primary_key(
        "pk_articles_to_tags", "articles_to_tags", ["article_id", "tag"]
    ) 
Example #10
Source File: fdf8821871d7_main_tables.py    From fastapi-realworld-example-app with MIT License 6 votes vote down vote up
def create_followers_to_followings_table() -> None:
    op.create_table(
        "followers_to_followings",
        sa.Column(
            "follower_id",
            sa.Integer,
            sa.ForeignKey("users.id", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column(
            "following_id",
            sa.Integer,
            sa.ForeignKey("users.id", ondelete="CASCADE"),
            nullable=False,
        ),
    )
    op.create_primary_key(
        "pk_followers_to_followings",
        "followers_to_followings",
        ["follower_id", "following_id"],
    ) 
Example #11
Source File: 611733367157_adding_new_fields_and_keys_indices_to_.py    From vulncode-db with Apache License 2.0 6 votes vote down vote up
def downgrade():
    op.drop_constraint('fk_vuln', 'vulnerability_git_commits', type_='foreignkey')
    op.alter_column('vulnerability_git_commits', 'vulnerability_details_id',
                    existing_type=mysql.INTEGER(display_width=11),
                    nullable=True)
    op.drop_column('vulnerability_git_commits', 'version')

    op.drop_index(op.f('ix_vulnerability_cve_id'), table_name='vulnerability')
    op.drop_constraint('uk_ver_cve_id', 'vulnerability', type_='unique')
    op.create_index('cve_id', 'vulnerability', ['cve_id'], unique=True)
    # Remove autoincrement from the PK as there can only be one auto key and it has to be the PK.
    op.alter_column('vulnerability', 'id', existing_type=sa.Integer(), autoincrement=False, nullable=False)
    op.drop_constraint('pk', 'vulnerability', type_='primary')
    op.create_primary_key('id', 'vulnerability', ['id'])
    op.alter_column('vulnerability', 'id', existing_type=sa.Integer(), autoincrement=True, nullable=False)

    op.drop_column('vulnerability', 'version')
    op.drop_column('vulnerability', 'state')
    op.drop_constraint('fk_reviewer_id', 'vulnerability', type_='foreignkey')
    op.drop_column('vulnerability', 'reviewer_id')
    op.drop_column('vulnerability', 'review_feedback')

    op.add_column('vulnerability_resources', sa.Column('vulnerability_details_id', mysql.INTEGER(display_width=11), autoincrement=False, nullable=True))
    op.create_foreign_key('vulnerability_resources_ibfk_1', 'vulnerability_resources', 'vulnerability', ['vulnerability_details_id'], ['id'])
    op.create_foreign_key('vulnerability_git_commits_ibfk_1', 'vulnerability_git_commits', 'vulnerability', ['vulnerability_details_id'], ['id']) 
Example #12
Source File: ops.py    From android_universal with MIT License 6 votes vote down vote up
def batch_create_primary_key(cls, operations, constraint_name, columns):
        """Issue a "create primary key" instruction using the
        current batch migration context.

        The batch form of this call omits the ``table_name`` and ``schema``
        arguments from the call.

        .. seealso::

            :meth:`.Operations.create_primary_key`

        """
        op = cls(
            constraint_name, operations.impl.table_name, columns,
            schema=operations.impl.schema
        )
        return operations.invoke(op) 
Example #13
Source File: ops.py    From jbox with MIT License 6 votes vote down vote up
def batch_create_primary_key(cls, operations, constraint_name, columns):
        """Issue a "create primary key" instruction using the
        current batch migration context.

        The batch form of this call omits the ``table_name`` and ``schema``
        arguments from the call.

        .. seealso::

            :meth:`.Operations.create_primary_key`

        """
        op = cls(
            constraint_name, operations.impl.table_name, columns,
            schema=operations.impl.schema
        )
        return operations.invoke(op) 
Example #14
Source File: 987edda096f5_access_id_in_user_projects.py    From pagure with GNU General Public License v2.0 6 votes vote down vote up
def downgrade():
    ''' Remove column access_id from user_projects and projects_groups '''

    # this removes the current constraints as well.
    op.drop_column('user_projects', 'access')
    op.drop_column('projects_groups', 'access')

    # recreate the previous constraints
    op.create_unique_constraint(
            None,
            'user_projects',
            ['project_id', 'user_id'],
    )
    op.create_primary_key(
            None,
            'projects_groups',
            ['project_id', 'group_id'],
    )
    op.drop_table('access_levels') 
Example #15
Source File: 5d73a92c8979_revamp_externallinks_table.py    From wiki-scripts with GNU General Public License v3.0 5 votes vote down vote up
def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('externallinks', 'el_index')
    op.drop_column('externallinks', 'el_id')
    # ### end Alembic commands ###
    op.create_primary_key('externallinks_pkey', 'externallinks', ['el_from', 'el_to']) 
Example #16
Source File: b3ae57ee07d4_drop_carbonmonoxide_id.py    From emissions-api with MIT License 5 votes vote down vote up
def downgrade():
    # Create new column 'id' and autoincrement its value
    op.execute(CreateSequence(Sequence("carbonmonoxide_id_seq")))
    op.add_column('carbonmonoxide', sa.Column(
        'id', sa.INTEGER(), nullable=False,
        server_default=sa.text("nextval('carbonmonoxide_id_seq'::regclass)")))

    # Use 'id' as the new primary key
    op.create_primary_key('carbonmonoxide_pkey', 'carbonmonoxide', ['id']) 
Example #17
Source File: e8ea58723178_remove_host_from_driver_private_data.py    From manila with Apache License 2.0 5 votes vote down vote up
def upgrade():
    bind = op.get_bind()
    engine = bind.engine
    try:
        if (engine.name == MYSQL_ENGINE):
            op.drop_constraint('PRIMARY', TABLE_NAME, type_='primary')
            op.create_primary_key('DRIVERS_PRIVATE_PK', TABLE_NAME,
                                  ['entity_uuid', 'key'])
        op.drop_column(TABLE_NAME, COLUMN_HOST)
    except Exception:
        LOG.error("Column '%s' could not be dropped", COLUMN_HOST)
        raise 
Example #18
Source File: 406821843b55_add_role_column_to_users_tenants_table.py    From cloudify-manager with Apache License 2.0 5 votes vote down vote up
def upgrade():
    op.add_column(
        'users_tenants',
        sa.Column('role_id', sa.Integer()),
    )
    op.create_foreign_key(
        'users_tenants_role_id_fkey',
        'users_tenants',
        'roles',
        ['role_id'],
        ['id'],
    )
    op.create_primary_key(
        'users_tenants_pkey',
        'users_tenants',
        ['user_id', 'tenant_id'],
    )

    # Set 'user' role as the default for every user in a tenant
    op.execute(
        users_tenants.update()
        .values(role_id=_get_role_id('user'))
    )
    op.alter_column('users_tenants', 'role_id', nullable=False)

    # Manually using old role IDs, because they have changed in this version.
    # Old roles were:
    # 1 - admin
    # 2 - user
    # New roles are:
    # 1 - sys_admin
    # 2 - manager
    # 3 - user
    # 4 - viewer
    # 5 - default
    update_system_role(OLD_USER_ROLE_ID, _get_role_id('default'))
    update_system_role(OLD_ADMIN_ROLE_ID, _get_role_id('sys_admin')) 
Example #19
Source File: 2019_04_15_35223866defb_fix_tag_schema.py    From sticker-finder with MIT License 5 votes vote down vote up
def upgrade():
    """Fix wrong constraints."""
    # Drop all constraints first
    op.drop_constraint('change_added_tags_tag_name_fkey', 'change_added_tags', type_='foreignkey')
    op.drop_constraint('change_removed_tags_tag_name_fkey', 'change_removed_tags', type_='foreignkey')
    op.drop_constraint('sticker_tag_tag_name_fkey', 'sticker_tag', type_='foreignkey')
    op.drop_column('sticker_tag', 'tag_is_default_language')

    op.drop_constraint('tag_pkey', 'tag')

    # Remove all tags that exist in both languages.
    session = Session(bind=op.get_bind())
    duplicate_tags = session.query(Tag.name) \
        .group_by(Tag.name) \
        .having(func.count(Tag.name) > 1) \
        .all()

    duplicate_names = [tag[0] for tag in duplicate_tags]

    session.query(Tag) \
        .filter(Tag.is_default_language.is_(False)) \
        .filter(Tag.name.in_(duplicate_names)) \
        .delete(synchronize_session='fetch')

    # Recreate tag.name pkey
    op.create_primary_key('tag_pkey', 'tag', ['name'])

    # Create other foreign keys
    op.create_foreign_key(
        'change_added_tags_tag_name_fkey', 'change_added_tags', 'tag',
        ['tag_name'], ['name'],
        onupdate='cascade', ondelete='cascade', deferrable=True)
    op.create_foreign_key(
        'change_removed_tags_tag_name_fkey', 'change_removed_tags', 'tag',
        ['tag_name'], ['name'],
        onupdate='cascade', ondelete='cascade', deferrable=True)
    op.create_foreign_key(
        'sticker_tag_tag_name_fkey', 'sticker_tag', 'tag',
        ['tag_name'], ['name'],
        onupdate='cascade', ondelete='cascade', deferrable=True) 
Example #20
Source File: 7aae863786af_add_role_column_groups_tenants_table.py    From cloudify-manager with Apache License 2.0 5 votes vote down vote up
def upgrade():
    op.add_column(
        'groups_tenants',
        sa.Column('role_id', sa.Integer()),
    )
    op.create_foreign_key(
        'groups_tenants_role_id_fkey',
        'groups_tenants',
        'roles',
        ['role_id'],
        ['id'],
    )
    op.create_primary_key(
        'groups_tenants_pkey',
        'groups_tenants',
        ['group_id', 'tenant_id'],
    )
    # Define tables with just the columns needed
    # to generate the UPDATE sql expression below
    groups_tenants = sa.table(
        'groups_tenants',
        sa.column('group_id', sa.Integer),
        sa.column('role_id', sa.Integer),
    )
    roles = sa.table(
        'roles',
        sa.column('id', sa.Integer),
        sa.column('name', sa.Text),
    )
    # Set 'user' role as the default for every group in a tenant
    op.execute(
        groups_tenants.update()
        .values(role_id=(
            sa.select([roles.c.id])
            .where(roles.c.name == 'user')
        ))
    )
    op.alter_column('groups_tenants', 'role_id', nullable=False) 
Example #21
Source File: 20160303155834_pfizer_takeda_add_pk.py    From collectors with MIT License 5 votes vote down vote up
def upgrade():
    op.create_primary_key('pfizer_pkey', 'pfizer', ['nct_id'])
    op.create_primary_key('takeda_pkey', 'takeda', ['takeda_trial_id']) 
Example #22
Source File: 47a5ad0d647f_add_missing_primary_keys.py    From wiki-scripts with GNU General Public License v3.0 5 votes vote down vote up
def upgrade():
    op.create_primary_key('imagelinks_pkey', 'imagelinks', ['il_from', 'il_to'])
    op.create_primary_key('categorylinks_pkey', 'categorylinks', ['cl_from', 'cl_to']) 
Example #23
Source File: 2ab3f5371bde_dsa_in_container_type_modelbase_to.py    From sgx-kms with Apache License 2.0 5 votes vote down vote up
def upgrade():
    op.add_column('container_secret', sa.Column('created_at', sa.DateTime(), nullable=False))
    op.add_column('container_secret', sa.Column('deleted', sa.Boolean(), nullable=False))
    op.add_column('container_secret', sa.Column('deleted_at', sa.DateTime(), nullable=True))
    op.add_column('container_secret', sa.Column('id', sa.String(length=36), nullable=False))
    op.add_column('container_secret', sa.Column('status', sa.String(length=20), nullable=False))
    op.add_column('container_secret', sa.Column('updated_at', sa.DateTime(), nullable=False))

    op.create_primary_key('pk_container_secret', 'container_secret', ['id'])
    op.create_unique_constraint(
        '_container_secret_name_uc',
        'container_secret',
        ['container_id', 'secret_id', 'name']
    ) 
Example #24
Source File: f8c799db4aa0_fix_unnamed_constraints.py    From cloudkitty with Apache License 2.0 5 votes vote down vote up
def translate_op(op_, constraint_type, name, table, *args, **kwargs):
    if op_ == 'drop':
        op.drop_constraint(name, table, type_=constraint_type)
    else:
        if constraint_type == 'primary':
            func = op.create_primary_key
        elif constraint_type == 'unique':
            func = op.create_unique_constraint
        elif constraint_type == 'foreignkey':
            func = op.create_foreign_key
        func(name, table, *args, **kwargs) 
Example #25
Source File: 5b84b3dba2ac_update_graph_table.py    From GraphSpace with GNU General Public License v2.0 5 votes vote down vote up
def downgrade():

	# Remove new foreign key reference
	op.drop_constraint('graph_owner_email_fkey', 'graph', type_='foreignkey')

	# Drop New Index
	op.drop_index('_graph_uc_name_owner_email', 'graph')

	# Rename email column
	op.alter_column('graph', 'owner_email', new_column_name='user_id')

	# Rename graph_id column
	op.alter_column('graph', 'name', new_column_name='graph_id')

	# Undrop shared_with_groups column
	op.add_column('graph', sa.Column('shared_with_groups', sa.Integer))

	# Rename public column
	op.alter_column('graph', 'is_public', new_column_name='public', nullable=None)

	# Drop date columns
	op.alter_column('graph', 'created_at', new_column_name='created', server_default=sa.func.current_timestamp())
	op.alter_column('graph', 'updated_at', new_column_name='modified', server_default=sa.func.current_timestamp())

	# Drop new ID Column
	op.drop_column('graph', 'id')

	# Reinstate OLD PKey
	op.create_primary_key("graph_pkey", "graph", ["graph_id", "user_id" ])

	pass 
Example #26
Source File: 13b73f6d1082_restructuring_vulnerability_pk_and_.py    From vulncode-db with Apache License 2.0 5 votes vote down vote up
def upgrade():
    op.drop_constraint('fk_vuln', 'vulnerability_git_commits', type_='foreignkey')
    op.drop_column('vulnerability_git_commits', 'version')
    # Remove autoincrement from the PK as there can only be one auto key and it has to be the PK.
    op.alter_column('vulnerability', 'id', existing_type=sa.Integer(), autoincrement=False, nullable=False)
    op.drop_constraint('pk', 'vulnerability', type_='primary')
    op.create_primary_key('id', 'vulnerability', ['id'])
    op.alter_column('vulnerability', 'id', existing_type=sa.Integer(), autoincrement=True, nullable=False)
    ##############
    op.create_foreign_key('vulnerability_git_commits_ibfk_1', 'vulnerability_git_commits', 'vulnerability', ['vulnerability_details_id'], ['id'])
    op.alter_column('vulnerability', 'version',
                    existing_type=mysql.INTEGER(display_width=11),
                    nullable=True)
    op.add_column('vulnerability', sa.Column('prev_version', sa.Integer(), nullable=True))
    op.add_column('vulnerability', sa.Column('vcdb_id', sa.Integer(), nullable=True))

    # Copy the whole ID column contents into the new vcdb_id column and set all existing entries to PUBLISHED.
    vuln_table_view = table(
        'vulnerability',
        sa.Column('id', sa.Integer),
        sa.Column('vcdb_id', sa.Integer()),
        sa.Column('state', sa.Enum('NEW', 'READY', 'IN_REVIEW', 'REVIEWED', 'PUBLISHED', 'ARCHIVED', name='vulnerabilitystate'), nullable=False)
        # Ignore all other columns.
    )
    op.execute(
        vuln_table_view.update().values({'vcdb_id': vuln_table_view.c.id, 'state': 'PUBLISHED'})
    )
    # -----------------------------------------------------

    op.create_unique_constraint('uk_vcdb_id_version', 'vulnerability', ['vcdb_id', 'version'])
    op.drop_index('uk_ver_cve_id', table_name='vulnerability')
    op.create_foreign_key('fk_vuln', 'vulnerability_git_commits', 'vulnerability', ['vulnerability_details_id'], ['id'])
    # ### end Alembic commands ### 
Example #27
Source File: 611733367157_adding_new_fields_and_keys_indices_to_.py    From vulncode-db with Apache License 2.0 5 votes vote down vote up
def upgrade():
    # Drop all foreign keys on vulnerability.id as we intend to update it.
    op.drop_constraint('vulnerability_git_commits_ibfk_1', 'vulnerability_git_commits', type_='foreignkey')
    # To make things simpler we will sever the complete link to vulnerability resources for now.
    op.drop_constraint('vulnerability_resources_ibfk_1', 'vulnerability_resources', type_='foreignkey')
    op.drop_column('vulnerability_resources', 'vulnerability_details_id')
    # ----------------------------------------------------------------------------------------------------
    # Add new columns to the vulnerability table.
    op.add_column('vulnerability', sa.Column('review_feedback', sa.Text(), nullable=True))
    op.add_column('vulnerability', sa.Column('reviewer_id', sa.Integer(), nullable=True))
    op.create_foreign_key('fk_reviewer_id', 'vulnerability', 'user', ['reviewer_id'], ['id'])
    op.add_column('vulnerability', sa.Column('state', sa.Enum('NEW', 'READY', 'IN_REVIEW', 'REVIEWED', 'PUBLISHED', 'ARCHIVED', name='vulnerabilitystate'), nullable=False))
    op.add_column('vulnerability', sa.Column('version', sa.Integer(), nullable=False))
    # Update the vulnerability primary key.
    # Remove autoincrement from the PK as there can only be one auto key and it has to be the PK.
    op.alter_column('vulnerability', 'id', existing_type=sa.Integer(), autoincrement=False, nullable=False)
    op.drop_constraint('id', 'vulnerability', type_='primary')
    # Now we can define a new primary key.
    op.create_primary_key('pk', 'vulnerability', ['id', 'version'])
    # Re-enable auto incrementing for the id column, too.
    op.alter_column('vulnerability', 'id', existing_type=sa.Integer(), autoincrement=True, nullable=False)
    # ---------------------------------------------------------------------------------------------------
    # A CVE ID can appear multiple times across different versions so we need to remove it's unique constraint.
    op.drop_index('cve_id', table_name='vulnerability')
    op.create_unique_constraint('uk_ver_cve_id', 'vulnerability', ['version', 'cve_id'])
    op.create_index(op.f('ix_vulnerability_cve_id'), 'vulnerability', ['cve_id'], unique=False)
    # ----------------------------------------------------------------------------------------------------
    # Now that the vulnerability multi column primary key is intact, create the foreign keys again.
    op.add_column('vulnerability_git_commits', sa.Column('version', sa.Integer(), nullable=False))
    op.alter_column('vulnerability_git_commits', 'vulnerability_details_id',
                    existing_type=mysql.INTEGER(display_width=11),
                    nullable=False)
    op.create_foreign_key('fk_vuln', 'vulnerability_git_commits', 'vulnerability', ['vulnerability_details_id', 'version'], ['id', 'version']) 
Example #28
Source File: cc54842bb6ba_updates_dataset_pk.py    From QCFractal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.execute("ALTER TABLE dataset_entry DROP CONSTRAINT dataset_entry_pkey")
    op.create_primary_key(None, "dataset_entry", ["dataset_id", "name"])
    # ### end Alembic commands ### 
Example #29
Source File: cc54842bb6ba_updates_dataset_pk.py    From QCFractal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.execute("ALTER TABLE dataset_entry DROP CONSTRAINT dataset_entry_pkey")
    op.create_primary_key(None, "dataset_entry", ["dataset_id", "molecule_id"])
    # ### end Alembic commands ### 
Example #30
Source File: 0179_billing_primary_const.py    From notifications-api with MIT License 5 votes vote down vote up
def upgrade():
    op.drop_column('ft_billing', 'crown')
    op.drop_column('ft_billing', 'annual_billing_id')
    op.drop_column('ft_billing', 'organisation_id')
    op.drop_constraint('ft_billing_pkey', 'ft_billing', type_='primary')
    # These are the orthogonal dimensions that define a row (except international).
    # These entries define a unique record.
    op.create_primary_key('ft_billing_pkey', 'ft_billing', ['bst_date',
                                                            'template_id',
                                                            'rate_multiplier',
                                                            'provider',
                                                            'international'])