diff --git a/packages/schemas/alterations/next-1719221205-fix-functions.ts b/packages/schemas/alterations/next-1719221205-fix-functions.ts new file mode 100644 index 00000000000..c513509a88a --- /dev/null +++ b/packages/schemas/alterations/next-1719221205-fix-functions.ts @@ -0,0 +1,25 @@ +/** + * In Logto Cloud, we have multiple schemas and the default search behavior will be problematic. + * This alteration script will fix it by setting the search path to public for the functions. + */ + +import { sql } from '@silverhand/slonik'; + +import type { AlterationScript } from '../lib/types/alteration.js'; + +const alteration: AlterationScript = { + up: async (pool) => { + await pool.query(sql` + alter function check_application_type set search_path = public; + alter function check_organization_role_type set search_path = public; + `); + }, + down: async (pool) => { + await pool.query(sql` + alter function check_application_type reset search_path; + alter function check_organization_role_type reset search_path; + `); + }, +}; + +export default alteration; diff --git a/packages/schemas/tables/applications.sql b/packages/schemas/tables/applications.sql index 3cf6cf33609..bf51c811828 100644 --- a/packages/schemas/tables/applications.sql +++ b/packages/schemas/tables/applications.sql @@ -37,4 +37,4 @@ create unique index applications__protected_app_metadata_custom_domain create function check_application_type(application_id varchar(21), target_type application_type) returns boolean as $$ begin return (select type from applications where id = application_id) = target_type; -end; $$ language plpgsql; +end; $$ language plpgsql set search_path = public; diff --git a/packages/schemas/tables/organization_roles.sql b/packages/schemas/tables/organization_roles.sql index 3d7b86efa3f..6f1308a239f 100644 --- a/packages/schemas/tables/organization_roles.sql +++ b/packages/schemas/tables/organization_roles.sql @@ -23,4 +23,4 @@ create index organization_roles__id create function check_organization_role_type(role_id varchar(21), target_type role_type) returns boolean as $$ begin return (select type from organization_roles where id = role_id) = target_type; -end; $$ language plpgsql; +end; $$ language plpgsql set search_path = public;