Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"@logto/schemas": "workspace:1.23.1",
"@logto/shared": "workspace:^3.1.2",
"@silverhand/essentials": "^2.9.1",
"@silverhand/slonik": "31.0.0-beta.2",
"@silverhand/slonik": "31.0.0-beta.3",
"chalk": "^5.3.0",
"decamelize": "^6.0.0",
"dotenv": "^16.4.5",
Expand Down
14 changes: 11 additions & 3 deletions packages/cli/src/commands/database/seed/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import type { DatabaseTransactionConnection } from '@silverhand/slonik';
import { sql } from '@silverhand/slonik';

import { insertInto } from '../../../database.js';
import { getDatabaseName, getDatabaseUser } from '../../../queries/database.js';
import { getDatabaseName, getDatabaseUser, getSchemaName } from '../../../queries/database.js';
import { updateDatabaseTimestamp } from '../../../queries/system.js';
import { convertToIdentifiers } from '../../../sql.js';
import { consoleLog, getPathInModule } from '../../../utils.js';
Expand Down Expand Up @@ -101,7 +101,13 @@ export const createTables = async (

const runLifecycleQuery = async (
lifecycle: Lifecycle,
parameters: { name?: string; database?: string; databaseUser?: string; password?: string } = {}
parameters: {
name?: string;
database?: string;
databaseUser?: string;
password?: string;
schema?: string;
} = {}
) => {
const query = queries.find(([file]) => file.slice(1, -4) === lifecycle)?.[1];

Expand All @@ -114,6 +120,7 @@ export const createTables = async (
.replaceAll('${database}', parameters.database ?? '')
.replaceAll('${password}', parameters.password ?? '')
.replaceAll('${databaseUser}', parameters.databaseUser ?? '')
.replaceAll('${schema}', parameters.schema ?? '')
/* eslint-enable no-template-curly-in-string */
)}`
);
Expand All @@ -128,8 +135,9 @@ export const createTables = async (
const database = await getDatabaseName(connection, true);
const password = encryptBaseRole ? generateStandardId(32) : '';
const databaseUser = await getDatabaseUser(connection);
const schema = await getSchemaName(connection, true);

await runLifecycleQuery('before_all', { database, password, databaseUser });
await runLifecycleQuery('before_all', { database, password, databaseUser, schema });

/* eslint-disable no-await-in-loop */
for (const [file, query] of sorted) {
Expand Down
8 changes: 8 additions & 0 deletions packages/cli/src/queries/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ export const getDatabaseUser = async (pool: CommonQueryMethods) => {

return currentUser;
};

export const getSchemaName = async (pool: CommonQueryMethods, normalized = false) => {
const { currentSchema } = await pool.one<{ currentSchema: string }>(sql`
SELECT current_schema();
`);

return normalized ? currentSchema.replaceAll('-', '_') : currentSchema;
};
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"@logto/schemas": "workspace:^1.23.1",
"@logto/shared": "workspace:^3.1.2",
"@silverhand/essentials": "^2.9.1",
"@silverhand/slonik": "31.0.0-beta.2",
"@silverhand/slonik": "31.0.0-beta.3",
"@simplewebauthn/server": "^10.0.0",
"@withtyped/client": "^0.8.8",
"camelcase": "^8.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,54 @@ const getDatabaseName = async (pool: CommonQueryMethods) => {

return currentDatabase.replaceAll('-', '_');
};

const getSchemaName = async (pool: CommonQueryMethods) => {
const { currentSchema } = await pool.one<{ currentSchema: string }>(sql`
SELECT current_schema;
`);

return currentSchema.replaceAll('-', '_');
};

const getId = (value: string) => sql.identifier([value]);

const alteration: AlterationScript = {
up: async (pool) => {
const database = await getDatabaseName(pool);
const schema = getId(`${await getSchemaName(pool)}`);
const adminId = getId(`logto_tenant_${database}_admin`);
const defaultId = getId(`logto_tenant_${database}_default`);
await pool.query(sql`
GRANT USAGE ON SCHEMA logto TO ${defaultId};
GRANT USAGE ON SCHEMA logto TO ${adminId};
GRANT USAGE ON SCHEMA ${schema} TO ${defaultId};
GRANT USAGE ON SCHEMA ${schema} TO ${adminId};
grant select, insert, update, delete
on all tables
in schema logto
in schema ${schema}
to ${adminId};
grant select, insert, update, delete
on all tables
in schema logto
in schema ${schema}
to ${defaultId};
ALTER ROLE ${adminId} SET search_path = logto;
ALTER ROLE ${defaultId} SET search_path = logto;
`);
},
down: async (pool) => {
const database = await getDatabaseName(pool);
const schema = getId(`${await getSchemaName(pool)}`);
const adminId = getId(`logto_tenant_${database}_admin`);
const defaultId = getId(`logto_tenant_${database}_default`);

await pool.query(sql`
revoke usage on schema logto from ${adminId};
ALTER ROLE ${adminId} SET search_path = "$user", public;
revoke usage on schema ${schema} from ${adminId};
revoke all privileges
on all tables
in schema logto
in schema ${schema}
from ${adminId};
`);
await pool.query(sql`
revoke usage on schema logto from ${defaultId};
ALTER ROLE ${defaultId} SET search_path = "$user", public;
revoke usage on schema ${schema} from ${defaultId};
revoke all privileges
on all tables
in schema logto
in schema ${schema}
from ${defaultId};
`);
},
Expand Down
2 changes: 1 addition & 1 deletion packages/schemas/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"devDependencies": {
"@silverhand/eslint-config": "6.0.1",
"@silverhand/essentials": "^2.9.1",
"@silverhand/slonik": "31.0.0-beta.2",
"@silverhand/slonik": "31.0.0-beta.3",
"@silverhand/ts-config": "6.0.0",
"@types/inquirer": "^9.0.0",
"@types/node": "^20.9.5",
Expand Down
9 changes: 3 additions & 6 deletions packages/schemas/tables/_before_all.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/* This SQL will run before all other queries. */
create schema if not exists logto;
set search_path = logto, "$user";
alter role ${databaseUser} set search_path = logto, "$user";
create role logto_tenant_${database} password '${password}' noinherit;

GRANT USAGE ON SCHEMA logto TO logto_tenant_${database};
ALTER ROLE logto_tenant_${database} SET search_path = logto, "$user";
create schema if not exists ${schema};
create role logto_tenant_${database} password '${password}' noinherit;
GRANT USAGE ON SCHEMA ${schema} TO logto_tenant_${database};
47 changes: 26 additions & 21 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading