-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathforeign-key-materialization.ts
More file actions
116 lines (108 loc) · 4.27 KB
/
Copy pathforeign-key-materialization.ts
File metadata and controls
116 lines (108 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { lowerAuthoredIndex } from './index-naming';
import type { ForeignKeyInput, ReferentialAction } from './ir/foreign-key';
import type { ForeignKeyReferenceInput } from './ir/foreign-key-reference';
import type { PrimaryKeyInput } from './ir/primary-key';
import type { IndexInput } from './ir/sql-index';
import type { UniqueConstraintInput } from './ir/unique-constraint';
export type BackingIndexCandidates = {
readonly indexes: readonly { readonly columns?: readonly string[] }[];
readonly uniques: readonly { readonly columns: readonly string[] }[];
readonly primaryKey?: { readonly columns: readonly string[] } | undefined;
};
/**
* The column-list keys (`"colA,colB"`, order preserved) a table's own
* indexes, unique constraints, and primary key already back. A foreign key
* whose source columns join to one of these keys needs no separately
* derived backing index.
*
* Shared by {@link isBackedByColumnKeys}'s callers: `materializeForeignKeysAndIndexes`
* (deriving the discrete backing-index entities persisted at `contract emit`)
* and the postgres PSL inferrer (deciding whether an introspected relation
* needs an explicit `index: false`).
*/
export function backingIndexColumnKeys(table: BackingIndexCandidates): readonly string[] {
return [
...table.indexes.flatMap((index) =>
index.columns !== undefined ? [index.columns.join(',')] : [],
),
...table.uniques.map((unique) => unique.columns.join(',')),
...(table.primaryKey ? [table.primaryKey.columns.join(',')] : []),
];
}
/**
* Whether `columns`, in order, matches one of `backingKeys` (see
* {@link backingIndexColumnKeys}). Order-sensitive: `(a, b)` does not
* satisfy a backing index declared as `(b, a)`.
*/
export function isBackedByColumnKeys(
columns: readonly string[],
backingKeys: readonly string[],
): boolean {
return backingKeys.includes(columns.join(','));
}
/**
* A foreign key as authored, before FK1 materialization: the referential
* coordinates plus the `constraint`/`index` intent booleans that drive
* whether — and how — it survives into the persisted contract.
*/
export interface ForeignKeyAuthoringInput {
readonly source: ForeignKeyReferenceInput;
readonly target: ForeignKeyReferenceInput;
readonly name?: string;
readonly onDelete?: ReferentialAction;
readonly onUpdate?: ReferentialAction;
readonly constraint: boolean;
readonly index: boolean;
}
export interface MaterializedTableConstraints {
readonly foreignKeys: readonly ForeignKeyInput[];
readonly indexes: readonly IndexInput[];
}
/**
* Lowers a table's authored foreign keys into the discrete entities
* `contract.json` persists: a `constraint: false` FK contributes no
* `foreignKeys[]` entry, and an `index: true` FK whose columns aren't already
* backed by a declared index/unique/primary-key contributes a managed
* `indexes[]` entry (default prefix + content-hash wire name). Declared
* indexes always survive unchanged; a second FK sharing already-synthesized
* backing columns does not mint a duplicate index.
*/
export function materializeForeignKeysAndIndexes(
tableName: string,
foreignKeys: readonly ForeignKeyAuthoringInput[],
declaredIndexes: readonly IndexInput[],
uniques: readonly UniqueConstraintInput[],
primaryKey: PrimaryKeyInput | undefined,
): MaterializedTableConstraints {
const satisfiedIndexColumns = new Set(
backingIndexColumnKeys({ indexes: declaredIndexes, uniques, primaryKey }),
);
const synthesizedIndexes: IndexInput[] = [];
const materializedForeignKeys: ForeignKeyInput[] = [];
for (const { constraint, index, ...reference } of foreignKeys) {
if (constraint !== false) {
materializedForeignKeys.push(reference);
}
if (index !== false) {
const key = reference.source.columns.join(',');
if (!satisfiedIndexColumns.has(key)) {
synthesizedIndexes.push(
lowerAuthoredIndex(tableName, {
columns: reference.source.columns,
where: undefined,
unique: undefined,
map: undefined,
name: undefined,
type: undefined,
options: undefined,
}),
);
satisfiedIndexColumns.add(key);
}
}
}
return {
foreignKeys: materializedForeignKeys,
indexes: [...declaredIndexes, ...synthesizedIndexes],
};
}