Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: workspace health target column map fix #3932

Merged
merged 7 commits into from
Feb 15, 2024
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: 2 additions & 0 deletions packages/twenty-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@
"@ptc-org/nestjs-query-graphql": "patch:@ptc-org/nestjs-query-graphql@4.2.0#./patches/@ptc-org+nestjs-query-graphql+4.2.0.patch",
"class-validator": "patch:class-validator@0.14.0#./patches/class-validator+0.14.0.patch",
"graphql-middleware": "^6.1.35",
"lodash.isequal": "^4.5.0",
"passport": "^0.7.0"
},
"devDependencies": {
"@nestjs/cli": "10.3.0",
"@nx/js": "17.2.8",
"@types/lodash.isempty": "^4.4.7",
"@types/lodash.isequal": "^4.5.8",
"@types/lodash.isobject": "^3.0.7",
"@types/lodash.omit": "^4.5.9",
"@types/lodash.snakecase": "^4.1.7",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ import {
RelationMetadataEntity,
RelationMetadataType,
} from 'src/metadata/relation-metadata/relation-metadata.entity';
import {
computeCustomName,
computeObjectTargetTable,
} from 'src/workspace/utils/compute-object-target-table.util';
import { computeCustomName } from 'src/workspace/utils/compute-custom-name.util';
import { computeObjectTargetTable } from 'src/workspace/utils/compute-object-target-table.util';
import { DeleteOneObjectInput } from 'src/metadata/object-metadata/dtos/delete-object.input';
import { RelationToDelete } from 'src/metadata/relation-metadata/types/relation-to-delete';
import { generateMigrationName } from 'src/metadata/workspace-migration/utils/generate-migration-name.util';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const customNamePrefix = '_';

export const computeCustomName = (name: string, isCustom: boolean) => {
return isCustom ? `${customNamePrefix}${name}` : name;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { FieldMetadataInterface } from 'src/metadata/field-metadata/interfaces/field-metadata.interface';

import { FieldMetadataEntity } from 'src/metadata/field-metadata/field-metadata.entity';
import { isCompositeFieldMetadataType } from 'src/metadata/field-metadata/utils/is-composite-field-metadata-type.util';
import { BasicFieldMetadataType } from 'src/metadata/workspace-migration/factories/basic-column-action.factory';

import { computeCustomName } from './compute-custom-name.util';

export const computeFieldTargetColumn = (
fieldMetadata:
| FieldMetadataEntity<BasicFieldMetadataType>
| FieldMetadataInterface<BasicFieldMetadataType>,
) => {
if (isCompositeFieldMetadataType(fieldMetadata.type)) {
throw new Error(
"Composite field metadata should not be computed here, as they're split into multiple fields.",
);
}

return computeCustomName(fieldMetadata.name, fieldMetadata.isCustom ?? false);
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ObjectMetadataInterface } from 'src/metadata/field-metadata/interfaces/object-metadata.interface';

import { computeCustomName } from './compute-custom-name.util';

export const computeObjectTargetTable = (
objectMetadata: ObjectMetadataInterface,
) => {
Expand All @@ -8,7 +10,3 @@ export const computeObjectTargetTable = (
objectMetadata.isCustom,
);
};

export const computeCustomName = (name: string, isCustom: boolean) => {
return isCustom ? `_${name}` : name;
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { CommandLogger } from 'src/commands/command-logger';

interface WorkspaceHealthCommandOptions {
workspaceId: string;
verbose?: boolean;
mode?: WorkspaceHealthMode;
fix?: WorkspaceHealthFixKind;
dryRun?: boolean;
Expand Down Expand Up @@ -49,7 +48,7 @@ export class WorkspaceHealthCommand extends CommandRunner {
chalk.red(`Workspace is not healthy, found ${issues.length} issues`),
);

if (options.verbose) {
if (options.dryRun) {
await this.commandLogger.writeLog(
`workspace-health-issues-${options.workspaceId}`,
issues,
Expand All @@ -61,25 +60,30 @@ export class WorkspaceHealthCommand extends CommandRunner {
if (options.fix) {
this.logger.log(chalk.yellow('Fixing issues'));

const workspaceMigrations = await this.workspaceHealthService.fixIssues(
options.workspaceId,
issues,
{
type: options.fix,
applyChanges: !options.dryRun,
},
);
const { workspaceMigrations, metadataEntities } =
await this.workspaceHealthService.fixIssues(
options.workspaceId,
issues,
{
type: options.fix,
applyChanges: !options.dryRun,
},
);
const totalCount = workspaceMigrations.length + metadataEntities.length;

if (options.dryRun) {
await this.commandLogger.writeLog(
`workspace-health-${options.fix}-migrations`,
workspaceMigrations,
);

await this.commandLogger.writeLog(
`workspace-health-${options.fix}-metadata-entities`,
metadataEntities,
);
} else {
this.logger.log(
chalk.green(
`Fixed ${workspaceMigrations.length}/${issues.length} issues`,
),
chalk.green(`Fixed ${totalCount}/${issues.length} issues`),
);
}
}
Expand Down Expand Up @@ -107,15 +111,6 @@ export class WorkspaceHealthCommand extends CommandRunner {
return value as WorkspaceHealthFixKind;
}

@Option({
flags: '-v, --verbose',
description: 'Detailed output',
required: false,
})
parseVerbose(): boolean {
return true;
}

@Option({
flags: '-m, --mode [mode]',
description: 'Mode of the health check [structure, metadata, all]',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { EntityManager } from 'typeorm';

import {
WorkspaceHealthIssue,
WorkspaceHealthIssueType,
WorkspaceIssueTypeToInterface,
} from 'src/workspace/workspace-health/interfaces/workspace-health-issue.interface';

import { ObjectMetadataEntity } from 'src/metadata/object-metadata/object-metadata.entity';
import { WorkspaceMigrationEntity } from 'src/metadata/workspace-migration/workspace-migration.entity';

export class CompareEntity<T> {
current: T | null;
altered: T | null;
}

export abstract class AbstractWorkspaceFixer<
IssueTypes extends WorkspaceHealthIssueType,
UpdateRecordEntities = unknown,
> {
private issueTypes: IssueTypes[];

constructor(...issueTypes: IssueTypes[]) {
this.issueTypes = issueTypes;
}

filterIssues(
issues: WorkspaceHealthIssue[],
): WorkspaceIssueTypeToInterface<IssueTypes>[] {
return issues.filter(
(issue): issue is WorkspaceIssueTypeToInterface<IssueTypes> =>
this.issueTypes.includes(issue.type as IssueTypes),
);
}

protected splitIssuesByType(
issues: WorkspaceIssueTypeToInterface<IssueTypes>[],
): Record<IssueTypes, WorkspaceIssueTypeToInterface<IssueTypes>[]> {
return issues.reduce(
(
acc: Record<IssueTypes, WorkspaceIssueTypeToInterface<IssueTypes>[]>,
issue,
) => {
const type = issue.type as IssueTypes;

if (!acc[type]) {
acc[type] = [];
}
acc[type].push(issue);

return acc;
},
{} as Record<IssueTypes, WorkspaceIssueTypeToInterface<IssueTypes>[]>,
);
}

async createWorkspaceMigrations?(
manager: EntityManager,
objectMetadataCollection: ObjectMetadataEntity[],
issues: WorkspaceIssueTypeToInterface<IssueTypes>[],
): Promise<Partial<WorkspaceMigrationEntity>[]>;

async createMetadataUpdates?(
manager: EntityManager,
objectMetadataCollection: ObjectMetadataEntity[],
issues: WorkspaceIssueTypeToInterface<IssueTypes>[],
): Promise<CompareEntity<UpdateRecordEntities>[]>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { WorkspaceNullableFixer } from './workspace-nullable.fixer';
import { WorkspaceDefaultValueFixer } from './workspace-default-value.fixer';
import { WorkspaceTypeFixer } from './workspace-type.fixer';
import { WorkspaceTargetColumnMapFixer } from './workspace-target-column-map.fixer';

export const workspaceFixers = [
WorkspaceNullableFixer,
WorkspaceDefaultValueFixer,
WorkspaceTypeFixer,
WorkspaceTargetColumnMapFixer,
];
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,26 @@ import { ObjectMetadataEntity } from 'src/metadata/object-metadata/object-metada
import { WorkspaceMigrationEntity } from 'src/metadata/workspace-migration/workspace-migration.entity';
import { WorkspaceMigrationFieldFactory } from 'src/workspace/workspace-migration-builder/factories/workspace-migration-field.factory';

type WorkspaceHealthDefaultValueIssue =
WorkspaceHealthColumnIssue<WorkspaceHealthIssueType.COLUMN_DEFAULT_VALUE_CONFLICT>;
import { AbstractWorkspaceFixer } from './abstract-workspace.fixer';

@Injectable()
export class WorkspaceFixDefaultValueService {
export class WorkspaceDefaultValueFixer extends AbstractWorkspaceFixer<WorkspaceHealthIssueType.COLUMN_DEFAULT_VALUE_CONFLICT> {
constructor(
private readonly workspaceMigrationFieldFactory: WorkspaceMigrationFieldFactory,
) {}
) {
super(WorkspaceHealthIssueType.COLUMN_DEFAULT_VALUE_CONFLICT);
}

async fix(
async createWorkspaceMigrations(
manager: EntityManager,
objectMetadataCollection: ObjectMetadataEntity[],
issues: WorkspaceHealthDefaultValueIssue[],
issues: WorkspaceHealthColumnIssue<WorkspaceHealthIssueType.COLUMN_DEFAULT_VALUE_CONFLICT>[],
): Promise<Partial<WorkspaceMigrationEntity>[]> {
const workspaceMigrations: Partial<WorkspaceMigrationEntity>[] = [];
const defaultValueIssues = issues.filter(
(issue) =>
issue.type === WorkspaceHealthIssueType.COLUMN_DEFAULT_VALUE_CONFLICT,
) as WorkspaceHealthColumnIssue<WorkspaceHealthIssueType.COLUMN_DEFAULT_VALUE_CONFLICT>[];

if (defaultValueIssues.length > 0) {
const columnDefaultValueWorkspaceMigrations =
await this.fixColumnDefaultValueIssues(
objectMetadataCollection,
defaultValueIssues,
);

workspaceMigrations.push(...columnDefaultValueWorkspaceMigrations);
if (issues.length <= 0) {
return [];
}

return workspaceMigrations;
return this.fixColumnDefaultValueIssues(objectMetadataCollection, issues);
}

private async fixColumnDefaultValueIssues(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,30 @@ import {
} from 'src/workspace/workspace-health/interfaces/workspace-health-issue.interface';
import { WorkspaceMigrationBuilderAction } from 'src/workspace/workspace-migration-builder/interfaces/workspace-migration-builder-action.interface';

import { WorkspaceMigrationEntity } from 'src/metadata/workspace-migration/workspace-migration.entity';
import { ObjectMetadataEntity } from 'src/metadata/object-metadata/object-metadata.entity';
import { WorkspaceMigrationEntity } from 'src/metadata/workspace-migration/workspace-migration.entity';
import { WorkspaceMigrationFieldFactory } from 'src/workspace/workspace-migration-builder/factories/workspace-migration-field.factory';

type WorkspaceHealthNullableIssue =
WorkspaceHealthColumnIssue<WorkspaceHealthIssueType.COLUMN_NULLABILITY_CONFLICT>;
import { AbstractWorkspaceFixer } from './abstract-workspace.fixer';

@Injectable()
export class WorkspaceFixNullableService {
export class WorkspaceNullableFixer extends AbstractWorkspaceFixer<WorkspaceHealthIssueType.COLUMN_NULLABILITY_CONFLICT> {
constructor(
private readonly workspaceMigrationFieldFactory: WorkspaceMigrationFieldFactory,
) {}
) {
super(WorkspaceHealthIssueType.COLUMN_NULLABILITY_CONFLICT);
}

async fix(
async createWorkspaceMigrations(
manager: EntityManager,
objectMetadataCollection: ObjectMetadataEntity[],
issues: WorkspaceHealthNullableIssue[],
issues: WorkspaceHealthColumnIssue<WorkspaceHealthIssueType.COLUMN_NULLABILITY_CONFLICT>[],
): Promise<Partial<WorkspaceMigrationEntity>[]> {
const workspaceMigrations: Partial<WorkspaceMigrationEntity>[] = [];
const nullabilityIssues = issues.filter(
(issue) =>
issue.type === WorkspaceHealthIssueType.COLUMN_NULLABILITY_CONFLICT,
) as WorkspaceHealthColumnIssue<WorkspaceHealthIssueType.COLUMN_NULLABILITY_CONFLICT>[];

if (nullabilityIssues.length > 0) {
const columnNullabilityWorkspaceMigrations =
await this.fixColumnNullabilityIssues(
objectMetadataCollection,
nullabilityIssues,
);

workspaceMigrations.push(...columnNullabilityWorkspaceMigrations);
if (issues.length <= 0) {
return [];
}

return workspaceMigrations;
return this.fixColumnNullabilityIssues(objectMetadataCollection, issues);
}

private async fixColumnNullabilityIssues(
Expand Down
Loading
Loading