From 4a0bf767a3c73effea111882f3fad4a5b3cc895e Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Mon, 16 Mar 2026 05:58:40 +0000 Subject: [PATCH] fix(export): use extensionName for services schema prefix instead of metaExtensionName The metaReplacer in exportMigrationsToDisk was using metaExtensionName (e.g. 'agent-db-services') as the schema prefix for the services package, producing incorrect schema names like 'agent_db_services_auth_public'. The services metadata INSERT statements reference the actual deployed schemas which are owned by the application package and use its prefix (e.g. 'agent_db_auth_public'), not the services package prefix. Fix: Add an optional 'schemaPrefix' parameter to makeReplacer. When provided, schema names use this prefix instead of 'name'. The metaReplacer now passes extensionName as schemaPrefix, so: - Extension name replacement: 'constructive-extension-name' -> metaExtensionName (correct) - Schema prefix replacement: uses extensionName (correct) This is backward compatible - existing callers that don't pass schemaPrefix work exactly as before. For constructive-db where schemas are already named 'constructive_*', the replacer produces the same output since extensionName='constructive' matches the existing prefix. --- pgpm/core/src/export/export-migrations.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/pgpm/core/src/export/export-migrations.ts b/pgpm/core/src/export/export-migrations.ts index ee9505584..cde04e545 100644 --- a/pgpm/core/src/export/export-migrations.ts +++ b/pgpm/core/src/export/export-migrations.ts @@ -335,7 +335,11 @@ const exportMigrationsToDisk = async ({ const metaReplacer = makeReplacer({ schemas: metaSchemasForReplacement, - name: metaExtensionName + name: metaExtensionName, + // Use extensionName for schema prefix — the services metadata references + // schemas owned by the application package (e.g. agent_db_auth_public), + // not the services package (agent_db_services_auth_public) + schemaPrefix: name }); // Create separate files for each table type @@ -527,6 +531,14 @@ interface Schema { interface MakeReplacerOptions { schemas: Schema[]; name: string; + /** + * Optional prefix for schema name replacement. + * When provided, schema names are replaced using this prefix instead of `name`. + * This is needed for the services/meta package where `name` is the services + * extension name (e.g. "agent-db-services") but schemas should use the + * application extension prefix (e.g. "agent-db" → "agent_db_auth_public"). + */ + schemaPrefix?: string; } interface ReplacerResult { @@ -606,11 +618,12 @@ const preparePackage = async ({ /** * Generates a function for replacing schema names and extension names in strings. */ -const makeReplacer = ({ schemas, name }: MakeReplacerOptions): ReplacerResult => { +const makeReplacer = ({ schemas, name, schemaPrefix }: MakeReplacerOptions): ReplacerResult => { const replacements: [string, string] = ['constructive-extension-name', name]; + const prefix = schemaPrefix || name; const schemaReplacers: [string, string][] = schemas.map((schema) => [ schema.schema_name, - toSnakeCase(`${name}_${schema.name}`) + toSnakeCase(`${prefix}_${schema.name}`) ]); const replace: [RegExp, string][] = [...schemaReplacers, replacements].map(