contrib/ast-db-manage: Add Postgres ENUM type support in auto DTMF mode update
[asterisk/asterisk.git] / contrib / ast-db-manage / config / versions / 31cd4f4891ec_add_auto_dtmf_mode.py
1 """Add auto DTMF mode
2
3 Revision ID: 31cd4f4891ec
4 Revises: 23530d604b96
5 Create Date: 2015-04-10 12:36:51.619419
6
7 """
8
9 # revision identifiers, used by Alembic.
10 revision = '31cd4f4891ec'
11 down_revision = '23530d604b96'
12
13 from alembic import op
14 from sqlalchemy.dialects.postgresql import ENUM
15 import sqlalchemy as sa
16
17 OLD_ENUM = ['rfc4733', 'inband', 'info']
18 NEW_ENUM = ['rfc4733', 'inband', 'info', 'auto']
19
20 old_type = sa.Enum(*OLD_ENUM, name='pjsip_dtmf_mode_values')
21 new_type = sa.Enum(*NEW_ENUM, name='pjsip_dtmf_mode_values_v2')
22
23 tcr = sa.sql.table('ps_endpoints', sa.Column('dtmf_mode', new_type,
24                    nullable=True))
25
26 def upgrade():
27     context = op.get_context()
28
29     # Upgrading to this revision WILL clear your directmedia values.
30     if context.bind.dialect.name != 'postgresql':
31         op.alter_column('ps_endpoints', 'dtmf_mode',
32                         type_=new_type,
33                         existing_type=old_type)
34     else:
35         enum = ENUM('rfc4733', 'inband', 'info', 'auto',
36                     name='pjsip_dtmf_mode_values_v2')
37         enum.create(op.get_bind(), checkfirst=False)
38
39         op.execute('ALTER TABLE ps_endpoints ALTER COLUMN dtmf_mode TYPE'
40                    ' pjsip_dtmf_mode_values_v2 USING'
41                    ' dtmf_mode::text::pjsip_dtmf_mode_values_v2')
42
43         ENUM(name="pjsip_dtmf_mode_values").drop(op.get_bind(), checkfirst=False)
44
45 def downgrade():
46     context = op.get_context()
47
48     op.execute(tcr.update().where(tcr.c.directmedia==u'outgoing')
49                .values(directmedia=None))
50
51     if context.bind.dialect.name != 'postgresql':
52         op.alter_column('ps_endpoints', 'dtmf_mode',
53                         type_=old_type,
54                         existing_type=new_type)
55     else:
56         enum = ENUM('rfc4733', 'inband', 'info',
57                     name='pjsip_dtmf_mode_values')
58         enum.create(op.get_bind(), checkfirst=False)
59
60         op.execute('ALTER TABLE ps_endpoints ALTER COLUMN dtmf_mode TYPE'
61                    ' pjsip_dtmf_mode_values USING'
62                    ' dtmf_mode::text::pjsip_dtmf_mode_values')
63
64         ENUM(name="pjsip_dtmf_mode_values_v2").drop(op.get_bind(), checkfirst=False)