Migrate domainName field from text type to links type (#6410)
Closes #5759.
This commit is contained in:
@ -0,0 +1,302 @@
|
||||
import { Logger } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
|
||||
import chalk from 'chalk';
|
||||
import { Command, CommandRunner, Option } from 'nest-commander';
|
||||
import { QueryRunner, Repository } from 'typeorm';
|
||||
|
||||
import { TypeORMService } from 'src/database/typeorm/typeorm.service';
|
||||
import { DataSourceEntity } from 'src/engine/metadata-modules/data-source/data-source.entity';
|
||||
import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service';
|
||||
import { CreateFieldInput } from 'src/engine/metadata-modules/field-metadata/dtos/create-field.input';
|
||||
import {
|
||||
FieldMetadataEntity,
|
||||
FieldMetadataType,
|
||||
} from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
|
||||
import { FieldMetadataService } from 'src/engine/metadata-modules/field-metadata/field-metadata.service';
|
||||
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
|
||||
import { WorkspaceStatusService } from 'src/engine/workspace-manager/workspace-status/services/workspace-status.service';
|
||||
import { COMPANY_STANDARD_FIELD_IDS } from 'src/engine/workspace-manager/workspace-sync-metadata/constants/standard-field-ids';
|
||||
import { ViewService } from 'src/modules/view/services/view.service';
|
||||
import { ViewFieldWorkspaceEntity } from 'src/modules/view/standard-objects/view-field.workspace-entity';
|
||||
|
||||
interface MigrateDomainNameFromTextToLinksCommandOptions {
|
||||
workspaceId?: string;
|
||||
}
|
||||
|
||||
@Command({
|
||||
name: 'migrate-0.23:migrate-domain-standard-field-to-links',
|
||||
description:
|
||||
'Migrating field domainName of deprecated type TEXT to type LINKS',
|
||||
})
|
||||
export class MigrateDomainNameFromTextToLinksCommand extends CommandRunner {
|
||||
private readonly logger = new Logger(
|
||||
MigrateDomainNameFromTextToLinksCommand.name,
|
||||
);
|
||||
constructor(
|
||||
@InjectRepository(FieldMetadataEntity, 'metadata')
|
||||
private readonly fieldMetadataRepository: Repository<FieldMetadataEntity>,
|
||||
private readonly fieldMetadataService: FieldMetadataService,
|
||||
private readonly twentyORMGlobalManager: TwentyORMGlobalManager,
|
||||
private readonly typeORMService: TypeORMService,
|
||||
private readonly dataSourceService: DataSourceService,
|
||||
private readonly workspaceStatusService: WorkspaceStatusService,
|
||||
private readonly viewService: ViewService,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
@Option({
|
||||
flags: '-w, --workspace-id [workspace_id]',
|
||||
description:
|
||||
'workspace id. Command runs on all active workspaces if not provided',
|
||||
required: false,
|
||||
})
|
||||
parseWorkspaceId(value: string): string {
|
||||
return value;
|
||||
}
|
||||
|
||||
async run(
|
||||
_passedParam: string[],
|
||||
options: MigrateDomainNameFromTextToLinksCommandOptions,
|
||||
): Promise<void> {
|
||||
this.logger.log(
|
||||
'Running command to migrate standard field domainName from text to Link',
|
||||
);
|
||||
let workspaceIds: string[] = [];
|
||||
|
||||
if (options.workspaceId) {
|
||||
workspaceIds = [options.workspaceId];
|
||||
} else {
|
||||
const activeWorkspaceIds =
|
||||
await this.workspaceStatusService.getActiveWorkspaceIds();
|
||||
|
||||
workspaceIds = activeWorkspaceIds;
|
||||
}
|
||||
|
||||
if (!workspaceIds.length) {
|
||||
this.logger.log(chalk.yellow('No workspace found'));
|
||||
|
||||
return;
|
||||
} else {
|
||||
this.logger.log(
|
||||
chalk.green(`Running command on ${workspaceIds.length} workspaces`),
|
||||
);
|
||||
}
|
||||
|
||||
for (const workspaceId of workspaceIds) {
|
||||
this.logger.log(`Running command for workspace ${workspaceId}`);
|
||||
try {
|
||||
const dataSourceMetadata =
|
||||
await this.dataSourceService.getLastDataSourceMetadataFromWorkspaceId(
|
||||
workspaceId,
|
||||
);
|
||||
|
||||
if (!dataSourceMetadata) {
|
||||
throw new Error(
|
||||
`Could not find dataSourceMetadata for workspace ${workspaceId}`,
|
||||
);
|
||||
}
|
||||
|
||||
const workspaceDataSource =
|
||||
await this.typeORMService.connectToDataSource(dataSourceMetadata);
|
||||
|
||||
if (!workspaceDataSource) {
|
||||
throw new Error(
|
||||
`Could not connect to dataSource for workspace ${workspaceId}`,
|
||||
);
|
||||
}
|
||||
|
||||
const domainNameField = await this.fieldMetadataRepository.findOneBy({
|
||||
workspaceId,
|
||||
standardId: COMPANY_STANDARD_FIELD_IDS.domainName,
|
||||
});
|
||||
|
||||
if (!domainNameField) {
|
||||
throw new Error('Could not find domainName field');
|
||||
}
|
||||
|
||||
if (domainNameField.type === FieldMetadataType.LINKS) {
|
||||
this.logger.log(
|
||||
`Field domainName is already of type LINKS, skipping migration.`,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
this.logger.log(`Attempting to migrate domainName field.`);
|
||||
|
||||
const workspaceQueryRunner = workspaceDataSource.createQueryRunner();
|
||||
|
||||
await workspaceQueryRunner.connect();
|
||||
|
||||
const fieldName = domainNameField.name;
|
||||
const { id: _id, ...domainNameFieldWithoutId } = domainNameField;
|
||||
|
||||
try {
|
||||
const tmpNewDomainLinksField =
|
||||
await this.fieldMetadataService.createOne({
|
||||
...domainNameFieldWithoutId,
|
||||
type: FieldMetadataType.LINKS,
|
||||
name: `${fieldName}Tmp`,
|
||||
defaultValue: {
|
||||
primaryLinkUrl: domainNameField.defaultValue,
|
||||
secondaryLinks: null,
|
||||
primaryLinkLabel: "''",
|
||||
},
|
||||
} satisfies CreateFieldInput);
|
||||
|
||||
// Migrate data from domainName to primaryLinkUrl
|
||||
await this.migrateDataWithinCompanyTable({
|
||||
sourceColumnName: `${domainNameField.name}`,
|
||||
targetColumnName: `${tmpNewDomainLinksField.name}PrimaryLinkUrl`,
|
||||
workspaceQueryRunner,
|
||||
dataSourceMetadata,
|
||||
});
|
||||
|
||||
// Duplicate initial domainName text field's views behaviour for new domainName field
|
||||
await this.viewService.removeFieldFromViews({
|
||||
workspaceId: workspaceId,
|
||||
fieldId: tmpNewDomainLinksField.id,
|
||||
});
|
||||
|
||||
const viewFieldRepository =
|
||||
await this.twentyORMGlobalManager.getRepositoryForWorkspace<ViewFieldWorkspaceEntity>(
|
||||
workspaceId,
|
||||
'viewField',
|
||||
);
|
||||
const viewFieldsWithDeprecatedField = await viewFieldRepository.find({
|
||||
where: {
|
||||
fieldMetadataId: domainNameField.id,
|
||||
isVisible: true,
|
||||
},
|
||||
});
|
||||
|
||||
await this.viewService.addFieldToViews({
|
||||
workspaceId: workspaceId,
|
||||
fieldId: tmpNewDomainLinksField.id,
|
||||
viewsIds: viewFieldsWithDeprecatedField
|
||||
.filter((viewField) => viewField.viewId !== null)
|
||||
.map((viewField) => viewField.viewId as string),
|
||||
positions: viewFieldsWithDeprecatedField.reduce(
|
||||
(acc, viewField) => {
|
||||
if (!viewField.viewId) {
|
||||
return acc;
|
||||
}
|
||||
acc[viewField.viewId] = viewField.position;
|
||||
|
||||
return acc;
|
||||
},
|
||||
[],
|
||||
),
|
||||
size: 150,
|
||||
});
|
||||
|
||||
// Delete initial domainName text field
|
||||
await this.fieldMetadataService.deleteOneField(
|
||||
{ id: domainNameField.id },
|
||||
workspaceId,
|
||||
);
|
||||
|
||||
// Rename temporary domainName links field
|
||||
await this.fieldMetadataService.updateOne(tmpNewDomainLinksField.id, {
|
||||
id: tmpNewDomainLinksField.id,
|
||||
workspaceId: tmpNewDomainLinksField.workspaceId,
|
||||
name: `${fieldName}`,
|
||||
isCustom: false,
|
||||
});
|
||||
|
||||
this.logger.log(`Migration of domainName done!`);
|
||||
} catch (error) {
|
||||
this.logger.log(
|
||||
`Failed to migrate domainName ${domainNameField.id}, rolling back.`,
|
||||
);
|
||||
|
||||
// Re-create initial field if it was deleted
|
||||
const initialField =
|
||||
await this.fieldMetadataService.findOneWithinWorkspace(
|
||||
workspaceId,
|
||||
{
|
||||
where: {
|
||||
name: `${domainNameField.name}`,
|
||||
objectMetadataId: domainNameField.objectMetadataId,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
const tmpNewDomainLinksField =
|
||||
await this.fieldMetadataService.findOneWithinWorkspace(
|
||||
workspaceId,
|
||||
{
|
||||
where: {
|
||||
name: `${domainNameField.name}Tmp`,
|
||||
objectMetadataId: domainNameField.objectMetadataId,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
if (!initialField) {
|
||||
this.logger.log(`Re-creating initial domainName field`);
|
||||
const restoredField = await this.fieldMetadataService.createOne({
|
||||
...domainNameField,
|
||||
});
|
||||
|
||||
if (tmpNewDomainLinksField) {
|
||||
this.logger.log(`Restoring data in domainName`);
|
||||
await this.migrateDataWithinCompanyTable({
|
||||
sourceColumnName: `${tmpNewDomainLinksField.name}PrimaryLinkLabel`,
|
||||
targetColumnName: `${restoredField.name}PrimaryLinkLabel`,
|
||||
workspaceQueryRunner,
|
||||
dataSourceMetadata,
|
||||
});
|
||||
|
||||
await this.migrateDataWithinCompanyTable({
|
||||
sourceColumnName: `${tmpNewDomainLinksField.name}PrimaryLinkUrl`,
|
||||
targetColumnName: `${restoredField.name}PrimaryLinkUrl`,
|
||||
workspaceQueryRunner,
|
||||
dataSourceMetadata,
|
||||
});
|
||||
} else {
|
||||
this.logger.log(
|
||||
`Failed to restore data in domainName field ${domainNameField.id}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (tmpNewDomainLinksField) {
|
||||
await this.fieldMetadataService.deleteOneField(
|
||||
{ id: tmpNewDomainLinksField.id },
|
||||
workspaceId,
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
await workspaceQueryRunner.release();
|
||||
}
|
||||
} catch (error) {
|
||||
this.logger.log(
|
||||
chalk.red(
|
||||
`Running command on workspace ${workspaceId} failed with error: ${error}`,
|
||||
),
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
this.logger.log(chalk.green(`Command completed!`));
|
||||
}
|
||||
}
|
||||
|
||||
private async migrateDataWithinCompanyTable({
|
||||
sourceColumnName,
|
||||
targetColumnName,
|
||||
workspaceQueryRunner,
|
||||
dataSourceMetadata,
|
||||
}: {
|
||||
sourceColumnName: string;
|
||||
targetColumnName: string;
|
||||
workspaceQueryRunner: QueryRunner;
|
||||
dataSourceMetadata: DataSourceEntity;
|
||||
}) {
|
||||
await workspaceQueryRunner.query(
|
||||
`UPDATE "${dataSourceMetadata.schema}"."company" SET "${targetColumnName}" = CASE WHEN "${sourceColumnName}" LIKE 'http%' THEN "${sourceColumnName}" ELSE 'https://' || "${sourceColumnName}" END;`,
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
import { Command, CommandRunner, Option } from 'nest-commander';
|
||||
|
||||
import { MigrateDomainNameFromTextToLinksCommand } from 'src/database/commands/upgrade-version/0-23/0-23-migrate-domain-to-links.command';
|
||||
import { MigrateLinkFieldsToLinksCommand } from 'src/database/commands/upgrade-version/0-23/0-23-migrate-link-fields-to-links.command';
|
||||
import { MigrateMessageChannelSyncStatusEnumCommand } from 'src/database/commands/upgrade-version/0-23/0-23-migrate-message-channel-sync-status-enum.command';
|
||||
|
||||
@ -14,6 +15,7 @@ interface Options {
|
||||
export class UpgradeTo0_23Command extends CommandRunner {
|
||||
constructor(
|
||||
private readonly migrateLinkFieldsToLinks: MigrateLinkFieldsToLinksCommand,
|
||||
private readonly migrateDomainNameFromTextToLinks: MigrateDomainNameFromTextToLinksCommand,
|
||||
private readonly migrateMessageChannelSyncStatusEnumCommand: MigrateMessageChannelSyncStatusEnumCommand,
|
||||
) {
|
||||
super();
|
||||
@ -31,6 +33,7 @@ export class UpgradeTo0_23Command extends CommandRunner {
|
||||
|
||||
async run(_passedParam: string[], options: Options): Promise<void> {
|
||||
await this.migrateLinkFieldsToLinks.run(_passedParam, options);
|
||||
await this.migrateDomainNameFromTextToLinks.run(_passedParam, options);
|
||||
await this.migrateMessageChannelSyncStatusEnumCommand.run(
|
||||
_passedParam,
|
||||
options,
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
|
||||
import { MigrateDomainNameFromTextToLinksCommand } from 'src/database/commands/upgrade-version/0-23/0-23-migrate-domain-to-links.command';
|
||||
import { MigrateLinkFieldsToLinksCommand } from 'src/database/commands/upgrade-version/0-23/0-23-migrate-link-fields-to-links.command';
|
||||
import { MigrateMessageChannelSyncStatusEnumCommand } from 'src/database/commands/upgrade-version/0-23/0-23-migrate-message-channel-sync-status-enum.command';
|
||||
import { UpgradeTo0_23Command } from 'src/database/commands/upgrade-version/0-23/0-23-upgrade-version.command';
|
||||
@ -28,6 +29,7 @@ import { ViewModule } from 'src/modules/view/view.module';
|
||||
],
|
||||
providers: [
|
||||
MigrateLinkFieldsToLinksCommand,
|
||||
MigrateDomainNameFromTextToLinksCommand,
|
||||
MigrateMessageChannelSyncStatusEnumCommand,
|
||||
UpgradeTo0_23Command,
|
||||
],
|
||||
|
||||
@ -28,7 +28,7 @@ export const seedCompanies = async (
|
||||
.into(`${schemaName}.${tableName}`, [
|
||||
'id',
|
||||
'name',
|
||||
'domainName',
|
||||
'domainNamePrimaryLinkUrl',
|
||||
'addressAddressStreet1',
|
||||
'addressAddressStreet2',
|
||||
'addressAddressCity',
|
||||
@ -42,7 +42,7 @@ export const seedCompanies = async (
|
||||
{
|
||||
id: DEV_SEED_COMPANY_IDS.LINKEDIN,
|
||||
name: 'Linkedin',
|
||||
domainName: 'linkedin.com',
|
||||
domainNamePrimaryLinkUrl: { primarlyLinkUrl: 'https://linkedin.com' },
|
||||
addressAddressStreet1: 'Eutaw Street',
|
||||
addressAddressStreet2: null,
|
||||
addressAddressCity: 'Dublin',
|
||||
@ -54,7 +54,7 @@ export const seedCompanies = async (
|
||||
{
|
||||
id: DEV_SEED_COMPANY_IDS.FACEBOOK,
|
||||
name: 'Facebook',
|
||||
domainName: 'facebook.com',
|
||||
domainNamePrimaryLinkUrl: { primarlyLinkUrl: 'https://facebook.com' },
|
||||
addressAddressStreet1: null,
|
||||
addressAddressStreet2: null,
|
||||
addressAddressCity: null,
|
||||
@ -66,7 +66,7 @@ export const seedCompanies = async (
|
||||
{
|
||||
id: DEV_SEED_COMPANY_IDS.QONTO,
|
||||
name: 'Qonto',
|
||||
domainName: 'qonto.com',
|
||||
domainNamePrimaryLinkUrl: { primarlyLinkUrl: 'https://qonto.com' },
|
||||
addressAddressStreet1: '18 rue de navarrin',
|
||||
addressAddressStreet2: null,
|
||||
addressAddressCity: 'Paris',
|
||||
@ -78,7 +78,7 @@ export const seedCompanies = async (
|
||||
{
|
||||
id: DEV_SEED_COMPANY_IDS.MICROSOFT,
|
||||
name: 'Microsoft',
|
||||
domainName: 'microsoft.com',
|
||||
domainNamePrimaryLinkUrl: { primarlyLinkUrl: 'https://microsoft.com' },
|
||||
addressAddressStreet1: null,
|
||||
addressAddressStreet2: null,
|
||||
addressAddressCity: null,
|
||||
@ -90,7 +90,7 @@ export const seedCompanies = async (
|
||||
{
|
||||
id: DEV_SEED_COMPANY_IDS.AIRBNB,
|
||||
name: 'Airbnb',
|
||||
domainName: 'airbnb.com',
|
||||
domainNamePrimaryLinkUrl: { primarlyLinkUrl: 'https://airbnb.com' },
|
||||
addressAddressStreet1: '888 Brannan St',
|
||||
addressAddressStreet2: null,
|
||||
addressAddressCity: 'San Francisco',
|
||||
@ -102,7 +102,7 @@ export const seedCompanies = async (
|
||||
{
|
||||
id: DEV_SEED_COMPANY_IDS.GOOGLE,
|
||||
name: 'Google',
|
||||
domainName: 'google.com',
|
||||
domainNamePrimaryLinkUrl: { primarlyLinkUrl: 'https://google.com' },
|
||||
addressAddressStreet1: '760 Market St',
|
||||
addressAddressStreet2: 'Floor 10',
|
||||
addressAddressCity: 'San Francisco',
|
||||
@ -114,7 +114,7 @@ export const seedCompanies = async (
|
||||
{
|
||||
id: DEV_SEED_COMPANY_IDS.NETFLIX,
|
||||
name: 'Netflix',
|
||||
domainName: 'netflix.com',
|
||||
domainNamePrimaryLinkUrl: { primarlyLinkUrl: 'https://netflix.com' },
|
||||
addressAddressStreet1: '2300 Harrison St',
|
||||
addressAddressStreet2: null,
|
||||
addressAddressCity: 'San Francisco',
|
||||
@ -126,7 +126,7 @@ export const seedCompanies = async (
|
||||
{
|
||||
id: DEV_SEED_COMPANY_IDS.LIBEO,
|
||||
name: 'Libeo',
|
||||
domainName: 'libeo.io',
|
||||
domainNamePrimaryLinkUrl: { primarlyLinkUrl: 'https://libeo.io' },
|
||||
addressAddressStreet1: null,
|
||||
addressAddressStreet2: null,
|
||||
addressAddressCity: null,
|
||||
@ -138,7 +138,7 @@ export const seedCompanies = async (
|
||||
{
|
||||
id: DEV_SEED_COMPANY_IDS.CLAAP,
|
||||
name: 'Claap',
|
||||
domainName: 'claap.io',
|
||||
domainNamePrimaryLinkUrl: { primarlyLinkUrl: 'https://claap.io' },
|
||||
addressAddressStreet1: null,
|
||||
addressAddressStreet2: null,
|
||||
addressAddressCity: null,
|
||||
@ -150,7 +150,7 @@ export const seedCompanies = async (
|
||||
{
|
||||
id: DEV_SEED_COMPANY_IDS.HASURA,
|
||||
name: 'Hasura',
|
||||
domainName: 'hasura.io',
|
||||
domainNamePrimaryLinkUrl: { primarlyLinkUrl: 'https://hasura.io' },
|
||||
addressAddressStreet1: null,
|
||||
addressAddressStreet2: null,
|
||||
addressAddressCity: null,
|
||||
@ -162,7 +162,7 @@ export const seedCompanies = async (
|
||||
{
|
||||
id: DEV_SEED_COMPANY_IDS.WEWORK,
|
||||
name: 'Wework',
|
||||
domainName: 'wework.com',
|
||||
domainNamePrimaryLinkUrl: { primarlyLinkUrl: 'https://wework.com' },
|
||||
addressAddressStreet1: null,
|
||||
addressAddressStreet2: null,
|
||||
addressAddressCity: null,
|
||||
@ -174,7 +174,7 @@ export const seedCompanies = async (
|
||||
{
|
||||
id: DEV_SEED_COMPANY_IDS.SAMSUNG,
|
||||
name: 'Samsung',
|
||||
domainName: 'samsung.com',
|
||||
domainNamePrimaryLinkUrl: { primarlyLinkUrl: 'https://samsung.com' },
|
||||
addressAddressStreet1: null,
|
||||
addressAddressStreet2: null,
|
||||
addressAddressCity: null,
|
||||
@ -186,7 +186,7 @@ export const seedCompanies = async (
|
||||
{
|
||||
id: DEV_SEED_COMPANY_IDS.ALGOLIA,
|
||||
name: 'Algolia',
|
||||
domainName: 'algolia.com',
|
||||
domainNamePrimaryLinkUrl: { primarlyLinkUrl: 'https://algolia.com' },
|
||||
addressAddressStreet1: null,
|
||||
addressAddressStreet2: null,
|
||||
addressAddressCity: null,
|
||||
|
||||
Reference in New Issue
Block a user