Skip to content

Commit

Permalink
Support custom composite field deletion (#6320)
Browse files Browse the repository at this point in the history
as per title
Fixes #6033 and closes #4841
  • Loading branch information
ijreilly authored Jul 18, 2024
1 parent 37720ea commit a4e82d6
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export const SettingsObjectFieldInactiveActionDropdown = ({
scopeKey,
onDelete,
isCustomField,
fieldType,
}: SettingsObjectFieldInactiveActionDropdownProps) => {
const dropdownId = `${scopeKey}-settings-field-disabled-action-dropdown`;

Expand All @@ -37,7 +36,7 @@ export const SettingsObjectFieldInactiveActionDropdown = ({
closeDropdown();
};

const isDeletable = isCustomField && fieldType !== FieldMetadataType.Address;
const isDeletable = isCustomField;

return (
<Dropdown
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import styled from '@emotion/styled';
import { useEffect } from 'react';
import { useNavigate, useParams } from 'react-router-dom';
import styled from '@emotion/styled';
import { H2Title, IconPlus, IconSettings } from 'twenty-ui';

import { LABEL_IDENTIFIER_FIELD_METADATA_TYPES } from '@/object-metadata/constants/LabelIdentifierFieldMetadataTypes';
Expand Down Expand Up @@ -198,7 +198,6 @@ export const SettingsObjectDetail = () => {
ActionIcon={
<SettingsObjectFieldInactiveActionDropdown
isCustomField={!!deactivatedMetadataField.isCustom}
fieldType={deactivatedMetadataField.type}
scopeKey={deactivatedMetadataField.id}
onActivate={() =>
activateMetadataField(deactivatedMetadataField)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { v4 as uuidV4 } from 'uuid';

import { TypeORMService } from 'src/database/typeorm/typeorm.service';
import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service';
import { compositeTypeDefintions } from 'src/engine/metadata-modules/field-metadata/composite-types';
import { CreateFieldInput } from 'src/engine/metadata-modules/field-metadata/dtos/create-field.input';
import { DeleteOneFieldInput } from 'src/engine/metadata-modules/field-metadata/dtos/delete-field.input';
import { FieldMetadataDTO } from 'src/engine/metadata-modules/field-metadata/dtos/field-metadata.dto';
Expand All @@ -20,8 +21,12 @@ import {
FieldMetadataExceptionCode,
} from 'src/engine/metadata-modules/field-metadata/field-metadata.exception';
import { assertDoesNotNullifyDefaultValueForNonNullableField } from 'src/engine/metadata-modules/field-metadata/utils/assert-does-not-nullify-default-value-for-non-nullable-field.util';
import { computeColumnName } from 'src/engine/metadata-modules/field-metadata/utils/compute-column-name.util';
import {
computeColumnName,
computeCompositeColumnName,
} from 'src/engine/metadata-modules/field-metadata/utils/compute-column-name.util';
import { generateNullable } from 'src/engine/metadata-modules/field-metadata/utils/generate-nullable';
import { isCompositeFieldMetadataType } from 'src/engine/metadata-modules/field-metadata/utils/is-composite-field-metadata-type.util';
import { ObjectMetadataService } from 'src/engine/metadata-modules/object-metadata/object-metadata.service';
import { assertMutationNotOnRemoteObject } from 'src/engine/metadata-modules/object-metadata/utils/assert-mutation-not-on-remote-object.util';
import {
Expand Down Expand Up @@ -460,22 +465,54 @@ export class FieldMetadataService extends TypeOrmQueryService<FieldMetadataEntit

await fieldMetadataRepository.delete(fieldMetadata.id);

await this.workspaceMigrationService.createCustomMigration(
generateMigrationName(`delete-${fieldMetadata.name}`),
workspaceId,
[
{
name: computeObjectTargetTable(objectMetadata),
action: WorkspaceMigrationTableActionType.ALTER,
columns: [
{
action: WorkspaceMigrationColumnActionType.DROP,
columnName: computeColumnName(fieldMetadata),
} satisfies WorkspaceMigrationColumnDrop,
],
} satisfies WorkspaceMigrationTableAction,
],
);
if (isCompositeFieldMetadataType(fieldMetadata.type)) {
const compositeType = compositeTypeDefintions.get(fieldMetadata.type);

if (!compositeType) {
throw new Error(
`Composite type not found for field metadata type: ${fieldMetadata.type}`,
);
}

await this.workspaceMigrationService.createCustomMigration(
generateMigrationName(
`delete-${fieldMetadata.name}-composite-columns`,
),
workspaceId,
[
{
name: computeObjectTargetTable(objectMetadata),
action: WorkspaceMigrationTableActionType.ALTER,
columns: compositeType.properties.map((property) => {
return {
action: WorkspaceMigrationColumnActionType.DROP,
columnName: computeCompositeColumnName(
fieldMetadata.name,
property,
),
} satisfies WorkspaceMigrationColumnDrop;
}),
} satisfies WorkspaceMigrationTableAction,
],
);
} else {
await this.workspaceMigrationService.createCustomMigration(
generateMigrationName(`delete-${fieldMetadata.name}`),
workspaceId,
[
{
name: computeObjectTargetTable(objectMetadata),
action: WorkspaceMigrationTableActionType.ALTER,
columns: [
{
action: WorkspaceMigrationColumnActionType.DROP,
columnName: computeColumnName(fieldMetadata),
} satisfies WorkspaceMigrationColumnDrop,
],
} satisfies WorkspaceMigrationTableAction,
],
);
}

await this.workspaceMigrationRunnerService.executeMigrationFromPendingMigrations(
workspaceId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { FieldMetadataInterface } from 'src/engine/metadata-modules/field-metadata/interfaces/field-metadata.interface';
import { CompositeProperty } from 'src/engine/metadata-modules/field-metadata/interfaces/composite-type.interface';
import { FieldMetadataInterface } from 'src/engine/metadata-modules/field-metadata/interfaces/field-metadata.interface';

import { FieldMetadataType } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
import { isCompositeFieldMetadataType } from 'src/engine/metadata-modules/field-metadata/utils/is-composite-field-metadata-type.util';
import { pascalCase } from 'src/utils/pascal-case';
import {
FieldMetadataException,
FieldMetadataExceptionCode,
} from 'src/engine/metadata-modules/field-metadata/field-metadata.exception';
import { isCompositeFieldMetadataType } from 'src/engine/metadata-modules/field-metadata/utils/is-composite-field-metadata-type.util';
import { pascalCase } from 'src/utils/pascal-case';

type ComputeColumnNameOptions = { isForeignKey?: boolean };

Expand All @@ -34,7 +34,7 @@ export function computeColumnName<T extends FieldMetadataType | 'default'>(

if (isCompositeFieldMetadataType(fieldMetadataOrFieldName.type)) {
throw new FieldMetadataException(
`Cannot compute composite column name for non-composite field metadata type: ${fieldMetadataOrFieldName.type}`,
`Cannot compute composite column name for field: ${fieldMetadataOrFieldName.type}`,
FieldMetadataExceptionCode.INVALID_FIELD_INPUT,
);
}
Expand Down

0 comments on commit a4e82d6

Please sign in to comment.