Fix add-subdomain-to-workspace command (#9005)

## Context
Fix add-subdomain-to-workspace command not included in global module
also fixing the command regex logic that was not generating subdomain
properly
This commit is contained in:
Weiko
2024-12-10 15:32:13 +01:00
committed by GitHub
parent 90d47a8fca
commit b0595e452a
3 changed files with 39 additions and 16 deletions

View File

@ -9,6 +9,7 @@ import { DataSeedWorkspaceCommand } from 'src/database/commands/data-seed-dev-wo
import { ConfirmationQuestion } from 'src/database/commands/questions/confirmation.question'; import { ConfirmationQuestion } from 'src/database/commands/questions/confirmation.question';
import { UpgradeTo0_32CommandModule } from 'src/database/commands/upgrade-version/0-32/0-32-upgrade-version.module'; import { UpgradeTo0_32CommandModule } from 'src/database/commands/upgrade-version/0-32/0-32-upgrade-version.module';
import { UpgradeTo0_33CommandModule } from 'src/database/commands/upgrade-version/0-33/0-33-upgrade-version.module'; import { UpgradeTo0_33CommandModule } from 'src/database/commands/upgrade-version/0-33/0-33-upgrade-version.module';
import { UpgradeTo0_34CommandModule } from 'src/database/commands/upgrade-version/0-34/0-34-upgrade-version.module';
import { TypeORMModule } from 'src/database/typeorm/typeorm.module'; import { TypeORMModule } from 'src/database/typeorm/typeorm.module';
import { BillingSubscription } from 'src/engine/core-modules/billing/entities/billing-subscription.entity'; import { BillingSubscription } from 'src/engine/core-modules/billing/entities/billing-subscription.entity';
import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-flag.entity'; import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-flag.entity';
@ -49,6 +50,7 @@ import { WorkspaceSyncMetadataModule } from 'src/engine/workspace-manager/worksp
WorkspaceMetadataVersionModule, WorkspaceMetadataVersionModule,
UpgradeTo0_32CommandModule, UpgradeTo0_32CommandModule,
UpgradeTo0_33CommandModule, UpgradeTo0_33CommandModule,
UpgradeTo0_34CommandModule,
FeatureFlagModule, FeatureFlagModule,
], ],
providers: [ providers: [

View File

@ -1,11 +1,11 @@
import { InjectRepository } from '@nestjs/typeorm'; import { InjectRepository } from '@nestjs/typeorm';
import { Command } from 'nest-commander'; import { Command } from 'nest-commander';
import { Repository, In } from 'typeorm'; import { In, Repository } from 'typeorm';
import { ActiveWorkspacesCommandRunner } from 'src/database/commands/active-workspaces.command'; import { ActiveWorkspacesCommandRunner } from 'src/database/commands/active-workspaces.command';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { BaseCommandOptions } from 'src/database/commands/base.command'; import { BaseCommandOptions } from 'src/database/commands/base.command';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
// For DX only // For DX only
type WorkspaceId = string; type WorkspaceId = string;
@ -41,19 +41,25 @@ export class GenerateDefaultSubdomainCommand extends ActiveWorkspacesCommandRunn
} }
if (!domainName && displayName) { if (!domainName && displayName) {
const displayNameWords = displayName.match(/(\w| |\d)+/); result.subdomain = this.sanitizeForSubdomain(displayName);
if (displayNameWords) {
result.subdomain = displayNameWords
.join('-')
.replace(/ /g, '')
.toLowerCase();
}
} }
return result; return result;
} }
private sanitizeForSubdomain(input: string) {
const normalized = input.normalize('NFKD').replace(/[\u0300-\u036f]/g, '');
const hyphenated = normalized
.replace(/[^a-zA-Z0-9]/g, '-')
.replace(/-+/g, '-')
.replace(/^-+|-+$/g, '')
.toLowerCase();
// DNS subdomain limit is 63, but we want to be conservative for duplicates
return hyphenated.substring(0, 60);
}
private groupBySubdomainName( private groupBySubdomainName(
acc: Record<Subdomain, Array<WorkspaceId>>, acc: Record<Subdomain, Array<WorkspaceId>>,
workspace: Workspace, workspace: Workspace,
@ -70,16 +76,23 @@ export class GenerateDefaultSubdomainCommand extends ActiveWorkspacesCommandRunn
private async deduplicateAndSave( private async deduplicateAndSave(
subdomain: Subdomain, subdomain: Subdomain,
workspaceIds: Array<WorkspaceId>, workspaceIds: Array<WorkspaceId>,
existingSubdomains: Set<string>,
options: BaseCommandOptions, options: BaseCommandOptions,
) { ) {
for (const [index, workspaceId] of workspaceIds.entries()) { for (const [index, workspaceId] of workspaceIds.entries()) {
const subdomainDeduplicated = const subdomainDeduplicated =
index === 0 ? subdomain : `${subdomain}-${index}`; index === 0
? existingSubdomains.has(subdomain)
? `${subdomain}-${index}`
: subdomain
: `${subdomain}-${index}`;
this.logger.log( this.logger.log(
`Updating workspace ${workspaceId} with subdomain ${subdomainDeduplicated}`, `Updating workspace ${workspaceId} with subdomain ${subdomainDeduplicated}`,
); );
existingSubdomains.add(subdomainDeduplicated);
if (!options.dryRun) { if (!options.dryRun) {
await this.workspaceRepository.update(workspaceId, { await this.workspaceRepository.update(workspaceId, {
subdomain: subdomainDeduplicated, subdomain: subdomainDeduplicated,
@ -89,7 +102,7 @@ export class GenerateDefaultSubdomainCommand extends ActiveWorkspacesCommandRunn
} }
async executeActiveWorkspacesCommand( async executeActiveWorkspacesCommand(
passedParam: string[], _passedParam: string[],
options: BaseCommandOptions, options: BaseCommandOptions,
activeWorkspaceIds: string[], activeWorkspaceIds: string[],
): Promise<void> { ): Promise<void> {
@ -116,8 +129,15 @@ export class GenerateDefaultSubdomainCommand extends ActiveWorkspacesCommandRunn
), ),
); );
const existingSubdomains: Set<string> = new Set();
for (const [subdomain, workspaceIds] of workspaceBySubdomain) { for (const [subdomain, workspaceIds] of workspaceBySubdomain) {
await this.deduplicateAndSave(subdomain, workspaceIds, options); await this.deduplicateAndSave(
subdomain,
workspaceIds,
existingSubdomains,
options,
);
} }
} }
} }

View File

@ -1,13 +1,14 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm'; import { TypeOrmModule } from '@nestjs/typeorm';
import { GenerateDefaultSubdomainCommand } from 'src/database/commands/upgrade-version/0-34/0-34-generate-subdomain.command';
import { UpgradeTo0_34Command } from 'src/database/commands/upgrade-version/0-34/0-34-upgrade-version.command';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity'; import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity'; import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity'; import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
import { SearchModule } from 'src/engine/metadata-modules/search/search.module'; import { SearchModule } from 'src/engine/metadata-modules/search/search.module';
import { WorkspaceMigrationRunnerModule } from 'src/engine/workspace-manager/workspace-migration-runner/workspace-migration-runner.module'; import { WorkspaceMigrationRunnerModule } from 'src/engine/workspace-manager/workspace-migration-runner/workspace-migration-runner.module';
import { WorkspaceSyncMetadataCommandsModule } from 'src/engine/workspace-manager/workspace-sync-metadata/commands/workspace-sync-metadata-commands.module'; import { WorkspaceSyncMetadataCommandsModule } from 'src/engine/workspace-manager/workspace-sync-metadata/commands/workspace-sync-metadata-commands.module';
import { UpgradeTo0_34Command } from 'src/database/commands/upgrade-version/0-34/0-34-upgrade-version.command';
@Module({ @Module({
imports: [ imports: [
@ -20,6 +21,6 @@ import { UpgradeTo0_34Command } from 'src/database/commands/upgrade-version/0-34
SearchModule, SearchModule,
WorkspaceMigrationRunnerModule, WorkspaceMigrationRunnerModule,
], ],
providers: [UpgradeTo0_34Command], providers: [UpgradeTo0_34Command, GenerateDefaultSubdomainCommand],
}) })
export class UpgradeTo0_33CommandModule {} export class UpgradeTo0_34CommandModule {}