Python sqlalchemy.Column() Examples

The following are 30 code examples of sqlalchemy.Column(). 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 sqlalchemy , or try the search function .
Example #1
Source File: aba5a217ca9b_merge_created_in_creator.py    From gnocchi with Apache License 2.0 6 votes vote down vote up
def upgrade():
    for table_name in ("resource", "resource_history", "metric"):
        creator_col = sa.Column("creator", sa.String(255))
        created_by_user_id_col = sa.Column("created_by_user_id",
                                           sa.String(255))
        created_by_project_id_col = sa.Column("created_by_project_id",
                                              sa.String(255))
        op.add_column(table_name, creator_col)
        t = sa.sql.table(
            table_name, creator_col,
            created_by_user_id_col, created_by_project_id_col)
        op.execute(
            t.update().values(
                creator=(
                    created_by_user_id_col + ":" + created_by_project_id_col
                )).where((created_by_user_id_col is not None)
                         | (created_by_project_id_col is not None)))
        op.drop_column(table_name, "created_by_user_id")
        op.drop_column(table_name, "created_by_project_id") 
Example #2
Source File: sqlalchemy_base.py    From gnocchi with Apache License 2.0 6 votes vote down vote up
def revision(cls):
        tablename_compact = cls.__tablename__
        if tablename_compact.endswith("_history"):
            tablename_compact = tablename_compact[:-6]
        return sqlalchemy.Column(
            sqlalchemy.Integer,
            sqlalchemy.ForeignKey(
                'resource_history.revision',
                ondelete="CASCADE",
                name="fk_%s_revision_rh_revision"
                % tablename_compact,
                # NOTE(sileht): We use to ensure that postgresql
                # does not use AccessExclusiveLock on destination table
                use_alter=True),
            primary_key=True
        ) 
Example #3
Source File: 875c52a485_.py    From comport with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('months',
    sa.Column('id', sa.INTEGER(), server_default=sa.text("nextval('months_id_seq'::regclass)"), nullable=False),
    sa.Column('month', sa.INTEGER(), autoincrement=False, nullable=True),
    sa.Column('year', sa.INTEGER(), autoincrement=False, nullable=True),
    sa.Column('department_id', sa.INTEGER(), autoincrement=False, nullable=True),
    sa.ForeignKeyConstraint(['department_id'], ['departments.id'], name='months_department_id_fkey'),
    sa.PrimaryKeyConstraint('id', name='months_pkey'),
    postgresql_ignore_search_path=False
    )
    op.create_table('serviceTypes',
    sa.Column('id', sa.INTEGER(), server_default=sa.text('nextval(\'"serviceTypes_id_seq"\'::regclass)'), nullable=False),
    sa.Column('month_id', sa.INTEGER(), autoincrement=False, nullable=True),
    sa.Column('service_type', sa.VARCHAR(length=36), autoincrement=False, nullable=False),
    sa.Column('count', sa.INTEGER(), autoincrement=False, nullable=True),
    sa.ForeignKeyConstraint(['month_id'], ['months.id'], name='serviceTypes_month_id_fkey'),
    sa.PrimaryKeyConstraint('id', name='serviceTypes_pkey')
    )
    op.drop_table('use_of_force_incidents')
    ### end Alembic commands ### 
Example #4
Source File: 1a869ac514c_.py    From comport with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('interesteds',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=255), nullable=False),
    sa.Column('agency', sa.String(length=255), nullable=False),
    sa.Column('location', sa.String(length=255), nullable=False),
    sa.Column('phone', sa.String(length=255), nullable=False),
    sa.Column('email', sa.String(length=255), nullable=False),
    sa.Column('comments', sa.String(length=255), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )
    ### end Alembic commands ### 
Example #5
Source File: 1c2c61ac1f4c_add_original_resource_id_column.py    From gnocchi with Apache License 2.0 5 votes vote down vote up
def upgrade():
    op.add_column('resource', sa.Column('original_resource_id',
                                        sa.String(length=255),
                                        nullable=True))
    op.add_column('resource_history', sa.Column('original_resource_id',
                                                sa.String(length=255),
                                                nullable=True)) 
Example #6
Source File: c62df18bf4ee_add_unit_column_for_metric.py    From gnocchi with Apache License 2.0 5 votes vote down vote up
def upgrade():
    op.add_column('metric', sa.Column('unit',
                                      sa.String(length=31),
                                      nullable=True)) 
Example #7
Source File: 828c16f70cce_create_resource_type_table.py    From gnocchi with Apache License 2.0 5 votes vote down vote up
def upgrade():
    resource_type = op.create_table(
        'resource_type',
        sa.Column('name', sa.String(length=255), nullable=False),
        sa.PrimaryKeyConstraint('name'),
        mysql_charset='utf8',
        mysql_engine='InnoDB'
    )

    resource = sa.Table('resource', sa.MetaData(),
                        type_string_col("type", "resource"))
    op.execute(resource_type.insert().from_select(
        ['name'], sa.select([resource.c.type]).distinct()))

    for table in ["resource", "resource_history"]:
        op.alter_column(table, "type", new_column_name="old_type",
                        existing_type=type_enum)
        op.add_column(table, type_string_col("type", table))
        sa_table = sa.Table(table, sa.MetaData(),
                            type_string_col("type", table),
                            type_enum_col('old_type'))
        op.execute(sa_table.update().values(
            {sa_table.c.type: sa_table.c.old_type}))
        op.drop_column(table, "old_type")
        op.alter_column(table, "type", nullable=False,
                        existing_type=type_string) 
Example #8
Source File: 828c16f70cce_create_resource_type_table.py    From gnocchi with Apache License 2.0 5 votes vote down vote up
def type_string_col(name, table):
    return sa.Column(
        name, type_string,
        sa.ForeignKey('resource_type.name',
                      ondelete="RESTRICT",
                      name="fk_%s_resource_type_name" % table)) 
Example #9
Source File: 0718ed97e5b3_add_tablename_to_resource_type.py    From gnocchi with Apache License 2.0 5 votes vote down vote up
def upgrade():
    op.add_column("resource_type", sa.Column('tablename', sa.String(18),
                                             nullable=True))

    resource_type = sa.Table(
        'resource_type', sa.MetaData(),
        sa.Column('name', sa.String(255), nullable=False),
        sa.Column('tablename', sa.String(18), nullable=True)
    )
    op.execute(resource_type.update().where(
        resource_type.c.name == "instance_network_interface"
    ).values({'tablename': op.inline_literal("'instance_net_int'")}))
    op.execute(resource_type.update().where(
        resource_type.c.name != "instance_network_interface"
    ).values({'tablename': resource_type.c.name}))

    op.alter_column("resource_type", "tablename", type_=sa.String(18),
                    nullable=False)
    op.create_unique_constraint("uniq_resource_type0tablename",
                                "resource_type", ["tablename"]) 
Example #10
Source File: d24877c22ab0_add_attributes_to_resource_type.py    From gnocchi with Apache License 2.0 5 votes vote down vote up
def upgrade():
    op.add_column("resource_type",
                  sa.Column('attributes', sa_utils.JSONType(),)) 
Example #11
Source File: 39b7d449d46a_create_metric_status_column.py    From gnocchi with Apache License 2.0 5 votes vote down vote up
def upgrade():
    enum = sa.Enum("active", "delete", name="metric_status_enum")
    enum.create(op.get_bind(), checkfirst=False)
    op.add_column("metric",
                  sa.Column('status', enum,
                            nullable=False,
                            server_default="active"))
    op.create_index('ix_metric_status', 'metric', ['status'], unique=False)

    op.drop_constraint("fk_metric_resource_id_resource_id",
                       "metric", type_="foreignkey")
    op.create_foreign_key("fk_metric_resource_id_resource_id",
                          "metric", "resource",
                          ("resource_id",), ("id",),
                          ondelete="SET NULL") 
Example #12
Source File: 7e6f9d542f8b_resource_type_state_column.py    From gnocchi with Apache License 2.0 5 votes vote down vote up
def upgrade():
    states = ("active", "creating", "creation_error", "deleting",
              "deletion_error")
    enum = sa.Enum(*states, name="resource_type_state_enum")
    enum.create(op.get_bind(), checkfirst=False)
    op.add_column("resource_type",
                  sa.Column('state', enum, nullable=False,
                            server_default="creating"))
    rt = sa.sql.table('resource_type', sa.sql.column('state', enum))
    op.execute(rt.update().values(state="active")) 
Example #13
Source File: 5c4f93e5bb4_mysql_float_to_timestamp.py    From gnocchi with Apache License 2.0 5 votes vote down vote up
def upgrade():
    bind = op.get_bind()
    if bind and bind.engine.name == "mysql":
        op.execute("SET time_zone = '+00:00'")
        # NOTE(jd) So that crappy engine that is MySQL does not have "ALTER
        # TABLE … USING …". We need to copy everything and convert…
        for table_name, column_name in (("resource", "started_at"),
                                        ("resource", "ended_at"),
                                        ("resource", "revision_start"),
                                        ("resource_history", "started_at"),
                                        ("resource_history", "ended_at"),
                                        ("resource_history", "revision_start"),
                                        ("resource_history", "revision_end"),
                                        ("resource_type", "updated_at")):

            nullable = column_name == "ended_at"

            existing_type = sa.types.DECIMAL(
                precision=20, scale=6, asdecimal=True)
            existing_col = sa.Column(
                column_name,
                existing_type,
                nullable=nullable)
            temp_col = sa.Column(
                column_name + "_ts",
                sqlalchemy_types.TimestampUTC(),
                nullable=True)
            op.add_column(table_name, temp_col)
            t = sa.sql.table(table_name, existing_col, temp_col)
            op.execute(t.update().values(
                **{column_name + "_ts": func.from_unixtime(existing_col)}))
            op.drop_column(table_name, column_name)
            op.alter_column(table_name,
                            column_name + "_ts",
                            nullable=nullable,
                            type_=sqlalchemy_types.TimestampUTC(),
                            existing_nullable=nullable,
                            existing_type=existing_type,
                            new_column_name=column_name) 
Example #14
Source File: 1d31622bbed_.py    From comport with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.add_column('departments', sa.Column('contact_us', sa.Text(convert_unicode=True), nullable=True))
    op.add_column('departments', sa.Column('how_you_can_use_this_data', sa.Text(convert_unicode=True), nullable=True))
    op.add_column('departments', sa.Column('what_this_is', sa.Text(convert_unicode=True), nullable=True))
    op.add_column('departments', sa.Column('why_we_are_doing_this', sa.Text(convert_unicode=True), nullable=True))
    ### end Alembic commands ### 
Example #15
Source File: 30e29c63aa6_.py    From comport with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.add_column('use_of_force_incidents', sa.Column('received_date', postgresql.TIMESTAMP(), autoincrement=False, nullable=True))
    ### end Alembic commands ### 
Example #16
Source File: 43fd1bb4848_.py    From comport with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.add_column('use_of_force_incidents', sa.Column('census_tract', sa.String(length=255), nullable=True))
    op.add_column('use_of_force_incidents', sa.Column('citizen_weapon', sa.String(length=255), nullable=True))
    ### end Alembic commands ### 
Example #17
Source File: 52979aa7977_.py    From comport with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('users',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('username', sa.String(length=80), nullable=False),
    sa.Column('email', sa.String(length=80), nullable=False),
    sa.Column('password', sa.String(length=128), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('first_name', sa.String(length=30), nullable=True),
    sa.Column('last_name', sa.String(length=30), nullable=True),
    sa.Column('active', sa.Boolean(), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('email'),
    sa.UniqueConstraint('username')
    )
    op.create_table('roles',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=80), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    ### end Alembic commands ### 
Example #18
Source File: 2f13ffb1ce60_.py    From comport with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def upgrade():
    op.add_column('pursuits_srpd', sa.Column('bureau', sa.String(length=255), nullable=True))
    op.add_column('pursuits_srpd', sa.Column('division', sa.String(length=255), nullable=True)) 
Example #19
Source File: 331c513e8d4_.py    From comport with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('months',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('month', sa.Integer(), nullable=True),
    sa.Column('year', sa.Integer(), nullable=True),
    sa.Column('department_id', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['department_id'], ['departments.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    ### end Alembic commands ### 
Example #20
Source File: 4e4710949dd_.py    From comport with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('chart_block_defaults',
    sa.Column('id', sa.INTEGER(), nullable=False),
    sa.Column('title', sa.VARCHAR(length=255), autoincrement=False, nullable=False),
    sa.Column('caption', sa.VARCHAR(length=255), autoincrement=False, nullable=True),
    sa.Column('slug', sa.VARCHAR(length=255), autoincrement=False, nullable=False),
    sa.Column('dataset', sa.VARCHAR(length=255), autoincrement=False, nullable=False),
    sa.Column('content', sa.TEXT(), autoincrement=False, nullable=True),
    sa.PrimaryKeyConstraint('id', name='chart_block_defaults_pkey')
    )
    ### end Alembic commands ### 
Example #21
Source File: 6d30846080b2_.py    From comport with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.add_column('departments', sa.Column('is_public', sa.Boolean(), server_default=sa.true(), nullable=False))
    ### end Alembic commands ### 
Example #22
Source File: 4f5fcadcf2d_.py    From comport with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('links',
    sa.Column('id', sa.INTEGER(), nullable=False),
    sa.Column('department_id', sa.INTEGER(), autoincrement=False, nullable=False),
    sa.Column('title', sa.VARCHAR(length=255), autoincrement=False, nullable=False),
    sa.Column('url', sa.VARCHAR(length=2083), autoincrement=False, nullable=False),
    sa.Column('type', sa.VARCHAR(length=255), autoincrement=False, nullable=False),
    sa.ForeignKeyConstraint(['department_id'], ['departments.id'], name='links_department_id_fkey'),
    sa.PrimaryKeyConstraint('id', name='links_pkey')
    )
    ### end Alembic commands ### 
Example #23
Source File: 9d6a8b74b21a_.py    From comport with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def downgrade():
    op.add_column('use_of_force_incidents_srpd', sa.Column('officer_age', sa.VARCHAR(length=255), autoincrement=False, nullable=True))
    op.add_column('pursuits_srpd', sa.Column('officer_age', sa.VARCHAR(length=255), autoincrement=False, nullable=True))
    op.add_column('officer_involved_shootings_srpd', sa.Column('officer_age', sa.VARCHAR(length=255), autoincrement=False, nullable=True))
    op.add_column('citizen_complaints_srpd', sa.Column('officer_age', sa.VARCHAR(length=255), autoincrement=False, nullable=True)) 
Example #24
Source File: 52eaf89477f_.py    From comport with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('user_department_relationship_table',
    sa.Column('department_id', sa.Integer(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['department_id'], ['departments.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('department_id', 'user_id')
    )
    ### end Alembic commands ### 
Example #25
Source File: 513f93441476_.py    From comport with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def downgrade():
    op.add_column('use_of_force_incidents_lmpd', sa.Column('resident_weapon_used', sa.VARCHAR(length=255), autoincrement=False, nullable=True)) 
Example #26
Source File: 3434f410ad7_.py    From comport with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('extractors',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.add_column('users', sa.Column('type', sa.String(length=50), nullable=True))
    op.create_index(op.f('ix_users_id'), 'users', ['id'], unique=False)
    ### end Alembic commands ### 
Example #27
Source File: 52887f8e06b_.py    From comport with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.add_column('citizen_complaint', sa.Column('category', sa.String(length=255), nullable=True))
    ### end Alembic commands ### 
Example #28
Source File: 4a7e5abdf57_.py    From comport with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.add_column('officer_involved_shootings', sa.Column('officer_force_type', sa.VARCHAR(length=255), autoincrement=False, nullable=True))
    op.drop_column('officer_involved_shootings', 'officer_weapon_used')
    ### end Alembic commands ### 
Example #29
Source File: 4a7e5abdf57_.py    From comport with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.add_column('officer_involved_shootings', sa.Column('officer_weapon_used', sa.String(length=255), nullable=True))
    op.drop_column('officer_involved_shootings', 'officer_force_type')
    ### end Alembic commands ### 
Example #30
Source File: 5e04dbf30fb0_first_commit_for_prod.py    From everyclass-server with Mozilla Public License 2.0 5 votes vote down vote up
def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('unavailable_room_report',
                    sa.Column('record_id', sa.Integer(), nullable=False),
                    sa.Column('room_id', sa.String(), nullable=True),
                    sa.Column('date', sa.Date(), nullable=True),
                    sa.Column('time', sa.String(length=4), nullable=True),
                    sa.Column('reporter', sa.String(length=15), nullable=True),
                    sa.Column('reporter_type', sa.String(), nullable=True),
                    sa.PrimaryKeyConstraint('record_id'),
                    sa.UniqueConstraint('room_id', 'date', 'time', 'reporter', 'reporter_type', name='unavailable_room_report_uniq')
                    )
    op.drop_index('idx_token', table_name='calendar_tokens')
    op.alter_column('identity_verify_requests', 'create_time',
                    existing_type=postgresql.TIMESTAMP(timezone=True),
                    nullable=True)
    op.create_index(op.f('ix_simple_passwords_time'), 'simple_passwords', ['time'], unique=False)
    op.drop_index('idx_host_time', table_name='visit_tracks')
    op.create_index('idx_host_time', 'visit_tracks', ['host_id', 'last_visit_time'], unique=False)
    # ### end Alembic commands ###