pgsafe 2.7.0 → 2.7.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.
- package/ALL-MILESTONES-TESTING.md +41 -0
- package/CHANGELOG.md +88 -0
- package/CONTRIBUTING.md +3 -0
- package/README.md +255 -116
- package/SECURITY.md +13 -0
- package/bin/cli.js +120 -0
- package/docs/RULES.md +31 -0
- package/examples/baseline.sql +7 -0
- package/examples/consumer-workflow.yml +22 -0
- package/examples/migrations/001_add_phone.sql +1 -0
- package/examples/migrations/002_add_email_index.sql +1 -0
- package/examples/reviewed-risk-baseline.json +7 -0
- package/examples/safe.sql +2 -0
- package/examples/suppression-gate-policy.json +11 -0
- package/examples/team-policy.json +7 -0
- package/examples/test-unsafe.sql +1 -0
- package/examples/unsafe.sql +2 -0
- package/examples/verify.sql +8 -0
- package/examples/week3-gate-policy.json +6 -0
- package/examples/week3-production-context.json +8 -0
- package/package.json +73 -46
- package/scripts/orm-runner/README.md +35 -0
- package/scripts/orm-runner/package-lock.json +659 -0
- package/scripts/orm-runner/package.json +14 -0
- package/scripts/orm-runner/verify.mjs +360 -0
- package/scripts/test-pg-matrix.cjs +30 -0
- package/src/advisor.js +77 -0
- package/src/analysis.js +411 -0
- package/src/combined.js +60 -0
- package/src/config.js +38 -0
- package/src/db-inspect.js +30 -0
- package/src/detector.js +25 -0
- package/src/engine.js +48 -0
- package/src/gitDiff.js +33 -0
- package/src/github.js +17 -0
- package/src/locks.js +134 -0
- package/src/parser.js +86 -0
- package/src/plans.js +9 -0
- package/src/presets.js +5 -0
- package/src/production-context.js +78 -0
- package/src/reports.js +38 -0
- package/src/risk-baseline.js +54 -0
- package/src/risk.js +29 -0
- package/src/rules/additionalRules.js +9 -0
- package/src/rules/alterTableRules.js +65 -0
- package/src/rules/concurrentIndex.js +18 -0
- package/src/rules/dataIntegrityRules.js +40 -0
- package/src/rules/dropColumn.js +12 -0
- package/src/rules/operationalRiskRules.js +57 -0
- package/src/rules/ruleCatalog.js +37 -0
- package/src/rules/versionCatalog.js +16 -0
- package/src/rules/versionRules.js +49 -0
- package/src/simulator.js +135 -0
- 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 +84 -0
- package/bin/pgsafe.js +0 -3
package/docs/RULES.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# Rule reference (development branch)
|
|
2
|
+
|
|
3
|
+
23 catalog identifiers, including legacy PG013; PG009 and PG018 are unused. PG013 is suppressed when PG025 is emitted. PG019 and PG020 require a target version below 15; PG014 targets PostgreSQL 10. Defaults below describe CLI reporting, not raw engine metadata. Catalog `defaultSeverity` preserves raw rule metadata; `defaultCliSeverity` describes default CLI reporting. PG024 and PG025 are raw warnings but CLI errors by default.
|
|
4
|
+
|
|
5
|
+
| ID | Diagnostic | Default | Guidance |
|
|
6
|
+
|---|---|---|---|
|
|
7
|
+
| PG001 | Non-concurrent index creation | error | Use CREATE INDEX CONCURRENTLY outside a transaction. |
|
|
8
|
+
| PG002 | Dropping a column | error | Deprecate consumers before removing the column. |
|
|
9
|
+
| PG003 | Changing a column type | error | Check binary compatibility; consider expand/backfill/contract. |
|
|
10
|
+
| PG004 | Adding NOT NULL column without default | error | Add nullable, backfill, then enforce NOT NULL. |
|
|
11
|
+
| PG005 | Adding validated foreign key | error | Add NOT VALID then VALIDATE CONSTRAINT separately. |
|
|
12
|
+
| PG006 | Renaming an object | error | Use an expand/contract deployment. |
|
|
13
|
+
| PG007 | Dropping index without CONCURRENTLY | error | Use DROP INDEX CONCURRENTLY where supported. |
|
|
14
|
+
| PG008 | Concurrent index operation inside transaction | error | Run outside BEGIN/COMMIT. |
|
|
15
|
+
| PG010 | Adding UNIQUE constraint | error | Consider a concurrent unique index and attach it where applicable. |
|
|
16
|
+
| PG011 | Dropping a table | error | Verify retention and application dependencies. |
|
|
17
|
+
| PG012 | Adding validated CHECK constraint | error | Add NOT VALID and validate separately. |
|
|
18
|
+
| PG013 | Legacy SET NOT NULL diagnostic (superseded by PG025) | error | Validate an equivalent CHECK constraint first. |
|
|
19
|
+
| PG014 | Adding column with default on PostgreSQL 10 | error | Use controlled backfill or evaluate upgrading PostgreSQL. |
|
|
20
|
+
| PG015 | Prisma concurrent index migration | warning | Use a separately managed nontransactional migration. |
|
|
21
|
+
| PG016 | Drizzle concurrent index migration | warning | Use a separately managed nontransactional migration. |
|
|
22
|
+
| PG017 | Potential cross-file dependency or Flyway transaction advisory | warning | Review migration order; this is a heuristic. |
|
|
23
|
+
| PG019 | MERGE requires PostgreSQL 15+ | error | Use a compatible INSERT ... ON CONFLICT pattern where applicable. |
|
|
24
|
+
| PG020 | UNIQUE NULLS NOT DISTINCT requires PostgreSQL 15+ | error | Use a version-compatible uniqueness strategy. |
|
|
25
|
+
| PG021 | Volatile default may rewrite table | error | Add column, backfill in batches, then set default. |
|
|
26
|
+
| PG022 | SET LOGGED rewrites table | error | Schedule controlled maintenance and evaluate alternatives. |
|
|
27
|
+
| PG023 | TRUNCATE removes table rows | error | Confirm retention, backups and downstream dependencies. |
|
|
28
|
+
| PG024 | Dropping a constraint | error | Review dependent queries and data-integrity requirements. |
|
|
29
|
+
| PG025 | SET NOT NULL may scan table | error | Backfill and validate a CHECK constraint first where appropriate. |
|
|
30
|
+
|
|
31
|
+
PG017 also covers Flyway nontransactional execution advice. Rules are configurable using `rules` with `off`, `warning`, or `error`. Static findings are not a downtime guarantee.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
name: PostgreSQL migration safety
|
|
2
|
+
on:
|
|
3
|
+
pull_request:
|
|
4
|
+
paths:
|
|
5
|
+
- '**/*.sql'
|
|
6
|
+
permissions:
|
|
7
|
+
contents: read
|
|
8
|
+
jobs:
|
|
9
|
+
migrations:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
steps:
|
|
12
|
+
- uses: actions/checkout@v4
|
|
13
|
+
with:
|
|
14
|
+
fetch-depth: 0
|
|
15
|
+
persist-credentials: false
|
|
16
|
+
- uses: actions/setup-node@v4
|
|
17
|
+
with:
|
|
18
|
+
node-version: '22'
|
|
19
|
+
- name: Check changed SQL
|
|
20
|
+
env:
|
|
21
|
+
BASE_REF: ${{ github.base_ref }}
|
|
22
|
+
run: npx --yes pgsafe@2.7.1 --changed "origin/$BASE_REF" --fail-on error
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ALTER TABLE users ADD COLUMN phone TEXT;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
CREATE INDEX CONCURRENTLY idx_users_email ON users(email);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
DROP TABLE users;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
DO $$ BEGIN
|
|
2
|
+
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='users' AND column_name='phone') THEN
|
|
3
|
+
RAISE EXCEPTION 'users.phone missing';
|
|
4
|
+
END IF;
|
|
5
|
+
IF NOT EXISTS (SELECT 1 FROM pg_indexes WHERE indexname='idx_users_email') THEN
|
|
6
|
+
RAISE EXCEPTION 'email index missing';
|
|
7
|
+
END IF;
|
|
8
|
+
END $$;
|
package/package.json
CHANGED
|
@@ -1,46 +1,73 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "pgsafe",
|
|
3
|
-
"version": "2.7.
|
|
4
|
-
"description": "PostgreSQL
|
|
5
|
-
"type": "module",
|
|
6
|
-
"bin": {
|
|
7
|
-
"pgsafe": "
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "pgsafe",
|
|
3
|
+
"version": "2.7.1",
|
|
4
|
+
"description": "Detect risky PostgreSQL schema migrations before deployment: SQL linting, lock analysis, risk scoring and CI reports for Prisma, Drizzle and Flyway",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"pgsafe": "bin/cli.js",
|
|
8
|
+
"pg-safe-migrate": "bin/cli.js"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"test": "node --test",
|
|
12
|
+
"test:unit": "node scripts/run-unit-tests.cjs",
|
|
13
|
+
"test:integration": "node scripts/test-pg-matrix.cjs",
|
|
14
|
+
"lint": "node bin/cli.js",
|
|
15
|
+
"pack:check": "npm pack --dry-run",
|
|
16
|
+
"test:release": "npm run test:unit && node test/release-smoke.cjs"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"postgresql",
|
|
20
|
+
"postgres",
|
|
21
|
+
"migration",
|
|
22
|
+
"linter",
|
|
23
|
+
"prisma",
|
|
24
|
+
"drizzle",
|
|
25
|
+
"zero-downtime",
|
|
26
|
+
"ast",
|
|
27
|
+
"database-migration",
|
|
28
|
+
"migration-linter",
|
|
29
|
+
"schema-migration",
|
|
30
|
+
"migration-safety",
|
|
31
|
+
"database-safety",
|
|
32
|
+
"postgres-locks",
|
|
33
|
+
"flyway",
|
|
34
|
+
"sarif"
|
|
35
|
+
],
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"chalk": "^5.3.0",
|
|
39
|
+
"commander": "^12.1.0",
|
|
40
|
+
"libpg-query": "^16.2.1"
|
|
41
|
+
},
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=18.0.0"
|
|
44
|
+
},
|
|
45
|
+
"files": [
|
|
46
|
+
"bin/",
|
|
47
|
+
"src/",
|
|
48
|
+
"README.md",
|
|
49
|
+
"LICENSE",
|
|
50
|
+
"examples/",
|
|
51
|
+
"scripts/test-pg-matrix.cjs",
|
|
52
|
+
"scripts/orm-runner/verify.mjs",
|
|
53
|
+
"scripts/orm-runner/package.json",
|
|
54
|
+
"scripts/orm-runner/package-lock.json",
|
|
55
|
+
"scripts/orm-runner/README.md",
|
|
56
|
+
"ALL-MILESTONES-TESTING.md",
|
|
57
|
+
"CHANGELOG.md",
|
|
58
|
+
"SECURITY.md",
|
|
59
|
+
"CONTRIBUTING.md",
|
|
60
|
+
"docs/RULES.md"
|
|
61
|
+
],
|
|
62
|
+
"repository": {
|
|
63
|
+
"type": "git",
|
|
64
|
+
"url": "git+https://github.com/subodhsingh001/pg-safe-migrate.git"
|
|
65
|
+
},
|
|
66
|
+
"bugs": {
|
|
67
|
+
"url": "https://github.com/subodhsingh001/pg-safe-migrate/issues"
|
|
68
|
+
},
|
|
69
|
+
"homepage": "https://github.com/subodhsingh001/pg-safe-migrate#readme",
|
|
70
|
+
"publishConfig": {
|
|
71
|
+
"access": "public"
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# Optional real ORM migration verification
|
|
2
|
+
|
|
3
|
+
This integration test runs **actual Prisma Migrate and Drizzle ORM migration runners** against a disposable PostgreSQL database. It is separate from the package's default `npm test` and GitHub Actions workflow.
|
|
4
|
+
|
|
5
|
+
## Prerequisites
|
|
6
|
+
|
|
7
|
+
- Node.js and npm
|
|
8
|
+
- Docker with an available local PostgreSQL port, or another **disposable** PostgreSQL database
|
|
9
|
+
- Dependencies installed in `scripts/orm-runner` with `npm install`
|
|
10
|
+
|
|
11
|
+
## Windows PowerShell
|
|
12
|
+
|
|
13
|
+
From the repository root:
|
|
14
|
+
|
|
15
|
+
```powershell
|
|
16
|
+
docker run -d --name pg-safe-orm-test -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=pg_safe_local_test_2026 -e POSTGRES_DB=pg_safe_migrate_test -p 127.0.0.1:55432:5432 postgres:16
|
|
17
|
+
$env:DATABASE_URL = "postgresql://postgres:pg_safe_local_test_2026@127.0.0.1:55432/pg_safe_migrate_test"
|
|
18
|
+
cd scripts/orm-runner
|
|
19
|
+
npm install
|
|
20
|
+
npm run verify
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
The Docker container command is for an isolated local test only. If the container already exists, start it with `docker start pg-safe-orm-test` instead of creating another one. Stop and remove it when finished with `docker rm -f pg-safe-orm-test`.
|
|
24
|
+
|
|
25
|
+
## Expected checks
|
|
26
|
+
|
|
27
|
+
1. Prisma applies a baseline table migration.
|
|
28
|
+
2. Prisma applies a **separate** concurrent-index migration and the index exists.
|
|
29
|
+
3. The tested Drizzle node-postgres migrator rejects `CREATE INDEX CONCURRENTLY` inside its transaction with PostgreSQL `25001`.
|
|
30
|
+
4. The rejected Drizzle index is absent.
|
|
31
|
+
5. Temporary database schemas and local temporary files are cleaned up.
|
|
32
|
+
|
|
33
|
+
`KEEP_TEST_SCHEMAS=1` retains test schemas for inspection; do not set it during normal verification.
|
|
34
|
+
|
|
35
|
+
**Safety:** This script creates and drops schemas. Run it only against a disposable test database, never a production or shared database. Results depend on the exact Prisma, Drizzle, PostgreSQL, and runner versions installed.
|