0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

refactor: update compare DB alteration scripts (#6152)

* refactor: update compare DB alteration scripts

* chore: add comments
This commit is contained in:
Darcy Ye 2024-07-02 11:25:01 +08:00 committed by GitHub
parent 2ce6ba3447
commit 6dc380145c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -99,26 +99,46 @@ const queryDatabaseManifest = async (database) => {
`); `);
// This function removes the last segment of grantee since Logto will use 'logto_tenant_fresh/alteration' for the role name. // This function removes the last segment of grantee since Logto will use 'logto_tenant_fresh/alteration' for the role name.
const normalizeGrantee = ({ grantee, ...rest }) => { const normalizeRoleName = (roleName) => {
if (grantee.startsWith('logto_tenant_')) { if (roleName.startsWith('logto_tenant_')) {
return { ...rest, grantee: 'logto_tenant' }; return 'logto_tenant';
} }
// Removes the last segment of region grantee since Logto will use 'logto_region_xxx' for the role name for different regions. // Removes the last segment of region grantee since Logto will use 'logto_region_xxx' for the role name for different regions.
if (grantee.startsWith('logto_region_')) { if (roleName.startsWith('logto_region_')) {
return { ...rest, grantee: 'logto_region' }; return 'logto_region';
} }
return { grantee, ...rest }; return roleName;
}; };
const normalizeGrantee = ({ grantee, ...rest }) => ({
...rest,
grantee: normalizeRoleName(grantee),
});
// Ditto. // Ditto.
const normalizeRoles = ({ roles: raw, ...rest }) => { const normalizeRoles = ({ roles: raw, ...rest }) => {
const roles = raw.slice(1, -1).split(',').map((name) => name.startsWith('logto_tenant_') ? 'logto_tenant' : name); const roles = raw
.slice(1, -1)
.split(',')
.map((name) => normalizeRoleName(name));
return { roles, ...rest }; return { roles, ...rest };
}; };
const normalizePolicyname = ({ policyname, ...rest }) => {
const prefix = 'allow_';
const suffix = '_access';
if (policyname && policyname.startsWith(prefix) && policyname.endsWith(suffix)) {
// This is a naming convention in Logto cloud, it is formatted as `allow_{role_name}_access`, we need to normalize the role name part for the convenience of comparing DB updates.
// Ref: https://github.com/logto-io/cloud/pull/738
return { policyname: `${prefix}${normalizeRoleName(policyname.slice(prefix.length, -suffix.length))}${suffix}`, ...rest };
}
return { policyname, ...rest };
};
// Omit generated ids and values // Omit generated ids and values
return { return {
tables: omitArray(tables, 'table_catalog'), tables: omitArray(tables, 'table_catalog'),
@ -149,7 +169,7 @@ const queryDatabaseManifest = async (database) => {
indexes, indexes,
funcs, funcs,
triggers: omitArray(triggers, 'trigger_catalog', 'event_object_catalog'), triggers: omitArray(triggers, 'trigger_catalog', 'event_object_catalog'),
policies: policies.map(normalizeRoles), policies: policies.map(normalizeRoles).map(normalizePolicyname),
columnGrants: omitArray(columnGrants, 'table_catalog').map(normalizeGrantee), columnGrants: omitArray(columnGrants, 'table_catalog').map(normalizeGrantee),
tableGrants: omitArray(tableGrants, 'table_catalog').map(normalizeGrantee), tableGrants: omitArray(tableGrants, 'table_catalog').map(normalizeGrantee),
}; };