Skip to content

Commit 0b0a1d3

Browse files
committed
refactor: update compare DB alteration scripts
1 parent f6e10f8 commit 0b0a1d3

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

.scripts/compare-database.js

+24-8
Original file line numberDiff line numberDiff line change
@@ -99,26 +99,42 @@ const queryDatabaseManifest = async (database) => {
9999
`);
100100

101101
// This function removes the last segment of grantee since Logto will use 'logto_tenant_fresh/alteration' for the role name.
102-
const normalizeGrantee = ({ grantee, ...rest }) => {
103-
if (grantee.startsWith('logto_tenant_')) {
104-
return { ...rest, grantee: 'logto_tenant' };
102+
const normalizeRoleName = (roleName) => {
103+
if (roleName.startsWith('logto_tenant_')) {
104+
return 'logto_tenant';
105105
}
106106

107107
// Removes the last segment of region grantee since Logto will use 'logto_region_xxx' for the role name for different regions.
108-
if (grantee.startsWith('logto_region_')) {
109-
return { ...rest, grantee: 'logto_region' };
108+
if (roleName.startsWith('logto_region_')) {
109+
return 'logto_region';
110110
}
111111

112-
return { grantee, ...rest };
112+
return roleName;
113113
};
114114

115+
const normalizeGrantee = ({ grantee, ...rest }) => ({
116+
...rest,
117+
grantee: normalizeRoleName(grantee),
118+
});
119+
115120
// Ditto.
116121
const normalizeRoles = ({ roles: raw, ...rest }) => {
117-
const roles = raw.slice(1, -1).split(',').map((name) => name.startsWith('logto_tenant_') ? 'logto_tenant' : name);
122+
const roles = raw
123+
.slice(1, -1)
124+
.split(',')
125+
.map((name) => normalizeRoleName(name));
118126

119127
return { roles, ...rest };
120128
};
121129

130+
const normalizePolicyName = ({ policyName, ...rest }) => {
131+
if (policyName && policyName.startsWith('allow_') && policyName.endsWith('_access')) {
132+
return { policyName: `allow_${normalizeRoleName(policyName.slice(6, -7))}_access`, ...rest };
133+
}
134+
135+
return { policyName, ...rest };
136+
};
137+
122138
// Omit generated ids and values
123139
return {
124140
tables: omitArray(tables, 'table_catalog'),
@@ -149,7 +165,7 @@ const queryDatabaseManifest = async (database) => {
149165
indexes,
150166
funcs,
151167
triggers: omitArray(triggers, 'trigger_catalog', 'event_object_catalog'),
152-
policies: policies.map(normalizeRoles),
168+
policies: policies.map(normalizeRoles).map(normalizePolicyName),
153169
columnGrants: omitArray(columnGrants, 'table_catalog').map(normalizeGrantee),
154170
tableGrants: omitArray(tableGrants, 'table_catalog').map(normalizeGrantee),
155171
};

0 commit comments

Comments
 (0)