Fix https://github.com/twentyhq/core-team-issues/issues/330#issue-2827026606 and https://github.com/twentyhq/core-team-issues/issues/327#issue-2827001814 What this PR does when `isNewRelationEnabled` is set to `true`: - [x] Drop the creation of the foreign key as a `FieldMetadata` - [x] Stop creating `RelationMetadata` - [x] Properly fill `FieldMetadata` of type `RELATION` during the sync command - [x] Use new relation settings in TwentyORM - [x] Properly create `FieldMetadata` relations when we create a new object - [x] Handle `database:reset` with new relations --------- Co-authored-by: Charles Bochet <charles@twenty.com> Co-authored-by: Charles Bochet <charlesBochet@users.noreply.github.com>
59 lines
2.4 KiB
TypeScript
59 lines
2.4 KiB
TypeScript
import { computeColumnName } from 'src/engine/metadata-modules/field-metadata/utils/compute-column-name.util';
|
|
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
|
|
import { RelationOnDeleteAction } from 'src/engine/metadata-modules/relation-metadata/relation-metadata.entity';
|
|
import {
|
|
WorkspaceMigrationColumnActionType,
|
|
WorkspaceMigrationColumnCreate,
|
|
WorkspaceMigrationTableAction,
|
|
WorkspaceMigrationTableActionType,
|
|
} from 'src/engine/metadata-modules/workspace-migration/workspace-migration.entity';
|
|
import { computeObjectTargetTable } from 'src/engine/utils/compute-object-target-table.util';
|
|
|
|
export const buildMigrationsForCustomObjectRelations = (
|
|
createdObjectMetadata: ObjectMetadataEntity,
|
|
relatedObjectMetadataCollection: ObjectMetadataEntity[],
|
|
): WorkspaceMigrationTableAction[] => {
|
|
const migrations: WorkspaceMigrationTableAction[] = [];
|
|
|
|
for (const relatedObjectMetadata of relatedObjectMetadataCollection) {
|
|
migrations.push(
|
|
{
|
|
name: computeObjectTargetTable(relatedObjectMetadata),
|
|
action: WorkspaceMigrationTableActionType.ALTER,
|
|
columns: [
|
|
{
|
|
action: WorkspaceMigrationColumnActionType.CREATE,
|
|
// TODO: When we get rid of this and use the sync metadata, columnName must be based on the joinColumnName from the field metadata settings
|
|
columnName: computeColumnName(createdObjectMetadata.nameSingular, {
|
|
isForeignKey: true,
|
|
}),
|
|
columnType: 'uuid',
|
|
isNullable: true,
|
|
defaultValue: null,
|
|
} satisfies WorkspaceMigrationColumnCreate,
|
|
],
|
|
},
|
|
{
|
|
name: computeObjectTargetTable(relatedObjectMetadata),
|
|
action: WorkspaceMigrationTableActionType.ALTER,
|
|
columns: [
|
|
{
|
|
action: WorkspaceMigrationColumnActionType.CREATE_FOREIGN_KEY,
|
|
// TODO: When we get rid of this and use the sync metadata, columnName must be based on the joinColumnName from the field metadata settings
|
|
columnName: computeColumnName(createdObjectMetadata.nameSingular, {
|
|
isForeignKey: true,
|
|
}),
|
|
referencedTableName: computeObjectTargetTable(
|
|
createdObjectMetadata,
|
|
),
|
|
referencedTableColumnName: 'id',
|
|
onDelete: RelationOnDeleteAction.CASCADE,
|
|
},
|
|
],
|
|
},
|
|
);
|
|
}
|
|
|
|
return migrations;
|
|
};
|