mirror of
https://github.com/logto-io/logto.git
synced 2025-01-13 21:30:30 -05:00
chore(cli,cloud,schemas): change default tenant name (#3890)
This commit is contained in:
parent
8cbf87bb73
commit
f1d8466919
8 changed files with 14 additions and 47 deletions
|
@ -1,6 +1,6 @@
|
||||||
import { createTenantMetadata } from '@logto/core-kit';
|
import { createTenantMetadata } from '@logto/core-kit';
|
||||||
import type {
|
import type {
|
||||||
TenantModel,
|
CreateTenant,
|
||||||
AdminData,
|
AdminData,
|
||||||
UpdateAdminData,
|
UpdateAdminData,
|
||||||
CreateScope,
|
CreateScope,
|
||||||
|
@ -18,9 +18,9 @@ import { getDatabaseName } from '../../../queries/database.js';
|
||||||
export const createTenant = async (pool: CommonQueryMethods, tenantId: string) => {
|
export const createTenant = async (pool: CommonQueryMethods, tenantId: string) => {
|
||||||
const database = await getDatabaseName(pool, true);
|
const database = await getDatabaseName(pool, true);
|
||||||
const { parentRole, role, password } = createTenantMetadata(database, tenantId);
|
const { parentRole, role, password } = createTenantMetadata(database, tenantId);
|
||||||
const tenantModel: TenantModel = { id: tenantId, dbUser: role, dbUserPassword: password };
|
const createTenant: CreateTenant = { id: tenantId, dbUser: role, dbUserPassword: password };
|
||||||
|
|
||||||
await pool.query(insertInto(tenantModel, 'tenants'));
|
await pool.query(insertInto(createTenant, 'tenants'));
|
||||||
await pool.query(sql`
|
await pool.query(sql`
|
||||||
create role ${sql.identifier([role])} with inherit login
|
create role ${sql.identifier([role])} with inherit login
|
||||||
password '${raw(password)}'
|
password '${raw(password)}'
|
||||||
|
|
|
@ -4,7 +4,7 @@ import {
|
||||||
} from '@logto/cli/lib/commands/database/utils.js';
|
} from '@logto/cli/lib/commands/database/utils.js';
|
||||||
import { DemoConnector } from '@logto/connector-kit';
|
import { DemoConnector } from '@logto/connector-kit';
|
||||||
import { createTenantMetadata } from '@logto/core-kit';
|
import { createTenantMetadata } from '@logto/core-kit';
|
||||||
import type { LogtoOidcConfigType, TenantInfo, TenantModel } from '@logto/schemas';
|
import type { LogtoOidcConfigType, TenantInfo, CreateTenant } from '@logto/schemas';
|
||||||
import {
|
import {
|
||||||
createAdminTenantApplicationRole,
|
createAdminTenantApplicationRole,
|
||||||
AdminTenantRole,
|
AdminTenantRole,
|
||||||
|
@ -70,7 +70,7 @@ export class TenantsLibrary {
|
||||||
const { id: tenantId, parentRole, role, password } = createTenantMetadata(databaseName);
|
const { id: tenantId, parentRole, role, password } = createTenantMetadata(databaseName);
|
||||||
|
|
||||||
// Init tenant
|
// Init tenant
|
||||||
const tenantModel: TenantModel = { id: tenantId, dbUser: role, dbUserPassword: password };
|
const createTenant: CreateTenant = { id: tenantId, dbUser: role, dbUserPassword: password };
|
||||||
const transaction = await this.queries.client.transaction();
|
const transaction = await this.queries.client.transaction();
|
||||||
const tenants = createTenantsQueries(transaction);
|
const tenants = createTenantsQueries(transaction);
|
||||||
const users = createUsersQueries(transaction);
|
const users = createUsersQueries(transaction);
|
||||||
|
@ -83,7 +83,7 @@ export class TenantsLibrary {
|
||||||
await transaction.start();
|
await transaction.start();
|
||||||
|
|
||||||
// Init tenant
|
// Init tenant
|
||||||
await tenants.insertTenant(tenantModel);
|
await tenants.insertTenant(createTenant);
|
||||||
await tenants.createTenantRole(parentRole, role, password);
|
await tenants.createTenantRole(parentRole, role, password);
|
||||||
|
|
||||||
// Create admin data set (resource, roles, etc.)
|
// Create admin data set (resource, roles, etc.)
|
||||||
|
@ -162,7 +162,7 @@ export class TenantsLibrary {
|
||||||
|
|
||||||
// Update Redirect URI for Admin Console
|
// Update Redirect URI for Admin Console
|
||||||
await tenants.appendAdminConsoleRedirectUris(
|
await tenants.appendAdminConsoleRedirectUris(
|
||||||
...cloudUrlSet.deduplicated().map((url) => appendPath(url, tenantModel.id, 'callback'))
|
...cloudUrlSet.deduplicated().map((url) => appendPath(url, createTenant.id, 'callback'))
|
||||||
);
|
);
|
||||||
|
|
||||||
await transaction.end();
|
await transaction.end();
|
||||||
|
|
|
@ -6,7 +6,7 @@ import {
|
||||||
getManagementApiResourceIndicator,
|
getManagementApiResourceIndicator,
|
||||||
PredefinedScope,
|
PredefinedScope,
|
||||||
} from '@logto/schemas';
|
} from '@logto/schemas';
|
||||||
import type { AdminData, TenantModel, CreateRolesScope } from '@logto/schemas';
|
import type { AdminData, CreateTenant, CreateRolesScope } from '@logto/schemas';
|
||||||
import { generateStandardId } from '@logto/shared';
|
import { generateStandardId } from '@logto/shared';
|
||||||
import type { PostgreSql } from '@withtyped/postgres';
|
import type { PostgreSql } from '@withtyped/postgres';
|
||||||
import { jsonb, dangerousRaw, id, sql } from '@withtyped/postgres';
|
import { jsonb, dangerousRaw, id, sql } from '@withtyped/postgres';
|
||||||
|
@ -34,7 +34,7 @@ export const createTenantsQueries = (client: Queryable<PostgreSql>) => {
|
||||||
where roles.tenant_id = ${adminTenantId};
|
where roles.tenant_id = ${adminTenantId};
|
||||||
`);
|
`);
|
||||||
|
|
||||||
const insertTenant = async (tenant: TenantModel) => client.query(insertInto(tenant, 'tenants'));
|
const insertTenant = async (tenant: CreateTenant) => client.query(insertInto(tenant, 'tenants'));
|
||||||
|
|
||||||
const createTenantRole = async (parentRole: string, role: string, password: string) =>
|
const createTenantRole = async (parentRole: string, role: string, password: string) =>
|
||||||
client.query(sql`
|
client.query(sql`
|
||||||
|
|
|
@ -6,36 +6,13 @@ const alteration: AlterationScript = {
|
||||||
up: async (pool) => {
|
up: async (pool) => {
|
||||||
// Add new tenant columns for name, tag, and created_at.
|
// Add new tenant columns for name, tag, and created_at.
|
||||||
await pool.query(sql`
|
await pool.query(sql`
|
||||||
alter table tenants add column name varchar(128);
|
alter table tenants add column name varchar(128) not null default 'My Project';
|
||||||
alter table tenants add column tag varchar(64) not null default 'development';
|
alter table tenants add column tag varchar(64) not null default 'development';
|
||||||
alter table tenants add column created_at timestamptz not null default(now());
|
alter table tenants add column created_at timestamptz not null default(now());
|
||||||
`);
|
`);
|
||||||
// Manually set the name for existing tenants since the trigger is for new tenants only.
|
|
||||||
await pool.query(sql`
|
|
||||||
update tenants set name = concat('tenant_', id);
|
|
||||||
`);
|
|
||||||
await pool.query(sql`
|
|
||||||
alter table tenants alter column name set not null;
|
|
||||||
`);
|
|
||||||
// Create a trigger to set the tenant name since column reference is not available as default value.
|
|
||||||
await pool.query(sql`
|
|
||||||
create function set_tenant_name() returns trigger as
|
|
||||||
$$ begin
|
|
||||||
new.name := concat('tenant_', new.id);
|
|
||||||
return new;
|
|
||||||
end; $$ language plpgsql;
|
|
||||||
`);
|
|
||||||
await pool.query(sql`
|
|
||||||
create trigger set_tenant_name_trigger
|
|
||||||
before insert on tenants
|
|
||||||
for each row when (new.name is null)
|
|
||||||
execute procedure set_tenant_name();
|
|
||||||
`);
|
|
||||||
},
|
},
|
||||||
down: async (pool) => {
|
down: async (pool) => {
|
||||||
await pool.query(sql`
|
await pool.query(sql`
|
||||||
drop trigger set_tenant_name_trigger on tenants;
|
|
||||||
drop function set_tenant_name;
|
|
||||||
alter table tenants drop column name;
|
alter table tenants drop column name;
|
||||||
alter table tenants drop column tag;
|
alter table tenants drop column tag;
|
||||||
alter table tenants drop column created_at;
|
alter table tenants drop column created_at;
|
||||||
|
|
|
@ -8,7 +8,7 @@ export const Tenants = createModel(/* sql */ `
|
||||||
id varchar(21) not null,
|
id varchar(21) not null,
|
||||||
db_user varchar(128),
|
db_user varchar(128),
|
||||||
db_user_password varchar(128),
|
db_user_password varchar(128),
|
||||||
name varchar(128) not null,
|
name varchar(128) not null default 'My Project',
|
||||||
tag varchar(64) not null default '${TenantTag.Development}',
|
tag varchar(64) not null default '${TenantTag.Development}',
|
||||||
created_at timestamptz not null default(now()),
|
created_at timestamptz not null default(now()),
|
||||||
primary key (id),
|
primary key (id),
|
||||||
|
|
|
@ -9,4 +9,6 @@ export const adminTenantId = 'admin';
|
||||||
* `createModel` from @withtyped/server can not properly infer the model
|
* `createModel` from @withtyped/server can not properly infer the model
|
||||||
* type, manually define it here for now.
|
* type, manually define it here for now.
|
||||||
*/
|
*/
|
||||||
export type TenantModel = Pick<InferModelType<typeof Tenants>, 'id' | 'dbUser' | 'dbUserPassword'>;
|
export type TenantModel = InferModelType<typeof Tenants>;
|
||||||
|
export type CreateTenant = Pick<TenantModel, 'id' | 'dbUser' | 'dbUserPassword'> &
|
||||||
|
Partial<Pick<TenantModel, 'name'>>;
|
||||||
|
|
|
@ -1,11 +1,5 @@
|
||||||
/* This SQL will run after all other queries. */
|
/* This SQL will run after all other queries. */
|
||||||
|
|
||||||
---- Create trigger to set tenant name ----
|
|
||||||
create trigger set_tenant_name_trigger
|
|
||||||
before insert on tenants
|
|
||||||
for each row when (new.name is null)
|
|
||||||
execute procedure set_tenant_name();
|
|
||||||
|
|
||||||
---- Grant CRUD access to the group ----
|
---- Grant CRUD access to the group ----
|
||||||
grant select, insert, update, delete
|
grant select, insert, update, delete
|
||||||
on all tables
|
on all tables
|
||||||
|
|
|
@ -13,10 +13,4 @@ $$ begin
|
||||||
return new;
|
return new;
|
||||||
end; $$ language plpgsql;
|
end; $$ language plpgsql;
|
||||||
|
|
||||||
create function set_tenant_name() returns trigger as
|
|
||||||
$$ begin
|
|
||||||
new.name := concat('tenant_', new.id);
|
|
||||||
return new;
|
|
||||||
end; $$ language plpgsql;
|
|
||||||
|
|
||||||
/* no_after_each */
|
/* no_after_each */
|
||||||
|
|
Loading…
Add table
Reference in a new issue