diff --git a/migrations/versions/9ee45f714f8b_set_unique_fields_in_users_table.py b/migrations/versions/9ee45f714f8b_set_unique_fields_in_users_table.py new file mode 100644 index 0000000..2ef36b4 --- /dev/null +++ b/migrations/versions/9ee45f714f8b_set_unique_fields_in_users_table.py @@ -0,0 +1,31 @@ +"""set unique fields in users table + +Revision ID: 9ee45f714f8b +Revises: 0a4f01e489de +Create Date: 2020-09-12 18:25:17.026880 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = "9ee45f714f8b" +down_revision = "0a4f01e489de" +branch_labels = None +depends_on = None + + +def upgrade(): + fields = ["email", "mobile"] + for item in fields: + query = "DELETE FROM users WHERE email = '' AND status = 0" + op.execute(query) + with op.batch_alter_table("users") as batch_op: + batch_op.create_unique_constraint( + constraint_name="uq_users_{}".format(item), columns=[item], + ) + + +def downgrade(): + pass