pg-dump-parser 1.0.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/LICENSE +24 -0
- package/README.md +63 -0
- package/dist/parsePgDump.d.ts +36 -0
- package/dist/parsePgDump.d.ts.map +1 -0
- package/dist/parsePgDump.js +111 -0
- package/dist/parsePgDump.js.map +1 -0
- package/dist/parsePgDump.test.d.ts +2 -0
- package/dist/parsePgDump.test.d.ts.map +1 -0
- package/dist/parsePgDump.test.js +695 -0
- package/dist/parsePgDump.test.js.map +1 -0
- package/package.json +60 -0
- package/src/parsePgDump.test.ts +736 -0
- package/src/parsePgDump.ts +143 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
Copyright (c) 2024, Gajus Kuizinas (https://gajus.com/)
|
|
2
|
+
All rights reserved.
|
|
3
|
+
|
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
|
6
|
+
* Redistributions of source code must retain the above copyright
|
|
7
|
+
notice, this list of conditions and the following disclaimer.
|
|
8
|
+
* Redistributions in binary form must reproduce the above copyright
|
|
9
|
+
notice, this list of conditions and the following disclaimer in the
|
|
10
|
+
documentation and/or other materials provided with the distribution.
|
|
11
|
+
* Neither the name of the Gajus Kuizinas (https://gajus.com/) nor the
|
|
12
|
+
names of its contributors may be used to endorse or promote products
|
|
13
|
+
derived from this software without specific prior written permission.
|
|
14
|
+
|
|
15
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
16
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
17
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
18
|
+
DISCLAIMED. IN NO EVENT SHALL GAJUS KUIZINAS BE LIABLE FOR ANY
|
|
19
|
+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
20
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
21
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
22
|
+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
23
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
24
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# pg-dump-parser
|
|
2
|
+
|
|
3
|
+
Parses PostgreSQL dump files into an array of schema objects.
|
|
4
|
+
|
|
5
|
+
## Motivation
|
|
6
|
+
|
|
7
|
+
This allows to submit a PostgreSQL schema dump to version control in a way that enables easy diffing.
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { parsePgDump } from 'pg-dump-parser';
|
|
13
|
+
|
|
14
|
+
const dump = await readFile('dump.sql', 'utf8');
|
|
15
|
+
|
|
16
|
+
const schemaObjects = parsePgDump(dump);
|
|
17
|
+
|
|
18
|
+
for (const schemaObject of schemaObjects) {
|
|
19
|
+
console.log(schemaObject);
|
|
20
|
+
}
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
> [!NOTE]
|
|
24
|
+
> The expected input is a PostgreSQL dump file created with `pg_dump --schema-only`.
|
|
25
|
+
|
|
26
|
+
The output is an array of objects, each representing a schema object in the dump file and the corresponding header, e.g.,
|
|
27
|
+
|
|
28
|
+
```json
|
|
29
|
+
[
|
|
30
|
+
{
|
|
31
|
+
"header": {
|
|
32
|
+
"Name": "bar",
|
|
33
|
+
"Owner": "postgres",
|
|
34
|
+
"Schema": "public",
|
|
35
|
+
"Type": "TABLE"
|
|
36
|
+
},
|
|
37
|
+
"sql": "CREATE TABLE public.bar (\n id integer NOT NULL,\n uid text NOT NULL,\n foo_id integer\n);"
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
"header": {
|
|
41
|
+
"Name": "bar",
|
|
42
|
+
"Owner": "postgres",
|
|
43
|
+
"Schema": "public",
|
|
44
|
+
"Type": "TABLE"
|
|
45
|
+
},
|
|
46
|
+
"sql": "ALTER TABLE public.bar OWNER TO postgres;"
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
"header": {
|
|
50
|
+
"Name": "bar_id_seq",
|
|
51
|
+
"Owner": "postgres",
|
|
52
|
+
"Schema": "public",
|
|
53
|
+
"Type": "SEQUENCE"
|
|
54
|
+
},
|
|
55
|
+
"sql": "ALTER TABLE public.bar ALTER COLUMN id ADD GENERATED ALWAYS AS IDENTITY (\n SEQUENCE NAME public.bar_id_seq\n START WITH 1\n INCREMENT BY 1\n NO MINVALUE\n NO MAXVALUE\n CACHE 1\n);"
|
|
56
|
+
}
|
|
57
|
+
]
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Alternatives
|
|
61
|
+
|
|
62
|
+
* https://github.com/omniti-labs/pg_extractor
|
|
63
|
+
* Prior to writing pg-dump-parser, I used this tool to extract the schema. It works well, but it's slow. It was taking a whole minute to parse our dump file. We needed something that implements equivalent functionality, but is faster. `pg-dump-parser` processes the same dump with in a few seconds.
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
declare const HeaderZodSchema: z.ZodUnion<[z.ZodObject<{
|
|
3
|
+
Title: z.ZodString;
|
|
4
|
+
}, "strip", z.ZodTypeAny, {
|
|
5
|
+
Title: string;
|
|
6
|
+
}, {
|
|
7
|
+
Title: string;
|
|
8
|
+
}>, z.ZodObject<{
|
|
9
|
+
Name: z.ZodString;
|
|
10
|
+
Owner: z.ZodNullable<z.ZodString>;
|
|
11
|
+
Schema: z.ZodNullable<z.ZodString>;
|
|
12
|
+
Type: z.ZodEnum<["ACL", "AGGREGATE", "CAST", "COMMENT", "CONSTRAINT", "DEFAULT", "EXTENSION", "FK CONSTRAINT", "FUNCTION", "INDEX", "MATERIALIZED VIEW", "PROCEDURE", "PUBLICATION", "SCHEMA", "SEQUENCE OWNED BY", "SEQUENCE", "TABLE", "TEXT SEARCH CONFIGURATION", "TEXT SEARCH DICTIONARY", "TRIGGER", "TYPE", "VIEW"]>;
|
|
13
|
+
}, "strip", z.ZodTypeAny, {
|
|
14
|
+
Name: string;
|
|
15
|
+
Owner: string | null;
|
|
16
|
+
Schema: string | null;
|
|
17
|
+
Type: "ACL" | "AGGREGATE" | "CAST" | "COMMENT" | "CONSTRAINT" | "DEFAULT" | "EXTENSION" | "FK CONSTRAINT" | "FUNCTION" | "INDEX" | "MATERIALIZED VIEW" | "PROCEDURE" | "PUBLICATION" | "SCHEMA" | "SEQUENCE OWNED BY" | "SEQUENCE" | "TABLE" | "TEXT SEARCH CONFIGURATION" | "TEXT SEARCH DICTIONARY" | "TRIGGER" | "TYPE" | "VIEW";
|
|
18
|
+
}, {
|
|
19
|
+
Name: string;
|
|
20
|
+
Owner: string | null;
|
|
21
|
+
Schema: string | null;
|
|
22
|
+
Type: "ACL" | "AGGREGATE" | "CAST" | "COMMENT" | "CONSTRAINT" | "DEFAULT" | "EXTENSION" | "FK CONSTRAINT" | "FUNCTION" | "INDEX" | "MATERIALIZED VIEW" | "PROCEDURE" | "PUBLICATION" | "SCHEMA" | "SEQUENCE OWNED BY" | "SEQUENCE" | "TABLE" | "TEXT SEARCH CONFIGURATION" | "TEXT SEARCH DICTIONARY" | "TRIGGER" | "TYPE" | "VIEW";
|
|
23
|
+
}>]>;
|
|
24
|
+
type Header = z.infer<typeof HeaderZodSchema>;
|
|
25
|
+
type Table = {
|
|
26
|
+
name: string;
|
|
27
|
+
schema: string;
|
|
28
|
+
};
|
|
29
|
+
type SchemaObject = {
|
|
30
|
+
header: Header;
|
|
31
|
+
sql: string;
|
|
32
|
+
table?: Table;
|
|
33
|
+
};
|
|
34
|
+
export declare const parsePgDump: (dump: string) => SchemaObject[];
|
|
35
|
+
export {};
|
|
36
|
+
//# sourceMappingURL=parsePgDump.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parsePgDump.d.ts","sourceRoot":"","sources":["../src/parsePgDump.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,QAAA,MAAM,eAAe;;;;;;;;;;;;;;;;;;;;;IAyCnB,CAAC;AAEH,KAAK,MAAM,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,eAAe,CAAC,CAAC;AAyD9C,KAAK,KAAK,GAAG;IACX,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,KAAK,YAAY,GAAG;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,CAAC,EAAE,KAAK,CAAC;CACf,CAAC;AAEF,eAAO,MAAM,WAAW,SAAU,MAAM,mBA6BvC,CAAC"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parsePgDump = void 0;
|
|
4
|
+
const zod_1 = require("zod");
|
|
5
|
+
const HeaderZodSchema = zod_1.z.union([
|
|
6
|
+
// These are the attribute less headers, e.g.
|
|
7
|
+
// --
|
|
8
|
+
// -- PostgreSQL database dump
|
|
9
|
+
// --
|
|
10
|
+
zod_1.z.object({
|
|
11
|
+
Title: zod_1.z.string(),
|
|
12
|
+
}),
|
|
13
|
+
// These are the objects with attributes, e.g.
|
|
14
|
+
// --
|
|
15
|
+
// -- Name: citext; Type: EXTENSION; Schema: -; Owner: -
|
|
16
|
+
// --
|
|
17
|
+
zod_1.z.object({
|
|
18
|
+
Name: zod_1.z.string(),
|
|
19
|
+
Owner: zod_1.z.string().nullable(),
|
|
20
|
+
Schema: zod_1.z.string().nullable(),
|
|
21
|
+
Type: zod_1.z.enum([
|
|
22
|
+
'ACL',
|
|
23
|
+
'AGGREGATE',
|
|
24
|
+
'CAST',
|
|
25
|
+
'COMMENT',
|
|
26
|
+
'CONSTRAINT',
|
|
27
|
+
'DEFAULT',
|
|
28
|
+
'EXTENSION',
|
|
29
|
+
'FK CONSTRAINT',
|
|
30
|
+
'FUNCTION',
|
|
31
|
+
'INDEX',
|
|
32
|
+
'MATERIALIZED VIEW',
|
|
33
|
+
'PROCEDURE',
|
|
34
|
+
'PUBLICATION',
|
|
35
|
+
'SCHEMA',
|
|
36
|
+
'SEQUENCE OWNED BY',
|
|
37
|
+
'SEQUENCE',
|
|
38
|
+
'TABLE',
|
|
39
|
+
'TEXT SEARCH CONFIGURATION',
|
|
40
|
+
'TEXT SEARCH DICTIONARY',
|
|
41
|
+
'TRIGGER',
|
|
42
|
+
'TYPE',
|
|
43
|
+
'VIEW',
|
|
44
|
+
]),
|
|
45
|
+
}),
|
|
46
|
+
]);
|
|
47
|
+
const isHeader = (fragment) => {
|
|
48
|
+
return fragment.startsWith('--\n--');
|
|
49
|
+
};
|
|
50
|
+
const parseValue = (value) => {
|
|
51
|
+
if (value === '-' || value === '' || value === undefined) {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
return value;
|
|
55
|
+
};
|
|
56
|
+
const parseAttribute = (attribute) => {
|
|
57
|
+
const [name, value] = attribute.split(':');
|
|
58
|
+
return [name, parseValue(value.trim())];
|
|
59
|
+
};
|
|
60
|
+
// --
|
|
61
|
+
// -- Name: TABLE user_survey; Type: ACL; Schema: public; Owner: postgres
|
|
62
|
+
// --
|
|
63
|
+
const parseHeader = (fragment) => {
|
|
64
|
+
const lines = fragment.split('\n');
|
|
65
|
+
if (lines.length !== 3) {
|
|
66
|
+
throw new Error('Invalid header');
|
|
67
|
+
}
|
|
68
|
+
const contentLine = lines[1].slice(3);
|
|
69
|
+
if (contentLine === 'PostgreSQL database dump' ||
|
|
70
|
+
contentLine === 'PostgreSQL database dump complete') {
|
|
71
|
+
return HeaderZodSchema.parse({
|
|
72
|
+
Title: contentLine,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
const content = Object.fromEntries(contentLine.split('; ').map((attribute) => {
|
|
76
|
+
return parseAttribute(attribute);
|
|
77
|
+
}));
|
|
78
|
+
const result = HeaderZodSchema.safeParse(content);
|
|
79
|
+
if (!result.success) {
|
|
80
|
+
throw new Error('Invalid header');
|
|
81
|
+
}
|
|
82
|
+
return result.data;
|
|
83
|
+
};
|
|
84
|
+
const parsePgDump = (dump) => {
|
|
85
|
+
const schemaObjects = [];
|
|
86
|
+
const fragments = dump.trim().split(/(--\n-- .*\n--)/u);
|
|
87
|
+
let lastHeader = null;
|
|
88
|
+
for (const fragment of fragments.map((chunk) => chunk.trim())) {
|
|
89
|
+
if (fragment === '') {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
if (isHeader(fragment)) {
|
|
93
|
+
lastHeader = parseHeader(fragment);
|
|
94
|
+
}
|
|
95
|
+
else if (lastHeader) {
|
|
96
|
+
const subFragments = fragment.split('\n\n\n');
|
|
97
|
+
for (const subFragment of subFragments) {
|
|
98
|
+
schemaObjects.push({
|
|
99
|
+
header: lastHeader,
|
|
100
|
+
sql: subFragment,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
throw new Error('No header');
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return schemaObjects;
|
|
109
|
+
};
|
|
110
|
+
exports.parsePgDump = parsePgDump;
|
|
111
|
+
//# sourceMappingURL=parsePgDump.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parsePgDump.js","sourceRoot":"","sources":["../src/parsePgDump.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AAExB,MAAM,eA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