Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(backend): use {schema_prefix} in raw SQL migrations instead of hardcoded 'platform.' (#12905)

Zamil Majdybe61dc430401
diff --git a/autogpt_platform/backend/backend/data/graph.py b/autogpt_platform/backend/backend/data/graph.py
index 6b25a2792c..54ba198936 100644
--- a/autogpt_platform/backend/backend/data/graph.py
+++ b/autogpt_platform/backend/backend/data/graph.py
@@ -36,9 +36,7 @@ from backend.util.models import Pagination
from backend.util.request import parse_url
from .block import BlockInput
-from .db import BaseDbModel
-from .db import prisma as db
-from .db import query_raw_with_schema, transaction
+from .db import BaseDbModel, execute_raw_with_schema, query_raw_with_schema, transaction
from .dynamic_fields import is_tool_pin, sanitize_pin_name
from .includes import AGENT_GRAPH_INCLUDE, AGENT_NODE_INCLUDE, MAX_GRAPH_VERSIONS_FETCH
from .model import CredentialsFieldInfo, CredentialsMetaInput, is_credentials_field_name
@@ -1858,15 +1856,15 @@ async def migrate_llm_models(migrate_to: LlmModel):
# Update each block
for id, path in llm_model_fields.items():
query = f"""
- UPDATE platform."AgentNode"
+ UPDATE {{schema_prefix}}"AgentNode"
SET "constantInput" = jsonb_set("constantInput", $1, to_jsonb($2), true)
WHERE "agentBlockId" = $3
AND "constantInput" ? ($4)::text
AND "constantInput"->>($4)::text NOT IN {escaped_enum_values}
"""
- await db.execute_raw(
- query, # type: ignore - is supposed to be LiteralString
+ await execute_raw_with_schema(
+ query,
[path],
migrate_to.value,
id,
diff --git a/autogpt_platform/backend/backend/data/graph_test.py b/autogpt_platform/backend/backend/data/graph_test.py
index fa985385e5..9d10ed16e2 100644
--- a/autogpt_platform/backend/backend/data/graph_test.py
+++ b/autogpt_platform/backend/backend/data/graph_test.py
@@ -2174,3 +2174,28 @@ def test_auto_credentials_non_str_non_dict_value_rejected(bad_value):
msg = errors[graph.nodes[0].id]["spreadsheet"]
# Must point the user at the correct fix (the Drive input block).
assert "AgentGoogleDriveFileInputBlock" in msg
+
+
+async def test_migrate_llm_models_uses_schema_prefix_placeholder():
+ """Regression: migrate_llm_models must use the {schema_prefix} placeholder
+ so environments where the Prisma datasource lands in `public` (no
+ ?schema=platform on DATABASE_URL) don't blow up with
+ `relation "platform.AgentNode" does not exist`."""
+ from backend.blocks.llm import LlmModel
+ from backend.data.graph import migrate_llm_models
+
+ with patch(
+ "backend.data.graph.execute_raw_with_schema",
+ new_callable=AsyncMock,
+ ) as mock_execute:
+ await migrate_llm_models(next(iter(LlmModel)))
+
+ for call in mock_execute.await_args_list:
+ query_template = call.args[0]
+ assert "{schema_prefix}" in query_template, (
+ "migrate_llm_models must pass the {schema_prefix} placeholder "
+ "to execute_raw_with_schema; hardcoding 'platform.' breaks when "
+ "DATABASE_URL has no ?schema= param."
+ )
+ assert 'platform."AgentNode"' not in query_template