immich/server/src/sql-tools/processors/check-constraint.processor.ts
2025-07-03 10:59:17 -04:00

27 lines
844 B
TypeScript

import { asKey } from 'src/sql-tools/helpers';
import { ConstraintType, Processor } from 'src/sql-tools/types';
export const processCheckConstraints: Processor = (builder, items) => {
for (const {
item: { object, options },
} of items.filter((item) => item.type === 'checkConstraint')) {
const table = builder.getTableByObject(object);
if (!table) {
builder.warnMissingTable('@Check', object);
continue;
}
const tableName = table.name;
table.constraints.push({
type: ConstraintType.CHECK,
name: options.name || asCheckConstraintName(tableName, options.expression),
tableName,
expression: options.expression,
synchronize: options.synchronize ?? true,
});
}
};
const asCheckConstraintName = (table: string, expression: string) => asKey('CHK_', table, [expression]);