Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Fix unique constraint (#16102)

Krzysztof Waliczek4e2c90387e9a
diff --git a/saleor/attribute/migrations/0040_auto_20240508_0854.py b/saleor/attribute/migrations/0040_auto_20240508_0854.py
new file mode 100644
index 0000000000..82a13f5356
--- /dev/null
+++ b/saleor/attribute/migrations/0040_auto_20240508_0854.py
@@ -0,0 +1,22 @@
+# Generated by Django 3.2.22 on 2024-05-08 08:54
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("attribute", "0039_merge_20231113_1132"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="assignedpageattributevalue",
+ name="page_uniq",
+ field=models.IntegerField(null=True),
+ ),
+ migrations.AddField(
+ model_name="assignedproductattributevalue",
+ name="product_uniq",
+ field=models.IntegerField(null=True),
+ ),
+ ]
diff --git a/saleor/attribute/migrations/0041_auto_20240508_0855.py b/saleor/attribute/migrations/0041_auto_20240508_0855.py
new file mode 100644
index 0000000000..63edd11bea
--- /dev/null
+++ b/saleor/attribute/migrations/0041_auto_20240508_0855.py
@@ -0,0 +1,59 @@
+# Generated by Django 3.2.22 on 2024-05-08 08:55
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+ atomic = False
+ dependencies = [
+ ("attribute", "0040_auto_20240508_0854"),
+ ]
+
+ operations = [
+ migrations.SeparateDatabaseAndState(
+ database_operations=[
+ migrations.RunSQL(
+ sql="""
+ CREATE UNIQUE INDEX CONCURRENTLY temp_uniq_value_page
+ ON attribute_assignedpageattributevalue
+ ("value_id", "page_uniq");
+ """,
+ reverse_sql="""
+ DROP INDEX CONCURRENTLY IF EXISTS temp_uniq_value_page
+ """,
+ ),
+ migrations.RunSQL(
+ sql="""
+ ALTER TABLE attribute_assignedpageattributevalue
+ ADD CONSTRAINT temp_uniq_value_page
+ UNIQUE USING INDEX temp_uniq_value_page;
+ """,
+ reverse_sql="""
+ ALTER TABLE attribute_assignedpageattributevalue DROP CONSTRAINT
+ IF EXISTS temp_uniq_value_page;
+ """,
+ ),
+ migrations.RunSQL(
+ sql="""
+ CREATE UNIQUE INDEX CONCURRENTLY temp_uniq_value_product
+ ON attribute_assignedproductattributevalue
+ ("value_id", "product_uniq");
+ """,
+ reverse_sql="""
+ DROP INDEX CONCURRENTLY IF EXISTS temp_uniq_value_product
+ """,
+ ),
+ migrations.RunSQL(
+ sql="""
+ ALTER TABLE attribute_assignedproductattributevalue
+ ADD CONSTRAINT temp_uniq_value_product
+ UNIQUE USING INDEX temp_uniq_value_product;
+ """,
+ reverse_sql="""
+ ALTER TABLE attribute_assignedproductattributevalue DROP CONSTRAINT
+ IF EXISTS temp_uniq_value_product;
+ """,
+ ),
+ ]
+ )
+ ]
diff --git a/saleor/attribute/migrations/0042_propagate_temporary_field.py b/saleor/attribute/migrations/0042_propagate_temporary_field.py
new file mode 100644
index 0000000000..c052b6d42d
--- /dev/null
+++ b/saleor/attribute/migrations/0042_propagate_temporary_field.py
@@ -0,0 +1,154 @@
+from django.db import connection, migrations
+
+BATCH = 1000
+
+
+def copy_product_id(_apps, _schema_name):
+ with connection.cursor() as cursor:
+ # The query will group by pair of values with the idea of getting duplicates
+ # in that pair and then copy the id to the temporary field, tricks used:
+ # min(id) - we need one pair of values with the id, we take the oldest one, but
+ # max would have also worked
+ # AVG(PRODUCT_UNIQ) - if value is not null it means we already have one value
+ # in the database with copied id to temporary field
+ result = (True,)
+ while result and result[0]:
+ cursor.execute(
+ """
+ UPDATE ATTRIBUTE_ASSIGNEDPRODUCTATTRIBUTEVALUE aa
+ SET PRODUCT_UNIQ = t.PRODUCT_ID
+ FROM (
+ SELECT MIN(id) AS id, product_id
+ FROM ATTRIBUTE_ASSIGNEDPRODUCTATTRIBUTEVALUE
+ GROUP BY PRODUCT_ID, VALUE_ID
+ HAVING AVG(PRODUCT_UNIQ) IS NULL
+ LIMIT %s
+ ) t
+ WHERE aa.id = t.id;
+ """,
+ [BATCH],
+ )
+
+ cursor.execute(
+ """
+ SELECT EXISTS(
+ SELECT 1
+ FROM ATTRIBUTE_ASSIGNEDPRODUCTATTRIBUTEVALUE
+ GROUP BY PRODUCT_ID, VALUE_ID
+ HAVING AVG(PRODUCT_UNIQ) IS NULL
+ );
+ """
+ )
+ result = cursor.fetchone()
+
+
+def remove_product_id_duplicates(_apps, _schema_name):
+ with connection.cursor() as cursor:
+ result = (True,)
+ while result and result[0]:
+ # We can remove duplicates. Using raw delete for better performance.
+ cursor.execute(
+ """
+ DELETE FROM ATTRIBUTE_ASSIGNEDPRODUCTATTRIBUTEVALUE aa
+ WHERE aa.id IN (
+ SELECT tt.id FROM ATTRIBUTE_ASSIGNEDPRODUCTATTRIBUTEVALUE tt
+ WHERE tt.product_uniq IS NULL
+ ORDER BY tt.id
+ LIMIT %s
+ );
+ """,
+ [BATCH],
+ )
+
+ cursor.execute(
+ """
+ SELECT EXISTS(
+ SELECT 1 FROM ATTRIBUTE_ASSIGNEDPRODUCTATTRIBUTEVALUE
+ WHERE product_uniq IS NULL
+ );
+ """
+ )
+
+ result = cursor.fetchone()
+
+
+def copy_page_id(_apps, _schema_name):
+ with connection.cursor() as cursor:
+ # The query will group by pair of values with the idea of getting duplicates
+ # in that pair and then copy the id to the temporary field, tricks used:
+ # min(id) - we need one pair of values with the id, we take the oldest one, but
+ # max would have also worked
+ # AVG(PAGE_UNIQ) - if value is not null it means we already have one value
+ # in the database with copied id to temporary field
+ result = (True,)
+ while result and result[0]:
+ cursor.execute(
+ """
+ UPDATE ATTRIBUTE_ASSIGNEDPAGEATTRIBUTEVALUE aa
+ SET PAGE_UNIQ = t.PAGE_ID
+ FROM (
+ SELECT MIN(id) AS id, page_id
+ FROM ATTRIBUTE_ASSIGNEDPAGEATTRIBUTEVALUE
+ GROUP BY PAGE_ID, VALUE_ID
+ HAVING AVG(PAGE_UNIQ) IS NULL
+ LIMIT %s
+ ) t
+ WHERE aa.id = t.id;
+ """,
+ [BATCH],
+ )
+
+ cursor.execute(
+ """
+ SELECT EXISTS(
+ SELECT 1
+ FROM ATTRIBUTE_ASSIGNEDPAGEATTRIBUTEVALUE
+ GROUP BY PAGE_ID, VALUE_ID
+ HAVING AVG(PAGE_UNIQ) IS NULL
+ );
+ """
+ )
+ result = cursor.fetchone()
+
+
+def remove_page_id_duplicates(_apps, _schema_name):
+ with connection.cursor() as cursor:
+ # We can remove duplicates. Using raw delete for better performance.
+ result = (True,)
+ while result and result[0]:
+ cursor.execute(
+ """
+ DELETE FROM ATTRIBUTE_ASSIGNEDPAGEATTRIBUTEVALUE aa
+ WHERE aa.id IN (
+ SELECT tt.id FROM ATTRIBUTE_ASSIGNEDPAGEATTRIBUTEVALUE tt
+ WHERE tt.page_uniq IS NULL
+ ORDER BY tt.id
+ LIMIT %s
+ );
+ """,
+ [BATCH],
+ )
+
+ cursor.execute(
+ """
+ SELECT EXISTS(
+ SELECT 1 FROM ATTRIBUTE_ASSIGNEDPAGEATTRIBUTEVALUE
+ WHERE page_uniq IS NULL
+ );
+ """
+ )
+
+ result = cursor.fetchone()
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("attribute", "0041_auto_20240508_0855"),
+ ]
+
+ operations = [
+ migrations.RunPython(copy_page_id, migrations.RunPython.noop),
+ migrations.RunPython(copy_product_id, migrations.RunPython.noop),
+ migrations.RunPython(remove_page_id_duplicates, migrations.RunPython.noop),
+ migrations.RunPython(remove_product_id_duplicates, migrations.RunPython.noop),
+ ]
diff --git a/saleor/attribute/migrations/0043_merge_20240606_1430.py b/saleor/attribute/migrations/0043_merge_20240606_1430.py
new file mode 100644
index 0000000000..5e1ad14363
--- /dev/null
+++ b/saleor/attribute/migrations/0043_merge_20240606_1430.py
@@ -0,0 +1,12 @@
+# Generated by Django 3.2.22 on 2024-06-06 14:30
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("attribute", "0040_clear_assignedattributes"),
+ ("attribute", "0042_propagate_temporary_field"),
+ ]
+
+ operations = []
diff --git a/saleor/attribute/migrations/0044_reintroduce_attribute_value_constraints.py b/saleor/attribute/migrations/0044_reintroduce_attribute_value_constraints.py
new file mode 100644
index 0000000000..c626d2a7fe
--- /dev/null
+++ b/saleor/attribute/migrations/0044_reintroduce_attribute_value_constraints.py
@@ -0,0 +1,88 @@
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+ atomic = False
+
+ dependencies = [
+ ("attribute", "0043_merge_20240606_1430"),
+ ]
+
+ operations = [
+ migrations.SeparateDatabaseAndState(
+ database_operations=[
+ migrations.RunSQL(
+ sql="""
+ CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS
+ attribute_assignedpageat_value_id_page_id_851cd501_uniq
+ ON attribute_assignedpageattributevalue
+ ("value_id", "page_id");
+ """,
+ reverse_sql="""
+ DROP INDEX CONCURRENTLY IF EXISTS
+ attribute_assignedpageat_value_id_page_id_851cd501_uniq;
+ """,
+ ),
+ migrations.RunSQL(
+ sql="""
+ DO
+ $do$
+ BEGIN
+ IF NOT EXISTS (
+ SELECT 1 FROM pg_catalog.pg_constraint
+ WHERE conname LIKE
+ 'attribute_assignedpageat_value_id_page_id_851cd501_uniq'
+ ) THEN
+ ALTER TABLE attribute_assignedpageattributevalue
+ ADD CONSTRAINT
+ attribute_assignedpageat_value_id_page_id_851cd501_uniq
+ UNIQUE USING INDEX
+ attribute_assignedpageat_value_id_page_id_851cd501_uniq;
+ END IF;
+ END
+ $do$
+ """,
+ reverse_sql="""
+ ALTER TABLE attribute_assignedpageattributevalue DROP CONSTRAINT
+ IF EXISTS attribute_assignedpageat_value_id_page_id_851cd501_uniq;
+ """,
+ ),
+ migrations.RunSQL(
+ sql="""
+ CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS
+ attribute_assignedproduc_value_id_product_id_6f6deb31_uniq
+ ON attribute_assignedproductattributevalue
+ ("value_id", "product_id");
+ """,
+ reverse_sql="""
+ DROP INDEX CONCURRENTLY IF EXISTS
+ attribute_assignedproduc_value_id_product_id_6f6deb31_uniq;
+ """,
+ ),
+ migrations.RunSQL(
+ sql="""
+ DO
+ $do$
+ BEGIN
+ IF NOT EXISTS (
+ SELECT 1 FROM pg_catalog.pg_constraint
+ WHERE conname LIKE
+ 'attribute_assignedproduc_value_id_product_id_6f6deb31_uniq'
+ ) THEN
+ ALTER TABLE attribute_assignedproductattributevalue
+ ADD CONSTRAINT
+ attribute_assignedproduc_value_id_product_id_6f6deb31_uniq
+ UNIQUE USING INDEX
+ attribute_assignedproduc_value_id_product_id_6f6deb31_uniq;
+ END IF;
+ END
+ $do$
+ """,
+ reverse_sql="""
+ ALTER TABLE attribute_assignedproductattributevalue DROP CONSTRAINT
+ IF EXISTS attribute_assignedproduc_value_id_product_id_6f6deb31_uniq;
+ """,
+ ),
+ ]
+ )
+ ]
diff --git a/saleor/attribute/migrations/0045_drop_temporary_fields.py b/saleor/attribute/migrations/0045_drop_temporary_fields.py
new file mode 100644
index 0000000000..8a3a556f3d
--- /dev/null
+++ b/saleor/attribute/migrations/0045_drop_temporary_fields.py
@@ -0,0 +1,22 @@
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("attribute", "0044_reintroduce_attribute_value_constraints"),
+ ]
+
+ operations = [
+ migrations.SeparateDatabaseAndState(
+ state_operations=[
+ migrations.RemoveField(
+ model_name="assignedpageattributevalue",
+ name="page_uniq",
+ ),
+ migrations.RemoveField(
+ model_name="assignedproductattributevalue",
+ name="product_uniq",
+ ),
+ ]
+ )
+ ]