-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Add delete name column from standard object tables #7470
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
|
||
import chalk from 'chalk'; | ||
import { Command } from 'nest-commander'; | ||
import { Repository } from 'typeorm'; | ||
|
||
import { | ||
ActiveWorkspacesCommandOptions, | ||
ActiveWorkspacesCommandRunner, | ||
} from 'src/database/commands/active-workspaces.command'; | ||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity'; | ||
import { FieldMetadataType } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity'; | ||
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity'; | ||
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager'; | ||
|
||
@Command({ | ||
name: 'upgrade-0.31:delete-name-column-standard-object-tables', | ||
description: 'Delete name column from standard object tables', | ||
}) | ||
export class DeleteNameColumnStandardObjectTablesCommand extends ActiveWorkspacesCommandRunner { | ||
constructor( | ||
@InjectRepository(Workspace, 'core') | ||
protected readonly workspaceRepository: Repository<Workspace>, | ||
@InjectRepository(ObjectMetadataEntity, 'metadata') | ||
private readonly objectMetadataRepository: Repository<ObjectMetadataEntity>, | ||
private readonly twentyORMGlobalManager: TwentyORMGlobalManager, | ||
) { | ||
super(workspaceRepository); | ||
} | ||
|
||
async executeActiveWorkspacesCommand( | ||
_passedParam: string[], | ||
options: ActiveWorkspacesCommandOptions, | ||
workspaceIds: string[], | ||
): Promise<void> { | ||
this.logger.log('Running command to fix migration'); | ||
|
||
for (const workspaceId of workspaceIds) { | ||
this.logger.log(`Running command for workspace ${workspaceId}`); | ||
|
||
try { | ||
this.logger.log( | ||
chalk.green(`Deleting name columns from workspace ${workspaceId}.`), | ||
); | ||
|
||
const standardObjects = await this.objectMetadataRepository.find({ | ||
where: { | ||
isCustom: false, | ||
workspaceId, | ||
}, | ||
relations: ['fields'], | ||
}); | ||
|
||
const dataSource = | ||
await this.twentyORMGlobalManager.getDataSourceForWorkspace( | ||
workspaceId, | ||
); | ||
|
||
dataSource.transaction(async (entityManager) => { | ||
const queryRunner = entityManager.queryRunner; | ||
|
||
for (const standardObject of standardObjects) { | ||
if (options.dryRun) { | ||
this.logger.log( | ||
chalk.yellow( | ||
`Dry run mode enabled. Skipping deletion of name column for workspace ${workspaceId} and table ${standardObject.nameSingular}.`, | ||
), | ||
); | ||
continue; | ||
} | ||
|
||
const nameColumnExists = await queryRunner?.hasColumn( | ||
standardObject.nameSingular, | ||
'name', | ||
); | ||
|
||
const nameFieldMetadataExists = standardObject.fields.some( | ||
(field) => | ||
field.name === 'name' && field.type === FieldMetadataType.TEXT, | ||
); | ||
|
||
if (nameFieldMetadataExists) { | ||
this.logger.log( | ||
chalk.yellow( | ||
`Name field exists for workspace ${workspaceId} and table ${standardObject.nameSingular}. Skipping deletion.`, | ||
), | ||
); | ||
continue; | ||
} | ||
|
||
if (!nameColumnExists) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a usecase in which we expect to have the fieldMetadata but not the column? or would that just not be our problem here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point however that shouldn't be possible or at least that should be part of the sync-metadata health-check (which is broken though...) |
||
this.logger.log( | ||
chalk.yellow( | ||
`Name column does not exist for workspace ${workspaceId} and table ${standardObject.nameSingular}. Skipping deletion.`, | ||
), | ||
); | ||
continue; | ||
} | ||
|
||
await queryRunner?.dropColumn(standardObject.nameSingular, 'name'); | ||
} | ||
}); | ||
} catch (error) { | ||
this.logger.log( | ||
chalk.red( | ||
`Running command on workspace ${workspaceId} failed with error: ${error}`, | ||
), | ||
); | ||
continue; | ||
} finally { | ||
this.logger.log( | ||
chalk.green(`Finished running command for workspace ${workspaceId}.`), | ||
); | ||
|
||
await this.twentyORMGlobalManager.destroyDataSourceForWorkspace( | ||
workspaceId, | ||
); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: Ensure queryRunner is not null before using optional chaining