From 7573e9ab4145294a7f2385f55144bdd1aaa99f49 Mon Sep 17 00:00:00 2001 From: coolneng Date: Thu, 17 Sep 2020 17:32:53 +0200 Subject: [PATCH] Set unique constraints for email and mobile --- ...714f8b_set_unique_fields_in_users_table.py | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 migrations/versions/9ee45f714f8b_set_unique_fields_in_users_table.py 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