Fix alembic script to work properly in offline mode.
[asterisk/asterisk.git] / contrib / ast-db-manage / config / versions / 2fc7930b41b3_add_pjsip_endpoint_options_for_12_1.py
1 """Add/Update tables for pjsip
2
3 Revision ID: 2fc7930b41b3
4 Revises: 581a4264e537
5 Create Date: 2014-01-14 09:23:53.923454
6
7 """
8
9 # revision identifiers, used by Alembic.
10 revision = '2fc7930b41b3'
11 down_revision = '581a4264e537'
12
13 from alembic import op
14 from alembic import context
15 import sqlalchemy as sa
16 from sqlalchemy.dialects.postgresql import ENUM
17
18 YESNO_NAME = 'yesno_values'
19 YESNO_VALUES = ['yes', 'no']
20
21 PJSIP_REDIRECT_METHOD_NAME = 'pjsip_redirect_method_values'
22 PJSIP_REDIRECT_METHOD_VALUES = ['user', 'uri_core', 'uri_pjsip']
23
24 PJSIP_TRANSPORT_METHOD_NAME = 'pjsip_transport_method_values'
25 PJSIP_TRANSPORT_METHOD_VALUES = ['default', 'unspecified', 'tlsv1', 'sslv2',
26                                  'sslv3', 'sslv23']
27
28 PJSIP_TRANSPORT_PROTOCOL_NAME = 'pjsip_transport_protocol_values'
29 PJSIP_TRANSPORT_PROTOCOL_VALUES = ['udp', 'tcp', 'tls', 'ws', 'wss']
30
31 def upgrade():
32     ############################# Enums ##############################
33
34     # yesno_values have already been created, so use postgres enum object
35     # type to get around "already created" issue - works okay with mysql
36     yesno_values = ENUM(*YESNO_VALUES, name=YESNO_NAME, create_type=False)
37
38     # for some reason when using 'add_column' if you don't create the enum
39     # first it will think it already exists and fail
40     pjsip_redirect_method_values = sa.Enum(
41         *PJSIP_REDIRECT_METHOD_VALUES, name=PJSIP_REDIRECT_METHOD_NAME)
42     check = False if context.is_offline_mode() else True
43     pjsip_redirect_method_values.create(op.get_bind(), checkfirst=check)
44
45     pjsip_transport_method_values = sa.Enum(
46         *PJSIP_TRANSPORT_METHOD_VALUES, name=PJSIP_TRANSPORT_METHOD_NAME)
47
48     pjsip_transport_protocol_values = sa.Enum(
49         *PJSIP_TRANSPORT_PROTOCOL_VALUES, name=PJSIP_TRANSPORT_PROTOCOL_NAME)
50
51     ######################### create tables ##########################
52
53     op.create_table(
54         'ps_systems',
55         sa.Column('id', sa.String(40), nullable=False, unique=True),
56         sa.Column('timer_t1', sa.Integer),
57         sa.Column('timer_b', sa.Integer),
58         sa.Column('compact_headers', yesno_values),
59         sa.Column('threadpool_initial_size', sa.Integer),
60         sa.Column('threadpool_auto_increment', sa.Integer),
61         sa.Column('threadpool_idle_timeout', sa.Integer),
62         sa.Column('threadpool_max_size', sa.Integer),
63     )
64
65     op.create_index('ps_systems_id', 'ps_systems', ['id'])
66
67     op.create_table(
68         'ps_globals',
69         sa.Column('id', sa.String(40), nullable=False, unique=True),
70         sa.Column('max_forwards', sa.Integer),
71         sa.Column('user_agent', sa.String(40)),
72         sa.Column('default_outbound_endpoint', sa.String(40)),
73     )
74
75     op.create_index('ps_globals_id', 'ps_globals', ['id'])
76
77     op.create_table(
78         'ps_transports',
79         sa.Column('id', sa.String(40), nullable=False, unique=True),
80         sa.Column('async_operations', sa.Integer),
81         sa.Column('bind', sa.String(40)),
82         sa.Column('ca_list_file', sa.String(200)),
83         sa.Column('cert_file', sa.String(200)),
84         sa.Column('cipher', sa.String(200)),
85         sa.Column('domain', sa.String(40)),
86         sa.Column('external_media_address', sa.String(40)),
87         sa.Column('external_signaling_address', sa.String(40)),
88         sa.Column('external_signaling_port', sa.Integer),
89         sa.Column('method', pjsip_transport_method_values),
90         sa.Column('local_net', sa.String(40)),
91         sa.Column('password', sa.String(40)),
92         sa.Column('priv_key_file', sa.String(200)),
93         sa.Column('protocol', pjsip_transport_protocol_values),
94         sa.Column('require_client_cert', yesno_values),
95         sa.Column('verify_client', yesno_values),
96         sa.Column('verifiy_server', yesno_values),
97         sa.Column('tos', yesno_values),
98         sa.Column('cos', yesno_values),
99     )
100
101     op.create_index('ps_transports_id', 'ps_transports', ['id'])
102
103     op.create_table(
104         'ps_registrations',
105         sa.Column('id', sa.String(40), nullable=False, unique=True),
106         sa.Column('auth_rejection_permanent', yesno_values),
107         sa.Column('client_uri', sa.String(40)),
108         sa.Column('contact_user', sa.String(40)),
109         sa.Column('expiration', sa.Integer),
110         sa.Column('max_retries', sa.Integer),
111         sa.Column('outbound_auth', sa.String(40)),
112         sa.Column('outbound_proxy', sa.String(40)),
113         sa.Column('retry_interval', sa.Integer),
114         sa.Column('forbidden_retry_interval', sa.Integer),
115         sa.Column('server_uri', sa.String(40)),
116         sa.Column('transport', sa.String(40)),
117         sa.Column('support_path', yesno_values),
118     )
119
120     op.create_index('ps_registrations_id', 'ps_registrations', ['id'])
121
122     ########################## add columns ###########################
123
124     # new columns for endpoints
125     op.add_column('ps_endpoints', sa.Column('media_address', sa.String(40)))
126     op.add_column('ps_endpoints', sa.Column('redirect_method',
127                                             pjsip_redirect_method_values))
128     op.add_column('ps_endpoints', sa.Column('set_var', sa.Text()))
129
130     # rename mwi_fromuser to mwi_from_user
131     op.alter_column('ps_endpoints', 'mwi_fromuser',
132                     new_column_name='mwi_from_user',
133                     existing_type=sa.String(40))
134
135     # new columns for contacts
136     op.add_column('ps_contacts', sa.Column('outbound_proxy', sa.String(40)))
137     op.add_column('ps_contacts', sa.Column('path', sa.Text()))
138
139     # new columns for aors
140     op.add_column('ps_aors', sa.Column('maximum_expiration', sa.Integer))
141     op.add_column('ps_aors', sa.Column('outbound_proxy', sa.String(40)))
142     op.add_column('ps_aors', sa.Column('support_path', yesno_values))
143
144 def downgrade():
145     ########################## drop columns ##########################
146
147     op.drop_column('ps_aors', 'support_path')
148     op.drop_column('ps_aors', 'outbound_proxy')
149     op.drop_column('ps_aors', 'maximum_expiration')
150
151     op.drop_column('ps_contacts', 'path')
152     op.drop_column('ps_contacts', 'outbound_proxy')
153
154     op.alter_column('ps_endpoints', 'mwi_from_user',
155                     new_column_name='mwi_fromuser',
156                     existing_type=sa.String(40))
157
158     op.drop_column('ps_endpoints', 'set_var')
159     op.drop_column('ps_endpoints', 'redirect_method')
160     op.drop_column('ps_endpoints', 'media_address')
161
162     ########################## drop tables ###########################
163
164     op.drop_table('ps_registrations')
165     op.drop_table('ps_transports')
166     op.drop_table('ps_globals')
167     op.drop_table('ps_systems')
168
169     ########################## drop enums ############################
170
171     sa.Enum(name=PJSIP_TRANSPORT_PROTOCOL_NAME).drop(
172         op.get_bind(), checkfirst=False)
173     sa.Enum(name=PJSIP_TRANSPORT_METHOD_NAME).drop(
174         op.get_bind(), checkfirst=False)
175     sa.Enum(name=PJSIP_REDIRECT_METHOD_NAME).drop(
176         op.get_bind(), checkfirst=False)