Skip to content

Commit

Permalink
fix: promote query
Browse files Browse the repository at this point in the history
  • Loading branch information
juanmahidalgo committed Nov 28, 2024
1 parent 68fd4be commit ecaf282
Showing 1 changed file with 9 additions and 29 deletions.
38 changes: 9 additions & 29 deletions src/ports/squids/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down

0 comments on commit ecaf282

Please sign in to comment.