b1a9f8be8edda701f61d9adbca2ec7c7aa75c3f7
[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 def upgrade():
24     context = op.get_context()
25
26     # Upgrading to this revision WILL clear your directmedia values.
27     if context.bind.dialect.name == 'sqlite':
28         with op.batch_alter_table('ps_endpoints') as batch_op:
29             batch_op.alter_column('dtmf_mode', type_=new_type)
30     elif 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     if context.bind.dialect.name == 'sqlite':
49         with op.batch_alter_table('ps_endpoints') as batch_op:
50             batch_op.alter_column('dtmf_mode', type_=old_type)
51     elif 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)