alembic: add auth_username to endpoint's identify_by enum
[asterisk/asterisk.git] / contrib / ast-db-manage / config / versions / 3772f8f828da_update_identify_by.py
1 """update_identify_by
2
3 Revision ID: 3772f8f828da
4 Revises: c7a44a5a0851
5 Create Date: 2016-08-11 10:47:29.211063
6
7 """
8
9 # revision identifiers, used by Alembic.
10 revision = '3772f8f828da'
11 down_revision = 'c7a44a5a0851'
12
13 from alembic import op
14 import sqlalchemy as sa
15
16
17 def enum_update(table_name, column_name, enum_name, enum_values):
18     if op.get_context().bind.dialect.name != 'postgresql':
19         op.alter_column(table_name, column_name,
20                         type_=sa.Enum(*enum_values, name=enum_name))
21         return
22
23     # Postgres requires a few more steps
24     tmp = enum_name + '_tmp'
25
26     op.execute('ALTER TYPE ' + enum_name + ' RENAME TO ' + tmp)
27
28     updated = sa.Enum(*enum_values, name=enum_name)
29     updated.create(op.get_bind(), checkfirst=False)
30
31     op.execute('ALTER TABLE ' + table_name + ' ALTER COLUMN ' + column_name +
32                ' TYPE ' + enum_name + ' USING identify_by::text::' + enum_name)
33
34     op.execute('DROP TYPE ' + tmp)
35
36
37 def upgrade():
38     enum_update('ps_endpoints', 'identify_by', 'pjsip_identify_by_values',
39                 ['username', 'auth_username'])
40
41
42 def downgrade():
43     enum_update('ps_endpoints', 'identify_by', 'pjsip_identify_by_values',
44                 ['username'])