コード例 #1
0
def reverse(apps, schema_editor):
    """
    Drop the new geo_version column from all data tables
    """
    session = get_session()
    inspector = inspect(session.bind)

    try:
        for data_table in DATA_TABLES.values():
            db_model = data_table.model
            table = db_model.__table__

            # remove the primary key constraint, if any
            pk = inspector.get_pk_constraint(table.name)['name']
            if pk:
                session.execute("ALTER TABLE %s DROP CONSTRAINT %s" %
                                (table.name, pk))

            # drop the new column
            session.execute("ALTER TABLE %s DROP COLUMN geo_version" %
                            table.name)

            # add the old pk constraint
            pk = table.primary_key
            pk.columns.remove(table.c.geo_version)
            session.execute(AddConstraint(pk))

        session.commit()
    finally:
        session.close()
コード例 #2
0
def forwards(apps, schema_editor):
    """
    Ensure all data tables have the new geo_version column, with a default of ''
    """
    session = get_session()
    inspector = inspect(session.bind)

    try:
        for data_table in DATA_TABLES.values():
            db_model = data_table.model
            table = db_model.__table__

            cols = [c['name'] for c in inspector.get_columns(table.name)]
            if 'geo_version' in cols:
                continue

            # remove the old primary key constraint, if any
            pk = inspector.get_pk_constraint(table.name)['name']
            if pk:
                session.execute("ALTER TABLE %s DROP CONSTRAINT %s" %
                                (table.name, pk))

            # add the new column
            session.execute(
                "ALTER TABLE %s ADD COLUMN geo_version VARCHAR(100) DEFAULT ''"
                % table.name)

            # add the correct new constraint
            session.execute(AddConstraint(table.primary_key))

        session.commit()
    finally:
        session.close()