Skip to content

Commit

Permalink
refactor: update compare DB alteration scripts (#6152)
Browse files Browse the repository at this point in the history
* refactor: update compare DB alteration scripts

* chore: add comments
  • Loading branch information
darcyYe authored Jul 2, 2024
1 parent 2ce6ba3 commit 6dc3801
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions .scripts/compare-database.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
const normalizeGrantee = ({ grantee, ...rest }) => {
if (grantee.startsWith('logto_tenant_')) {
return { ...rest, grantee: 'logto_tenant' };
const normalizeRoleName = (roleName) => {
if (roleName.startsWith('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.
if (grantee.startsWith('logto_region_')) {
return { ...rest, grantee: 'logto_region' };
if (roleName.startsWith('logto_region_')) {
return 'logto_region';
}

return { grantee, ...rest };
return roleName;
};

const normalizeGrantee = ({ grantee, ...rest }) => ({
...rest,
grantee: normalizeRoleName(grantee),
});

// Ditto.
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 };
};

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
return {
tables: omitArray(tables, 'table_catalog'),
Expand Down Expand Up @@ -149,7 +169,7 @@ const queryDatabaseManifest = async (database) => {
indexes,
funcs,
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),
tableGrants: omitArray(tableGrants, 'table_catalog').map(normalizeGrantee),
};
Expand Down

0 comments on commit 6dc3801

Please sign in to comment.