@ultimat3/db 31.2.0 → 31.3.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ultimat3/db",
3
- "version": "31.2.0",
3
+ "version": "31.3.0",
4
4
  "description": "Postgres access, transactions, migrations and drift detection",
5
5
  "keywords": [
6
6
  "ultimate",
@@ -45,7 +45,7 @@
45
45
  "test": "bun test"
46
46
  },
47
47
  "dependencies": {
48
- "@ultimat3/core": "31.2.0"
48
+ "@ultimat3/core": "31.3.0"
49
49
  },
50
50
  "peerDependencies": {
51
51
  "@electric-sql/pglite": ">=0.5.0"
@@ -4,6 +4,8 @@
4
4
  // declaration-based drift check can see them, because a regenerated sidecar equals the declaration
5
5
  // by construction. The rail is the same shape `destructive.ts` is — statements in, statements out.
6
6
 
7
+ import { PARTITION_FUNCTION } from './generate-partition';
8
+ import { ROW_SECURITY_POLICY } from './row-security';
7
9
  import { stripSqlNoise } from './sql-noise';
8
10
  import { statementExcerpt } from './statement-excerpt';
9
11
  import { statementsOf } from './statement-split';
@@ -14,6 +16,12 @@ export interface GeneratableForm {
14
16
  readonly name: string;
15
17
  /** Anchored at the statement's start, against blanked and lowercased text. */
16
18
  readonly pattern: RegExp;
19
+ /**
20
+ * The name the statement's FIRST quoted identifier must be, read off the original text. Blanking
21
+ * erases every identifier, so a hand-written `select "backfill"('a', 'b', 'c', 2, null)` has the
22
+ * partition call's exact shape; only the generator's own name tells the two apart.
23
+ */
24
+ readonly identifier?: string;
17
25
  }
18
26
 
19
27
  /**
@@ -106,6 +114,41 @@ export const GENERATABLE_FORMS: readonly GeneratableForm[] = [
106
114
  name: 'select set_config(…, true)',
107
115
  pattern: /^select\s+set_config\s*\(\s*''\s*,\s*''\s*,\s*true\s*\)$/,
108
116
  },
117
+ // `entity({ partitionBy })` (`generate-partition.ts`): the retired signature's drop, the
118
+ // maintenance function and the call that makes the first partitions. Absent until 31.3, so an app
119
+ // declaring `partitionBy` had its own generated migration reported by its own `drift` step.
120
+ {
121
+ name: 'drop function if exists … (retired partition signature)',
122
+ identifier: PARTITION_FUNCTION,
123
+ pattern:
124
+ /^drop\s+function\s+if\s+exists\s+""\s*\(\s*text\s*,\s*text\s*,\s*text\s*,\s*integer\s*,\s*bigint\s*,\s*timestamptz\s*\)$/,
125
+ },
126
+ {
127
+ name: 'create or replace function … returns jsonb',
128
+ identifier: PARTITION_FUNCTION,
129
+ pattern:
130
+ /^create\s+or\s+replace\s+function\s+""\s*\(\s*parent\s+text\s*,\s*kind\s+text\s*,[\s\S]*\)\s+returns\s+jsonb\s+language\s+plpgsql\s+as\s*$/,
131
+ },
132
+ {
133
+ name: 'select … partitions',
134
+ identifier: PARTITION_FUNCTION,
135
+ pattern: /^select\s+""\s*\(\s*''\s*,\s*''\s*,\s*''\s*,\s*\d+\s*,\s*null\s*\)$/,
136
+ },
137
+ // `database.rowSecurity` (`row-security-plan.ts`): both halves. Absent until 31.3 as well.
138
+ {
139
+ name: 'alter table … enable/disable/force/no force row level security',
140
+ pattern: /^alter\s+table\s+""\s+(?:enable|disable|force|no\s+force)\s+row\s+level\s+security$/,
141
+ },
142
+ {
143
+ name: 'create policy',
144
+ identifier: ROW_SECURITY_POLICY,
145
+ pattern: /^create\s+policy\s+""\s+on\s+""\s+using\s*\(/,
146
+ },
147
+ {
148
+ name: 'drop policy if exists',
149
+ identifier: ROW_SECURITY_POLICY,
150
+ pattern: /^drop\s+policy\s+if\s+exists\s+""\s+on\s+""$/,
151
+ },
109
152
  {
110
153
  name: 'update … recount',
111
154
  pattern:
@@ -113,6 +156,13 @@ export const GENERATABLE_FORMS: readonly GeneratableForm[] = [
113
156
  },
114
157
  ];
115
158
 
159
+ /** The first `"…"` identifier after any leading comments, unescaped; `undefined` when none. */
160
+ const firstIdentifier = (statement: string): string | undefined => {
161
+ const code = statement.replace(/^(?:\s*--[^\n]*(?:\n|$)|\s*\/\*[\s\S]*?\*\/)*/, '');
162
+ const found = /"((?:[^"]|"")*)"/.exec(code)?.[1];
163
+ return found?.replaceAll('""', '"');
164
+ };
165
+
116
166
  /**
117
167
  * Every statement in `up` that a regenerated migration would not carry, in apply order, as the one
118
168
  * capped line an error prints — the statement, never a count, because the whole value is telling an
@@ -131,7 +181,10 @@ export function ungeneratableStatements(up: string): readonly string[] {
131
181
  const found: string[] = [];
132
182
  for (const statement of statementsOf(up)) {
133
183
  const bare = stripSqlNoise(statement).trim().toLowerCase();
134
- if (GENERATABLE_FORMS.some((form) => form.pattern.test(bare))) continue;
184
+ const named = firstIdentifier(statement);
185
+ const generated = (form: GeneratableForm): boolean =>
186
+ form.pattern.test(bare) && (form.identifier === undefined || form.identifier === named);
187
+ if (GENERATABLE_FORMS.some(generated)) continue;
135
188
  found.push(statementExcerpt(statement));
136
189
  }
137
190
  return found;