-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathplan-resolution.ts
More file actions
258 lines (232 loc) · 7.17 KB
/
plan-resolution.ts
File metadata and controls
258 lines (232 loc) · 7.17 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import type { Contract } from '@prisma-next/contract/types';
import type { ContractSpaceMember } from '@prisma-next/migration-tools/aggregate';
import { MigrationToolsError } from '@prisma-next/migration-tools/errors';
import type { MigrationGraph } from '@prisma-next/migration-tools/graph';
import {
assertHashIsGraphNode,
findLatestMigration,
isGraphNode,
} from '@prisma-next/migration-tools/migration-graph';
import type { ContractRef } from '@prisma-next/migration-tools/ref-resolution';
import { parseContractRef } from '@prisma-next/migration-tools/ref-resolution';
import type { Refs } from '@prisma-next/migration-tools/refs';
import { notOk, ok, type Result } from '@prisma-next/utils/result';
import {
CliStructuredError,
errorPlanForgotTheFlag,
errorSnapshotMissing,
mapRefResolutionError,
} from './cli-errors';
import { mapContractAtError } from './contract-at-errors';
const FULL_HASH_PATTERN = /^sha256:([0-9a-f]{64}|empty)$/;
export function looksLikeFullHash(input: string): boolean {
return FULL_HASH_PATTERN.test(input);
}
export type FromResolution =
| { kind: 'greenfield'; fromHash: null; fromContract: null }
| { kind: 'graph-node'; fromHash: string; fromContract: Contract; sourceDir: string }
| {
kind: 'snapshot';
fromHash: string;
fromContract: Contract;
contractDts: string;
contractJson: unknown;
}
| {
kind: 'auto-baseline';
fromHash: string;
fromContract: Contract;
contractDts: string;
contractJson: unknown;
};
export interface ResolveFromForPlanInput {
readonly optionsFrom?: string | undefined;
readonly member: ContractSpaceMember;
}
function graphIsEmpty(member: ContractSpaceMember): boolean {
return member.packages.length === 0;
}
function getReachableRefs(
refs: Refs,
graph: MigrationGraph,
): ReadonlyArray<{ name: string; hash: string }> {
return Object.entries(refs)
.flatMap(([name, entry]) =>
entry && isGraphNode(entry.hash, graph) ? [{ name, hash: entry.hash }] : [],
)
.sort((a, b) => a.name.localeCompare(b.name));
}
export function assertFromIsGraphNode(
fromHash: string,
graph: MigrationGraph,
refs: Refs,
graphTipHash: string | null,
): void {
try {
assertHashIsGraphNode(fromHash, graph);
} catch (error) {
if (MigrationToolsError.is(error) && error.code === 'MIGRATION.HASH_NOT_IN_GRAPH') {
throw errorPlanForgotTheFlag(fromHash, getReachableRefs(refs, graph), graphTipHash);
}
throw error;
}
}
type RefContractResolution =
| {
kind: 'snapshot';
hash: string;
contract: Contract;
contractJson: unknown;
contractDts: string;
}
| {
kind: 'graph-node';
hash: string;
contract: Contract;
contractJson: unknown;
contractDts: string;
sourceDir: string;
};
async function resolveContractRef(
parsed: ContractRef,
member: ContractSpaceMember,
options?: { readonly explicitLabel?: string; readonly artifactRole?: 'from' | 'to' },
): Promise<Result<RefContractResolution, CliStructuredError>> {
const { hash, provenance } = parsed;
const refName = provenance.kind === 'ref' ? provenance.refName : undefined;
try {
const at = await member.contractAt(hash, refName !== undefined ? { refName } : undefined);
if (at.provenance === 'snapshot') {
return ok({
kind: 'snapshot',
hash: at.hash,
contract: at.contract,
contractJson: at.contractJson,
contractDts: at.contractDts,
});
}
return ok({
kind: 'graph-node',
hash: at.hash,
contract: at.contract,
contractJson: at.contractJson,
contractDts: at.contractDts,
sourceDir: at.sourceDir,
});
} catch (error) {
return mapContractAtError(
error,
options?.artifactRole !== undefined ? { artifactRole: options.artifactRole } : undefined,
);
}
}
async function resolveFromPolicy(
parsed: ContractRef,
input: ResolveFromForPlanInput,
refs: Refs,
explicitFromLabel?: string,
): Promise<Result<FromResolution, CliStructuredError>> {
const resolution = await resolveContractRef(parsed, input.member, {
...(explicitFromLabel !== undefined ? { explicitLabel: explicitFromLabel } : {}),
artifactRole: 'from',
});
if (!resolution.ok) {
return resolution;
}
if (resolution.value.kind === 'graph-node') {
return ok({
kind: 'graph-node',
fromHash: resolution.value.hash,
fromContract: resolution.value.contract,
sourceDir: resolution.value.sourceDir,
});
}
const { hash, contract, contractJson, contractDts } = resolution.value;
if (graphIsEmpty(input.member)) {
return ok({
kind: 'auto-baseline',
fromHash: hash,
fromContract: contract,
contractDts,
contractJson,
});
}
const graph = input.member.graph();
const graphTip = findLatestMigration(graph)?.to ?? null;
try {
assertFromIsGraphNode(hash, graph, refs, graphTip);
} catch (error) {
if (CliStructuredError.is(error)) {
return notOk(error);
}
throw error;
}
return ok({
kind: 'snapshot',
fromHash: hash,
fromContract: contract,
contractDts,
contractJson,
});
}
export async function resolveFromForPlan(
input: ResolveFromForPlanInput,
): Promise<Result<FromResolution, CliStructuredError>> {
const { optionsFrom, member } = input;
const graph = member.graph();
const refs = member.refs;
if (optionsFrom === undefined) {
const dbRef = refs['db'];
if (!dbRef) {
return ok({ kind: 'greenfield', fromHash: null, fromContract: null });
}
return resolveFromPolicy(
{ hash: dbRef.hash, provenance: { kind: 'ref', refName: 'db' } },
input,
refs,
);
}
const refResult = parseContractRef(optionsFrom, { graph, refs });
if (!refResult.ok) {
if (looksLikeFullHash(optionsFrom)) {
const empty = graphIsEmpty(member);
const graphTip = findLatestMigration(graph)?.to ?? null;
if (empty) {
return notOk(errorSnapshotMissing(optionsFrom, { viaRef: false }));
}
return notOk(errorPlanForgotTheFlag(optionsFrom, getReachableRefs(refs, graph), graphTip));
}
return notOk(mapRefResolutionError(refResult.failure));
}
return resolveFromPolicy(refResult.value, input, refs, optionsFrom);
}
export interface ResolveToForPlanInput {
readonly member: ContractSpaceMember;
}
export interface ResolvedContractRef {
readonly hash: string;
readonly contract: Contract;
readonly contractJson: unknown;
readonly contractDts: string;
}
export async function resolveToForPlan(
optionsTo: string,
input: ResolveToForPlanInput,
): Promise<Result<ResolvedContractRef, CliStructuredError>> {
const { member } = input;
const graph = member.graph();
const refs = member.refs;
const refResult = parseContractRef(optionsTo, { graph, refs });
if (!refResult.ok) {
return notOk(mapRefResolutionError(refResult.failure));
}
const resolution = await resolveContractRef(refResult.value, member, {
explicitLabel: optionsTo,
artifactRole: 'to',
});
if (!resolution.ok) {
return resolution;
}
const { hash, contract, contractJson, contractDts } = resolution.value;
return ok({ hash, contract, contractJson, contractDts });
}