52798ad97bdf_add_pjsip_identify_by_header.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. """add pjsip identify by header
  2. Revision ID: 52798ad97bdf
  3. Revises: 20abce6d1e3c
  4. Create Date: 2018-01-08 12:16:02.782277
  5. """
  6. # revision identifiers, used by Alembic.
  7. revision = '52798ad97bdf'
  8. down_revision = '20abce6d1e3c'
  9. from alembic import op
  10. import sqlalchemy as sa
  11. def column_upgrade(table_name, column_name, enum_name):
  12. if op.get_context().bind.dialect.name != 'postgresql':
  13. if op.get_context().bind.dialect.name == 'mssql':
  14. op.drop_constraint('ck_ps_endpoints_identify_by_pjsip_identify_by_values',
  15. table_name)
  16. op.alter_column(table_name, column_name, type_=sa.String(80))
  17. return
  18. # Postgres requires a few more steps
  19. op.execute('ALTER TABLE ' + table_name + ' ALTER COLUMN ' + column_name +
  20. ' TYPE varchar(80) USING identify_by::text::' + enum_name)
  21. op.execute('DROP TYPE ' + enum_name)
  22. def column_downgrade(table_name, column_name, enum_name, enum_values):
  23. if op.get_context().bind.dialect.name != 'postgresql':
  24. op.alter_column(table_name, column_name,
  25. type_=sa.Enum(*enum_values, name=enum_name))
  26. return
  27. # Postgres requires a few more steps
  28. updated = sa.Enum(*enum_values, name=enum_name)
  29. updated.create(op.get_bind(), checkfirst=False)
  30. op.execute('ALTER TABLE ' + table_name + ' ALTER COLUMN ' + column_name +
  31. ' TYPE ' + enum_name + ' USING identify_by::text::' + enum_name)
  32. def upgrade():
  33. # The ps_endpoints identify_by column has always been a comma separated
  34. # list of enum values. This is better represented as a string anyway to
  35. # avoid database compatibility issues. Also future changes are likely
  36. # to allow loadable endpoint identifier names and negating fixed enum
  37. # benefits.
  38. column_upgrade('ps_endpoints', 'identify_by', 'pjsip_identify_by_values')
  39. def downgrade():
  40. column_downgrade('ps_endpoints', 'identify_by', 'pjsip_identify_by_values',
  41. ['username', 'auth_username', 'ip'])