-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Alter comment on foreign key deletion (#5406)
We do not update the comment on the local table when a foreign table key is deleted. This was not breaking, which is why we did not see it. But comments should be kept up to date. --------- Co-authored-by: Thomas Trompette <thomast@twenty.com>
- Loading branch information
Showing
5 changed files
with
191 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...metadata/utils/__tests__/create-migration-for-foreign-key-comment-alteration.util.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { buildAlteredCommentOnForeignKeyDeletion } from 'src/engine/metadata-modules/object-metadata/utils/create-migration-for-foreign-key-comment-alteration.util'; | ||
|
||
describe('buildAlteredCommentOnForeignKeyDeletion', () => { | ||
const localObjectMetadataName = 'favorite'; | ||
const remoteObjectMetadataName = 'blog'; | ||
const schema = 'schema'; | ||
const workspaceDataSource = { | ||
query: jest.fn(), | ||
}; | ||
|
||
it('should return null if no comment ', async () => { | ||
workspaceDataSource.query.mockResolvedValueOnce([]); | ||
|
||
const result = await buildAlteredCommentOnForeignKeyDeletion( | ||
localObjectMetadataName, | ||
remoteObjectMetadataName, | ||
schema, | ||
workspaceDataSource as any, | ||
); | ||
|
||
expect(result).toBeNull(); | ||
}); | ||
|
||
it('should return null if the existing comment does not contain foreign keys', async () => { | ||
workspaceDataSource.query.mockResolvedValueOnce([ | ||
{ col_description: '@graphql({"totalCount":{"enabled":true}})' }, | ||
]); | ||
|
||
const result = await buildAlteredCommentOnForeignKeyDeletion( | ||
localObjectMetadataName, | ||
remoteObjectMetadataName, | ||
schema, | ||
workspaceDataSource as any, | ||
); | ||
|
||
expect(result).toBeNull(); | ||
}); | ||
|
||
it('should return altered comment without foreign key', async () => { | ||
const existingComment = { | ||
col_description: `@graphql({"totalCount":{"enabled":true},"foreign_keys":[{"local_name":"favoriteCollection","local_columns":["${remoteObjectMetadataName}Id"],"foreign_name":"${remoteObjectMetadataName}","foreign_schema":"schema","foreign_table":"${remoteObjectMetadataName}","foreign_columns":["id"]}]})`, | ||
}; | ||
|
||
workspaceDataSource.query.mockResolvedValueOnce([existingComment]); | ||
|
||
const result = await buildAlteredCommentOnForeignKeyDeletion( | ||
localObjectMetadataName, | ||
remoteObjectMetadataName, | ||
schema, | ||
workspaceDataSource as any, | ||
); | ||
|
||
expect(result).toBe( | ||
'@graphql({"totalCount":{"enabled":true},"foreign_keys":[]})', | ||
); | ||
}); | ||
|
||
it('should return altered comment without the input foreign key', async () => { | ||
const existingComment = { | ||
col_description: `@graphql({"totalCount":{"enabled":true},"foreign_keys":[{"local_name":"favoriteCollection","local_columns":["${remoteObjectMetadataName}Id"],"foreign_name":"${remoteObjectMetadataName}","foreign_schema":"schema","foreign_table":"${remoteObjectMetadataName}","foreign_columns":["id"]}, {"local_name":"favoriteCollection","local_columns":["testId"],"foreign_name":"test","foreign_schema":"schema","foreign_table":"test","foreign_columns":["id"]}]})`, | ||
}; | ||
|
||
workspaceDataSource.query.mockResolvedValueOnce([existingComment]); | ||
|
||
const result = await buildAlteredCommentOnForeignKeyDeletion( | ||
localObjectMetadataName, | ||
remoteObjectMetadataName, | ||
schema, | ||
workspaceDataSource as any, | ||
); | ||
|
||
expect(result).toBe( | ||
'@graphql({"totalCount":{"enabled":true},"foreign_keys":[{"local_name":"favoriteCollection","local_columns":["testId"],"foreign_name":"test","foreign_schema":"schema","foreign_table":"test","foreign_columns":["id"]}]})', | ||
); | ||
}); | ||
}); |
97 changes: 97 additions & 0 deletions
97
...modules/object-metadata/utils/create-migration-for-foreign-key-comment-alteration.util.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { DataSource } from 'typeorm'; | ||
|
||
import { TypeORMService } from 'src/database/typeorm/typeorm.service'; | ||
import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service'; | ||
import { RelationToDelete } from 'src/engine/metadata-modules/relation-metadata/types/relation-to-delete'; | ||
import { generateMigrationName } from 'src/engine/metadata-modules/workspace-migration/utils/generate-migration-name.util'; | ||
import { | ||
WorkspaceMigrationTableActionType, | ||
WorkspaceMigrationColumnActionType, | ||
WorkspaceMigrationCreateComment, | ||
} from 'src/engine/metadata-modules/workspace-migration/workspace-migration.entity'; | ||
import { WorkspaceMigrationService } from 'src/engine/metadata-modules/workspace-migration/workspace-migration.service'; | ||
import { computeTableName } from 'src/engine/utils/compute-table-name.util'; | ||
|
||
export const buildAlteredCommentOnForeignKeyDeletion = async ( | ||
localObjectMetadataName: string, | ||
remoteObjectMetadataName: string, | ||
schema: string, | ||
workspaceDataSource: DataSource | undefined, | ||
): Promise<string | null> => { | ||
const existingComment = await workspaceDataSource?.query( | ||
`SELECT col_description('${schema}."${localObjectMetadataName}"'::regclass, 0)`, | ||
); | ||
|
||
if (!existingComment[0]?.col_description) { | ||
return null; | ||
} | ||
|
||
const commentWithoutGraphQL = existingComment[0].col_description | ||
.replace('@graphql(', '') | ||
.replace(')', ''); | ||
|
||
const parsedComment = JSON.parse(commentWithoutGraphQL); | ||
|
||
const currentForeignKeys = parsedComment.foreign_keys; | ||
|
||
if (!currentForeignKeys) { | ||
return null; | ||
} | ||
|
||
const updatedForeignKeys = currentForeignKeys.filter( | ||
(foreignKey: any) => | ||
foreignKey.foreign_name !== remoteObjectMetadataName && | ||
foreignKey.foreign_table !== remoteObjectMetadataName, | ||
); | ||
|
||
parsedComment.foreign_keys = updatedForeignKeys; | ||
|
||
return `@graphql(${JSON.stringify(parsedComment)})`; | ||
}; | ||
|
||
export const createMigrationToAlterCommentOnForeignKeyDeletion = async ( | ||
dataSourceService: DataSourceService, | ||
typeORMService: TypeORMService, | ||
workspaceMigrationService: WorkspaceMigrationService, | ||
workspaceId: string, | ||
relationToDelete: RelationToDelete, | ||
) => { | ||
const dataSourceMetadata = | ||
await dataSourceService.getLastDataSourceMetadataFromWorkspaceIdOrFail( | ||
workspaceId, | ||
); | ||
|
||
const workspaceDataSource = | ||
await typeORMService.connectToDataSource(dataSourceMetadata); | ||
|
||
const alteredComment = await buildAlteredCommentOnForeignKeyDeletion( | ||
relationToDelete.toObjectName, | ||
relationToDelete.fromObjectName, | ||
dataSourceMetadata.schema, | ||
workspaceDataSource, | ||
); | ||
|
||
if (alteredComment) { | ||
await workspaceMigrationService.createCustomMigration( | ||
generateMigrationName( | ||
`alter-comment-${relationToDelete.fromObjectName}-${relationToDelete.toObjectName}`, | ||
), | ||
workspaceId, | ||
[ | ||
{ | ||
name: computeTableName( | ||
relationToDelete.toObjectName, | ||
relationToDelete.toObjectMetadataIsCustom, | ||
), | ||
action: WorkspaceMigrationTableActionType.ALTER, | ||
columns: [ | ||
{ | ||
action: WorkspaceMigrationColumnActionType.CREATE_COMMENT, | ||
comment: alteredComment, | ||
} satisfies WorkspaceMigrationCreateComment, | ||
], | ||
}, | ||
], | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters