Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update compare DB alteration scripts #6152

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading