pg-dump-parser 1.7.0 → 1.8.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/src/index.ts CHANGED
@@ -6,3 +6,8 @@ export {
6
6
  type TitleHeader,
7
7
  } from './parsePgDump';
8
8
  export { type SchemaObjectScope, scopeSchemaObject } from './scopeSchemaObject';
9
+ export {
10
+ groupAndSortSchemaObjects,
11
+ sortSchemaObjects,
12
+ sortSchemaObjectsByScope,
13
+ } from './sortSchemaObjects';
@@ -417,7 +417,7 @@ const omit = <T extends Record<string, unknown>>(
417
417
  );
418
418
 
419
419
  const expectSchemeObject = (
420
- expectedSchemaObject: { scope: null | SchemaObjectScope } & SchemaObject,
420
+ expectedSchemaObject: SchemaObject & { scope: null | SchemaObjectScope },
421
421
  ) => {
422
422
  const schemaObjects = parsePgDump(dump);
423
423
 
@@ -50,9 +50,9 @@ const HeaderZodSchema = z.union([
50
50
  AttributedHeaderZodSchema,
51
51
  ]);
52
52
 
53
+ export type AttributedHeader = z.infer<typeof AttributedHeaderZodSchema>;
53
54
  export type Header = z.infer<typeof HeaderZodSchema>;
54
55
  export type TitleHeader = z.infer<typeof TitleHeaderZodSchema>;
55
- export type AttributedHeader = z.infer<typeof AttributedHeaderZodSchema>;
56
56
 
57
57
  const isHeader = (fragment: string): boolean => {
58
58
  return fragment.startsWith('--\n--');
@@ -0,0 +1,372 @@
1
+ import { parsePgDump } from './parsePgDump';
2
+ import {
3
+ groupAndSortSchemaObjects,
4
+ sortSchemaObjects,
5
+ sortSchemaObjectsByScope,
6
+ } from './sortSchemaObjects';
7
+ import multiline from 'multiline-ts';
8
+ import { describe, expect, test } from 'vitest';
9
+
10
+ describe('sortSchemaObjects', () => {
11
+ test('sorts schema objects by type order', () => {
12
+ const dump = multiline`
13
+ --
14
+ -- Name: foo; Type: TABLE; Schema: public; Owner: postgres
15
+ --
16
+
17
+ CREATE TABLE public.foo (
18
+ id integer NOT NULL,
19
+ name text NOT NULL
20
+ );
21
+
22
+
23
+ --
24
+ -- Name: bar; Type: INDEX; Schema: public; Owner: postgres
25
+ --
26
+
27
+ CREATE INDEX bar ON public.foo USING btree (name);
28
+
29
+
30
+ --
31
+ -- Name: baz; Type: EXTENSION; Schema: -; Owner: -
32
+ --
33
+
34
+ CREATE EXTENSION IF NOT EXISTS baz;
35
+
36
+
37
+ --
38
+ -- Name: qux; Type: CONSTRAINT; Schema: public; Owner: postgres
39
+ --
40
+
41
+ ALTER TABLE ONLY public.foo
42
+ ADD CONSTRAINT qux PRIMARY KEY (id);
43
+ `;
44
+
45
+ const schemaObjects = parsePgDump(dump);
46
+ const sorted = sortSchemaObjects(schemaObjects);
47
+
48
+ // Extension should come first
49
+ expect(sorted[0].header).toMatchObject({ Name: 'baz', Type: 'EXTENSION' });
50
+ // Then table
51
+ expect(sorted[1].header).toMatchObject({ Name: 'foo', Type: 'TABLE' });
52
+ // Then constraint
53
+ expect(sorted[2].header).toMatchObject({ Name: 'qux', Type: 'CONSTRAINT' });
54
+ // Finally index
55
+ expect(sorted[3].header).toMatchObject({ Name: 'bar', Type: 'INDEX' });
56
+ });
57
+
58
+ test('sorts constraints by type (PRIMARY, UNIQUE, FOREIGN, CHECK)', () => {
59
+ const dump = multiline`
60
+ --
61
+ -- Name: foo foo_check; Type: CONSTRAINT; Schema: public; Owner: postgres
62
+ --
63
+
64
+ ALTER TABLE ONLY public.foo
65
+ ADD CONSTRAINT foo_check CHECK (id > 0);
66
+
67
+
68
+ --
69
+ -- Name: foo foo_fkey; Type: FK CONSTRAINT; Schema: public; Owner: postgres
70
+ --
71
+
72
+ ALTER TABLE ONLY public.foo
73
+ ADD CONSTRAINT foo_fkey FOREIGN KEY (bar_id) REFERENCES public.bar(id);
74
+
75
+
76
+ --
77
+ -- Name: foo foo_unique; Type: CONSTRAINT; Schema: public; Owner: postgres
78
+ --
79
+
80
+ ALTER TABLE ONLY public.foo
81
+ ADD CONSTRAINT foo_unique UNIQUE (name);
82
+
83
+
84
+ --
85
+ -- Name: foo foo_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
86
+ --
87
+
88
+ ALTER TABLE ONLY public.foo
89
+ ADD CONSTRAINT foo_pkey PRIMARY KEY (id);
90
+ `;
91
+
92
+ const schemaObjects = parsePgDump(dump);
93
+ const sorted = sortSchemaObjects(schemaObjects);
94
+
95
+ // Primary key should come first
96
+ expect(sorted[0].sql).toContain('PRIMARY KEY');
97
+ // Then unique
98
+ expect(sorted[1].sql).toContain('UNIQUE');
99
+ // Then check
100
+ expect(sorted[2].sql).toContain('CHECK');
101
+ // Finally foreign key
102
+ expect(sorted[3].sql).toContain('FOREIGN KEY');
103
+ });
104
+
105
+ test('sorts indexes alphabetically within the same table', () => {
106
+ const dump = multiline`
107
+ --
108
+ -- Name: foo; Type: TABLE; Schema: public; Owner: postgres
109
+ --
110
+
111
+ CREATE TABLE public.foo (
112
+ id integer NOT NULL,
113
+ name text NOT NULL,
114
+ email text
115
+ );
116
+
117
+
118
+ --
119
+ -- Name: foo_name_idx; Type: INDEX; Schema: public; Owner: postgres
120
+ --
121
+
122
+ CREATE INDEX foo_name_idx ON public.foo USING btree (name);
123
+
124
+
125
+ --
126
+ -- Name: foo_email_idx; Type: INDEX; Schema: public; Owner: postgres
127
+ --
128
+
129
+ CREATE INDEX foo_email_idx ON public.foo USING btree (email);
130
+
131
+
132
+ --
133
+ -- Name: foo_id_idx; Type: INDEX; Schema: public; Owner: postgres
134
+ --
135
+
136
+ CREATE INDEX foo_id_idx ON public.foo USING btree (id);
137
+ `;
138
+
139
+ const schemaObjects = parsePgDump(dump);
140
+ const sorted = sortSchemaObjects(schemaObjects);
141
+
142
+ const indexes = sorted.filter(
143
+ (object) => 'Type' in object.header && object.header.Type === 'INDEX',
144
+ );
145
+
146
+ expect(indexes[0].header).toMatchObject({ Name: 'foo_email_idx' });
147
+ expect(indexes[1].header).toMatchObject({ Name: 'foo_id_idx' });
148
+ expect(indexes[2].header).toMatchObject({ Name: 'foo_name_idx' });
149
+ });
150
+
151
+ test('sorts comments by type and target', () => {
152
+ const dump = multiline`
153
+ --
154
+ -- Name: COLUMN foo.name; Type: COMMENT; Schema: public; Owner: postgres
155
+ --
156
+
157
+ COMMENT ON COLUMN public.foo.name IS 'Name column';
158
+
159
+
160
+ --
161
+ -- Name: TABLE foo; Type: COMMENT; Schema: public; Owner: postgres
162
+ --
163
+
164
+ COMMENT ON TABLE public.foo IS 'Foo table';
165
+
166
+
167
+ --
168
+ -- Name: COLUMN foo.id; Type: COMMENT; Schema: public; Owner: postgres
169
+ --
170
+
171
+ COMMENT ON COLUMN public.foo.id IS 'ID column';
172
+
173
+
174
+ --
175
+ -- Name: EXTENSION pgcrypto; Type: COMMENT; Schema: -; Owner: -
176
+ --
177
+
178
+ COMMENT ON EXTENSION pgcrypto IS 'Crypto extension';
179
+ `;
180
+
181
+ const schemaObjects = parsePgDump(dump);
182
+ const sorted = sortSchemaObjects(schemaObjects);
183
+
184
+ const comments = sorted.filter(
185
+ (object) => 'Type' in object.header && object.header.Type === 'COMMENT',
186
+ );
187
+
188
+ // Extension comment first
189
+ expect(comments[0].header).toMatchObject({ Name: 'EXTENSION pgcrypto' });
190
+ // Then table comment
191
+ expect(comments[1].header).toMatchObject({ Name: 'TABLE foo' });
192
+ // Then column comments sorted by name
193
+ expect(comments[2].header).toMatchObject({ Name: 'COLUMN foo.id' });
194
+ expect(comments[3].header).toMatchObject({ Name: 'COLUMN foo.name' });
195
+ });
196
+
197
+ test('groups and sorts schema objects by scope', () => {
198
+ const dump = multiline`
199
+ --
200
+ -- Name: foo; Type: TABLE; Schema: public; Owner: postgres
201
+ --
202
+
203
+ CREATE TABLE public.foo (
204
+ id integer NOT NULL,
205
+ name text NOT NULL
206
+ );
207
+
208
+
209
+ --
210
+ -- Name: foo_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
211
+ --
212
+
213
+ ALTER TABLE ONLY public.foo
214
+ ADD CONSTRAINT foo_pkey PRIMARY KEY (id);
215
+
216
+
217
+ --
218
+ -- Name: foo_name_idx; Type: INDEX; Schema: public; Owner: postgres
219
+ --
220
+
221
+ CREATE INDEX foo_name_idx ON public.foo USING btree (name);
222
+
223
+
224
+ --
225
+ -- Name: bar; Type: TABLE; Schema: public; Owner: postgres
226
+ --
227
+
228
+ CREATE TABLE public.bar (
229
+ id integer NOT NULL,
230
+ foo_id integer
231
+ );
232
+
233
+
234
+ --
235
+ -- Name: bar_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
236
+ --
237
+
238
+ ALTER TABLE ONLY public.bar
239
+ ADD CONSTRAINT bar_pkey PRIMARY KEY (id);
240
+
241
+
242
+ --
243
+ -- Name: bar_foo_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: postgres
244
+ --
245
+
246
+ ALTER TABLE ONLY public.bar
247
+ ADD CONSTRAINT bar_foo_id_fkey FOREIGN KEY (foo_id) REFERENCES public.foo(id);
248
+ `;
249
+
250
+ const schemaObjects = parsePgDump(dump);
251
+ const grouped = groupAndSortSchemaObjects(schemaObjects);
252
+
253
+ // Should have two groups for the two tables
254
+ const tableGroups = Array.from(grouped.keys()).filter((key) =>
255
+ key.includes('TABLE:'),
256
+ );
257
+ expect(tableGroups).toHaveLength(2);
258
+
259
+ // Each group should be sorted internally
260
+ for (const [key, objects] of grouped.entries()) {
261
+ if (key.includes('TABLE:public:foo')) {
262
+ // foo table group should have table, constraint, and index
263
+ expect(objects).toHaveLength(3);
264
+ expect(objects[0].header).toMatchObject({ Type: 'TABLE' });
265
+ expect(objects[1].header).toMatchObject({ Type: 'CONSTRAINT' });
266
+ expect(objects[2].header).toMatchObject({ Type: 'INDEX' });
267
+ } else if (key.includes('TABLE:public:bar')) {
268
+ // bar table group should have table and two constraints
269
+ expect(objects).toHaveLength(3);
270
+ expect(objects[0].header).toMatchObject({ Type: 'TABLE' });
271
+ expect(objects[1].header).toMatchObject({ Type: 'CONSTRAINT' });
272
+ expect(objects[2].header).toMatchObject({ Type: 'FK CONSTRAINT' });
273
+ }
274
+ }
275
+ });
276
+
277
+ test('sortSchemaObjectsByScope returns flat sorted array', () => {
278
+ const dump = multiline`
279
+ --
280
+ -- Name: foo; Type: TABLE; Schema: public; Owner: postgres
281
+ --
282
+
283
+ CREATE TABLE public.foo (
284
+ id integer NOT NULL
285
+ );
286
+
287
+
288
+ --
289
+ -- Name: foo_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
290
+ --
291
+
292
+ ALTER TABLE ONLY public.foo
293
+ ADD CONSTRAINT foo_pkey PRIMARY KEY (id);
294
+
295
+
296
+ --
297
+ -- Name: bar; Type: TABLE; Schema: public; Owner: postgres
298
+ --
299
+
300
+ CREATE TABLE public.bar (
301
+ id integer NOT NULL
302
+ );
303
+
304
+
305
+ --
306
+ -- Name: bar_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
307
+ --
308
+
309
+ ALTER TABLE ONLY public.bar
310
+ ADD CONSTRAINT bar_pkey PRIMARY KEY (id);
311
+ `;
312
+
313
+ const schemaObjects = parsePgDump(dump);
314
+ const sorted = sortSchemaObjectsByScope(schemaObjects);
315
+
316
+ expect(sorted).toHaveLength(4);
317
+
318
+ // Objects should be grouped by table
319
+ // First bar table and its constraint
320
+ expect(sorted[0].header).toMatchObject({ Name: 'bar', Type: 'TABLE' });
321
+ expect(sorted[1].header).toMatchObject({
322
+ Name: 'bar_pkey',
323
+ Type: 'CONSTRAINT',
324
+ });
325
+
326
+ // Then foo table and its constraint
327
+ expect(sorted[2].header).toMatchObject({ Name: 'foo', Type: 'TABLE' });
328
+ expect(sorted[3].header).toMatchObject({
329
+ Name: 'foo_pkey',
330
+ Type: 'CONSTRAINT',
331
+ });
332
+ });
333
+
334
+ test('handles schemas correctly in sorting', () => {
335
+ const dump = multiline`
336
+ --
337
+ -- Name: foo; Type: TABLE; Schema: custom; Owner: postgres
338
+ --
339
+
340
+ CREATE TABLE custom.foo (
341
+ id integer NOT NULL
342
+ );
343
+
344
+
345
+ --
346
+ -- Name: foo; Type: TABLE; Schema: public; Owner: postgres
347
+ --
348
+
349
+ CREATE TABLE public.foo (
350
+ id integer NOT NULL
351
+ );
352
+
353
+
354
+ --
355
+ -- Name: bar; Type: TABLE; Schema: -; Owner: postgres
356
+ --
357
+
358
+ CREATE TABLE bar (
359
+ id integer NOT NULL
360
+ );
361
+ `;
362
+
363
+ const schemaObjects = parsePgDump(dump);
364
+ const sorted = sortSchemaObjects(schemaObjects);
365
+
366
+ // Tables with null schema should come first
367
+ expect(sorted[0].header).toMatchObject({ Name: 'bar', Schema: null });
368
+ // Then sorted by schema name
369
+ expect(sorted[1].header).toMatchObject({ Name: 'foo', Schema: 'custom' });
370
+ expect(sorted[2].header).toMatchObject({ Name: 'foo', Schema: 'public' });
371
+ });
372
+ });