From ecaf282f3373d66d94e093bc96fcbcf751a77e97 Mon Sep 17 00:00:00 2001 From: Juanma Hidalgo Date: Thu, 28 Nov 2024 13:09:08 +0100 Subject: [PATCH 1/3] fix: promote query --- src/ports/squids/queries.ts | 38 +++++++++---------------------------- 1 file changed, 9 insertions(+), 29 deletions(-) diff --git a/src/ports/squids/queries.ts b/src/ports/squids/queries.ts index 58b7310..03cfe22 100644 --- a/src/ports/squids/queries.ts +++ b/src/ports/squids/queries.ts @@ -27,49 +27,29 @@ export const getPromoteQuery = (serviceName: string, schemaName: string, project -- Fetch the new schema name and database user from the indexers table SELECT schema, db_user INTO new_schema_name, writer_user FROM public.indexers - WHERE service = ` - .append(safeServiceName) - .append( - SQL` ORDER BY created_at DESC LIMIT 1; + WHERE service = `.append(safeServiceName).append(SQL` + ORDER BY created_at DESC LIMIT 1; -- Fetch the old schema name from the squids table SELECT schema INTO old_schema_name FROM squids - WHERE name = ` - .append(safeProjectName) - .append( - SQL`; + WHERE name = `.append(safeProjectName).append(SQL`; -- Rename the old schema - EXECUTE format('ALTER SCHEMA ` - .append(safeSchemaName) - .append( - SQL` RENAME TO %I', old_schema_name); + EXECUTE format('ALTER SCHEMA %I RENAME TO %I', `.append(safeSchemaName).append(SQL`, old_schema_name); -- Rename the new schema to the desired name - EXECUTE format('ALTER SCHEMA %I RENAME TO ` - .append(safeSchemaName) - .append( - SQL`, new_schema_name); + EXECUTE format('ALTER SCHEMA %I RENAME TO %I', new_schema_name, `.append(safeSchemaName).append(SQL`); -- Update the search path for the user - EXECUTE format('ALTER USER %I SET search_path TO ` - .append(safeSchemaName) - .append( - SQL`, writer_user); + EXECUTE format('ALTER USER %I SET search_path TO %I', writer_user, `.append(safeSchemaName).append(SQL`); -- Update the schema in the squids table - UPDATE squids SET schema = new_schema_name WHERE name = `.append(safeProjectName).append(SQL`; - - -- Commit the transaction - COMMIT; + UPDATE squids + SET schema = `.append(safeSchemaName).append(SQL` + WHERE name = `.append(safeProjectName).append(SQL`; END $$; `) - ) - ) - ) - ) - ) } export const getSchemaByServiceNameQuery = (serviceName: string): SQLStatement => { From 3aac825c2261ad87d6b2332f69904c6de96ff484 Mon Sep 17 00:00:00 2001 From: Juanma Hidalgo Date: Thu, 28 Nov 2024 13:45:00 +0100 Subject: [PATCH 2/3] fix: promote query --- src/ports/squids/queries.ts | 39 +++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/src/ports/squids/queries.ts b/src/ports/squids/queries.ts index 03cfe22..af7a0a5 100644 --- a/src/ports/squids/queries.ts +++ b/src/ports/squids/queries.ts @@ -9,12 +9,11 @@ export function escapeLiteral(value: string): string { } export function escapeIdentifier(value: string): string { - return client.escapeIdentifier(value) // Escapes an identifier (e.g., schema, table names) + return client.escapeIdentifier(value) // Escapes an identifier (e.g., table names) } export const getPromoteQuery = (serviceName: string, schemaName: string, project: string): SQLStatement => { const safeServiceName = escapeLiteral(serviceName) - const safeSchemaName = escapeIdentifier(schemaName) const safeProjectName = escapeLiteral(project) return SQL` @@ -27,29 +26,53 @@ export const getPromoteQuery = (serviceName: string, schemaName: string, project -- Fetch the new schema name and database user from the indexers table SELECT schema, db_user INTO new_schema_name, writer_user FROM public.indexers - WHERE service = `.append(safeServiceName).append(SQL` + WHERE service = ` + .append(safeServiceName) + .append( + SQL` ORDER BY created_at DESC LIMIT 1; -- Fetch the old schema name from the squids table SELECT schema INTO old_schema_name FROM squids - WHERE name = `.append(safeProjectName).append(SQL`; + WHERE name = ` + .append(safeProjectName) + .append( + SQL`; -- Rename the old schema - EXECUTE format('ALTER SCHEMA %I RENAME TO %I', `.append(safeSchemaName).append(SQL`, old_schema_name); + EXECUTE format('ALTER SCHEMA %I RENAME TO %I', '` + .append(schemaName) + .append( + SQL`', old_schema_name); -- Rename the new schema to the desired name - EXECUTE format('ALTER SCHEMA %I RENAME TO %I', new_schema_name, `.append(safeSchemaName).append(SQL`); + EXECUTE format('ALTER SCHEMA %I RENAME TO %I', new_schema_name, '` + .append(schemaName) + .append( + SQL`'); -- Update the search path for the user - EXECUTE format('ALTER USER %I SET search_path TO %I', writer_user, `.append(safeSchemaName).append(SQL`); + EXECUTE format('ALTER USER %I SET search_path TO %I', writer_user, '` + .append(schemaName) + .append( + SQL`'); -- Update the schema in the squids table UPDATE squids - SET schema = `.append(safeSchemaName).append(SQL` + SET schema = ` + .append(escapeLiteral(schemaName)) + .append( + SQL` WHERE name = `.append(safeProjectName).append(SQL`; END $$; `) + ) + ) + ) + ) + ) + ) } export const getSchemaByServiceNameQuery = (serviceName: string): SQLStatement => { From 8d2dedec1c6f43835e4c45fd1a03a6e1c83cf863 Mon Sep 17 00:00:00 2001 From: Juanma Hidalgo Date: Thu, 28 Nov 2024 14:06:47 +0100 Subject: [PATCH 3/3] fix: promote query --- src/ports/squids/queries.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/ports/squids/queries.ts b/src/ports/squids/queries.ts index af7a0a5..5e06999 100644 --- a/src/ports/squids/queries.ts +++ b/src/ports/squids/queries.ts @@ -60,14 +60,9 @@ export const getPromoteQuery = (serviceName: string, schemaName: string, project -- Update the schema in the squids table UPDATE squids - SET schema = ` - .append(escapeLiteral(schemaName)) - .append( - SQL` - WHERE name = `.append(safeProjectName).append(SQL`; + SET schema = new_schema_name WHERE name = `.append(safeProjectName).append(SQL`; END $$; `) - ) ) ) )