Asterisk - The Open Source Telephony Project GIT-master-a358458
fe6592859b85_fix_mwi_subscribe_replaces_.py
Go to the documentation of this file.
1"""Fix mwi_subscribe_replaces_unsolicited
2
3Revision ID: fe6592859b85
4Revises: 1d3ed26d9978
5Create Date: 2018-08-06 15:50:44.405534
6
7"""
8
9# revision identifiers, used by Alembic.
10revision = 'fe6592859b85'
11down_revision = '1d3ed26d9978'
12
13from alembic import op
14import sqlalchemy as sa
15from sqlalchemy.dialects.postgresql import ENUM
16
17AST_BOOL_NAME = 'ast_bool_values'
18# We'll just ignore the n/y and f/t abbreviations as Asterisk does not write
19# those aliases.
20AST_BOOL_VALUES = [ '0', '1',
21 'off', 'on',
22 'false', 'true',
23 'no', 'yes' ]
24
25
26def upgrade():
27 # Create the new enum
28 ast_bool_values = ENUM(*AST_BOOL_VALUES, name=AST_BOOL_NAME, create_type=False)
29 if op.get_context().bind.dialect.name == 'postgresql':
30 ast_bool_values.create(op.get_bind(), checkfirst=False)
31
32 # There is no direct way to convert from Integer to ENUM that is
33 # not database specific so we transition through a string type.
34 op.alter_column('ps_endpoints', 'mwi_subscribe_replaces_unsolicited',
35 type_=sa.String(5))
36 op.alter_column('ps_endpoints', 'mwi_subscribe_replaces_unsolicited',
37 type_=ast_bool_values, postgresql_using='mwi_subscribe_replaces_unsolicited::{0}'.format(AST_BOOL_NAME))
38
39
41 # First we need to ensure the column is using only the 'numeric' bool enum values.
42 op.execute("UPDATE ps_endpoints SET mwi_subscribe_replaces_unsolicited='0'"
43 " WHERE mwi_subscribe_replaces_unsolicited='off'"
44 " OR mwi_subscribe_replaces_unsolicited='false'"
45 " OR mwi_subscribe_replaces_unsolicited='no'")
46 op.execute("UPDATE ps_endpoints SET mwi_subscribe_replaces_unsolicited='1'"
47 " WHERE mwi_subscribe_replaces_unsolicited='on'"
48 " OR mwi_subscribe_replaces_unsolicited='true'"
49 " OR mwi_subscribe_replaces_unsolicited='yes'")
50
51 # There is no direct way to convert from ENUM to Integer that is
52 # not database specific so we transition through a string type.
53 if op.get_context().bind.dialect.name == 'mssql':
54 op.drop_constraint('ck_ps_endpoints_mwi_subscribe_replaces_unsolicited_ast_bool_values', 'ps_endpoints')
55 op.alter_column('ps_endpoints', 'mwi_subscribe_replaces_unsolicited',
56 type_=sa.String(5))
57 op.alter_column('ps_endpoints', 'mwi_subscribe_replaces_unsolicited',
58 type_=sa.Integer, postgresql_using='mwi_subscribe_replaces_unsolicited::Integer')
59
60 if op.get_context().bind.dialect.name == 'postgresql':
61 ENUM(name=AST_BOOL_NAME).drop(op.get_bind(), checkfirst=False)