Skip to content

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

Fix setting invalid identifier (#15378)

Krzysztof Waliczek6aa2ddb77df7
diff --git a/saleor/app/management/commands/create_app.py b/saleor/app/management/commands/create_app.py
index 9de9b79728..cd1b5226e6 100644
--- a/saleor/app/management/commands/create_app.py
+++ b/saleor/app/management/commands/create_app.py
@@ -1,6 +1,7 @@
import json
from typing import Any, Optional
+import graphene
from django.core.management import BaseCommand, CommandError
from django.core.management.base import CommandParser
from django.urls import reverse
@@ -19,6 +20,15 @@ class Command(BaseCommand):
def add_arguments(self, parser: CommandParser) -> None:
parser.add_argument("name", type=str)
+ parser.add_argument(
+ "--identifier",
+ dest="identifier",
+ default="",
+ help=(
+ "Canonical app ID. If not provided, "
+ "the identifier will be generated based on app.id."
+ ),
+ )
parser.add_argument(
"--permission",
action="append",
@@ -66,9 +76,13 @@ class Command(BaseCommand):
name = options["name"]
is_active = options["activate"]
target_url = options["target_url"]
+ identifier = options["identifier"]
permissions = list(set(options["permissions"]))
permissions = clean_permissions(permissions)
- app = App.objects.create(name=name, is_active=is_active)
+ app = App.objects.create(name=name, is_active=is_active, identifier=identifier)
+ if not identifier:
+ app.identifier = graphene.Node.to_global_id("App", app.pk)
+ app.save()
app.permissions.set(permissions)
_, auth_token = app.tokens.create() # type: ignore[call-arg] # method of a related manager # noqa: E501
data = {
diff --git a/saleor/app/migrations/0028_set_identifier.py b/saleor/app/migrations/0028_set_identifier.py
new file mode 100644
index 0000000000..1323fc7425
--- /dev/null
+++ b/saleor/app/migrations/0028_set_identifier.py
@@ -0,0 +1,82 @@
+import graphene
+from django.db import migrations
+from django.db.models import Q
+
+# Fixes the issue from previous identifier changes, where `app_create` was setting
+# identifier based on NULL PK
+# Need to rerun migration task from migration 0027 cause of missing
+# setting up `identifier` on create_app command
+
+APP_BATCH_SIZE = 100
+TRANSACTION_BATCH_SIZE = 10000
+
+
+def set_local_app_identifier_in_transaction_item(apps, _schema_editor):
+ TransactionItem = apps.get_model("payment", "TransactionItem")
+ none_global_id = graphene.Node.to_global_id("App", None)
+
+ while True:
+ transaction_items = TransactionItem.objects.filter(
+ Q(app_id__isnull=False)
+ & (Q(app_identifier__isnull=True) | Q(app_identifier=none_global_id))
+ )[:TRANSACTION_BATCH_SIZE]
+ if not transaction_items:
+ break
+
+ for transaction_item in transaction_items:
+ transaction_item.app_identifier = graphene.Node.to_global_id(
+ "App", transaction_item.app_id
+ )
+ TransactionItem.objects.bulk_update(transaction_items, ["app_identifier"])
+
+
+def set_local_app_identifier_in_transaction_event(apps, _schema_editor):
+ TransactionEvent = apps.get_model("payment", "TransactionEvent")
+ none_global_id = graphene.Node.to_global_id("App", None)
+
+ while True:
+ transaction_events = TransactionEvent.objects.filter(
+ Q(app_id__isnull=False)
+ & (Q(app_identifier__isnull=True) | Q(app_identifier=none_global_id))
+ )[:TRANSACTION_BATCH_SIZE]
+
+ if not transaction_events:
+ break
+
+ for event in transaction_events:
+ event.app_identifier = graphene.Node.to_global_id("App", event.app_id)
+
+ TransactionEvent.objects.bulk_update(transaction_events, ["app_identifier"])
+
+
+def set_identifier_for_local_apps(apps, _schema_editor):
+ App = apps.get_model("app", "App")
+ none_global_id = graphene.Node.to_global_id("App", None)
+
+ while True:
+ apps = App.objects.filter(
+ Q(identifier__isnull=True) | Q(identifier=none_global_id)
+ )[:APP_BATCH_SIZE]
+ if not apps:
+ break
+
+ for app in apps:
+ app.identifier = graphene.Node.to_global_id("App", app.pk)
+
+ App.objects.bulk_update(apps, ["identifier"])
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("app", "0027_set_identifier_when_missing"),
+ ]
+
+ operations = [
+ migrations.RunPython(set_identifier_for_local_apps, migrations.RunPython.noop),
+ migrations.RunPython(
+ set_local_app_identifier_in_transaction_item, migrations.RunPython.noop
+ ),
+ migrations.RunPython(
+ set_local_app_identifier_in_transaction_event, migrations.RunPython.noop
+ ),
+ ]
diff --git a/saleor/app/tests/test_app_commands.py b/saleor/app/tests/test_app_commands.py
index 711a3b32ed..94ce4d4a07 100644
--- a/saleor/app/tests/test_app_commands.py
+++ b/saleor/app/tests/test_app_commands.py
@@ -1,5 +1,6 @@
from unittest.mock import ANY, Mock, call
+import graphene
import pytest
from django.core.management import call_command
from django.forms import ValidationError
@@ -116,6 +117,7 @@ def test_creates_app_object():
tokens = app.tokens.all()
assert len(tokens) == 1
assert app.uuid is not None
+ assert app.identifier == graphene.Node.to_global_id("App", app.id)
def test_app_has_all_required_permissions():
@@ -157,3 +159,22 @@ def test_sends_data_to_target_url(monkeypatch):
json={"auth_token": ANY},
allow_redirects=False,
)
+
+
+def test_creates_app_with_identifier():
+ # given
+ name = "Single App"
+ permissions = ["MANAGE_USERS", "MANAGE_ORDERS"]
+
+ # when
+ call_command("create_app", name, permission=permissions, identifier="test.test")
+
+ # then
+ apps = App.objects.filter(name=name)
+ assert len(apps) == 1
+
+ app = apps[0]
+ tokens = app.tokens.all()
+ assert len(tokens) == 1
+ assert app.uuid is not None
+ assert app.identifier == "test.test"
diff --git a/saleor/graphql/app/mutations/app_create.py b/saleor/graphql/app/mutations/app_create.py
index 314a4ed9c7..f018a8b8ab 100644
--- a/saleor/graphql/app/mutations/app_create.py
+++ b/saleor/graphql/app/mutations/app_create.py
@@ -84,6 +84,7 @@ class AppCreate(ModelMutation):
@classmethod
def save(cls, info, instance, cleaned_input):
+ instance.save()
instance.identifier = graphene.Node.to_global_id("App", instance.pk)
instance.save()
_, auth_token = instance.tokens.create(name="Default")
diff --git a/saleor/graphql/app/tests/mutations/test_app_create.py b/saleor/graphql/app/tests/mutations/test_app_create.py
index 55b06c9303..416d6234ef 100644
--- a/saleor/graphql/app/tests/mutations/test_app_create.py
+++ b/saleor/graphql/app/tests/mutations/test_app_create.py
@@ -70,6 +70,7 @@ def test_app_create_mutation(
assert default_token
assert default_token[-4:] == app.tokens.get().token_last_4
assert app.uuid is not None
+ assert app.identifier == graphene.Node.to_global_id("App", app.pk)
@freeze_time("2022-05-12 12:00:00")