Timestamp column migration in all workspace schema tables (#13679)

fixes https://github.com/twentyhq/private-issues/issues/288

Test with : 
`npx nx run twenty-server:command
upgrade:1-3:update-timestamp-column-type-in-workspace-schema`
This commit is contained in:
Etienne
2025-08-06 22:12:52 +02:00
committed by GitHub
parent e857860e21
commit 723f1016a6
9 changed files with 80 additions and 42 deletions
@@ -1,23 +0,0 @@
import { FieldMetadataType } from 'twenty-shared/types';
import { getFieldMetadataType } from 'src/engine/api/graphql/workspace-schema-builder/utils/get-field-metadata-type.util';
describe('getFieldMetadataType', () => {
it.each([
['uuid', FieldMetadataType.UUID],
['timestamptz', FieldMetadataType.DATE_TIME],
])(
'should return correct FieldMetadataType for type %s',
(type, expectedMetadataType) => {
expect(getFieldMetadataType(type)).toBe(expectedMetadataType);
},
);
it('should throw an error for an unknown type', () => {
const unknownType = 'unknownType';
expect(() => getFieldMetadataType(unknownType)).toThrow(
`Unknown type ${unknownType}`,
);
});
});
@@ -1,16 +0,0 @@
import { FieldMetadataType } from 'twenty-shared/types';
const typeOrmTypeMapping = new Map<string, FieldMetadataType>([
['uuid', FieldMetadataType.UUID],
['timestamptz', FieldMetadataType.DATE_TIME],
// Add more types here if we need to support more than id, and createdAt/updatedAt/deletedAt
]);
export const getFieldMetadataType = (type: string) => {
const fieldType = typeOrmTypeMapping.get(type);
if (fieldType === undefined || fieldType === null) {
throw new Error(`Unknown type ${type}`);
}
return fieldType;
};
@@ -90,6 +90,8 @@ export class EntitySchemaColumnFactory {
entitySchemaColumnMap[key] = {
name: key,
type: columnType as ColumnType,
precision:
fieldMetadata.type === FieldMetadataType.DATE_TIME ? 3 : undefined,
// TODO: We should double check that
primary: key === 'id',
nullable: fieldMetadata.isNullable ?? false,
@@ -179,6 +179,10 @@ export class WorkspaceMigrationColumnService {
column: {
name: createColumnMigration.columnName,
type: createColumnMigration.columnType,
precision:
createColumnMigration.columnType === 'timestamptz'
? 3
: undefined,
isArray: createColumnMigration.isArray ?? false,
isNullable: createColumnMigration.isNullable,
default: createColumnMigration.defaultValue,
@@ -15,13 +15,16 @@ export class WorkspaceMigrationTypeService {
migrationColumn: WorkspaceMigrationColumnAlter,
) {
const columnDefinition = migrationColumn.alteredColumnDefinition;
const computedColumnType = ` ${columnDefinition.columnType}${
columnDefinition.columnType === 'timestamptz' ? `(3)` : ''
}`;
// Update the column type
// If casting is not possible, the query will fail
await queryRunner.query(`
ALTER TABLE "${schemaName}"."${tableName}"
ALTER COLUMN "${columnDefinition.columnName}" TYPE ${columnDefinition.columnType}
USING "${columnDefinition.columnName}"::${columnDefinition.columnType}
ALTER COLUMN "${columnDefinition.columnName}" TYPE ${computedColumnType}
USING "${columnDefinition.columnName}"::${computedColumnType}
`);
// Update the column default value
@@ -19,6 +19,7 @@ export const typeormBuildCreateColumnSql = ({
| 'default'
| 'generatedType'
| 'asExpression'
| 'precision'
>;
}): string => {
let columnSql = '"' + column.name + '"';
@@ -30,6 +31,7 @@ export const typeormBuildCreateColumnSql = ({
})}"`;
} else {
columnSql += ' ' + column.type;
if (column.precision) columnSql += `(${column.precision})`;
}
if (column.isArray) columnSql += '[]';