pg-migration-engine 0.1.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.
Files changed (104) hide show
  1. package/LICENSE +35 -0
  2. package/README.md +304 -0
  3. package/package.json +52 -0
  4. package/src/behavioral/behavioral-applier.js +374 -0
  5. package/src/behavioral/behavioral-extractor.js +266 -0
  6. package/src/behavioral/behavioral-puller.js +35 -0
  7. package/src/behavioral/index.js +4 -0
  8. package/src/behavioral/phase-sorter.js +24 -0
  9. package/src/constants.js +97 -0
  10. package/src/ddl-generator/alter-generator.js +1512 -0
  11. package/src/ddl-generator/comment-generator.js +95 -0
  12. package/src/ddl-generator/create-generator.js +1426 -0
  13. package/src/ddl-generator/drop-generator.js +194 -0
  14. package/src/ddl-generator/generator.js +78 -0
  15. package/src/ddl-generator/grant-generator.js +31 -0
  16. package/src/ddl-generator/index.js +3 -0
  17. package/src/ddl-generator/pg-version.js +15 -0
  18. package/src/ddl-generator/rename-generator.js +88 -0
  19. package/src/ddl-generator/safe-patterns.js +241 -0
  20. package/src/differ/change-classifier.js +192 -0
  21. package/src/differ/dependency-resolver.js +523 -0
  22. package/src/differ/index.js +12 -0
  23. package/src/differ/object-matcher.js +510 -0
  24. package/src/differ/property-differ.js +1134 -0
  25. package/src/differ/risk-tagger.js +561 -0
  26. package/src/differ/schema-differ.js +309 -0
  27. package/src/differ/utils/levenshtein.js +210 -0
  28. package/src/differ/utils/path-builder.js +198 -0
  29. package/src/differ/utils/type-compatibility.js +472 -0
  30. package/src/errors.js +147 -0
  31. package/src/executor/drift-detector.js +221 -0
  32. package/src/executor/index.js +7 -0
  33. package/src/executor/lock-manager.js +308 -0
  34. package/src/executor/migration-executor.js +1249 -0
  35. package/src/executor/progress-tracker.js +81 -0
  36. package/src/executor/recovery-manager.js +47 -0
  37. package/src/executor/snapshot-manager.js +22 -0
  38. package/src/executor/sql-splitter.js +120 -0
  39. package/src/executor/transaction-manager.js +288 -0
  40. package/src/index.js +483 -0
  41. package/src/introspection/index.js +4 -0
  42. package/src/introspection/introspector.js +250 -0
  43. package/src/introspection/queries/access-methods.js +29 -0
  44. package/src/introspection/queries/casts.js +38 -0
  45. package/src/introspection/queries/collations.js +51 -0
  46. package/src/introspection/queries/comments.js +66 -0
  47. package/src/introspection/queries/constraints.js +65 -0
  48. package/src/introspection/queries/conversions.js +39 -0
  49. package/src/introspection/queries/databases.js +43 -0
  50. package/src/introspection/queries/default-privileges.js +37 -0
  51. package/src/introspection/queries/event-triggers.js +43 -0
  52. package/src/introspection/queries/extensions.js +21 -0
  53. package/src/introspection/queries/foreign-data.js +136 -0
  54. package/src/introspection/queries/functions.js +75 -0
  55. package/src/introspection/queries/grants.js +38 -0
  56. package/src/introspection/queries/index.js +33 -0
  57. package/src/introspection/queries/indexes.js +99 -0
  58. package/src/introspection/queries/inheritance.js +24 -0
  59. package/src/introspection/queries/multiranges.js +37 -0
  60. package/src/introspection/queries/operators.js +180 -0
  61. package/src/introspection/queries/partitions.js +42 -0
  62. package/src/introspection/queries/pg18-19.js +43 -0
  63. package/src/introspection/queries/policies.js +35 -0
  64. package/src/introspection/queries/procedural-languages.js +36 -0
  65. package/src/introspection/queries/publications.js +114 -0
  66. package/src/introspection/queries/roles.js +66 -0
  67. package/src/introspection/queries/rules.js +49 -0
  68. package/src/introspection/queries/sequences.js +37 -0
  69. package/src/introspection/queries/statistics.js +74 -0
  70. package/src/introspection/queries/subscriptions.js +99 -0
  71. package/src/introspection/queries/tables.js +151 -0
  72. package/src/introspection/queries/tablespaces.js +36 -0
  73. package/src/introspection/queries/text-search.js +146 -0
  74. package/src/introspection/queries/triggers.js +61 -0
  75. package/src/introspection/queries/types.js +106 -0
  76. package/src/introspection/queries/views.js +146 -0
  77. package/src/introspection/translator.js +1283 -0
  78. package/src/introspection/version-detector.js +25 -0
  79. package/src/planner/backfill-planner.js +36 -0
  80. package/src/planner/dry-run.js +21 -0
  81. package/src/planner/index.js +6 -0
  82. package/src/planner/migration-planner.js +356 -0
  83. package/src/planner/smart-migrator.js +82 -0
  84. package/src/planner/step-sequencer.js +61 -0
  85. package/src/planner/type-registry.js +47 -0
  86. package/src/risk/compatibility-checker.js +29 -0
  87. package/src/risk/data-loss-checker.js +32 -0
  88. package/src/risk/destructive-checker.js +38 -0
  89. package/src/risk/index.js +6 -0
  90. package/src/risk/lock-analyzer.js +39 -0
  91. package/src/risk/recommendations.js +44 -0
  92. package/src/risk/risk-engine.js +25 -0
  93. package/src/storage/index.js +5 -0
  94. package/src/storage/memory-storage.js +87 -0
  95. package/src/storage/migration-table.js +423 -0
  96. package/src/storage/rollback-generator.js +344 -0
  97. package/src/types/changes.js +136 -0
  98. package/src/types/connection.js +17 -0
  99. package/src/types/execution.js +107 -0
  100. package/src/types/index.js +1 -0
  101. package/src/types/migration.js +67 -0
  102. package/src/types/risk.js +32 -0
  103. package/src/types/schema.d.ts +1004 -0
  104. package/src/types/schema.js +561 -0
package/LICENSE ADDED
@@ -0,0 +1,35 @@
1
+ Business Source License 1.1
2
+
3
+ Copyright (c) 2026 Schema Weaver
4
+
5
+ Terms
6
+ The Licensor hereby grants you the right to copy, modify, create derivative
7
+ works, redistribute, and make use of the Licensed Work, provided that:
8
+
9
+ 1. **Non-Production Use**: You may use the Licensed Work for development,
10
+ testing, evaluation, and non-production purposes without restriction.
11
+
12
+ 2. **Production Use**: Use of the Licensed Work in a production environment
13
+ (defined as an environment used by end users for business-critical
14
+ operations) requires a separate commercial license agreement with the
15
+ Licensor.
16
+
17
+ 3. **Commercial License**: To obtain a commercial license for production use,
18
+ contact: vivek@vivekmind.com
19
+
20
+ 4. **Change Date**: This license will automatically convert to the Apache
21
+ License, Version 2.0 on January 1, 2030, at which point the Licensed Work
22
+ will become freely available for any purpose, including production use.
23
+
24
+ 5. **No Trademark Rights**: This license does not grant you any right to use
25
+ the trademarks, service marks, or logos of the Licensor.
26
+
27
+ 6. **Disclaimer**: THE LICENSED WORK IS PROVIDED "AS IS", WITHOUT WARRANTY
28
+ OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
29
+ WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
30
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33
+ WITH THE LICENSED WORK.
34
+
35
+ See https://mariadb.com/bsl11/ for the full text of the BSL 1.1 license.
package/README.md ADDED
@@ -0,0 +1,304 @@
1
+ # pg-migration-engine
2
+
3
+ [![npm version](https://img.shields.io/npm/v/pg-migration-engine.svg)](https://www.npmjs.com/package/pg-migration-engine)
4
+ [![license](https://img.shields.io/badge/license-BSL--1.1-red.svg)](https://github.com/Schema-Weaver/pg-migration-engine/blob/main/LICENSE)
5
+ [![PostgreSQL](https://img.shields.io/badge/PostgreSQL-10%20%E2%80%93%2018-blue.svg)](https://www.postgresql.org/)
6
+
7
+ **PostgreSQL schema introspection, diff, DDL generation, and safe migration execution.**
8
+
9
+ ---
10
+
11
+ ## Why This Exists
12
+
13
+ No existing open-source tool covers PostgreSQL object types comprehensively:
14
+
15
+ | Tool | Coverage | Limitations |
16
+ |------|----------|-------------|
17
+ | pgAdmin | UI only | No programmatic API, no diff |
18
+ | Atlas | ~15 types | Missing views, functions, triggers, policies |
19
+ | Prisma | ~10 types | Ignores behavioral objects entirely |
20
+ | Django Migrations | ~12 types | ORM-specific, limited DDL control |
21
+
22
+ **This engine covers EVERYTHING** — 50+ object types with extensive property coverage.
23
+
24
+ ---
25
+
26
+ ## Supported Object Types (50+)
27
+
28
+ ### Track 1: Structural Objects
29
+
30
+ | Type | Category | Coverage |
31
+ |------|----------|----------|
32
+ | Table | Core | Full |
33
+ | Column | Core | Full |
34
+ | Index | Core | Full |
35
+ | Constraint (PK/FK/UNIQUE/CHECK/EXCLUSION) | Core | Full |
36
+ | Sequence | Core | Full |
37
+ | Enum Type | Type System | Full |
38
+ | Composite Type | Type System | Full |
39
+ | Domain Type | Type System | Full |
40
+ | Range Type | Type System | Full |
41
+ | Multirange Type | Type System (PG14+) | Full |
42
+ | Schema | Container | Full |
43
+ | Extension | Package | Full |
44
+ | Statistics | Optimizer | Full |
45
+ | Partition | Table Property | Full |
46
+ | Collation | Locale | ~90% |
47
+ | Cast | Type System | Full |
48
+ | Operator | Advanced | Full |
49
+ | Operator Class | Advanced | Full |
50
+ | Operator Family | Advanced | Full |
51
+ | Access Method | Storage | Full |
52
+ | Foreign Table | FDW | Full |
53
+ | Default Privileges | ACL | Full |
54
+
55
+ ### Track 2: Behavioral Objects
56
+
57
+ | Type | Category | Coverage |
58
+ |------|----------|----------|
59
+ | View | Query | Full |
60
+ | Materialized View | Query | Full |
61
+ | Function | Procedural | Full |
62
+ | Procedure | Procedural (PG11+) | Full |
63
+ | Aggregate | Procedural | Full |
64
+ | Trigger | Automation | Full |
65
+ | Event Trigger | Automation | Full |
66
+ | Policy | RLS | Full |
67
+ | Rule | Rewrite | Full |
68
+ | Text Search Config | FTS | Full |
69
+ | Text Search Dictionary | FTS | Full |
70
+ | Text Search Parser | FTS | Full |
71
+ | Text Search Template | FTS | Full |
72
+ | Conversion | Encoding | Full |
73
+ | Foreign Data Wrapper | FDW | Full |
74
+ | Foreign Server | FDW | Full |
75
+ | User Mapping | FDW | Full |
76
+
77
+ ### Track 3: Cross-Database Objects
78
+
79
+ | Type | Category | Coverage |
80
+ |------|----------|----------|
81
+ | Database | Instance | Full |
82
+ | Tablespace | Storage | Full |
83
+ | Role | Auth | Full |
84
+ | Publication | Replication | Full |
85
+ | Subscription | Replication | Full |
86
+
87
+ **Overall: 190+ properties tracked across PG10–PG18**
88
+
89
+ ---
90
+
91
+ ## Architecture (8 Modules)
92
+
93
+ ```
94
+ SQL ──► Introspector ──► Translator ──► Differ ──► DDL Generator ──► Planner ──► Executor
95
+ │ │ │
96
+ ▼ ▼ ▼
97
+ Risk Tagger Safe Patterns Storage
98
+ (assess change risk) (history)
99
+
100
+
101
+ Behavioral
102
+ (views/fns/triggers)
103
+ ```
104
+
105
+ | Module | Purpose |
106
+ |--------|---------|
107
+ | **Introspector** | Queries `pg_catalog` for 50+ object types |
108
+ | **Translator** | Normalizes raw PG rows into canonical schema model |
109
+ | **Differ** | Detects CREATE/ALTER/DROP/RENAME with property-level diff |
110
+ | **Risk Tagger** | 5-level risk assessment per change |
111
+ | **DDL Generator** | Generates safe DDL with 6+ safe patterns |
112
+ | **Planner** | Dependency-ordered, phase-based plan (Track 1 → Track 2) |
113
+ | **Executor** | Advisory locks, savepoints, drift detection, progress |
114
+ | **Storage** | Migration history (PostgreSQL / Memory / File / GitHub backends) |
115
+
116
+ ---
117
+
118
+ ## Installation
119
+
120
+ ```bash
121
+ npm install pg-migration-engine
122
+ ```
123
+
124
+ ---
125
+
126
+ ## Quick Start
127
+
128
+ ```javascript
129
+ import pg from 'pg';
130
+ import { PgMigrationEngine } from 'pg-migration-engine';
131
+
132
+ const pool = new pg.Pool({
133
+ host: 'localhost',
134
+ port: 5432,
135
+ database: 'mydb',
136
+ user: 'postgres',
137
+ password: 'pass'
138
+ });
139
+
140
+ const engine = new PgMigrationEngine();
141
+
142
+ // Introspect current database
143
+ const snapshot = await engine.introspect(pool);
144
+
145
+ // Diff against desired schema
146
+ const desired = /* your target schema */;
147
+ const diff = engine.diff(desired, snapshot);
148
+
149
+ // Generate DDL
150
+ const ddl = engine.generateDDL(diff);
151
+
152
+ // Plan migration respecting dependencies
153
+ const plan = engine.plan(diff);
154
+
155
+ // Execute with safety checks
156
+ if (!plan.blocked) {
157
+ const result = await engine.execute(pool, plan);
158
+ console.log('Migrated:', result.migrationId);
159
+ }
160
+
161
+ // Or one-shot migrate
162
+ const result = await engine.migrate(pool, desired);
163
+ ```
164
+
165
+ ---
166
+
167
+ ## Key Features
168
+
169
+ ### 50+ PostgreSQL Object Types
170
+ Most comprehensive coverage available. See [Supported Objects](https://github.com/Schema-Weaver/pg-migration-engine/wiki/Supported-Objects).
171
+
172
+ ### PG 10–18 Support
173
+ Version-gated queries automatically adapt to your PostgreSQL version.
174
+
175
+ ### Safe Migration Patterns (6+ patterns)
176
+
177
+ | Pattern | How It Works |
178
+ |---------|--------------|
179
+ | **NOT NULL** | 3-step: CHECK(NOT VALID) → validate → SET NOT NULL |
180
+ | **FK Constraint** | ADD NOT VALID → validate separately |
181
+ | **Index** | CREATE INDEX CONCURRENTLY for zero-downtime |
182
+ | **Enum ADD VALUE** | Pre-transaction for PG < 14 |
183
+ | **UNIQUE constraint** | CREATE UNIQUE INDEX CONCURRENTLY → ADD CONSTRAINT USING INDEX |
184
+ | **CHECK constraint** | ADD NOT VALID → validate separately |
185
+ | **Type change** | ALTER TYPE with USING clause for explicit conversion |
186
+
187
+ ### 5-Level Risk Assessment
188
+
189
+ | Level | Examples | Default |
190
+ |-------|----------|---------|
191
+ | `none` | CREATE TABLE, ADD nullable column | ✅ Allow |
192
+ | `low` | ADD COLUMN with default, CREATE INDEX | ✅ Allow |
193
+ | `medium` | ALTER COLUMN type (widening) | ✅ Allow |
194
+ | `high` | DROP INDEX, ALTER type (narrowing) | ❌ Block |
195
+ | `critical` | DROP TABLE, DROP COLUMN | ❌ Block |
196
+
197
+ ### RENAME Detection
198
+ Smart matching detects renames instead of DROP+CREATE, preserving data.
199
+
200
+ ### Drift Detection
201
+ Fingerprint-based schema drift alerts when database changed outside migrations.
202
+
203
+ ### Rollback Generation
204
+ Automatic reverse DDL for supported changes.
205
+
206
+ ### Non-Transactional Aware
207
+ Handles `ALTER ENUM ADD VALUE`, `CREATE INDEX CONCURRENTLY`, `REINDEX CONCURRENTLY` outside transactions.
208
+
209
+ ---
210
+
211
+ ## API Reference
212
+
213
+ | Method | Description |
214
+ |--------|-------------|
215
+ | `introspect(pool)` | Capture database schema as `SchemaSnapshot` |
216
+ | `diff(desired, current)` | Detect changes between schemas |
217
+ | `generateDDL(diff)` | Generate PostgreSQL DDL statements |
218
+ | `plan(diff)` | Create dependency-ordered migration plan |
219
+ | `execute(pool, plan)` | Apply migration transactionally |
220
+ | `migrate(pool, desired)` | One-shot: introspect → diff → plan → execute |
221
+ | `rollback(pool, migrationId)` | Revert migration (best-effort) |
222
+ | `createMigrationPlan(changes)` | Create plan from change array (alias) |
223
+ | `diffSchemas(desired, current)` | Alias for `diff()` |
224
+
225
+ Full API: [API Reference](https://github.com/Schema-Weaver/pg-migration-engine/wiki/API-Reference)
226
+
227
+ ---
228
+
229
+ ## Ecosystem
230
+
231
+ | Package | Purpose |
232
+ |---------|---------|
233
+ | **[sw-agent](https://www.npmjs.com/package/@vivekmind/sw-agent)** | PostgreSQL connection pool, security, audit |
234
+ | **[pg-ddl-parser](https://www.npmjs.com/package/pg-ddl-parser)** | DDL parsing (CREATE/ALTER/DROP → schema) |
235
+ | **pg-migration-engine** | Schema introspection, diff, migration |
236
+
237
+ ```javascript
238
+ import { parsePostgresSQL } from 'pg-ddl-parser';
239
+ import { PgMigrationEngine } from 'pg-migration-engine';
240
+
241
+ // Parse DDL → desired schema
242
+ const desired = parsePostgresSQL(fs.readFileSync('schema.sql', 'utf8'));
243
+
244
+ // Migrate
245
+ const engine = new PgMigrationEngine();
246
+ await engine.migrate(pool, convertParsedToSnapshot(desired));
247
+ ```
248
+
249
+ ---
250
+
251
+ ## Test Results
252
+
253
+ | Layer | Test | Assertions | Result |
254
+ |-------|------|------------|--------|
255
+ | 1 | Introspection Accuracy | 93 | ✅ 100% PASS |
256
+ | 2 | Diff Detection | — | ✅ PASS |
257
+ | 3 | E2E Pipeline | 12 | ✅ 100% PASS |
258
+ | 4 | Planner Order | — | ✅ Phase order verified |
259
+ | 5 | Execution | — | Partial — needs production |
260
+ | 6 | Recovery | — | Pending |
261
+
262
+ Tested against **PostgreSQL 18.1** with a 25-table, 4-schema, 1052-object database.
263
+
264
+ ---
265
+
266
+ ## Documentation
267
+
268
+ - [Architecture](https://github.com/Schema-Weaver/pg-migration-engine/wiki/Architecture) — 8-module deep dive
269
+ - [Supported Objects](https://github.com/Schema-Weaver/pg-migration-engine/wiki/Supported-Objects) — Full 50+ type coverage
270
+ - [Safe Patterns](https://github.com/Schema-Weaver/pg-migration-engine/wiki/Safe-Migration-Patterns) — 6+ safe workflows
271
+ - [Risk Assessment](https://github.com/Schema-Weaver/pg-migration-engine/wiki/Risk-Assessment) — 5 levels explained
272
+ - [PG Version Matrix](https://github.com/Schema-Weaver/pg-migration-engine/wiki/PG-Version-Matrix) — Feature support PG10-18
273
+
274
+ ---
275
+
276
+ ## PostgreSQL Version Support
277
+
278
+ | Version | Status | Key Features |
279
+ |---------|--------|--------------|
280
+ | PG 10 | ✅ | Generated columns |
281
+ | PG 11 | ✅ | Procedures, INCLUDE indexes |
282
+ | PG 12 | ✅ | GENERATED ALWAYS |
283
+ | PG 13 | ✅ | Incremental sort |
284
+ | PG 14 | ✅ | Multirange, security invoker |
285
+ | PG 15 | ✅ | NULLS NOT DISTINCT |
286
+ | PG 16 | ✅ | ANY_VALUE |
287
+ | PG 17 | ✅ | MERGE improvements |
288
+ | PG 18 | ✅ | Temporal tables, PERIOD, NOT ENFORCED constraints |
289
+
290
+ ---
291
+
292
+ ## License
293
+
294
+ Business Source License 1.1 (BSL) — see [LICENSE](./LICENSE)
295
+
296
+ - ✅ Free for development, testing, evaluation
297
+ - ✅ Free for non-production environments
298
+ - ❌ Production use requires commercial license
299
+
300
+ Contact: **vivek@vivekmind.com**
301
+
302
+ ---
303
+
304
+ Built by [Schema Weaver](https://schemaweaver.vivekmind.com).
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "pg-migration-engine",
3
+ "version": "0.1.0",
4
+ "description": "PostgreSQL schema migration engine. Introspect, diff, generate DDL, and execute safe migrations for 50+ object types across PG10-18.",
5
+ "license": "BSL-1.1",
6
+ "author": "Schema Weaver",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/Schema-Weaver/pg-migration-engine.git"
10
+ },
11
+ "homepage": "https://schemaweaver.vivekmind.com",
12
+ "bugs": {
13
+ "url": "https://github.com/Schema-Weaver/pg-migration-engine/issues"
14
+ },
15
+ "keywords": [
16
+ "postgresql",
17
+ "postgres",
18
+ "migration",
19
+ "schema",
20
+ "ddl",
21
+ "introspection",
22
+ "diff",
23
+ "pg-migration"
24
+ ],
25
+ "type": "module",
26
+ "main": "./src/index.js",
27
+ "exports": {
28
+ ".": {
29
+ "import": "./src/index.js",
30
+ "types": "./src/types/schema.d.ts"
31
+ }
32
+ },
33
+ "files": [
34
+ "src/",
35
+ "README.md",
36
+ "LICENSE"
37
+ ],
38
+ "engines": {
39
+ "node": ">=18.0.0"
40
+ },
41
+ "dependencies": {
42
+ "pg": "^8.13.0"
43
+ },
44
+ "peerDependencies": {
45
+ "pg-ddl-parser": ">=4.0.0"
46
+ },
47
+ "peerDependenciesMeta": {
48
+ "pg-ddl-parser": {
49
+ "optional": true
50
+ }
51
+ }
52
+ }