mirror of
https://github.com/logto-io/logto.git
synced 2024-12-30 20:33:54 -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 type {
|
||||
TenantModel,
|
||||
CreateTenant,
|
||||
AdminData,
|
||||
UpdateAdminData,
|
||||
CreateScope,
|
||||
|
@ -18,9 +18,9 @@ import { getDatabaseName } from '../../../queries/database.js';
|
|||
export const createTenant = async (pool: CommonQueryMethods, tenantId: string) => {
|
||||
const database = await getDatabaseName(pool, true);
|
||||
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`
|
||||
create role ${sql.identifier([role])} with inherit login
|
||||
password '${raw(password)}'
|
||||
|
|
|
@ -4,7 +4,7 @@ import {
|
|||
} from '@logto/cli/lib/commands/database/utils.js';
|
||||
import { DemoConnector } from '@logto/connector-kit';
|
||||
import { createTenantMetadata } from '@logto/core-kit';
|
||||
import type { LogtoOidcConfigType, TenantInfo, TenantModel } from '@logto/schemas';
|
||||
import type { LogtoOidcConfigType, TenantInfo, CreateTenant } from '@logto/schemas';
|
||||
import {
|
||||
createAdminTenantApplicationRole,
|
||||
AdminTenantRole,
|
||||
|
@ -70,7 +70,7 @@ export class TenantsLibrary {
|
|||
const { id: tenantId, parentRole, role, password } = createTenantMetadata(databaseName);
|
||||
|
||||
// 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 tenants = createTenantsQueries(transaction);
|
||||
const users = createUsersQueries(transaction);
|
||||
|
@ -83,7 +83,7 @@ export class TenantsLibrary {
|
|||
await transaction.start();
|
||||
|
||||
// Init tenant
|
||||
await tenants.insertTenant(tenantModel);
|
||||
await tenants.insertTenant(createTenant);
|
||||
await tenants.createTenantRole(parentRole, role, password);
|
||||
|
||||
// Create admin data set (resource, roles, etc.)
|
||||
|
@ -162,7 +162,7 @@ export class TenantsLibrary {
|
|||
|
||||
// Update Redirect URI for Admin Console
|
||||
await tenants.appendAdminConsoleRedirectUris(
|
||||
...cloudUrlSet.deduplicated().map((url) => appendPath(url, tenantModel.id, 'callback'))
|
||||
...cloudUrlSet.deduplicated().map((url) => appendPath(url, createTenant.id, 'callback'))
|
||||
);
|
||||
|
||||
await transaction.end();
|
||||
|
|
|
@ -6,7 +6,7 @@ import {
|
|||
getManagementApiResourceIndicator,
|
||||
PredefinedScope,
|
||||
} 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 type { PostgreSql } 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};
|
||||
`);
|
||||
|
||||
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) =>
|
||||
client.query(sql`
|
||||
|
|
|
@ -6,36 +6,13 @@ const alteration: AlterationScript = {
|
|||
up: async (pool) => {
|
||||
// Add new tenant columns for name, tag, and created_at.
|
||||
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 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) => {
|
||||
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 tag;
|
||||
alter table tenants drop column created_at;
|
||||
|
|
|
@ -8,7 +8,7 @@ export const Tenants = createModel(/* sql */ `
|
|||
id varchar(21) not null,
|
||||
db_user 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}',
|
||||
created_at timestamptz not null default(now()),
|
||||
primary key (id),
|
||||
|
|
|
@ -9,4 +9,6 @@ export const adminTenantId = 'admin';
|
|||
* `createModel` from @withtyped/server can not properly infer the model
|
||||
* 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. */
|
||||
|
||||
---- 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 select, insert, update, delete
|
||||
on all tables
|
||||
|
|
|
@ -13,10 +13,4 @@ $$ begin
|
|||
return new;
|
||||
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 */
|
||||
|
|
Loading…
Reference in a new issue