summaryrefslogtreecommitdiff
path: root/src/mailman/database/alembic/versions/d4fbb4fd34ca_header_match_order.py
blob: 313963b86672d42f95259fc47be83321c053ad06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""Add a numerical position column to sort header matches.

Revision ID: d4fbb4fd34ca
Revises: bfda02ab3a9b
Create Date: 2016-02-01 15:57:09.807678

"""

import sqlalchemy as sa

from alembic import op
from mailman.database.helpers import is_mysql


# Revision identifiers, used by Alembic.
revision = 'd4fbb4fd34ca'
down_revision = 'bfda02ab3a9b'


def upgrade():
    with op.batch_alter_table('headermatch') as batch_op:
        batch_op.add_column(
            sa.Column('position', sa.Integer(), nullable=True))
        batch_op.create_index(
            op.f('ix_headermatch_position'), ['position'], unique=False)
        if not is_mysql(op.get_bind()):
            # MySQL automatically creates indexes for primary keys.
            batch_op.create_index(
                op.f('ix_headermatch_mailing_list_id'), ['mailing_list_id'],
                unique=False)
            # MySQL doesn't allow changing columns used in a foreign key
            # constrains since MySQL version 5.6.  We need to drop the
            # constraint before changing the column.  But, since the
            # constraint name is auto-generated, we can't really hardcode the
            # name here to use batch_op.drop_constraint().  Until we have a
            # better fix for this, it should be safe to skip this.
            batch_op.alter_column(
                'mailing_list_id', existing_type=sa.INTEGER(), nullable=False)


def downgrade():
    with op.batch_alter_table('headermatch') as batch_op:
        batch_op.drop_index(op.f('ix_headermatch_position'))
        batch_op.drop_column('position')

        if not is_mysql(op.get_bind()):
            # MySQL automatically creates and removes the indexes for primary
            # keys.  So, you cannot drop it without removing the foreign key
            # constraint.
            batch_op.drop_index(op.f('ix_headermatch_mailing_list_id'))
            # MySQL doesn't allow changing columns used in foreign_key
            # constraints.
            batch_op.alter_column(
                'mailing_list_id', existing_type=sa.INTEGER(), nullable=True)