diff --git a/pgpm/constructive-infra-seed/deploy/schemas/constructive_store_private/tables/platform_secrets/fixtures/standalone_compat.sql b/pgpm/constructive-infra-seed/deploy/schemas/constructive_store_private/tables/platform_secrets/fixtures/standalone_compat.sql new file mode 100644 index 0000000..18b706b --- /dev/null +++ b/pgpm/constructive-infra-seed/deploy/schemas/constructive_store_private/tables/platform_secrets/fixtures/standalone_compat.sql @@ -0,0 +1,31 @@ +-- Deploy: schemas/constructive_store_private/tables/platform_secrets/fixtures/standalone_compat +-- made with <3 @ constructive.io +-- +-- Standalone compatibility patch for platform_secrets. +-- +-- In the monolith, database_id is set by provisioning triggers and the unique +-- constraint evolves across migrations. The slicer extracts the final table +-- state (UNIQUE on database_id, namespace_id, name) but the functions were +-- generated for the earlier schema (UNIQUE on namespace_id, name only). +-- +-- This patch bridges the gap for standalone mode: +-- 1. DEFAULT on database_id → filled from jwt_private.current_database_id() +-- so INSERT without explicit database_id still populates it. +-- 2. Unique index on (namespace_id, name) → matches the ON CONFLICT clause +-- in the generated platform_secrets_set() function. +-- +-- Safe for standalone (single database_id). The upstream AST builders will +-- eventually generate entity-aware functions that include database_id directly. + +BEGIN; + +-- 1. Set DEFAULT on database_id so INSERTs without it use the JWT claim +ALTER TABLE constructive_store_private.platform_secrets + ALTER COLUMN database_id SET DEFAULT jwt_private.current_database_id(); + +-- 2. Create unique index matching the ON CONFLICT (namespace_id, name) clause +-- in the generated platform_secrets_set/del functions +CREATE UNIQUE INDEX IF NOT EXISTS platform_secrets_namespace_id_name_idx + ON constructive_store_private.platform_secrets (namespace_id, name); + +COMMIT; diff --git a/pgpm/constructive-infra-seed/pgpm.plan b/pgpm/constructive-infra-seed/pgpm.plan index 7ec9bd3..c5e05a9 100644 --- a/pgpm/constructive-infra-seed/pgpm.plan +++ b/pgpm/constructive-infra-seed/pgpm.plan @@ -5,3 +5,4 @@ schemas/constructive_infra_public/partitions/create_default_partitions 2017-08-11T08:11:51Z Constructive # create default partitions for range-partitioned tables schemas/constructive_infra_public/tables/platform_namespaces/fixtures/seed_default_namespace [schemas/constructive_infra_public/partitions/create_default_partitions] 2017-08-11T08:11:51Z Constructive # seed default platform namespace schemas/constructive_infra_public/tables/platform_function_definitions/fixtures/seed_built_in_functions [schemas/constructive_infra_public/tables/platform_namespaces/fixtures/seed_default_namespace] 2017-08-11T08:11:51Z Constructive # seed built-in platform functions with required_secrets and namespace +schemas/constructive_store_private/tables/platform_secrets/fixtures/standalone_compat [schemas/constructive_infra_public/tables/platform_namespaces/fixtures/seed_default_namespace] 2017-08-11T08:11:51Z Constructive # standalone compat: DEFAULT database_id from JWT + unique index for ON CONFLICT diff --git a/pgpm/constructive-infra-seed/revert/schemas/constructive_store_private/tables/platform_secrets/fixtures/standalone_compat.sql b/pgpm/constructive-infra-seed/revert/schemas/constructive_store_private/tables/platform_secrets/fixtures/standalone_compat.sql new file mode 100644 index 0000000..80f73e4 --- /dev/null +++ b/pgpm/constructive-infra-seed/revert/schemas/constructive_store_private/tables/platform_secrets/fixtures/standalone_compat.sql @@ -0,0 +1,11 @@ +-- Revert: schemas/constructive_store_private/tables/platform_secrets/fixtures/standalone_compat +-- made with <3 @ constructive.io + +BEGIN; + +DROP INDEX IF EXISTS constructive_store_private.platform_secrets_namespace_id_name_idx; + +ALTER TABLE constructive_store_private.platform_secrets + ALTER COLUMN database_id DROP DEFAULT; + +COMMIT; diff --git a/pgpm/constructive-infra-seed/verify/schemas/constructive_store_private/tables/platform_secrets/fixtures/standalone_compat.sql b/pgpm/constructive-infra-seed/verify/schemas/constructive_store_private/tables/platform_secrets/fixtures/standalone_compat.sql new file mode 100644 index 0000000..6aa1b65 --- /dev/null +++ b/pgpm/constructive-infra-seed/verify/schemas/constructive_store_private/tables/platform_secrets/fixtures/standalone_compat.sql @@ -0,0 +1,11 @@ +-- Verify: schemas/constructive_store_private/tables/platform_secrets/fixtures/standalone_compat +-- made with <3 @ constructive.io + +BEGIN; + +SELECT 1 FROM pg_catalog.pg_indexes +WHERE schemaname = 'constructive_store_private' + AND tablename = 'platform_secrets' + AND indexname = 'platform_secrets_namespace_id_name_idx'; + +ROLLBACK; diff --git a/scripts/setup-platform-db.sh b/scripts/setup-platform-db.sh index 029283d..0141e23 100755 --- a/scripts/setup-platform-db.sh +++ b/scripts/setup-platform-db.sh @@ -36,7 +36,7 @@ echo "→ Deploying constructive-infra..." cd "$ROOT_DIR/pgpm" pgpm deploy --yes --database "$DB_NAME" --package constructive-infra -# --- Deploy constructive-infra-seed (built-in function definitions) --- +# --- Deploy constructive-infra-seed (built-in function definitions + compat patches) --- echo "→ Deploying constructive-infra-seed..." pgpm deploy --yes --database "$DB_NAME" --package constructive-infra-seed