alembic: Add support for MS-SQL
authorFlorian Floimair <f.floimair@commend.com>
Tue, 5 Sep 2017 16:13:19 +0000 (18:13 +0200)
committerKevin Harwell <kharwell@digium.com>
Fri, 8 Sep 2017 16:51:20 +0000 (11:51 -0500)
MS-SQL has no native Enum-type support and therefore
needs to work with constraints.
Since these constraints need unique names the suggested approach
referenced in the following alembic documentation has been applied:
http://bit.ly/2x9r8pb

ASTERISK-27255 #close

Change-Id: I8b579750dae0c549f1103ee50172644afb9b2f95

30 files changed:
contrib/ast-db-manage/config/versions/15b1430ad6f1_add_moh_passthrough_option_to_pjsip.py
contrib/ast-db-manage/config/versions/15db7b91a97a_add_rtcp_mux.py
contrib/ast-db-manage/config/versions/23530d604b96_add_rpid_immediate.py
contrib/ast-db-manage/config/versions/26d7f3bf0fa5_add_bind_rtp_to_media_address_to_pjsip.py
contrib/ast-db-manage/config/versions/28ab27a7826d_add_srv_lookups_to_identify.py
contrib/ast-db-manage/config/versions/28b8e71e541f_add_g726_non_standard.py
contrib/ast-db-manage/config/versions/2fc7930b41b3_add_pjsip_endpoint_options_for_12_1.py
contrib/ast-db-manage/config/versions/371a3bf4143e_add_user_eq_phone_option_to_pjsip.py
contrib/ast-db-manage/config/versions/3772f8f828da_update_identify_by.py
contrib/ast-db-manage/config/versions/3bcc0b5bc2c9_add_allow_reload_to_ps_transports.py
contrib/ast-db-manage/config/versions/4468b4a91372_add_pjsip_asymmetric_rtp_codec.py
contrib/ast-db-manage/config/versions/4c573e7135bd_fix_tos_field_types.py
contrib/ast-db-manage/config/versions/5139253c0423_make_q_member_uniqueid_autoinc.py
contrib/ast-db-manage/config/versions/51f8cb66540e_add_further_dtls_options.py
contrib/ast-db-manage/config/versions/5950038a6ead_fix_pjsip_verifiy_typo.py
contrib/ast-db-manage/config/versions/837aa67461fb_ps_contacts_add_authenticate_qualify.py
contrib/ast-db-manage/config/versions/86bb1efa278d_add_ps_endpoints_refer_blind_progress.py
contrib/ast-db-manage/config/versions/8d478ab86e29_pjsip_add_disable_multi_domain.py
contrib/ast-db-manage/config/versions/8fce4c573e15_add_pjsip_allow_overlap.py
contrib/ast-db-manage/config/versions/945b1098bdd_add_media_encryption_optimistic_to_pjsip.py
contrib/ast-db-manage/config/versions/a6ef36f1309_ps_globals_add_ignore_uri_user_options.py
contrib/ast-db-manage/config/versions/b83645976fdd_add_dtls_fingerprint_to_ps_endpoints.py
contrib/ast-db-manage/config/versions/c7a44a5a0851_pjsip_add_global_mwi_options.py
contrib/ast-db-manage/config/versions/d7983954dd96_add_ps_endpoints_notify_early_inuse_.py
contrib/ast-db-manage/config/versions/dbc44d5a908_add_missing_columns_to_sys_and_reg.py
contrib/ast-db-manage/config/versions/e96a0b8071c_increase_pjsip_column_size.py
contrib/ast-db-manage/config/versions/ef7efc2d3964_ps_contacts_add_endpoint_and_modify_.py
contrib/ast-db-manage/config/versions/f3d1c5d38b56_add_prune_on_boot.py
contrib/ast-db-manage/config/versions/f638dbe2eb23_symmetric_transport.py
contrib/ast-db-manage/env.py

index 725238c..2456dac 100644 (file)
@@ -27,5 +27,7 @@ def upgrade():
     op.add_column('ps_endpoints', sa.Column('moh_passthrough', yesno_values))
 
 def downgrade():
+    if op.get_context().bind.dialect.name == 'mssql':
+        op.drop_constraint('ck_ps_endpoints_moh_passthrough_yesno_values','ps_endpoints')
     with op.batch_alter_table('ps_endpoints') as batch_op:
         batch_op.drop_column('moh_passthrough')
index 8b0214a..de9e10a 100644 (file)
@@ -28,4 +28,6 @@ def upgrade():
 
 
 def downgrade():
+    if op.get_context().bind.dialect.name == 'mssql':
+        op.drop_constraint('ck_ps_endpoints_rtcp_mux_yesno_values','ps_endpoints')
     op.drop_column('ps_endpoints', 'rtcp_mux')
index dc0c01c..b3ffaee 100644 (file)
@@ -45,4 +45,6 @@ def upgrade():
     op.add_column('ps_endpoints', sa.Column('rpid_immediate', yesno_values))
 
 def downgrade():
+    if op.get_context().bind.dialect.name == 'mssql':
+        op.drop_constraint('ck_ps_endpoints_rpid_immediate_yesno_values','ps_endpoints')
     op.drop_column('ps_endpoints', 'rpid_immediate')
index e7c11da..ce2ac1e 100644 (file)
@@ -28,4 +28,6 @@ def upgrade():
 
 
 def downgrade():
+    if op.get_context().bind.dialect.name == 'mssql':
+        op.drop_constraint('ck_ps_endpoints_bind_rtp_to_media_address_yesno_values','ps_endpoints')
     op.drop_column('ps_endpoints', 'bind_rtp_to_media_address')
index 8831e20..9325da3 100644 (file)
@@ -28,4 +28,6 @@ def upgrade():
 
 
 def downgrade():
+    if op.get_context().bind.dialect.name == 'mssql':
+        op.drop_constraint('ck_ps_endpoint_id_ips_srv_lookups_yesno_values','ps_endpoint_id_ips')
     op.drop_column('ps_endpoint_id_ips', 'srv_lookups')
index ad36bd9..08e2e3f 100644 (file)
@@ -27,4 +27,6 @@ def upgrade():
 
 
 def downgrade():
+    if op.get_context().bind.dialect.name == 'mssql':
+        op.drop_constraint('ck_ps_endpoints_g726_non_standard_yesno_values','ps_endpoints')
     op.drop_column('ps_endpoints', 'g726_non_standard')
index d39ddb4..3ca16fe 100644 (file)
@@ -142,6 +142,8 @@ def upgrade():
 def downgrade():
     ########################## drop columns ##########################
 
+    if op.get_context().bind.dialect.name == 'mssql':
+        op.drop_constraint('ck_ps_aors_support_path_yesno_values', 'ps_aors')
     op.drop_column('ps_aors', 'support_path')
     op.drop_column('ps_aors', 'outbound_proxy')
     op.drop_column('ps_aors', 'maximum_expiration')
@@ -153,6 +155,8 @@ def downgrade():
         new_column_name='mwi_fromuser', existing_type=sa.String(40))
 
     op.drop_column('ps_endpoints', 'set_var')
+    if op.get_context().bind.dialect.name == 'mssql':
+        op.drop_constraint('ck_ps_endpoints_redirect_method_pjsip_redirect_method_values', 'ps_endpoints')
     op.drop_column('ps_endpoints', 'redirect_method')
     op.drop_column('ps_endpoints', 'media_address')
 
index 145d6be..29558ce 100644 (file)
@@ -27,4 +27,6 @@ def upgrade():
     op.add_column('ps_endpoints', sa.Column('user_eq_phone', yesno_values))
 
 def downgrade():
+    if op.get_context().bind.dialect.name == 'mssql':
+        op.drop_constraint('ck_ps_endpoints_user_eq_phone_yesno_values','ps_endpoints')
     op.drop_column('ps_endpoints', 'user_eq_phone')
index 92695b0..3c80f48 100644 (file)
@@ -16,6 +16,8 @@ import sqlalchemy as sa
 
 def enum_update(table_name, column_name, enum_name, enum_values):
     if op.get_context().bind.dialect.name != 'postgresql':
+        if op.get_context().bind.dialect.name == 'mssql':
+            op.drop_constraint('ck_ps_endpoints_identify_by_pjsip_identify_by_values', 'ps_endpoints')
         op.alter_column(table_name, column_name,
                         type_=sa.Enum(*enum_values, name=enum_name))
         return
index 0becc1e..093a5d4 100644 (file)
@@ -22,4 +22,6 @@ def upgrade():
     op.add_column('ps_transports', sa.Column('allow_reload', yesno_values))
 
 def downgrade():
+    if op.get_context().bind.dialect.name == 'mssql':
+        op.drop_constraint('ck_ps_transports_allow_reload_yesno_values','ps_transports')
     op.drop_column('ps_transports', 'allow_reload')
index c121495..1a8ede6 100644 (file)
@@ -28,4 +28,6 @@ def upgrade():
 
 
 def downgrade():
+    if op.get_context().bind.dialect.name == 'mssql':
+        op.drop_constraint('ck_ps_endpoints_asymmetric_rtp_codec_yesno_values','ps_endpoints')
     op.drop_column('ps_endpoints', 'asymmetric_rtp_codec')
index 17906ae..0821164 100644 (file)
@@ -28,7 +28,7 @@ def upgrade():
     op.alter_column('ps_transports', 'tos', type_=sa.String(10))
 
     # Can't cast YENO_VALUES to Integers, so dropping and adding is required
-    op.drop_column('ps_transports', 'cos')
+    op.drop_column('ps_transports', 'cos', schema=None, mssql_drop_check=True)
     op.add_column('ps_transports', sa.Column('cos', sa.Integer))
 
 def downgrade():
@@ -45,6 +45,8 @@ def downgrade():
     op.add_column('ps_endpoints', sa.Column('cos_audio', yesno_values))
     op.add_column('ps_endpoints', sa.Column('cos_video', yesno_values))
 
+    if op.get_context().bind.dialect.name == 'mssql':
+        op.drop_constraint('ck_ps_transports_tos_yesno_values', 'ps_transports')
     op.drop_column('ps_transports', 'tos')
     op.add_column('ps_transports', sa.Column('tos', yesno_values))
     # Can't cast integers to YESNO_VALUES, so dropping and adding is required
index 72f5ee1..7efd8b2 100644 (file)
@@ -38,7 +38,7 @@ def upgrade():
     op.drop_column('queue_members', 'uniqueid')
     op.add_column('queue_members', sa.Column(name='uniqueid', type_=sa.Integer,
                                              nullable=False, unique=True))
-    # The postgres backend does not like the autoincrement needed for
+    # The postgres and mssql backends do not like the autoincrement needed for
     # mysql here.  It is just the backend that is giving a warning and
     # not the database itself.
     op.alter_column(table_name='queue_members', column_name='uniqueid',
@@ -49,5 +49,7 @@ def upgrade():
 def downgrade():
     # Was unable to find a way to use op.alter_column() to remove the
     # unique index property.
+    if op.get_context().bind.dialect.name == 'mssql':
+        op.drop_constraint('uq_queue_members_uniqueid', 'queue_members')
     op.drop_column('queue_members', 'uniqueid')
     op.add_column('queue_members', sa.Column(name='uniqueid', type_=sa.String(80), nullable=False))
index c2dacda..2eea713 100644 (file)
@@ -28,5 +28,8 @@ def upgrade():
     op.add_column('ps_endpoints', sa.Column('media_use_received_transport', yesno_values))
 
 def downgrade():
+    if op.get_context().bind.dialect.name == 'mssql':
+        op.drop_constraint('ck_ps_endpoints_force_avp_yesno_values', 'ps_endpoints')
+        op.drop_constraint('ck_ps_endpoints_media_use_received_transport_yesno_values', 'ps_endpoints')
     op.drop_column('ps_endpoints', 'force_avp')
     op.drop_column('ps_endpoints', 'media_use_received_transport')
index 28ebc8b..a1154a3 100644 (file)
@@ -11,19 +11,32 @@ revision = '5950038a6ead'
 down_revision = 'd39508cb8d8'
 
 from alembic import op
+import sqlalchemy as sa
 from sqlalchemy.dialects.postgresql import ENUM
 
 YESNO_NAME = 'yesno_values'
 YESNO_VALUES = ['yes', 'no']
 
-
 def upgrade():
     yesno_values = ENUM(*YESNO_VALUES, name=YESNO_NAME, create_type=False)
-    op.alter_column('ps_transports', 'verifiy_server', type_=yesno_values,
-                    new_column_name='verify_server')
+
+    if op.get_context().bind.dialect.name != 'mssql':
+        op.alter_column('ps_transports', 'verifiy_server', type_=yesno_values,
+                        new_column_name='verify_server')
+    else:
+        op.alter_column('ps_transports', 'verifiy_server', existing_type=yesno_values, type_=sa.String(3),
+                        new_column_name='verify_server')
+        yesno_values = ENUM(*YESNO_VALUES, name=YESNO_NAME, create_type=True)
+        op.alter_column('ps_transports', 'verify_server', existing_type=sa.String(3), type_=yesno_values)
 
 
 def downgrade():
     yesno_values = ENUM(*YESNO_VALUES, name=YESNO_NAME, create_type=False)
-    op.alter_column('ps_transports', 'verify_server', type_=yesno_values,
-                    new_column_name='verifiy_server')
+    if op.get_context().bind.dialect.name != 'mssql':
+        op.alter_column('ps_transports', 'verify_server', type_=yesno_values,
+                        new_column_name='verifiy_server')
+    else:
+        op.alter_column('ps_transports', 'verify_server', existing_type=yesno_values, type_=sa.String(3),
+                        new_column_name='verifiy_server')
+        yesno_values = ENUM(*YESNO_VALUES, name=YESNO_NAME, create_type=True)
+        op.alter_column('ps_transports', 'verifiy_server', existing_type=sa.String(3), type_=yesno_values)
index 76faf39..033a999 100644 (file)
@@ -28,5 +28,7 @@ def upgrade():
 
 
 def downgrade():
+    if op.get_context().bind.dialect.name == 'mssql':
+        op.drop_constraint('ck_ps_contacts_authenticate_qualify_yesno_values','ps_contacts')
     op.drop_column('ps_contacts', 'authenticate_qualify')
 
index 9b0f6d4..a46ed0c 100644 (file)
@@ -27,4 +27,6 @@ def upgrade():
     op.add_column('ps_endpoints', sa.Column('refer_blind_progress', yesno_values))
 
 def downgrade():
+    if op.get_context().bind.dialect.name == 'mssql':
+        op.drop_constraint('ck_ps_endpoints_refer_blind_progress_yesno_values','ps_endpoints')
     op.drop_column('ps_endpoints', 'refer_blind_progress')
index a782685..a8d7b48 100644 (file)
@@ -28,4 +28,6 @@ def upgrade():
 
 
 def downgrade():
+    if op.get_context().bind.dialect.name == 'mssql':
+        op.drop_constraint('ck_ps_globals_disable_multi_domain_yesno_values','ps_globals')
     op.drop_column('ps_globals', 'disable_multi_domain')
index 24057ec..539944e 100644 (file)
@@ -28,4 +28,6 @@ def upgrade():
 
 
 def downgrade():
+    if op.get_context().bind.dialect.name == 'mssql':
+        op.drop_constraint('ck_ps_endpoints_allow_overlap_yesno_values','ps_endpoints')
     op.drop_column('ps_endpoints', 'allow_overlap')
index 7a463f0..61ec272 100644 (file)
@@ -28,4 +28,6 @@ def upgrade():
 
 
 def downgrade():
+    if op.get_context().bind.dialect.name == 'mssql':
+        op.drop_constraint('ck_ps_endpoints_media_encryption_optimistic_yesno_values','ps_endpoints')
     op.drop_column('ps_endpoints', 'media_encryption_optimistic')
index 1556521..55d3d38 100644 (file)
@@ -28,5 +28,7 @@ def upgrade():
 
 
 def downgrade():
+    if op.get_context().bind.dialect.name == 'mssql':
+        op.drop_constraint('ck_ps_globals_ignore_uri_user_options_yesno_values','ps_globals')
     op.drop_column('ps_globals', 'ignore_uri_user_options')
 
index 3d4f74d..42b522c 100644 (file)
@@ -31,6 +31,8 @@ def upgrade():
 def downgrade():
     context = op.get_context()
 
+    if context.bind.dialect.name == 'mssql':
+        op.drop_constraint('ck_ps_endpoints_dtls_fingerprint_sha_hash_values', 'ps_endpoints')
     op.drop_column('ps_endpoints', 'dtls_fingerprint')
 
     if context.bind.dialect.name == 'postgresql':
index d3efa22..c762fc4 100644 (file)
@@ -32,4 +32,6 @@ def upgrade():
 def downgrade():
     op.drop_column('ps_globals', 'mwi_tps_queue_high')
     op.drop_column('ps_globals', 'mwi_tps_queue_low')
+    if op.get_context().bind.dialect.name == 'mssql':
+        op.drop_constraint('ck_ps_globals_mwi_disable_initial_unsolicited_yesno_values','ps_globals')
     op.drop_column('ps_globals', 'mwi_disable_initial_unsolicited')
index e1dcdd1..a5419a1 100644 (file)
@@ -27,4 +27,6 @@ def upgrade():
     op.add_column('ps_endpoints', sa.Column('notify_early_inuse_ringing', yesno_values))
 
 def downgrade():
+    if op.get_context().bind.dialect.name == 'mssql':
+        op.drop_constraint('ck_ps_endpoints_notify_early_inuse_ringing_yesno_values', 'ps_endpoints')
     op.drop_column('ps_endpoints', 'notify_early_inuse_ringing')
index 8aa16f1..9acd4a0 100644 (file)
@@ -29,6 +29,9 @@ def upgrade():
     op.add_column('ps_registrations', sa.Column('endpoint', sa.String(40)))
 
 def downgrade():
+    if op.get_context().bind.dialect.name == 'mssql':
+        op.drop_constraint('ck_ps_systems_disable_tcp_switch_yesno_values','ps_systems')
+        op.drop_constraint('ck_ps_registrations_line_yesno_values','ps_registrations')
     op.drop_column('ps_systems', 'disable_tcp_switch')
     op.drop_column('ps_registrations', 'line')
     op.drop_column('ps_registrations', 'endpoint')
index 9c00c01..da103b1 100644 (file)
@@ -28,7 +28,13 @@ def upgrade():
 def downgrade():
     op.alter_column('ps_globals', 'user_agent', type_=sa.String(40))
 
-    op.alter_column('ps_contacts', 'id', type_=sa.String(40))
+    if op.get_context().bind.dialect.name != 'mssql':
+        op.alter_column('ps_contacts', 'id', type_=sa.String(40))
+    else:
+        op.drop_constraint('uq_ps_contacts_id', 'ps_contacts')
+        op.drop_index('ps_contacts_id', 'ps_contacts')
+        op.alter_column('ps_contacts', 'id', type_=sa.String(40))
+        op.create_index('ps_contacts_id', 'ps_contacts', ['id'])
     op.alter_column('ps_contacts', 'uri', type_=sa.String(40))
     op.alter_column('ps_contacts', 'user_agent', type_=sa.String(40))
 
index 43d4028..0ac63c8 100644 (file)
@@ -27,7 +27,11 @@ def upgrade():
     op.create_index('ps_contacts_qualifyfreq_exp', 'ps_contacts', ['qualify_frequency', 'expiration_time'])
     op.create_index('ps_aors_qualifyfreq_contact', 'ps_aors', ['qualify_frequency', 'contact'])
 def downgrade():
-    op.drop_index('ps_aors_qualifyfreq_contact')
-    op.drop_index('ps_contacts_qualifyfreq_exp')
+    if op.get_context().bind.dialect.name != 'mssql':
+        op.drop_index('ps_aors_qualifyfreq_contact')
+        op.drop_index('ps_contacts_qualifyfreq_exp')
+    else:
+        op.drop_index('ps_aors_qualifyfreq_contact', table_name='ps_aors')
+        op.drop_index('ps_contacts_qualifyfreq_exp', table_name='ps_contacts')
     op.drop_column('ps_contacts', 'endpoint')
     op.alter_column('ps_contacts', 'expiration_time', type_=sa.String(40))
index bea1673..179efba 100644 (file)
@@ -28,4 +28,6 @@ def upgrade():
 
 
 def downgrade():
+    if op.get_context().bind.dialect.name == 'mssql':
+        op.drop_constraint('ck_ps_contacts_prune_on_boot_yesno_values', 'ps_contacts')
     op.drop_column('ps_contacts', 'prune_on_boot')
index 51b5066..0585aa5 100644 (file)
@@ -29,4 +29,6 @@ def upgrade():
 
 def downgrade():
     op.drop_column('ps_subscription_persistence', 'contact_uri')
+    if op.get_context().bind.dialect.name == 'mssql':
+        op.drop_constraint('ck_ps_transports_symmetric_transport_yesno_values','ps_transports')
     op.drop_column('ps_transports', 'symmetric_transport')
index a903451..1864239 100644 (file)
@@ -2,7 +2,8 @@ from __future__ import with_statement
 from alembic import context
 from alembic.script import ScriptDirectory
 from alembic.operations import Operations
-from sqlalchemy import engine_from_config, pool
+from sqlalchemy import engine_from_config, pool, MetaData
+from sqlalchemy.ext.declarative import declarative_base
 from logging.config import fileConfig
 import logging
 
@@ -17,12 +18,24 @@ try:
 except:
     pass
 
+## below block is needed for mssql
+meta = MetaData(naming_convention = {
+       "ix": 'ix_%(column_0_label)s',
+       "uq": "uq_%(table_name)s_%(column_0_name)s",
+       "ck": "ck_%(table_name)s_%(column_0_name)s_%(constraint_name)s",
+       "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
+       "pk": "pk_%(table_name)s"
+})
+Base = declarative_base(metadata=meta)
+
 logger = logging.getLogger('alembic.runtime.setup')
 # add your model's MetaData object here
 # for 'autogenerate' support
 # from myapp import mymodel
 # target_metadata = mymodel.Base.metadata
 target_metadata = None
+#Comment above line and uncomment below line for mssql
+#target_metadata = Base.metadata
 
 # other values from the config, defined by the needs of env.py,
 # can be acquired:
@@ -42,7 +55,7 @@ def run_migrations_offline():
 
     """
     url = config.get_main_option("sqlalchemy.url")
-    context.configure(url=url)
+    context.configure(url=url,target_metadata=target_metadata)
 
     with context.begin_transaction():
         context.run_migrations()