pg-dump-parser 1.7.0 → 1.8.1

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.
@@ -0,0 +1,343 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const parsePgDump_1 = require("./parsePgDump");
7
+ const sortSchemaObjects_1 = require("./sortSchemaObjects");
8
+ const multiline_ts_1 = __importDefault(require("multiline-ts"));
9
+ const vitest_1 = require("vitest");
10
+ (0, vitest_1.describe)('sortSchemaObjects', () => {
11
+ (0, vitest_1.test)('sorts schema objects by type order', () => {
12
+ const dump = (0, multiline_ts_1.default) `
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
+ const schemaObjects = (0, parsePgDump_1.parsePgDump)(dump);
45
+ const sorted = (0, sortSchemaObjects_1.sortSchemaObjects)(schemaObjects);
46
+ // Extension should come first
47
+ (0, vitest_1.expect)(sorted[0].header).toMatchObject({ Name: 'baz', Type: 'EXTENSION' });
48
+ // Then table
49
+ (0, vitest_1.expect)(sorted[1].header).toMatchObject({ Name: 'foo', Type: 'TABLE' });
50
+ // Then constraint
51
+ (0, vitest_1.expect)(sorted[2].header).toMatchObject({ Name: 'qux', Type: 'CONSTRAINT' });
52
+ // Finally index
53
+ (0, vitest_1.expect)(sorted[3].header).toMatchObject({ Name: 'bar', Type: 'INDEX' });
54
+ });
55
+ (0, vitest_1.test)('sorts constraints by type (PRIMARY, UNIQUE, FOREIGN, CHECK)', () => {
56
+ const dump = (0, multiline_ts_1.default) `
57
+ --
58
+ -- Name: foo foo_check; Type: CONSTRAINT; Schema: public; Owner: postgres
59
+ --
60
+
61
+ ALTER TABLE ONLY public.foo
62
+ ADD CONSTRAINT foo_check CHECK (id > 0);
63
+
64
+
65
+ --
66
+ -- Name: foo foo_fkey; Type: FK CONSTRAINT; Schema: public; Owner: postgres
67
+ --
68
+
69
+ ALTER TABLE ONLY public.foo
70
+ ADD CONSTRAINT foo_fkey FOREIGN KEY (bar_id) REFERENCES public.bar(id);
71
+
72
+
73
+ --
74
+ -- Name: foo foo_unique; Type: CONSTRAINT; Schema: public; Owner: postgres
75
+ --
76
+
77
+ ALTER TABLE ONLY public.foo
78
+ ADD CONSTRAINT foo_unique UNIQUE (name);
79
+
80
+
81
+ --
82
+ -- Name: foo foo_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
83
+ --
84
+
85
+ ALTER TABLE ONLY public.foo
86
+ ADD CONSTRAINT foo_pkey PRIMARY KEY (id);
87
+ `;
88
+ const schemaObjects = (0, parsePgDump_1.parsePgDump)(dump);
89
+ const sorted = (0, sortSchemaObjects_1.sortSchemaObjects)(schemaObjects);
90
+ // Primary key should come first
91
+ (0, vitest_1.expect)(sorted[0].sql).toContain('PRIMARY KEY');
92
+ // Then unique
93
+ (0, vitest_1.expect)(sorted[1].sql).toContain('UNIQUE');
94
+ // Then check
95
+ (0, vitest_1.expect)(sorted[2].sql).toContain('CHECK');
96
+ // Finally foreign key
97
+ (0, vitest_1.expect)(sorted[3].sql).toContain('FOREIGN KEY');
98
+ });
99
+ (0, vitest_1.test)('sorts indexes alphabetically within the same table', () => {
100
+ const dump = (0, multiline_ts_1.default) `
101
+ --
102
+ -- Name: foo; Type: TABLE; Schema: public; Owner: postgres
103
+ --
104
+
105
+ CREATE TABLE public.foo (
106
+ id integer NOT NULL,
107
+ name text NOT NULL,
108
+ email text
109
+ );
110
+
111
+
112
+ --
113
+ -- Name: foo_name_idx; Type: INDEX; Schema: public; Owner: postgres
114
+ --
115
+
116
+ CREATE INDEX foo_name_idx ON public.foo USING btree (name);
117
+
118
+
119
+ --
120
+ -- Name: foo_email_idx; Type: INDEX; Schema: public; Owner: postgres
121
+ --
122
+
123
+ CREATE INDEX foo_email_idx ON public.foo USING btree (email);
124
+
125
+
126
+ --
127
+ -- Name: foo_id_idx; Type: INDEX; Schema: public; Owner: postgres
128
+ --
129
+
130
+ CREATE INDEX foo_id_idx ON public.foo USING btree (id);
131
+ `;
132
+ const schemaObjects = (0, parsePgDump_1.parsePgDump)(dump);
133
+ const sorted = (0, sortSchemaObjects_1.sortSchemaObjects)(schemaObjects);
134
+ const indexes = sorted.filter((object) => 'Type' in object.header && object.header.Type === 'INDEX');
135
+ (0, vitest_1.expect)(indexes[0].header).toMatchObject({ Name: 'foo_email_idx' });
136
+ (0, vitest_1.expect)(indexes[1].header).toMatchObject({ Name: 'foo_id_idx' });
137
+ (0, vitest_1.expect)(indexes[2].header).toMatchObject({ Name: 'foo_name_idx' });
138
+ });
139
+ (0, vitest_1.test)('sorts comments by type and target', () => {
140
+ const dump = (0, multiline_ts_1.default) `
141
+ --
142
+ -- Name: COLUMN foo.name; Type: COMMENT; Schema: public; Owner: postgres
143
+ --
144
+
145
+ COMMENT ON COLUMN public.foo.name IS 'Name column';
146
+
147
+
148
+ --
149
+ -- Name: TABLE foo; Type: COMMENT; Schema: public; Owner: postgres
150
+ --
151
+
152
+ COMMENT ON TABLE public.foo IS 'Foo table';
153
+
154
+
155
+ --
156
+ -- Name: COLUMN foo.id; Type: COMMENT; Schema: public; Owner: postgres
157
+ --
158
+
159
+ COMMENT ON COLUMN public.foo.id IS 'ID column';
160
+
161
+
162
+ --
163
+ -- Name: EXTENSION pgcrypto; Type: COMMENT; Schema: -; Owner: -
164
+ --
165
+
166
+ COMMENT ON EXTENSION pgcrypto IS 'Crypto extension';
167
+ `;
168
+ const schemaObjects = (0, parsePgDump_1.parsePgDump)(dump);
169
+ const sorted = (0, sortSchemaObjects_1.sortSchemaObjects)(schemaObjects);
170
+ const comments = sorted.filter((object) => 'Type' in object.header && object.header.Type === 'COMMENT');
171
+ // Extension comment first
172
+ (0, vitest_1.expect)(comments[0].header).toMatchObject({ Name: 'EXTENSION pgcrypto' });
173
+ // Then table comment
174
+ (0, vitest_1.expect)(comments[1].header).toMatchObject({ Name: 'TABLE foo' });
175
+ // Then column comments sorted by name
176
+ (0, vitest_1.expect)(comments[2].header).toMatchObject({ Name: 'COLUMN foo.id' });
177
+ (0, vitest_1.expect)(comments[3].header).toMatchObject({ Name: 'COLUMN foo.name' });
178
+ });
179
+ (0, vitest_1.test)('groups and sorts schema objects by scope', () => {
180
+ const dump = (0, multiline_ts_1.default) `
181
+ --
182
+ -- Name: foo; Type: TABLE; Schema: public; Owner: postgres
183
+ --
184
+
185
+ CREATE TABLE public.foo (
186
+ id integer NOT NULL,
187
+ name text NOT NULL
188
+ );
189
+
190
+
191
+ --
192
+ -- Name: foo_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
193
+ --
194
+
195
+ ALTER TABLE ONLY public.foo
196
+ ADD CONSTRAINT foo_pkey PRIMARY KEY (id);
197
+
198
+
199
+ --
200
+ -- Name: foo_name_idx; Type: INDEX; Schema: public; Owner: postgres
201
+ --
202
+
203
+ CREATE INDEX foo_name_idx ON public.foo USING btree (name);
204
+
205
+
206
+ --
207
+ -- Name: bar; Type: TABLE; Schema: public; Owner: postgres
208
+ --
209
+
210
+ CREATE TABLE public.bar (
211
+ id integer NOT NULL,
212
+ foo_id integer
213
+ );
214
+
215
+
216
+ --
217
+ -- Name: bar_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
218
+ --
219
+
220
+ ALTER TABLE ONLY public.bar
221
+ ADD CONSTRAINT bar_pkey PRIMARY KEY (id);
222
+
223
+
224
+ --
225
+ -- Name: bar_foo_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: postgres
226
+ --
227
+
228
+ ALTER TABLE ONLY public.bar
229
+ ADD CONSTRAINT bar_foo_id_fkey FOREIGN KEY (foo_id) REFERENCES public.foo(id);
230
+ `;
231
+ const schemaObjects = (0, parsePgDump_1.parsePgDump)(dump);
232
+ const grouped = (0, sortSchemaObjects_1.groupAndSortSchemaObjects)(schemaObjects);
233
+ // Should have two groups for the two tables
234
+ const tableGroups = Array.from(grouped.keys()).filter((key) => key.includes('TABLE:'));
235
+ (0, vitest_1.expect)(tableGroups).toHaveLength(2);
236
+ // Each group should be sorted internally
237
+ for (const [key, objects] of grouped.entries()) {
238
+ if (key.includes('TABLE:public:foo')) {
239
+ // foo table group should have table, constraint, and index
240
+ (0, vitest_1.expect)(objects).toHaveLength(3);
241
+ (0, vitest_1.expect)(objects[0].header).toMatchObject({ Type: 'TABLE' });
242
+ (0, vitest_1.expect)(objects[1].header).toMatchObject({ Type: 'CONSTRAINT' });
243
+ (0, vitest_1.expect)(objects[2].header).toMatchObject({ Type: 'INDEX' });
244
+ }
245
+ else if (key.includes('TABLE:public:bar')) {
246
+ // bar table group should have table and two constraints
247
+ (0, vitest_1.expect)(objects).toHaveLength(3);
248
+ (0, vitest_1.expect)(objects[0].header).toMatchObject({ Type: 'TABLE' });
249
+ (0, vitest_1.expect)(objects[1].header).toMatchObject({ Type: 'CONSTRAINT' });
250
+ (0, vitest_1.expect)(objects[2].header).toMatchObject({ Type: 'FK CONSTRAINT' });
251
+ }
252
+ }
253
+ });
254
+ (0, vitest_1.test)('sortSchemaObjectsByScope returns flat sorted array', () => {
255
+ const dump = (0, multiline_ts_1.default) `
256
+ --
257
+ -- Name: foo; Type: TABLE; Schema: public; Owner: postgres
258
+ --
259
+
260
+ CREATE TABLE public.foo (
261
+ id integer NOT NULL
262
+ );
263
+
264
+
265
+ --
266
+ -- Name: foo_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
267
+ --
268
+
269
+ ALTER TABLE ONLY public.foo
270
+ ADD CONSTRAINT foo_pkey PRIMARY KEY (id);
271
+
272
+
273
+ --
274
+ -- Name: bar; Type: TABLE; Schema: public; Owner: postgres
275
+ --
276
+
277
+ CREATE TABLE public.bar (
278
+ id integer NOT NULL
279
+ );
280
+
281
+
282
+ --
283
+ -- Name: bar_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
284
+ --
285
+
286
+ ALTER TABLE ONLY public.bar
287
+ ADD CONSTRAINT bar_pkey PRIMARY KEY (id);
288
+ `;
289
+ const schemaObjects = (0, parsePgDump_1.parsePgDump)(dump);
290
+ const sorted = (0, sortSchemaObjects_1.sortSchemaObjectsByScope)(schemaObjects);
291
+ (0, vitest_1.expect)(sorted).toHaveLength(4);
292
+ // Objects should be grouped by table
293
+ // First bar table and its constraint
294
+ (0, vitest_1.expect)(sorted[0].header).toMatchObject({ Name: 'bar', Type: 'TABLE' });
295
+ (0, vitest_1.expect)(sorted[1].header).toMatchObject({
296
+ Name: 'bar_pkey',
297
+ Type: 'CONSTRAINT',
298
+ });
299
+ // Then foo table and its constraint
300
+ (0, vitest_1.expect)(sorted[2].header).toMatchObject({ Name: 'foo', Type: 'TABLE' });
301
+ (0, vitest_1.expect)(sorted[3].header).toMatchObject({
302
+ Name: 'foo_pkey',
303
+ Type: 'CONSTRAINT',
304
+ });
305
+ });
306
+ (0, vitest_1.test)('handles schemas correctly in sorting', () => {
307
+ const dump = (0, multiline_ts_1.default) `
308
+ --
309
+ -- Name: foo; Type: TABLE; Schema: custom; Owner: postgres
310
+ --
311
+
312
+ CREATE TABLE custom.foo (
313
+ id integer NOT NULL
314
+ );
315
+
316
+
317
+ --
318
+ -- Name: foo; Type: TABLE; Schema: public; Owner: postgres
319
+ --
320
+
321
+ CREATE TABLE public.foo (
322
+ id integer NOT NULL
323
+ );
324
+
325
+
326
+ --
327
+ -- Name: bar; Type: TABLE; Schema: -; Owner: postgres
328
+ --
329
+
330
+ CREATE TABLE bar (
331
+ id integer NOT NULL
332
+ );
333
+ `;
334
+ const schemaObjects = (0, parsePgDump_1.parsePgDump)(dump);
335
+ const sorted = (0, sortSchemaObjects_1.sortSchemaObjects)(schemaObjects);
336
+ // Tables with null schema should come first
337
+ (0, vitest_1.expect)(sorted[0].header).toMatchObject({ Name: 'bar', Schema: null });
338
+ // Then sorted by schema name
339
+ (0, vitest_1.expect)(sorted[1].header).toMatchObject({ Name: 'foo', Schema: 'custom' });
340
+ (0, vitest_1.expect)(sorted[2].header).toMatchObject({ Name: 'foo', Schema: 'public' });
341
+ });
342
+ });
343
+ //# sourceMappingURL=sortSchemaObjects.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sortSchemaObjects.test.js","sourceRoot":"","sources":["../src/sortSchemaObjects.test.ts"],"names":[],"mappings":";;;;;AAAA,+CAA4C;AAC5C,2DAI6B;AAC7B,gEAAqC;AACrC,mCAAgD;AAEhD,IAAA,iBAAQ,EAAC,mBAAmB,EAAE,GAAG,EAAE;IACjC,IAAA,aAAI,EAAC,oCAAoC,EAAE,GAAG,EAAE;QAC9C,MAAM,IAAI,GAAG,IAAA,sBAAS,EAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KA+BrB,CAAC;QAEF,MAAM,aAAa,GAAG,IAAA,yBAAW,EAAC,IAAI,CAAC,CAAC;QACxC,MAAM,MAAM,GAAG,IAAA,qCAAiB,EAAC,aAAa,CAAC,CAAC;QAEhD,8BAA8B;QAC9B,IAAA,eAAM,EAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,aAAa,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC,CAAC;QAC3E,aAAa;QACb,IAAA,eAAM,EAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,a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AM,EAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,aAAa,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;QACtE,6BAA6B;QAC7B,IAAA,eAAM,EAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,aAAa,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAC;QAC1E,IAAA,eAAM,EAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,aAAa,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAC;IAC5E,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
package/package.json CHANGED
@@ -2,24 +2,24 @@
2
2
  "author": {
3
3
  "email": "gajus@gajus.com",
4
4
  "name": "Gajus Kuizinas",
5
- "url": "http://gajus.com"
5
+ "url": "https://gajus.com"
6
6
  },
7
7
  "dependencies": {
8
8
  "multiline-ts": "^4.0.1",
9
- "zod": "^3.23.8"
9
+ "zod": "^4.3.5"
10
10
  },
11
11
  "description": "Parses PostgreSQL dump files into an array of schema objects.",
12
12
  "devDependencies": {
13
- "@semantic-release/commit-analyzer": "^9.0.2",
14
- "@semantic-release/github": "^8.0.7",
15
- "@semantic-release/npm": "^9.0.2",
16
- "@types/node": "^18.15.3",
17
- "cspell": "^6.30.2",
18
- "eslint": "^9.14.0",
19
- "eslint-config-canonical": "^44.3.28",
20
- "semantic-release": "^20.1.3",
21
- "typescript": "^5.0.2",
22
- "vitest": "^0.29.7"
13
+ "@semantic-release/commit-analyzer": "^13.0.1",
14
+ "@semantic-release/github": "^12.0.2",
15
+ "@semantic-release/npm": "^13.1.3",
16
+ "@types/node": "^25.0.8",
17
+ "cspell": "^9.6.0",
18
+ "eslint": "^9.39.2",
19
+ "eslint-config-canonical": "^47.3.7",
20
+ "semantic-release": "^25.0.2",
21
+ "typescript": "^5.9.3",
22
+ "vitest": "^4.0.17"
23
23
  },
24
24
  "engines": {
25
25
  "node": ">=22"
@@ -56,5 +56,5 @@
56
56
  "test:vitest": "vitest --run --passWithNoTests"
57
57
  },
58
58
  "types": "./dist/index.d.ts",
59
- "version": "1.7.0"
59
+ "version": "1.8.1"
60
60
  }
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';
@@ -45,6 +45,25 @@ CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA public;
45
45
  COMMENT ON EXTENSION pgcrypto IS 'cryptographic functions';
46
46
 
47
47
 
48
+ --
49
+ -- Name: uid; Type: DOMAIN; Schema: public; Owner: postgres
50
+ --
51
+
52
+ CREATE DOMAIN public.uid AS text
53
+ DEFAULT uuid_generate_v4()
54
+ CONSTRAINT uid_not_empty CHECK ((length(VALUE) > 0));
55
+
56
+
57
+ ALTER DOMAIN public.uid OWNER TO postgres;
58
+
59
+
60
+ --
61
+ -- Name: DOMAIN uid; Type: COMMENT; Schema: public; Owner: postgres
62
+ --
63
+
64
+ COMMENT ON DOMAIN public.uid IS 'User identifier domain';
65
+
66
+
48
67
  --
49
68
  -- Name: status; Type: TYPE; Schema: public; Owner: postgres
50
69
  --
@@ -417,7 +436,7 @@ const omit = <T extends Record<string, unknown>>(
417
436
  );
418
437
 
419
438
  const expectSchemeObject = (
420
- expectedSchemaObject: { scope: null | SchemaObjectScope } & SchemaObject,
439
+ expectedSchemaObject: SchemaObject & { scope: null | SchemaObjectScope },
421
440
  ) => {
422
441
  const schemaObjects = parsePgDump(dump);
423
442
 
@@ -1034,6 +1053,46 @@ test('extracts EXTENSION', async () => {
1034
1053
  });
1035
1054
  });
1036
1055
 
1056
+ test('extracts DOMAIN', async () => {
1057
+ expectSchemeObject({
1058
+ header: {
1059
+ Name: 'uid',
1060
+ Owner: 'postgres',
1061
+ Schema: 'public',
1062
+ Type: 'DOMAIN',
1063
+ },
1064
+ scope: {
1065
+ name: 'uid',
1066
+ schema: 'public',
1067
+ type: 'DOMAIN',
1068
+ },
1069
+ sql: multiline`
1070
+ CREATE DOMAIN public.uid AS text
1071
+ DEFAULT uuid_generate_v4()
1072
+ CONSTRAINT uid_not_empty CHECK ((length(VALUE) > 0));
1073
+ `,
1074
+ });
1075
+ });
1076
+
1077
+ test('extracts COMMENT on DOMAIN', async () => {
1078
+ expectSchemeObject({
1079
+ header: {
1080
+ Name: 'DOMAIN uid',
1081
+ Owner: 'postgres',
1082
+ Schema: 'public',
1083
+ Type: 'COMMENT',
1084
+ },
1085
+ scope: {
1086
+ name: 'uid',
1087
+ schema: 'public',
1088
+ type: 'DOMAIN',
1089
+ },
1090
+ sql: multiline`
1091
+ COMMENT ON DOMAIN public.uid IS 'User identifier domain';
1092
+ `,
1093
+ });
1094
+ });
1095
+
1037
1096
  test('extracts ACL (ON TABLE)', async () => {
1038
1097
  expectSchemeObject({
1039
1098
  header: {
@@ -25,6 +25,7 @@ const AttributedHeaderZodSchema = z.object({
25
25
  'CONSTRAINT',
26
26
  'DEFAULT ACL',
27
27
  'DEFAULT',
28
+ 'DOMAIN',
28
29
  'EXTENSION',
29
30
  'FK CONSTRAINT',
30
31
  'FUNCTION',
@@ -50,9 +51,9 @@ const HeaderZodSchema = z.union([
50
51
  AttributedHeaderZodSchema,
51
52
  ]);
52
53
 
54
+ export type AttributedHeader = z.infer<typeof AttributedHeaderZodSchema>;
53
55
  export type Header = z.infer<typeof HeaderZodSchema>;
54
56
  export type TitleHeader = z.infer<typeof TitleHeaderZodSchema>;
55
- export type AttributedHeader = z.infer<typeof AttributedHeaderZodSchema>;
56
57
 
57
58
  const isHeader = (fragment: string): boolean => {
58
59
  return fragment.startsWith('--\n--');
@@ -97,6 +97,7 @@ type CommentOnTarget = {
97
97
  type:
98
98
  | 'AGGREGATE'
99
99
  | 'COLUMN'
100
+ | 'DOMAIN'
100
101
  | 'EXTENSION'
101
102
  | 'FUNCTION'
102
103
  | 'INDEX'
@@ -112,7 +113,7 @@ type CommentOnTarget = {
112
113
  const extractCommentOnTarget = (fragment: string): CommentOnTarget => {
113
114
  const { target, type } =
114
115
  fragment.match(
115
- /COMMENT ON (?<type>AGGREGATE|COLUMN|EXTENSION|FUNCTION|INDEX|MATERIALIZED VIEW|PROCEDURE|SEQUENCE|TABLE|TYPE|VIEW)\s(?<target>.+?) IS/u,
116
+ /COMMENT ON (?<type>AGGREGATE|COLUMN|DOMAIN|EXTENSION|FUNCTION|INDEX|MATERIALIZED VIEW|PROCEDURE|SEQUENCE|TABLE|TYPE|VIEW)\s(?<target>.+?) IS/u,
116
117
  )?.groups ?? {};
117
118
 
118
119
  if (!target) {
@@ -140,6 +141,7 @@ export type SchemaObjectScope =
140
141
  schema: string;
141
142
  type:
142
143
  | 'AGGREGATE'
144
+ | 'DOMAIN'
143
145
  | 'FUNCTION'
144
146
  | 'MATERIALIZED VIEW'
145
147
  | 'PROCEDURE'
@@ -339,6 +341,18 @@ const scopeComment = (
339
341
  };
340
342
  }
341
343
 
344
+ if (target.type === 'DOMAIN') {
345
+ const [, domainName] = z
346
+ .tuple([z.string(), z.string()])
347
+ .parse(subject.header.Name.split(' '));
348
+
349
+ return {
350
+ name: domainName,
351
+ schema: subject.header.Schema ?? 'public',
352
+ type: 'DOMAIN',
353
+ };
354
+ }
355
+
342
356
  return null;
343
357
  };
344
358
 
@@ -424,6 +438,14 @@ const scopeAttributedSchemaObject = (
424
438
  };
425
439
  }
426
440
 
441
+ if (subject.header.Type === 'DOMAIN') {
442
+ return {
443
+ name: subject.header.Name,
444
+ schema: subject.header.Schema ?? 'public',
445
+ type: 'DOMAIN',
446
+ };
447
+ }
448
+
427
449
  if (subject.header.Type === 'VIEW') {
428
450
  return {
429
451
  name: subject.header.Name,