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/LICENSE +1 -1
- package/README.md +34 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/parsePgDump.d.ts +58 -36
- package/dist/parsePgDump.d.ts.map +1 -1
- package/dist/parsePgDump.js.map +1 -1
- package/dist/parsePgDump.test.js.map +1 -1
- package/dist/scopeSchemaObject.d.ts.map +1 -1
- package/dist/scopeSchemaObject.js +22 -29
- package/dist/scopeSchemaObject.js.map +1 -1
- package/dist/sortSchemaObjects.d.ts +14 -0
- package/dist/sortSchemaObjects.d.ts.map +1 -0
- package/dist/sortSchemaObjects.js +261 -0
- package/dist/sortSchemaObjects.js.map +1 -0
- package/dist/sortSchemaObjects.test.d.ts +2 -0
- package/dist/sortSchemaObjects.test.d.ts.map +1 -0
- package/dist/sortSchemaObjects.test.js +343 -0
- package/dist/sortSchemaObjects.test.js.map +1 -0
- package/package.json +13 -13
- package/src/index.ts +5 -0
- package/src/parsePgDump.test.ts +1 -1
- package/src/parsePgDump.ts +1 -1
- package/src/sortSchemaObjects.test.ts +372 -0
- package/src/sortSchemaObjects.ts +323 -0
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
import { type AttributedHeader, type SchemaObject } from './parsePgDump';
|
|
2
|
+
import { scopeSchemaObject } from './scopeSchemaObject';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Sort order for different schema object types
|
|
6
|
+
*/
|
|
7
|
+
const TYPE_SORT_ORDER: Record<string, number> = {
|
|
8
|
+
// ACLs and other
|
|
9
|
+
ACL: 18,
|
|
10
|
+
|
|
11
|
+
AGGREGATE: 6,
|
|
12
|
+
CAST: 22,
|
|
13
|
+
|
|
14
|
+
// Comments
|
|
15
|
+
COMMENT: 17,
|
|
16
|
+
// Constraints (sorted by type)
|
|
17
|
+
CONSTRAINT: 11,
|
|
18
|
+
// Table modifications and defaults
|
|
19
|
+
DEFAULT: 10,
|
|
20
|
+
|
|
21
|
+
'DEFAULT ACL': 19,
|
|
22
|
+
// Extensions first
|
|
23
|
+
EXTENSION: 1,
|
|
24
|
+
'FK CONSTRAINT': 12,
|
|
25
|
+
|
|
26
|
+
// Functions and procedures
|
|
27
|
+
FUNCTION: 4,
|
|
28
|
+
|
|
29
|
+
// Indexes
|
|
30
|
+
INDEX: 13,
|
|
31
|
+
'MATERIALIZED VIEW': 15,
|
|
32
|
+
|
|
33
|
+
PROCEDURE: 5,
|
|
34
|
+
|
|
35
|
+
// Publications and casts
|
|
36
|
+
PUBLICATION: 21,
|
|
37
|
+
// Types and schemas
|
|
38
|
+
SCHEMA: 2,
|
|
39
|
+
|
|
40
|
+
SEQUENCE: 8,
|
|
41
|
+
|
|
42
|
+
'SEQUENCE OWNED BY': 9,
|
|
43
|
+
|
|
44
|
+
// Tables and sequences
|
|
45
|
+
TABLE: 7,
|
|
46
|
+
// Table attachments
|
|
47
|
+
'TABLE ATTACH': 20,
|
|
48
|
+
|
|
49
|
+
'TEXT SEARCH CONFIGURATION': 24,
|
|
50
|
+
|
|
51
|
+
// Text search
|
|
52
|
+
'TEXT SEARCH DICTIONARY': 23,
|
|
53
|
+
// Triggers
|
|
54
|
+
TRIGGER: 16,
|
|
55
|
+
|
|
56
|
+
TYPE: 3,
|
|
57
|
+
// Views
|
|
58
|
+
VIEW: 14,
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Get the sort key for a constraint based on its type
|
|
63
|
+
*/
|
|
64
|
+
const getConstraintSortKey = (sql: string): string => {
|
|
65
|
+
const upperSql = sql.toUpperCase();
|
|
66
|
+
|
|
67
|
+
if (upperSql.includes('PRIMARY KEY')) {
|
|
68
|
+
return '1_PRIMARY';
|
|
69
|
+
} else if (upperSql.includes('UNIQUE')) {
|
|
70
|
+
return '2_UNIQUE';
|
|
71
|
+
} else if (upperSql.includes('FOREIGN KEY')) {
|
|
72
|
+
return '3_FOREIGN';
|
|
73
|
+
} else if (upperSql.includes('CHECK')) {
|
|
74
|
+
return '4_CHECK';
|
|
75
|
+
} else if (upperSql.includes('EXCLUDE')) {
|
|
76
|
+
return '5_EXCLUDE';
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return '9_OTHER';
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Compare two schema objects for sorting
|
|
84
|
+
*/
|
|
85
|
+
const compareSchemaObjects = (
|
|
86
|
+
a: SchemaObject,
|
|
87
|
+
b: SchemaObject,
|
|
88
|
+
schemaObjects: SchemaObject[],
|
|
89
|
+
): number => {
|
|
90
|
+
// Handle non-attributed headers (like database dump headers)
|
|
91
|
+
if (!('Type' in a.header) && !('Type' in b.header)) {
|
|
92
|
+
return 0;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (!('Type' in a.header)) {
|
|
96
|
+
return -1;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (!('Type' in b.header)) {
|
|
100
|
+
return 1;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const aHeader = a.header as AttributedHeader;
|
|
104
|
+
const bHeader = b.header as AttributedHeader;
|
|
105
|
+
|
|
106
|
+
// First, sort by type order
|
|
107
|
+
const aTypeOrder = TYPE_SORT_ORDER[aHeader.Type] ?? 999;
|
|
108
|
+
const bTypeOrder = TYPE_SORT_ORDER[bHeader.Type] ?? 999;
|
|
109
|
+
|
|
110
|
+
if (aTypeOrder !== bTypeOrder) {
|
|
111
|
+
return aTypeOrder - bTypeOrder;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// For the same type, apply specific sorting rules
|
|
115
|
+
|
|
116
|
+
// Sort by schema first (null schemas come first)
|
|
117
|
+
const aSchema = aHeader.Schema ?? '';
|
|
118
|
+
const bSchema = bHeader.Schema ?? '';
|
|
119
|
+
if (aSchema !== bSchema) {
|
|
120
|
+
if (aSchema === '') {
|
|
121
|
+
return -1;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (bSchema === '') {
|
|
125
|
+
return 1;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return aSchema.localeCompare(bSchema);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Special handling for constraints
|
|
132
|
+
if (aHeader.Type === 'CONSTRAINT' || aHeader.Type === 'FK CONSTRAINT') {
|
|
133
|
+
// Extract table name from constraint name (format: "table constraint_name")
|
|
134
|
+
const aTableName = aHeader.Name.split(' ')[0];
|
|
135
|
+
const bTableName = bHeader.Name.split(' ')[0];
|
|
136
|
+
|
|
137
|
+
if (aTableName !== bTableName) {
|
|
138
|
+
return aTableName.localeCompare(bTableName);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Within the same table, sort by constraint type
|
|
142
|
+
const aConstraintKey = getConstraintSortKey(a.sql);
|
|
143
|
+
const bConstraintKey = getConstraintSortKey(b.sql);
|
|
144
|
+
|
|
145
|
+
if (aConstraintKey !== bConstraintKey) {
|
|
146
|
+
return aConstraintKey.localeCompare(bConstraintKey);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Special handling for indexes
|
|
151
|
+
if (aHeader.Type === 'INDEX') {
|
|
152
|
+
// Group indexes by their target table
|
|
153
|
+
const aScopeObject = scopeSchemaObject(schemaObjects, a);
|
|
154
|
+
const bScopeObject = scopeSchemaObject(schemaObjects, b);
|
|
155
|
+
|
|
156
|
+
if (aScopeObject && bScopeObject) {
|
|
157
|
+
const aTableName = aScopeObject.name;
|
|
158
|
+
const bTableName = bScopeObject.name;
|
|
159
|
+
|
|
160
|
+
if (aTableName !== bTableName) {
|
|
161
|
+
return aTableName.localeCompare(bTableName);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Special handling for comments - sort by what they comment on
|
|
167
|
+
if (aHeader.Type === 'COMMENT') {
|
|
168
|
+
const aTarget = aHeader.Name;
|
|
169
|
+
const bTarget = bHeader.Name;
|
|
170
|
+
|
|
171
|
+
// Extract the type of comment (TABLE, COLUMN, etc.)
|
|
172
|
+
const aCommentType = aTarget.split(' ')[0];
|
|
173
|
+
const bCommentType = bTarget.split(' ')[0];
|
|
174
|
+
|
|
175
|
+
if (aCommentType !== bCommentType) {
|
|
176
|
+
// Define order for comment types
|
|
177
|
+
const commentTypeOrder: Record<string, number> = {
|
|
178
|
+
AGGREGATE: 6,
|
|
179
|
+
COLUMN: 8,
|
|
180
|
+
EXTENSION: 1,
|
|
181
|
+
FUNCTION: 4,
|
|
182
|
+
INDEX: 10,
|
|
183
|
+
MATERIALIZED: 12,
|
|
184
|
+
PROCEDURE: 5,
|
|
185
|
+
SCHEMA: 2,
|
|
186
|
+
SEQUENCE: 9,
|
|
187
|
+
TABLE: 7,
|
|
188
|
+
TYPE: 3,
|
|
189
|
+
VIEW: 11,
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
const aOrder = commentTypeOrder[aCommentType] ?? 999;
|
|
193
|
+
const bOrder = commentTypeOrder[bCommentType] ?? 999;
|
|
194
|
+
|
|
195
|
+
if (aOrder !== bOrder) {
|
|
196
|
+
return aOrder - bOrder;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// For COLUMN comments, sort by table name then column position
|
|
201
|
+
if (aCommentType === 'COLUMN') {
|
|
202
|
+
const aMatch = aTarget.match(/COLUMN\s+(\S+\.\S+)\.(.+)/u);
|
|
203
|
+
const bMatch = bTarget.match(/COLUMN\s+(\S+\.\S+)\.(.+)/u);
|
|
204
|
+
|
|
205
|
+
if (aMatch && bMatch) {
|
|
206
|
+
const aTable = aMatch[1];
|
|
207
|
+
const bTable = bMatch[1];
|
|
208
|
+
|
|
209
|
+
if (aTable !== bTable) {
|
|
210
|
+
return aTable.localeCompare(bTable);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// Same table, sort by column name
|
|
214
|
+
const aColumn = aMatch[2];
|
|
215
|
+
const bColumn = bMatch[2];
|
|
216
|
+
|
|
217
|
+
return aColumn.localeCompare(bColumn);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Finally, sort by name
|
|
223
|
+
return aHeader.Name.localeCompare(bHeader.Name);
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Groups schema objects by their scope (table, view, etc.) and sorts them
|
|
228
|
+
*/
|
|
229
|
+
export const groupAndSortSchemaObjects = (
|
|
230
|
+
schemaObjects: SchemaObject[],
|
|
231
|
+
): Map<string, SchemaObject[]> => {
|
|
232
|
+
const grouped = new Map<string, SchemaObject[]>();
|
|
233
|
+
|
|
234
|
+
// First, group objects by their scope
|
|
235
|
+
for (const schemaObject of schemaObjects) {
|
|
236
|
+
const scope = scopeSchemaObject(schemaObjects, schemaObject);
|
|
237
|
+
|
|
238
|
+
let key: string;
|
|
239
|
+
if (scope) {
|
|
240
|
+
// Create a unique key for each scope
|
|
241
|
+
key = `${scope.type}:${scope.schema ?? 'null'}:${scope.name}`;
|
|
242
|
+
} else if ('Type' in schemaObject.header) {
|
|
243
|
+
// For objects without a scope, group by type
|
|
244
|
+
const header = schemaObject.header as AttributedHeader;
|
|
245
|
+
key = `_UNSCOPED:${header.Type}:${header.Schema ?? 'null'}:${header.Name}`;
|
|
246
|
+
} else {
|
|
247
|
+
// Title headers
|
|
248
|
+
key = '_TITLE';
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (!grouped.has(key)) {
|
|
252
|
+
grouped.set(key, []);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const groupArray = grouped.get(key);
|
|
256
|
+
if (groupArray) {
|
|
257
|
+
groupArray.push(schemaObject);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// Sort objects within each group
|
|
262
|
+
for (const objects of grouped.values()) {
|
|
263
|
+
objects.sort((a, b) => compareSchemaObjects(a, b, schemaObjects));
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// Sort the groups themselves
|
|
267
|
+
const sortedGrouped = new Map(
|
|
268
|
+
// eslint-disable-next-line unicorn/no-array-sort
|
|
269
|
+
[...grouped.entries()].sort(([keyA], [keyB]) => {
|
|
270
|
+
// Title headers first
|
|
271
|
+
if (keyA === '_TITLE') {
|
|
272
|
+
return -1;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (keyB === '_TITLE') {
|
|
276
|
+
return 1;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// Then unscoped objects
|
|
280
|
+
if (keyA.startsWith('_UNSCOPED:') && !keyB.startsWith('_UNSCOPED:')) {
|
|
281
|
+
return -1;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
if (!keyA.startsWith('_UNSCOPED:') && keyB.startsWith('_UNSCOPED:')) {
|
|
285
|
+
return 1;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// Sort by type, schema, then name
|
|
289
|
+
return keyA.localeCompare(keyB);
|
|
290
|
+
}),
|
|
291
|
+
);
|
|
292
|
+
|
|
293
|
+
return sortedGrouped;
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Sorts an array of schema objects
|
|
298
|
+
*/
|
|
299
|
+
export const sortSchemaObjects = (
|
|
300
|
+
schemaObjects: SchemaObject[],
|
|
301
|
+
): SchemaObject[] => {
|
|
302
|
+
const sorted = [...schemaObjects];
|
|
303
|
+
|
|
304
|
+
sorted.sort((a, b) => compareSchemaObjects(a, b, schemaObjects));
|
|
305
|
+
|
|
306
|
+
return sorted;
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Sorts schema objects while preserving their grouping by scope
|
|
311
|
+
*/
|
|
312
|
+
export const sortSchemaObjectsByScope = (
|
|
313
|
+
schemaObjects: SchemaObject[],
|
|
314
|
+
): SchemaObject[] => {
|
|
315
|
+
const grouped = groupAndSortSchemaObjects(schemaObjects);
|
|
316
|
+
const result: SchemaObject[] = [];
|
|
317
|
+
|
|
318
|
+
for (const objects of grouped.values()) {
|
|
319
|
+
result.push(...objects);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
return result;
|
|
323
|
+
};
|