@subodh001/pg-safe-migrate 2.5.0 → 2.7.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/CHANGELOG.md +81 -0
- package/CONTRIBUTING.md +3 -0
- package/README.md +208 -320
- package/SECURITY.md +13 -0
- package/bin/cli.js +18 -10
- package/docs/RULES.md +31 -0
- package/examples/consumer-workflow.yml +4 -17
- package/examples/suppression-gate-policy.json +11 -0
- package/package.json +6 -2
- package/src/analysis.js +69 -6
- package/src/config.js +12 -1
- package/src/locks.js +128 -12
- package/src/parser.js +46 -10
- package/src/reports.js +8 -5
- package/src/rules/ruleCatalog.js +6 -2
- package/src/suppression-matcher.js +24 -0
- package/src/suppression-report.js +19 -0
- package/src/suppression.js +78 -0
- package/src/team-policy.js +38 -2
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [2.7.0]
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- SQL-aware inline rule suppression.
|
|
8
|
+
|
|
9
|
+
- AST-based suppression targeting for individual SQL statements.
|
|
10
|
+
|
|
11
|
+
- Mandatory suppression reasons for auditability.
|
|
12
|
+
|
|
13
|
+
- Configurable protected migration safety rules.
|
|
14
|
+
|
|
15
|
+
- Suppression audit reporting in terminal, JSON, SARIF and Markdown formats.
|
|
16
|
+
|
|
17
|
+
- Enterprise suppression policies with configurable limits.
|
|
18
|
+
|
|
19
|
+
- Detection of rejected, unused and invalid suppression directives.
|
|
20
|
+
|
|
21
|
+
- Suppression-aware VS Code diagnostics and audit reporting.
|
|
22
|
+
|
|
23
|
+
### Security
|
|
24
|
+
|
|
25
|
+
- Protected rules cannot be suppressed through ordinary inline directives.
|
|
26
|
+
|
|
27
|
+
- Enterprise gates reject project exclusions and severity-weakening overrides.
|
|
28
|
+
|
|
29
|
+
- Suppressed findings remain included in enterprise risk calculations.
|
|
30
|
+
|
|
31
|
+
- Ambiguous SQL scanning fails closed.
|
|
32
|
+
|
|
33
|
+
### Verification
|
|
34
|
+
|
|
35
|
+
- 363 automated regression tests passed.
|
|
36
|
+
|
|
37
|
+
- Compatibility verified on Node.js 18, 20, 22 and 24.
|
|
38
|
+
|
|
39
|
+
- Repeated memory retention testing across 200,000 measured SQL statements.
|
|
40
|
+
|
|
41
|
+
- Performance benchmarks covering up to 10,000 SQL statements per workload.
|
|
42
|
+
|
|
43
|
+
### Compatibility and limitations
|
|
44
|
+
|
|
45
|
+
- Inline suppression is disabled by default.
|
|
46
|
+
|
|
47
|
+
- Enterprise gates now reject configurations that weaken rule severity or exclude files.
|
|
48
|
+
|
|
49
|
+
- Some combinations of suppression, risk baselines and combined simulation are not supported.
|
|
50
|
+
|
|
51
|
+
- Enterprise enforcement requires trusted CLI code, configuration, policies and CI workflows.
|
|
52
|
+
|
|
53
|
+
- Static migration analysis does not guarantee zero downtime.
|
|
54
|
+
|
|
55
|
+
## [2.6.0]
|
|
56
|
+
|
|
57
|
+
* Unicode-aware SQL positions, leading comment handling and transaction tracking for savepoints and chained transactions.
|
|
58
|
+
* Optimized statement position tracking.
|
|
59
|
+
* Corrected rule catalog coverage and default severity metadata (no enforcement changes).
|
|
60
|
+
|
|
61
|
+
## [2.5.0] - 2026-11-20
|
|
62
|
+
|
|
63
|
+
### Added
|
|
64
|
+
|
|
65
|
+
* Configurable CI migration safety gates.
|
|
66
|
+
* Reviewed migration risk baselines to distinguish new, existing, and resolved findings.
|
|
67
|
+
* Production-aware migration recommendations using optional workload and table-size estimates.
|
|
68
|
+
* GitHub Actions pull-request safety reporting.
|
|
69
|
+
* Improved onboarding, examples, and adoption documentation.
|
|
70
|
+
|
|
71
|
+
### Improved
|
|
72
|
+
|
|
73
|
+
* JSON reporting for risk baselines, production context, and policy decisions.
|
|
74
|
+
* Migration safety guidance for deployment planning.
|
|
75
|
+
* Release-readiness verification and regression coverage.
|
|
76
|
+
|
|
77
|
+
### Notes
|
|
78
|
+
|
|
79
|
+
* Production context uses supplied estimates, not live database measurements.
|
|
80
|
+
* Static analysis does not guarantee zero downtime.
|
|
81
|
+
* Existing migration safety rules remain available.
|
package/CONTRIBUTING.md
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
# Contributing
|
|
2
|
+
|
|
3
|
+
Fork the repository and create a focused branch. Install dependencies with `npm ci`. Add regression tests for rule changes and preserve stable diagnostic IDs. Run `npm run test:release` and `git diff --check`. For extension changes run `npm run build:standalone` and `npm run verify:standalone` in `vscode-extension`. Document limitations and include sanitized SQL reproductions. Do not commit secrets, generated VSIX files or local logs. Open a pull request describing the change and test results. Security reports should follow SECURITY.md.
|
package/README.md
CHANGED
|
@@ -1,160 +1,106 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
# pg-safe-migrate
|
|
1
|
+
# PG Safe Migrate
|
|
4
2
|
|
|
5
3
|
**Catch risky PostgreSQL migrations before they reach production.**
|
|
6
4
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
Detect risky PostgreSQL DDL patterns and provide additional framework-specific migration advisories.
|
|
10
|
-
|
|
11
|
-
**Features:** PostgreSQL AST parsing · PG001–PG017 identifiers (see rule coverage below) · Git-diff mode · JSON/SARIF · configurable severity · optional advisor · GitHub Actions
|
|
12
|
-
|
|
13
|
-
> **Important:** This tool identifies configured migration risks. It does not guarantee zero downtime.
|
|
14
|
-
|
|
15
|
-
## Demo
|
|
16
|
-
|
|
17
|
-
See how `pg-safe-migrate` catches an unsafe PostgreSQL migration and suggests a safer alternative.
|
|
18
|
-
|
|
19
|
-

|
|
20
|
-
|
|
21
|
-
### Example
|
|
22
|
-
|
|
23
|
-
Unsafe migration:
|
|
24
|
-
|
|
25
|
-
```sql
|
|
26
|
-
CREATE INDEX idx_users_email ON users(email);
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
Detected issue:
|
|
30
|
-
|
|
31
|
-
```text
|
|
32
|
-
[PG001] CREATE INDEX on "users" acquires a SHARE lock and blocks writes.
|
|
33
|
-
|
|
34
|
-
Fix: Use CREATE INDEX CONCURRENTLY and execute outside a transaction block.
|
|
35
|
-
|
|
36
|
-
Found 1 blocking migration issue(s).
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
Safer alternative:
|
|
40
|
-
|
|
41
|
-
```sql
|
|
42
|
-
CREATE INDEX CONCURRENTLY idx_users_email ON users(email);
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
`CREATE INDEX CONCURRENTLY` must run outside a transaction block.
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
## Week 3 preview: opt-in CI policy gate (unreleased)
|
|
49
|
-
|
|
50
|
-
Run a deterministic, policy-driven static lint check in CI:
|
|
5
|
+
Open-source, AST-based migration linting for Prisma, Drizzle, Flyway and raw SQL. Detect destructive changes, locking risks and transaction hazards before deployment.
|
|
51
6
|
|
|
52
|
-
|
|
53
|
-
node bin/cli.js db/migrations --gate-policy examples/week3-gate-policy.json --fail-on none
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
The gate can block selected `PGxxx` rules, excessive warnings, and heuristic risk scores. It reports a block to stderr and exits with code `1`; malformed configuration exits `2`. This is a **development-only preview**, not available in the published v2.4.0 package. See [Week 3 implementation notes](WEEK3-DAY1-IMPLEMENTED.md).
|
|
57
|
-
|
|
58
|
-
[Install from npm](https://www.npmjs.com/package/@subodh001/pg-safe-migrate) · [Install the VS Code extension](https://marketplace.visualstudio.com/items?itemName=sukoaa.pg-safe-migrate-vscode) · [Report an issue](https://github.com/subodhsingh001/pg-safe-migrate/issues)
|
|
59
|
-
|
|
60
|
-
## Adoption and developer resources (Week 3 candidate)
|
|
61
|
-
|
|
62
|
-
- [Safe onboarding and CI troubleshooting](docs/ADOPTION-GUIDE.md)
|
|
63
|
-
- [Short demonstration recording script](docs/DEMO-SCRIPT.md)
|
|
64
|
-
- [Proposed npm and VS Code Marketplace copy](docs/MARKETPLACE-COPY.md)
|
|
7
|
+
[](https://www.npmjs.com/package/@subodh001/pg-safe-migrate) [](https://marketplace.visualstudio.com/items?itemName=sukoaa.pg-safe-migrate-vscode) [](LICENSE)
|
|
65
8
|
|
|
66
|
-
|
|
9
|
+
[Product website](https://www.sukoaa.com/pg-safe-migrate) · [Rule reference](docs/RULES.md) · [VS Code](https://marketplace.visualstudio.com/items?itemName=sukoaa.pg-safe-migrate-vscode) · [Report an issue](https://github.com/subodhsingh001/pg-safe-migrate/issues)
|
|
67
10
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
Install as a development dependency:
|
|
71
|
-
|
|
72
|
-
```bash
|
|
73
|
-
npm install --save-dev @subodh001/pg-safe-migrate
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
Or execute the published package without installing it in your project:
|
|
77
|
-
|
|
78
|
-
```bash
|
|
79
|
-
npx --yes --package=@subodh001/pg-safe-migrate@2.4.0 pg-safe-migrate --help
|
|
80
|
-
```
|
|
11
|
+

|
|
81
12
|
|
|
82
13
|
## Quick start
|
|
83
14
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
```bash
|
|
87
|
-
npx pg-safe-migrate
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
The CLI searches these directories:
|
|
91
|
-
|
|
92
|
-
- `prisma/migrations`
|
|
93
|
-
- `drizzle`
|
|
94
|
-
- `migrations`
|
|
95
|
-
- `db/migrations`
|
|
96
|
-
|
|
97
|
-
### Prisma
|
|
98
|
-
|
|
99
|
-
```bash
|
|
100
|
-
npx pg-safe-migrate prisma/migrations
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
### Drizzle
|
|
15
|
+
Requires Node.js 18 or later; use a maintained Node.js release for new installations.
|
|
104
16
|
|
|
105
|
-
```
|
|
106
|
-
npx pg-safe-migrate
|
|
17
|
+
```sh
|
|
18
|
+
npx --yes --package=@subodh001/pg-safe-migrate@2.7.0 pg-safe-migrate
|
|
107
19
|
```
|
|
108
20
|
|
|
109
|
-
|
|
21
|
+
Or install in your project:
|
|
110
22
|
|
|
111
|
-
```
|
|
112
|
-
|
|
23
|
+
```sh
|
|
24
|
+
npm install --save-dev @subodh001/pg-safe-migrate
|
|
25
|
+
npx --no-install pg-safe-migrate prisma/migrations --advisor
|
|
113
26
|
```
|
|
114
27
|
|
|
115
|
-
|
|
28
|
+
Use `drizzle`, `db/migrations` or a specific SQL file instead of `prisma/migrations`. With no path, the CLI detects common migration directories.
|
|
116
29
|
|
|
117
|
-
|
|
118
|
-
npx pg-safe-migrate path/to/migration.sql
|
|
119
|
-
```
|
|
120
|
-
|
|
121
|
-
### Check changed migrations only
|
|
30
|
+
### Example
|
|
122
31
|
|
|
123
|
-
```
|
|
124
|
-
|
|
32
|
+
```sql
|
|
33
|
+
ALTER TABLE users DROP COLUMN email;
|
|
125
34
|
```
|
|
126
35
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
36
|
+
```text
|
|
37
|
+
unsafe.sql:1:1 [PG002] error: Dropping column "users.email" can break older application instances.
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Remove application references before dropping the column in a later deployment. A passing lint check is not a guarantee of zero downtime.
|
|
41
|
+
|
|
42
|
+
## GitHub Actions
|
|
43
|
+
|
|
44
|
+
Save as `.github/workflows/migration-lint.yml`:
|
|
45
|
+
|
|
46
|
+
```yaml
|
|
47
|
+
name: PostgreSQL migration safety
|
|
48
|
+
on:
|
|
49
|
+
pull_request:
|
|
50
|
+
paths:
|
|
51
|
+
- '**/*.sql'
|
|
52
|
+
permissions:
|
|
53
|
+
contents: read
|
|
54
|
+
jobs:
|
|
55
|
+
migrations:
|
|
56
|
+
runs-on: ubuntu-latest
|
|
57
|
+
steps:
|
|
58
|
+
- uses: actions/checkout@v4
|
|
59
|
+
with:
|
|
60
|
+
fetch-depth: 0
|
|
61
|
+
persist-credentials: false
|
|
62
|
+
- uses: actions/setup-node@v4
|
|
63
|
+
with:
|
|
64
|
+
node-version: '22'
|
|
65
|
+
- name: Check changed SQL
|
|
66
|
+
env:
|
|
67
|
+
BASE_REF: ${{ github.base_ref }}
|
|
68
|
+
run: npx --yes --package=@subodh001/pg-safe-migrate@2.7.0 pg-safe-migrate --changed "origin/$BASE_REF" --fail-on error
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Start with reporting-only mode (`--fail-on none`) if introducing linting into an existing project. Review findings before enforcing a gate. See the [adoption guide](docs/ADOPTION-GUIDE.md).
|
|
72
|
+
|
|
73
|
+
## What it detects
|
|
74
|
+
|
|
75
|
+
| ID | Diagnostic | Default | Guidance |
|
|
76
|
+
|---|---|---|---|
|
|
77
|
+
| PG001 | Non-concurrent index creation | error | Use CREATE INDEX CONCURRENTLY outside a transaction. |
|
|
78
|
+
| PG002 | Dropping a column | error | Deprecate consumers before removing the column. |
|
|
79
|
+
| PG003 | Changing a column type | error | Check binary compatibility; consider expand/backfill/contract. |
|
|
80
|
+
| PG004 | Adding NOT NULL column without default | error | Add nullable, backfill, then enforce NOT NULL. |
|
|
81
|
+
| PG005 | Adding validated foreign key | error | Add NOT VALID then VALIDATE CONSTRAINT separately. |
|
|
82
|
+
| PG006 | Renaming an object | error | Use an expand/contract deployment. |
|
|
83
|
+
| PG007 | Dropping index without CONCURRENTLY | error | Use DROP INDEX CONCURRENTLY where supported. |
|
|
84
|
+
| PG008 | Concurrent index operation inside transaction | error | Run outside BEGIN/COMMIT. |
|
|
85
|
+
| PG010 | Adding UNIQUE constraint | error | Consider a concurrent unique index and attach it where applicable. |
|
|
86
|
+
| PG011 | Dropping a table | error | Verify retention and application dependencies. |
|
|
87
|
+
| PG012 | Adding validated CHECK constraint | error | Add NOT VALID and validate separately. |
|
|
88
|
+
| PG013 | Legacy SET NOT NULL diagnostic (superseded by PG025) | error | Validate an equivalent CHECK constraint first. |
|
|
89
|
+
| PG014 | Adding column with default on PostgreSQL 10 | error | Use controlled backfill or evaluate upgrading PostgreSQL. |
|
|
90
|
+
| PG015 | Prisma concurrent index migration | warning | Use a separately managed nontransactional migration. |
|
|
91
|
+
| PG016 | Drizzle concurrent index migration | warning | Use a separately managed nontransactional migration. |
|
|
92
|
+
| PG017 | Potential cross-file dependency or Flyway transaction advisory | warning | Review migration order; this is a heuristic. |
|
|
93
|
+
| PG019 | MERGE requires PostgreSQL 15+ | error | Use a compatible INSERT ... ON CONFLICT pattern where applicable. |
|
|
94
|
+
| PG020 | UNIQUE NULLS NOT DISTINCT requires PostgreSQL 15+ | error | Use a version-compatible uniqueness strategy. |
|
|
95
|
+
| PG021 | Volatile default may rewrite table | error | Add column, backfill in batches, then set default. |
|
|
96
|
+
| PG022 | SET LOGGED rewrites table | error | Schedule controlled maintenance and evaluate alternatives. |
|
|
97
|
+
| PG023 | TRUNCATE removes table rows | error | Confirm retention, backups and downstream dependencies. |
|
|
98
|
+
| PG024 | Dropping a constraint | error | Review dependent queries and data-integrity requirements. |
|
|
99
|
+
| PG025 | SET NOT NULL may scan table | error | Backfill and validate a CHECK constraint first where appropriate. |
|
|
100
|
+
|
|
101
|
+
See the [full rule reference](docs/RULES.md) for defaults, version constraints and legacy identifiers. PG013 is superseded by PG025; PG009 and PG018 are unused.
|
|
102
|
+
|
|
103
|
+
## Reporting and configuration
|
|
158
104
|
|
|
159
105
|
Example configuration (`pg-safe-migrate.config.json`):
|
|
160
106
|
|
|
@@ -167,197 +113,139 @@ Example configuration (`pg-safe-migrate.config.json`):
|
|
|
167
113
|
}
|
|
168
114
|
```
|
|
169
115
|
|
|
170
|
-
Report options: `--format text|json|sarif`, `--output <file>`, `--advisor`, `--postgres-version <major>`, and `--fail-on warning|error|none`.
|
|
116
|
+
Report options: `--format text|json|sarif|markdown`, `--output <file>`, `--advisor`, `--postgres-version <major>`, and `--fail-on warning|error|none`.
|
|
171
117
|
|
|
172
118
|
Each finding includes a description of the risk and a suggested safer migration approach.
|
|
173
119
|
|
|
174
120
|
A passing result means no configured patterns were detected, not that a migration is guaranteed to be safe.
|
|
175
121
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
```
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
```sql
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
122
|
+
|
|
123
|
+
## Inline Rule Suppression
|
|
124
|
+
|
|
125
|
+
Introduced in v2.7.0, inline suppression allows teams to acknowledge reviewed migration risks without disabling an entire safety rule.
|
|
126
|
+
|
|
127
|
+
Suppression is disabled by default.
|
|
128
|
+
|
|
129
|
+
### Enable suppression
|
|
130
|
+
|
|
131
|
+
Create or update `pg-safe-migrate.config.json`:
|
|
132
|
+
|
|
133
|
+
```json
|
|
134
|
+
{
|
|
135
|
+
"suppression": {
|
|
136
|
+
"enabled": true,
|
|
137
|
+
"requireReason": true,
|
|
138
|
+
"reportSuppressed": true,
|
|
139
|
+
"protectedRules": ["PG011", "PG023"]
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Suppress a reviewed finding
|
|
145
|
+
|
|
146
|
+
```sql
|
|
147
|
+
-- pg-safe-migrate-disable-next-line PG002 -- reason: DBA approved after application compatibility review
|
|
148
|
+
ALTER TABLE users DROP COLUMN legacy_email;
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
The directive applies only to the immediately following SQL statement.
|
|
152
|
+
|
|
153
|
+
Multiple rule IDs can be specified using commas.
|
|
154
|
+
|
|
155
|
+
### Built-in safeguards
|
|
156
|
+
|
|
157
|
+
- Mandatory suppression reasons.
|
|
158
|
+
- PostgreSQL AST-based statement targeting.
|
|
159
|
+
- SQL-aware comment scanning.
|
|
160
|
+
- Protected rule enforcement.
|
|
161
|
+
- Unknown rule detection.
|
|
162
|
+
- Rejected and unused directive reporting.
|
|
163
|
+
- Fail-closed handling of ambiguous SQL scanning.
|
|
164
|
+
- Audit reporting in terminal, JSON, SARIF and Markdown.
|
|
165
|
+
- Suppression-aware VS Code diagnostics.
|
|
166
|
+
|
|
167
|
+
### Generate an audit report
|
|
168
|
+
|
|
169
|
+
```sh
|
|
170
|
+
npx --no-install pg-safe-migrate migrations \
|
|
171
|
+
--config pg-safe-migrate.config.json \
|
|
172
|
+
--format json \
|
|
173
|
+
--output migration-report.json
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Suppressed findings remain available in the audit report.
|
|
177
|
+
|
|
178
|
+
### Enterprise Migration Governance
|
|
179
|
+
|
|
180
|
+
Teams can enforce additional suppression restrictions using a trusted gate policy.
|
|
181
|
+
|
|
182
|
+
Example `enterprise-policy.json`:
|
|
183
|
+
|
|
184
|
+
```json
|
|
185
|
+
{
|
|
186
|
+
"name": "Production Migration Safety",
|
|
187
|
+
"maxRiskScore": 60,
|
|
188
|
+
"maxWarnings": 5,
|
|
189
|
+
"blockedRules": ["PG002", "PG011", "PG023"],
|
|
190
|
+
"suppression": {
|
|
191
|
+
"protectedRules": ["PG008", "PG011", "PG023"],
|
|
192
|
+
"maxSuppressedFindings": 2,
|
|
193
|
+
"failOnRejected": true,
|
|
194
|
+
"failOnUnused": true
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
Run the enterprise gate:
|
|
200
|
+
|
|
201
|
+
```sh
|
|
202
|
+
npx --no-install pg-safe-migrate migrations \
|
|
203
|
+
--config pg-safe-migrate.config.json \
|
|
204
|
+
--gate-policy enterprise-policy.json \
|
|
205
|
+
--format json \
|
|
206
|
+
--output migration-report.json
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
Enterprise enforcement:
|
|
210
|
+
|
|
211
|
+
- Prevents suppression of blocked and protected rules.
|
|
212
|
+
- Includes suppressed findings in risk calculations.
|
|
213
|
+
- Enforces suppression limits.
|
|
214
|
+
- Rejects project exclusions and severity-weakening overrides.
|
|
215
|
+
- Can block a migration even when `--fail-on none` is supplied.
|
|
216
|
+
|
|
217
|
+
Use a separate trusted configuration for enterprise gates. Do not reuse ordinary lint configurations containing exclusions or warning/off rule overrides.
|
|
218
|
+
|
|
219
|
+
The CLI, policy, configuration and CI workflow must come from trusted sources.
|
|
220
|
+
|
|
221
|
+
### VS Code Integration
|
|
222
|
+
|
|
223
|
+
The PG Safe Migrate extension supports inline suppression through workspace configuration.
|
|
224
|
+
|
|
225
|
+
- Suppressed findings are removed from active diagnostics.
|
|
226
|
+
- Protected findings remain visible.
|
|
227
|
+
- Suppression audit counts appear in the PG Safe Migrate Output channel.
|
|
228
|
+
- Disabling suppression restores the original findings.
|
|
229
|
+
|
|
230
|
+
### Limitations
|
|
231
|
+
|
|
232
|
+
- Suppression is not evidence of migration safety.
|
|
233
|
+
- Some combinations of suppression, risk baselines and combined simulation are not supported.
|
|
234
|
+
- Static analysis cannot guarantee zero downtime.
|
|
235
|
+
|
|
236
|
+
## Advanced capabilities
|
|
237
|
+
|
|
238
|
+
- [Advanced usage, simulation, database inspection and limitations](docs/advanced.md)
|
|
239
|
+
- [Adoption and CI troubleshooting](docs/ADOPTION-GUIDE.md)
|
|
240
|
+
- [Release history](CHANGELOG.md)
|
|
241
|
+
- [Verification commands](ALL-MILESTONES-TESTING.md)
|
|
213
242
|
|
|
214
243
|
## Development
|
|
215
244
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
```bash
|
|
219
|
-
git clone https://github.com/subodhsingh001/pg-safe-migrate.git
|
|
220
|
-
cd pg-safe-migrate
|
|
221
|
-
```
|
|
222
|
-
|
|
223
|
-
Install dependencies:
|
|
224
|
-
|
|
225
|
-
```bash
|
|
245
|
+
```sh
|
|
226
246
|
npm ci
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
Run tests:
|
|
230
|
-
|
|
231
|
-
```bash
|
|
232
|
-
npm test
|
|
233
|
-
```
|
|
234
|
-
|
|
235
|
-
Test the rule engine:
|
|
236
|
-
|
|
237
|
-
```bash
|
|
238
|
-
node bin/cli.js test/unsafe_migration.sql
|
|
239
|
-
```
|
|
240
|
-
|
|
241
|
-
Check the npm package:
|
|
242
|
-
|
|
243
|
-
```bash
|
|
247
|
+
npm run test:release
|
|
244
248
|
npm pack --dry-run
|
|
245
249
|
```
|
|
246
250
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
**v2.0.0**
|
|
250
|
-
|
|
251
|
-
- Expanded PostgreSQL migration risk checks and framework advisories
|
|
252
|
-
- PostgreSQL AST-based parsing
|
|
253
|
-
- Prisma and Drizzle migration directory detection
|
|
254
|
-
- Git-diff mode
|
|
255
|
-
- GitHub Actions support
|
|
256
|
-
- JSON/SARIF reports and optional advisor
|
|
257
|
-
- Configurable severity, exclusions, and PostgreSQL-version guidance
|
|
258
|
-
- Actionable migration recommendations
|
|
259
|
-
|
|
260
|
-
[View npm package](https://www.npmjs.com/package/@subodh001/pg-safe-migrate) · [View GitHub releases](https://github.com/subodhsingh001/pg-safe-migrate/releases) · [Report an issue](https://github.com/subodhsingh001/pg-safe-migrate/issues)
|
|
261
|
-
|
|
262
|
-
## Limitations
|
|
263
|
-
|
|
264
|
-
`pg-safe-migrate` is a static AST-based linter, not a migration execution simulator.
|
|
265
|
-
|
|
266
|
-
It does not:
|
|
267
|
-
|
|
268
|
-
- Connect to your production database
|
|
269
|
-
- Estimate actual lock waiting times
|
|
270
|
-
- Validate rollback procedures
|
|
271
|
-
- Inspect live database schemas
|
|
272
|
-
- Guarantee zero downtime
|
|
273
|
-
- Analyze SQL embedded inside changed JavaScript or TypeScript files
|
|
274
|
-
|
|
275
|
-
Some flagged operations may be acceptable under controlled conditions. Some unflagged operations may still cause downtime.
|
|
276
|
-
|
|
277
|
-
Always consider PostgreSQL version, table size, transaction behavior, lock timeouts, long-running transactions, and application deployment sequencing.
|
|
278
|
-
|
|
279
|
-
## Validation and real ORM runners
|
|
280
|
-
|
|
281
|
-
The repository includes a curated SQL regression suite (`test/real-world-cases.test.js`) and an opt-in integration runner (`scripts/orm-runner/verify.mjs`) using a disposable PostgreSQL database. On 2026-11-17 the maintainer verified that Prisma 6.19.x `migrate deploy` applied a **standalone** `CREATE INDEX CONCURRENTLY` migration; the tested Drizzle ORM 0.44.x node-postgres migrator rejected the same operation inside its transaction with PostgreSQL error `25001`. The runner asserts both outcomes and checks that temporary schemas are cleaned up. See `scripts/orm-runner/README.md` and `RELEASE-VERIFICATION.md`. These observations are version- and runner-specific and do not establish a production false-positive rate.
|
|
282
|
-
|
|
283
|
-
## Contributing
|
|
284
|
-
|
|
285
|
-
Contributions, bug reports, PostgreSQL edge cases, and suggestions for additional safety rules are welcome.
|
|
286
|
-
|
|
287
|
-
Open an issue or submit a pull request on GitHub.
|
|
288
|
-
|
|
289
|
-
## License
|
|
290
|
-
|
|
291
|
-
MIT License.
|
|
292
|
-
|
|
293
|
-
## v2.0 features
|
|
294
|
-
|
|
295
|
-
- `--postgres-version 10` enables PostgreSQL-10-specific default-column rewrite warnings; version 11+ does not trigger that rule. This is a targeted version check, not complete PostgreSQL-version simulation.
|
|
296
|
-
- `--advisor` gives Prisma/Drizzle/raw-SQL-aware deployment guidance. Recommendations are advisory and do not inspect the live database.
|
|
297
|
-
- Cross-file analysis checks files in lexicographic order and warns on selected operations involving tables changed earlier. This is a heuristic, not a dependency graph.
|
|
298
|
-
- Transaction analysis uses parsed SQL statements, avoiding comment/string false positives from keyword matching. It cannot know whether an external ORM runner wraps a migration in a transaction.
|
|
299
|
-
- JSON and SARIF outputs include source locations when the PostgreSQL parser supplies statement offsets.
|
|
300
|
-
- `examples/consumer-workflow.yml` illustrates GitHub code-scanning SARIF upload and policy enforcement. GitHub code-scanning availability and permissions depend on repository settings and plan.
|
|
301
|
-
|
|
302
|
-
### Examples
|
|
303
|
-
|
|
304
|
-
```bash
|
|
305
|
-
node bin/cli.js prisma/migrations --postgres-version 16 --advisor
|
|
306
|
-
node bin/cli.js db/migrations --format json --output report.json
|
|
307
|
-
node bin/cli.js --changed origin/main --format sarif --output report.sarif --fail-on none
|
|
308
|
-
```
|
|
309
|
-
|
|
310
|
-
### Known gaps
|
|
311
|
-
|
|
312
|
-
The analyzer does not connect to PostgreSQL, determine real table sizes, model application compatibility, or prove migrations are safe. Prisma and Drizzle transaction behavior depends on the actual runner and configuration. SARIF code-scanning upload is optional and needs GitHub security permissions.
|
|
313
|
-
|
|
314
|
-
### Experimental: read-only database catalog snapshot
|
|
315
|
-
|
|
316
|
-
The opt-in `--inspect-db` mode returns PostgreSQL major version, approximate table row counts and table sizes as JSON. It **does not execute migrations** or assess production safety. Install the optional `pg` dependency in your project and provide a least-privilege connection URL:
|
|
317
|
-
|
|
318
|
-
```powershell
|
|
319
|
-
npm install pg
|
|
320
|
-
$env:DATABASE_URL = 'postgresql://readonly_user:password@127.0.0.1:5432/testdb'
|
|
321
|
-
node bin/cli.js --inspect-db
|
|
322
|
-
```
|
|
323
|
-
|
|
324
|
-
This mode uses a read-only transaction, catalog queries, a five-second statement timeout and a 50-table default limit. Do not use privileged production credentials. Real-database integration testing remains outstanding.
|
|
325
|
-
|
|
326
|
-
### Experimental disposable PostgreSQL simulation
|
|
327
|
-
|
|
328
|
-
To check whether a SQL migration executes against an **empty disposable PostgreSQL database** (requires local Docker and a locally available PostgreSQL image):
|
|
329
|
-
|
|
330
|
-
```sh
|
|
331
|
-
node bin/cli.js --simulate examples/safe.sql --postgres-version 16
|
|
332
|
-
node bin/cli.js --simulate examples/unsafe.sql --postgres-version 16 --simulate-timeout 60
|
|
333
|
-
```
|
|
334
|
-
|
|
335
|
-
The simulator launches a new `postgres:14` through `postgres:18` container, disables its network, limits memory/CPU/processes, does not mount host files or use `DATABASE_URL`, runs SQL via `docker exec -i`, and forcibly removes the container afterward. Docker images may be downloaded automatically. **Do not use it as a sandbox for hostile code:** Docker isolation is not a hardened security boundary. Run only migrations from trusted contributors, preferably on a dedicated CI runner. Simulation against an empty database cannot predict production locks, data-dependent failures, rollback success, or downtime. It never runs against a real database. A nonzero exit code indicates SQL execution failure (1) or a tool/runtime error (2).
|
|
336
|
-
|
|
337
|
-
### Docker simulator with baseline and ordered migrations (experimental)
|
|
338
|
-
|
|
339
|
-
The simulator starts a disposable PostgreSQL container without published ports or external networking.
|
|
340
|
-
Use a baseline to create tables that a migration expects:
|
|
341
|
-
|
|
342
|
-
```cmd
|
|
343
|
-
node bin/cli.js --simulate examples/safe.sql --baseline examples/baseline.sql --postgres-version 16
|
|
344
|
-
node bin/cli.js --simulate examples/unsafe.sql --baseline examples/baseline.sql --postgres-version 16
|
|
345
|
-
```
|
|
346
|
-
|
|
347
|
-
Run all top-level `.sql` files in a folder in lexicographic filename order:
|
|
348
|
-
|
|
349
|
-
```cmd
|
|
350
|
-
node bin/cli.js --migrations-dir path/to/migrations --baseline examples/baseline.sql --postgres-version 16
|
|
351
|
-
```
|
|
352
|
-
|
|
353
|
-
Each JSON result contains a `steps` array with distinct `baseline` and `migration` stages. Execution stops on the first failure, and the disposable container is removed. This is a functional smoke test, **not** a guarantee of production safety, downtime, data preservation, or rollback. A successful `unsafe.sql` simulation does not mean the migration is safe.
|
|
354
|
-
|
|
355
|
-
The directory mode processes only top-level `.sql` files (maximum 100); nested ORM-specific migration directories are not automatically traversed.
|
|
356
|
-
|
|
357
|
-
## Consolidated experimental development capabilities
|
|
358
|
-
|
|
359
|
-
See [ALL-MILESTONES-TESTING.md](ALL-MILESTONES-TESTING.md) for exact commands and verification limitations. `--assess` combines lint findings, lock classifications, heuristic risk score, deployment advice, Docker execution, optional `--verify-sql` assertions, and optional `--team-policy` checks. The Docker matrix script covers PostgreSQL 14–18 when Docker is available. `vscode-extension/` is a local VS Code extension prototype; team policy is a local JSON configuration, **not** a hosted team management service.
|
|
360
|
-
|
|
361
|
-
## Markdown migration report (Week 2 Day 6)
|
|
362
|
-
|
|
363
|
-
Run `pg-safe-migrate migrations --format markdown --risk --output migration-report.md` to generate an actionable Markdown report with rule IDs, locations, recommended actions, and an optional heuristic risk breakdown. This does not predict production downtime.
|
|
251
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md) and [SECURITY.md](SECURITY.md). MIT licensed.
|
package/SECURITY.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Security policy
|
|
2
|
+
|
|
3
|
+
## Reporting
|
|
4
|
+
|
|
5
|
+
Use the repository [private vulnerability reporting page](https://github.com/subodhsingh001/pg-safe-migrate/security/advisories/new) if enabled. If unavailable, request a private reporting channel through a public issue without disclosing exploit details, credentials or customer SQL. Include affected version, environment and a minimal sanitized reproduction.
|
|
6
|
+
|
|
7
|
+
## Safety boundaries
|
|
8
|
+
|
|
9
|
+
Static linting does not execute migrations or guarantee downtime avoidance. Optional Docker simulation executes trusted SQL in disposable containers and is not a hostile-code sandbox. Database inspection requires least-privilege credentials. Do not submit secrets or production data. The extension file size and output limits are not hard process memory limits.
|
|
10
|
+
|
|
11
|
+
## Support
|
|
12
|
+
|
|
13
|
+
No formal support lifetime or response SLA is promised. Use the latest reviewed release and report reproducible security issues.
|