fd1b6c7d40b67d9ae4ffe219e7133ee7ea2cfb6f
[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 import sqlalchemy as sa
15
16 OLD_ENUM = ['rfc4733', 'inband', 'info']
17 NEW_ENUM = ['rfc4733', 'inband', 'info', 'auto']
18
19 old_type = sa.Enum(*OLD_ENUM, name='pjsip_dtmf_mode_values')
20 new_type = sa.Enum(*NEW_ENUM, name='pjsip_dtmf_mode_values_v2')
21
22 tcr = sa.sql.table('ps_endpoints', sa.Column('dtmf_mode', new_type,
23                    nullable=True))
24
25 def upgrade():
26     context = op.get_context()
27
28     # Upgrading to this revision WILL clear your directmedia values.
29     if context.bind.dialect.name != 'postgresql':
30         op.alter_column('ps_endpoints', 'dtmf_mode',
31                         type_=new_type,
32                         existing_type=old_type)
33     else:
34         enum = ENUM('rfc4733', 'inband', 'info', 'auto',
35                     name='pjsip_dtmf_mode_values_v2')
36         enum.create(op.get_bind(), checkfirst=False)
37
38         op.execute('ALTER TABLE ps_endpoints ALTER COLUMN dtmf_mode TYPE'
39                    ' pjsip_dtmf_mode_values_v2 USING'
40                    ' dtmf_mode::text::pjsip_dtmf_mode_values_v2')
41
42         ENUM(name="pjsip_dtmf_mode_values").drop(op.get_bind(), checkfirst=False)
43
44 def downgrade():
45     context = op.get_context()
46
47     op.execute(tcr.update().where(tcr.c.directmedia==u'outgoing')
48                .values(directmedia=None))
49
50     if context.bind.dialect.name != 'postgresql':
51         op.alter_column('ps_endpoints', 'dtmf_mode',
52                         type_=old_type,
53                         existing_type=new_type)
54     else:
55         enum = ENUM('rfc4733', 'inband', 'info',
56                     name='pjsip_dtmf_mode_values')
57         enum.create(op.get_bind(), checkfirst=False)
58
59         op.execute('ALTER TABLE ps_endpoints ALTER COLUMN dtmf_mode TYPE'
60                    ' pjsip_dtmf_mode_values USING'
61                    ' dtmf_mode::text::pjsip_dtmf_mode_values')
62
63         ENUM(name="pjsip_dtmf_mode_values_v2").drop(op.get_bind(), checkfirst=False)