summaryrefslogtreecommitdiff
path: root/contrib/ast-db-manage/config/versions/3772f8f828da_update_identify_by.py
blob: 92695b09fc0e9e18b4be9bea22852f633f158ff5 (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
"""update_identify_by

Revision ID: 3772f8f828da
Revises: c7a44a5a0851
Create Date: 2016-08-11 10:47:29.211063

"""

# revision identifiers, used by Alembic.
revision = '3772f8f828da'
down_revision = 'c7a44a5a0851'

from alembic import op
import sqlalchemy as sa


def enum_update(table_name, column_name, enum_name, enum_values):
    if op.get_context().bind.dialect.name != 'postgresql':
        op.alter_column(table_name, column_name,
                        type_=sa.Enum(*enum_values, name=enum_name))
        return

    # Postgres requires a few more steps
    tmp = enum_name + '_tmp'

    op.execute('ALTER TYPE ' + enum_name + ' RENAME TO ' + tmp)

    updated = sa.Enum(*enum_values, name=enum_name)
    updated.create(op.get_bind(), checkfirst=False)

    op.execute('ALTER TABLE ' + table_name + ' ALTER COLUMN ' + column_name +
               ' TYPE ' + enum_name + ' USING identify_by::text::' + enum_name)

    op.execute('DROP TYPE ' + tmp)


def upgrade():
    enum_update('ps_endpoints', 'identify_by', 'pjsip_identify_by_values',
                ['username', 'auth_username'])


def downgrade():
    enum_update('ps_endpoints', 'identify_by', 'pjsip_identify_by_values',
                ['username'])