3772f8f828da_update_identify_by.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. """update_identify_by
  2. Revision ID: 3772f8f828da
  3. Revises: c7a44a5a0851
  4. Create Date: 2016-08-11 10:47:29.211063
  5. """
  6. # revision identifiers, used by Alembic.
  7. revision = '3772f8f828da'
  8. down_revision = 'c7a44a5a0851'
  9. from alembic import op
  10. import sqlalchemy as sa
  11. def enum_update(table_name, column_name, enum_name, enum_values):
  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', 'ps_endpoints')
  15. op.alter_column(table_name, column_name,
  16. type_=sa.Enum(*enum_values, name=enum_name))
  17. return
  18. # Postgres requires a few more steps
  19. tmp = enum_name + '_tmp'
  20. op.execute('ALTER TYPE ' + enum_name + ' RENAME TO ' + tmp)
  21. updated = sa.Enum(*enum_values, name=enum_name)
  22. updated.create(op.get_bind(), checkfirst=False)
  23. op.execute('ALTER TABLE ' + table_name + ' ALTER COLUMN ' + column_name +
  24. ' TYPE ' + enum_name + ' USING identify_by::text::' + enum_name)
  25. op.execute('DROP TYPE ' + tmp)
  26. def upgrade():
  27. enum_update('ps_endpoints', 'identify_by', 'pjsip_identify_by_values',
  28. ['username', 'auth_username'])
  29. def downgrade():
  30. enum_update('ps_endpoints', 'identify_by', 'pjsip_identify_by_values',
  31. ['username'])