alembic: script modifications due to errors
[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 import sqlalchemy as sa
15
16 YESNO_NAME = 'yesno_values'
17 YESNO_VALUES = ['yes', 'no']
18
19 PJSIP_REDIRECT_METHOD_NAME = 'pjsip_redirect_method_values'
20 PJSIP_REDIRECT_METHOD_VALUES = ['user', 'uri_core', 'uri_pjsip']
21
22 PJSIP_TRANSPORT_METHOD_NAME = 'pjsip_transport_method_values'
23 PJSIP_TRANSPORT_METHOD_VALUES = ['default', 'unspecified', 'tlsv1', 'sslv2',
24                                  'sslv3', 'sslv23']
25
26 PJSIP_TRANSPORT_PROTOCOL_NAME = 'pjsip_transport_protocol_values'
27 PJSIP_TRANSPORT_PROTOCOL_VALUES = ['udp', 'tcp', 'tls', 'ws', 'wss']
28
29 def create_enum(name, check_first, *args):
30     """Create an enumeration with the given name."""
31     res = sa.Enum(*args, name=name)
32     res.create(op.get_bind(), checkfirst=check_first)
33     return res
34
35 def drop_enum(name):
36     """Drop the named enumeration from the database."""
37     sa.Enum(name=name).drop(op.get_bind(), checkfirst=False)
38
39 def upgrade():
40     ############################# Enums ##############################
41
42     yesno_values = sa.Enum(*YESNO_VALUES, name=YESNO_NAME)
43
44     # for some reason when using 'add_column' if you don't create the enum
45     # first it will think it already exists and fail
46     pjsip_redirect_method_values = sa.Enum(
47         *PJSIP_REDIRECT_METHOD_VALUES, name=PJSIP_REDIRECT_METHOD_NAME)
48     pjsip_redirect_method_values.create(op.get_bind(), checkfirst=True)
49
50     pjsip_transport_method_values = sa.Enum(
51         *PJSIP_TRANSPORT_METHOD_VALUES, name=PJSIP_TRANSPORT_METHOD_NAME)
52
53     pjsip_transport_protocol_values = sa.Enum(
54         *PJSIP_TRANSPORT_PROTOCOL_VALUES, name=PJSIP_TRANSPORT_PROTOCOL_NAME)
55
56     ######################### create tables ##########################
57
58     op.create_table(
59         'ps_systems',
60         sa.Column('id', sa.String(40), nullable=False, unique=True),
61         sa.Column('timer_t1', sa.Integer),
62         sa.Column('timer_b', sa.Integer),
63         sa.Column('compact_headers', yesno_values),
64         sa.Column('threadpool_initial_size', sa.Integer),
65         sa.Column('threadpool_auto_increment', sa.Integer),
66         sa.Column('threadpool_idle_timeout', sa.Integer),
67         sa.Column('threadpool_max_size', sa.Integer),
68     )
69
70     op.create_index('ps_systems_id', 'ps_systems', ['id'])
71
72     op.create_table(
73         'ps_globals',
74         sa.Column('id', sa.String(40), nullable=False, unique=True),
75         sa.Column('max_forwards', sa.Integer),
76         sa.Column('user_agent', sa.String(40)),
77         sa.Column('default_outbound_endpoint', sa.String(40)),
78     )
79
80     op.create_index('ps_globals_id', 'ps_globals', ['id'])
81
82     op.create_table(
83         'ps_transports',
84         sa.Column('id', sa.String(40), nullable=False, unique=True),
85         sa.Column('async_operations', sa.Integer),
86         sa.Column('bind', sa.String(40)),
87         sa.Column('ca_list_file', sa.String(200)),
88         sa.Column('cert_file', sa.String(200)),
89         sa.Column('cipher', sa.String(200)),
90         sa.Column('domain', sa.String(40)),
91         sa.Column('external_media_address', sa.String(40)),
92         sa.Column('external_signaling_address', sa.String(40)),
93         sa.Column('external_signaling_port', sa.Integer),
94         sa.Column('method', pjsip_transport_method_values),
95         sa.Column('local_net', sa.String(40)),
96         sa.Column('password', sa.String(40)),
97         sa.Column('priv_key_file', sa.String(200)),
98         sa.Column('protocol', pjsip_transport_protocol_values),
99         sa.Column('require_client_cert', yesno_values),
100         sa.Column('verify_client', yesno_values),
101         sa.Column('verifiy_server', yesno_values),
102         sa.Column('tos', yesno_values),
103         sa.Column('cos', yesno_values),
104     )
105
106     op.create_index('ps_transports_id', 'ps_transports', ['id'])
107
108     op.create_table(
109         'ps_registrations',
110         sa.Column('id', sa.String(40), nullable=False, unique=True),
111         sa.Column('auth_rejection_permanent', yesno_values),
112         sa.Column('client_uri', sa.String(40)),
113         sa.Column('contact_user', sa.String(40)),
114         sa.Column('expiration', sa.Integer),
115         sa.Column('max_retries', sa.Integer),
116         sa.Column('outbound_auth', sa.String(40)),
117         sa.Column('outbound_proxy', sa.String(40)),
118         sa.Column('retry_interval', sa.Integer),
119         sa.Column('forbidden_retry_interval', sa.Integer),
120         sa.Column('server_uri', sa.String(40)),
121         sa.Column('transport', sa.String(40)),
122         sa.Column('support_path', yesno_values),
123     )
124
125     op.create_index('ps_registrations_id', 'ps_registrations', ['id'])
126
127     ########################## add columns ###########################
128
129     # new columns for endpoints
130     op.add_column('ps_endpoints', sa.Column('media_address', sa.String(40)))
131     op.add_column('ps_endpoints', sa.Column('redirect_method',
132                                             pjsip_redirect_method_values))
133     op.add_column('ps_endpoints', sa.Column('set_var', sa.Text()))
134
135     # rename mwi_fromuser to mwi_from_user
136     op.alter_column('ps_endpoints', 'mwi_fromuser',
137                     new_column_name='mwi_from_user',
138                     existing_type=sa.String(40))
139
140     # new columns for contacts
141     op.add_column('ps_contacts', sa.Column('outbound_proxy', sa.String(40)))
142     op.add_column('ps_contacts', sa.Column('path', sa.Text()))
143
144     # new columns for aors
145     op.add_column('ps_aors', sa.Column('maximum_expiration', sa.Integer))
146     op.add_column('ps_aors', sa.Column('outbound_proxy', sa.String(40)))
147     op.add_column('ps_aors', sa.Column('support_path', yesno_values))
148
149 def downgrade():
150     ########################## drop columns ##########################
151
152     op.drop_column('ps_aors', 'support_path')
153     op.drop_column('ps_aors', 'outbound_proxy')
154     op.drop_column('ps_aors', 'maximum_expiration')
155
156     op.drop_column('ps_contacts', 'path')
157     op.drop_column('ps_contacts', 'outbound_proxy')
158
159     op.alter_column('ps_endpoints', 'mwi_from_user',
160                     new_column_name='mwi_fromuser',
161                     existing_type=sa.String(40))
162
163     op.drop_column('ps_endpoints', 'set_var')
164     op.drop_column('ps_endpoints', 'redirect_method')
165     op.drop_column('ps_endpoints', 'media_address')
166
167     ########################## drop tables ###########################
168
169     op.drop_table('ps_registrations')
170     op.drop_table('ps_transports')
171     op.drop_table('ps_globals')
172     op.drop_table('ps_systems')
173
174     ########################## drop enums ############################
175
176     sa.Enum(name=PJSIP_TRANSPORT_PROTOCOL_NAME).drop(
177         op.get_bind(), checkfirst=False)
178     sa.Enum(name=PJSIP_TRANSPORT_METHOD_NAME).drop(
179         op.get_bind(), checkfirst=False)
180     sa.Enum(name=PJSIP_REDIRECT_METHOD_NAME).drop(
181         op.get_bind(), checkfirst=False)