@prisma-next/sql-runtime 0.12.0-dev.6 → 0.12.0-dev.60

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/sql-marker.ts DELETED
@@ -1,143 +0,0 @@
1
- import { APP_SPACE_ID } from '@prisma-next/framework-components/control';
2
- import type { MarkerStatement } from '@prisma-next/sql-relational-core/ast';
3
-
4
- export { APP_SPACE_ID };
5
-
6
- export interface SqlStatement {
7
- readonly sql: string;
8
- readonly params: readonly unknown[];
9
- }
10
-
11
- export interface WriteMarkerInput {
12
- /**
13
- * Logical space identifier for this marker row. Required at every
14
- * call site so the type system surfaces every place that needs to
15
- * thread the value (rather than letting an `?? APP_SPACE_ID`
16
- * fall-through silently collapse per-space markers onto the
17
- * `'app'` row). App-plan callers pass {@link APP_SPACE_ID}
18
- * (`'app'`); per-extension callers pass the extension's space id.
19
- */
20
- readonly space: string;
21
- readonly storageHash: string;
22
- readonly profileHash: string;
23
- readonly contractJson?: unknown;
24
- readonly canonicalVersion?: number;
25
- readonly appTag?: string;
26
- readonly meta?: Record<string, unknown>;
27
- /**
28
- * Applied-invariants set on the marker.
29
- *
30
- * - `undefined` → existing column left untouched. Sign and
31
- * verify-database paths use this; they don't accumulate invariants.
32
- * - explicit value (including `[]`) → column overwritten with
33
- * exactly that value.
34
- */
35
- readonly invariants?: readonly string[];
36
- }
37
-
38
- export const ensureSchemaStatement: SqlStatement = {
39
- sql: 'create schema if not exists prisma_contract',
40
- params: [],
41
- };
42
-
43
- /**
44
- * Schema for `prisma_contract.marker`. The `space text` primary key
45
- * supports one row per loaded contract space (`'app'`,
46
- * `'<extension-id>'`, …); brand-new databases create this shape
47
- * directly. Pre-1.0 single-row markers (no `space` column) are not
48
- * auto-migrated — the target-specific migration runner detects the
49
- * legacy shape at boot and surfaces a structured `LEGACY_MARKER_SHAPE`
50
- * failure pointing the operator at re-running `dbInit`.
51
- *
52
- * @see specs/framework-mechanism.spec.md § 2.
53
- */
54
- export const ensureTableStatement: SqlStatement = {
55
- sql: `create table if not exists prisma_contract.marker (
56
- space text not null primary key default '${APP_SPACE_ID}',
57
- core_hash text not null,
58
- profile_hash text not null,
59
- contract_json jsonb,
60
- canonical_version int,
61
- updated_at timestamptz not null default now(),
62
- app_tag text,
63
- meta jsonb not null default '{}',
64
- invariants text[] not null default '{}'
65
- )`,
66
- params: [],
67
- };
68
-
69
- export function readContractMarker(space: string): MarkerStatement {
70
- return {
71
- sql: `select
72
- core_hash,
73
- profile_hash,
74
- contract_json,
75
- canonical_version,
76
- updated_at,
77
- app_tag,
78
- meta,
79
- invariants
80
- from prisma_contract.marker
81
- where space = $1`,
82
- params: [space],
83
- };
84
- }
85
-
86
- export interface WriteContractMarkerStatements {
87
- readonly insert: SqlStatement;
88
- readonly update: SqlStatement;
89
- }
90
-
91
- /**
92
- * Variable columns that participate in INSERT/UPDATE alongside the
93
- * always-on `space = $1` and `updated_at = now()`. Each column declares
94
- * its name, optional cast type, and parameter value; the placeholder
95
- * (`$N`) is computed positionally below — adding or reordering a
96
- * column doesn't desync indices. `invariants` only appears when the
97
- * caller supplies it — see `WriteMarkerInput.invariants`.
98
- */
99
- function markerColumns(
100
- input: WriteMarkerInput,
101
- ): ReadonlyArray<{ readonly name: string; readonly type?: string; readonly param: unknown }> {
102
- return [
103
- { name: 'core_hash', param: input.storageHash },
104
- { name: 'profile_hash', param: input.profileHash },
105
- { name: 'contract_json', type: 'jsonb', param: input.contractJson ?? null },
106
- { name: 'canonical_version', param: input.canonicalVersion ?? null },
107
- { name: 'app_tag', param: input.appTag ?? null },
108
- { name: 'meta', type: 'jsonb', param: JSON.stringify(input.meta ?? {}) },
109
- ...(input.invariants !== undefined
110
- ? [{ name: 'invariants' as const, type: 'text[]' as const, param: input.invariants }]
111
- : []),
112
- ];
113
- }
114
-
115
- export function writeContractMarker(input: WriteMarkerInput): WriteContractMarkerStatements {
116
- const cols = markerColumns(input);
117
- // $1 is reserved for `space`; subsequent positions follow the order of cols.
118
- const placed = cols.map((c, i) => ({
119
- name: c.name,
120
- expr: c.type ? `$${i + 2}::${c.type}` : `$${i + 2}`,
121
- param: c.param,
122
- }));
123
- const params: readonly unknown[] = [input.space, ...placed.map((c) => c.param)];
124
-
125
- // `updated_at = now()` is a SQL literal with no parameter slot, so it
126
- // sits outside `placed` and is appended directly to each statement.
127
- const insertColumns = ['space', ...placed.map((c) => c.name), 'updated_at'].join(', ');
128
- const insertValues = ['$1', ...placed.map((c) => c.expr), 'now()'].join(', ');
129
- const setClauses = [...placed.map((c) => `${c.name} = ${c.expr}`), 'updated_at = now()'].join(
130
- ', ',
131
- );
132
-
133
- return {
134
- insert: {
135
- sql: `insert into prisma_contract.marker (${insertColumns}) values (${insertValues})`,
136
- params,
137
- },
138
- update: {
139
- sql: `update prisma_contract.marker set ${setClauses} where space = $1`,
140
- params,
141
- },
142
- };
143
- }