e9d5d71cd3
## Part 1 - Exact scope of the current PR (#21964) close https://github.com/twentyhq/core-team-issues/issues/2586 This PR introduces `searchFieldMetadata` as a first-class flat metadata entity and migrates the existing search surface onto it, with **no change to which records are searchable** (ISO with `main`). In scope (what the PR does): - New flat entity `searchFieldMetadata` (universalIdentifier, applicationId, **`position`**, maps, conversions), registered in the central flat-entity constants and the migration build orchestrator. - `searchVector.asExpression` is **derived server-side** from `searchFieldMetadata` rows (validated by `isSafeTsVectorExpression`); never trusted from client input. - **Derivation order is deterministic, driven by each row's `position`** ([compute-search-vector-as-expression-from-search-field-metadatas.util.ts](packages/twenty-server/src/engine/metadata-modules/flat-search-field-metadata/utils/compute-search-vector-as-expression-from-search-field-metadatas.util.ts)), replacing the previous non-deterministic `(createdAt, id)` sort. That sort collapsed to random UUIDs for standard fields (same `createdAt`), so any rename/relabel rewrote the `STORED` generated column to a logically-identical-but-textually-different expression and produced a permanent per-workspace diff vs the standard definition. Ordering now equals provisioning order; ties break on `universalIdentifier`. - Provisioning at object creation mirrors the existing surface exactly **and seeds `position`**: - custom objects -> the `name` field only, at `position: 0` ([build-default-search-field-metadatas-for-custom-object.util.ts](packages/twenty-server/src/engine/metadata-modules/object-metadata/utils/build-default-search-field-metadatas-for-custom-object.util.ts)) - standard objects -> their curated `SEARCH_FIELDS_FOR_*` sets, `position` = the curated index - Backfill (instance + workspace commands in `2-16`) provisions rows for existing workspaces with the same surface **and the same positions** (standard from the curated standard maps, custom `name` = `0`), scoped to the workspace's own custom application ([build-search-field-metadata-backfill-operations.util.ts](packages/twenty-server/src/database/commands/upgrade-version-command/2-16/utils/build-search-field-metadata-backfill-operations.util.ts)). The `position` column is added in the same `2-16` fast instance command as `universalIdentifier`/`applicationId`. - Field rename of an already-indexed field recomputes `asExpression` (positions preserved, so order is stable) ([recompute-search-vector-on-field-rename.util.ts](packages/twenty-server/src/engine/metadata-modules/flat-field-metadata/utils/recompute-search-vector-on-field-rename.util.ts)). - Field delete drops the matching row(s) and recomputes; remaining rows keep their relative order (no renumber) ([from-delete-field-input-to-flat-field-metadatas-to-delete.util.ts](packages/twenty-server/src/engine/metadata-modules/flat-field-metadata/utils/from-delete-field-input-to-flat-field-metadatas-to-delete.util.ts)). - Object relabel is **additive** and ISO/regression-fix only: it indexes the new label identifier **appended last (`position = max(existing) + 1`)** without dropping `name` ([recompute-search-vector-on-label-identifier-update.util.ts](packages/twenty-server/src/engine/metadata-modules/flat-object-metadata/utils/recompute-search-vector-on-label-identifier-update.util.ts)). This is a deliberate, temporary bridge. Explicitly OUT of scope (deferred): - No API to edit `searchFieldMetadata` (no user-facing search-field configuration, including `position` — it is internal and only written by provisioning/backfill/recompute). - No auto-indexing of arbitrary searchable fields. Creating a custom TEXT/EMAILS/etc. field does NOT add it to search (the `computeSearchFieldMetadataCreationForFields` behavior was removed in `e6820ad`). - No field-type-transition handling (field type is immutable - not in `FLAT_FIELD_METADATA_EDITABLE_PROPERTIES`, so that path was dead code). - No `position` validation (uniqueness/range) and no multi-vector / per-field `weight` config — deferred to the configurable-search follow-up (#1428). Net: `searchFieldMetadata` becomes the source of truth for the *same* surface as `main`. The only intentional divergences from `main` are "relabel preserves `name`" (additive) and the deterministic `position`-ordered `asExpression` (a correctness/perf fix that is byte-identical to provisioning order, so it does not change the searchable surface). --------- Co-authored-by: Félix Malfait <felix@twenty.com>
192 lines
6.1 KiB
TypeScript
192 lines
6.1 KiB
TypeScript
import { createManyOperation } from 'test/integration/graphql/utils/create-many-operation.util';
|
|
import { search } from 'test/integration/graphql/utils/search.util';
|
|
import { createOneFieldMetadata } from 'test/integration/metadata/suites/field-metadata/utils/create-one-field-metadata.util';
|
|
import { createOneObjectMetadata } from 'test/integration/metadata/suites/object-metadata/utils/create-one-object-metadata.util';
|
|
import { deleteOneObjectMetadata } from 'test/integration/metadata/suites/object-metadata/utils/delete-one-object-metadata.util';
|
|
import { findManyObjectMetadata } from 'test/integration/metadata/suites/object-metadata/utils/find-many-object-metadata.util';
|
|
import { updateOneObjectMetadata } from 'test/integration/metadata/suites/object-metadata/utils/update-one-object-metadata.util';
|
|
import { jestExpectToBeDefined } from 'test/utils/jest-expect-to-be-defined.util.test';
|
|
import { FieldMetadataType } from 'twenty-shared/types';
|
|
|
|
import { type FieldMetadataDTO } from 'src/engine/metadata-modules/field-metadata/dtos/field-metadata.dto';
|
|
|
|
describe('Object metadata update - search vector side effect', () => {
|
|
let testObjectMetadataId: string;
|
|
let testFieldMetadataId: string;
|
|
let createdRecordId: string;
|
|
|
|
const OBJECT_NAME_SINGULAR = 'searchVectorTestObject';
|
|
const OBJECT_NAME_PLURAL = 'searchVectorTestObjects';
|
|
const NEW_LABEL_IDENTIFIER_FIELD_NAME = 'searchableTitle';
|
|
const RECORD_FIELD_VALUE = 'UniqueSearchableValue123';
|
|
const RECORD_NAME_FIELD_VALUE = 'NameValue';
|
|
|
|
beforeAll(async () => {
|
|
const {
|
|
data: {
|
|
createOneObject: { id: objectMetadataId },
|
|
},
|
|
} = await createOneObjectMetadata({
|
|
expectToFail: false,
|
|
input: {
|
|
nameSingular: OBJECT_NAME_SINGULAR,
|
|
namePlural: OBJECT_NAME_PLURAL,
|
|
labelSingular: 'Search Vector Test Object',
|
|
labelPlural: 'Search Vector Test Objects',
|
|
icon: 'IconSearch',
|
|
isLabelSyncedWithName: false,
|
|
},
|
|
});
|
|
|
|
testObjectMetadataId = objectMetadataId;
|
|
|
|
const {
|
|
data: {
|
|
createOneField: { id: fieldMetadataId },
|
|
},
|
|
} = await createOneFieldMetadata({
|
|
expectToFail: false,
|
|
input: {
|
|
name: NEW_LABEL_IDENTIFIER_FIELD_NAME,
|
|
label: 'Searchable Title',
|
|
type: FieldMetadataType.TEXT,
|
|
objectMetadataId: testObjectMetadataId,
|
|
isLabelSyncedWithName: false,
|
|
},
|
|
gqlFields: `
|
|
id
|
|
name
|
|
label
|
|
`,
|
|
});
|
|
|
|
testFieldMetadataId = fieldMetadataId;
|
|
|
|
const { data } = await createManyOperation({
|
|
objectMetadataSingularName: OBJECT_NAME_SINGULAR,
|
|
objectMetadataPluralName: OBJECT_NAME_PLURAL,
|
|
gqlFields: `id name ${NEW_LABEL_IDENTIFIER_FIELD_NAME}`,
|
|
data: [
|
|
{
|
|
[NEW_LABEL_IDENTIFIER_FIELD_NAME]: RECORD_FIELD_VALUE,
|
|
name: RECORD_NAME_FIELD_VALUE,
|
|
},
|
|
],
|
|
expectToFail: false,
|
|
});
|
|
|
|
createdRecordId = data.createdRecords[0].id;
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await updateOneObjectMetadata({
|
|
expectToFail: false,
|
|
input: {
|
|
idToUpdate: testObjectMetadataId,
|
|
updatePayload: {
|
|
isActive: false,
|
|
},
|
|
},
|
|
});
|
|
await deleteOneObjectMetadata({
|
|
expectToFail: false,
|
|
input: { idToDelete: testObjectMetadataId },
|
|
});
|
|
});
|
|
|
|
it('should index the new label identifier without dropping the existing name surface', async () => {
|
|
await updateOneObjectMetadata({
|
|
input: {
|
|
idToUpdate: testObjectMetadataId,
|
|
updatePayload: {
|
|
labelIdentifierFieldMetadataId: testFieldMetadataId,
|
|
},
|
|
},
|
|
expectToFail: false,
|
|
});
|
|
|
|
const { objects } = await findManyObjectMetadata({
|
|
expectToFail: false,
|
|
input: {
|
|
filter: {
|
|
id: { eq: testObjectMetadataId },
|
|
},
|
|
paging: { first: 1 },
|
|
},
|
|
gqlFields: `
|
|
id
|
|
nameSingular
|
|
fieldsList {
|
|
id
|
|
name
|
|
type
|
|
settings
|
|
}
|
|
`,
|
|
});
|
|
|
|
expect(objects).toBeDefined();
|
|
expect(objects.length).toBe(1);
|
|
|
|
const testObject = objects[0];
|
|
|
|
jestExpectToBeDefined(testObject);
|
|
jestExpectToBeDefined(testObject.fieldsList);
|
|
|
|
const searchVectorField = testObject.fieldsList.find(
|
|
(field: FieldMetadataDTO) => field.type === FieldMetadataType.TS_VECTOR,
|
|
);
|
|
|
|
jestExpectToBeDefined(searchVectorField);
|
|
|
|
const settings = searchVectorField.settings as {
|
|
asExpression?: string;
|
|
generatedType?: string;
|
|
};
|
|
|
|
jestExpectToBeDefined(settings);
|
|
expect(settings.asExpression).toBeDefined();
|
|
// Relabeling is additive: the new label identifier joins the search surface while the
|
|
// previously provisioned name row is preserved.
|
|
expect(settings.asExpression).toContain(NEW_LABEL_IDENTIFIER_FIELD_NAME);
|
|
expect(settings.asExpression).toContain('name');
|
|
|
|
// Position-ordered derivation appends the new label identifier last: the provisioned
|
|
// name row (position 0) stays first, the relabel row (max position + 1) comes after.
|
|
const asExpression = settings.asExpression as string;
|
|
|
|
expect(asExpression.indexOf('"name"')).toBeLessThan(
|
|
asExpression.indexOf(`"${NEW_LABEL_IDENTIFIER_FIELD_NAME}"`),
|
|
);
|
|
|
|
// The record is now reachable through the new label identifier value.
|
|
const searchByNewLabelField = await search({
|
|
searchInput: RECORD_FIELD_VALUE,
|
|
includedObjectNameSingulars: [OBJECT_NAME_SINGULAR],
|
|
limit: 10,
|
|
expectToFail: false,
|
|
});
|
|
|
|
expect(searchByNewLabelField.data.search.edges.length).toBe(1);
|
|
expect(searchByNewLabelField.data.search.edges[0].node.recordId).toBe(
|
|
createdRecordId,
|
|
);
|
|
expect(
|
|
searchByNewLabelField.data.search.edges[0].node.objectNameSingular,
|
|
).toBe(OBJECT_NAME_SINGULAR);
|
|
|
|
// The previously indexed name field remains searchable.
|
|
const searchByName = await search({
|
|
searchInput: RECORD_NAME_FIELD_VALUE,
|
|
includedObjectNameSingulars: [OBJECT_NAME_SINGULAR],
|
|
limit: 10,
|
|
expectToFail: false,
|
|
});
|
|
|
|
expect(searchByName.data.search.edges.length).toBe(1);
|
|
expect(searchByName.data.search.edges[0].node.recordId).toBe(
|
|
createdRecordId,
|
|
);
|
|
});
|
|
});
|