@cleverbrush/orm-cli 4.3.2 → 4.4.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/README.md +16 -0
- package/dist/bin.js +1 -1
- package/dist/{cli-Q4EBNEOJ.js → cli-AOZENPBD.js} +8 -2
- package/dist/commands/validate.d.ts +7 -0
- package/dist/validate-KJTRW3BY.js +66 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,6 +8,7 @@ cb-orm migrate generate [name] # diff DB → schema, emit TS migration file (n
|
|
|
8
8
|
cb-orm migrate run # apply pending migrations
|
|
9
9
|
cb-orm migrate rollback # roll back last batch
|
|
10
10
|
cb-orm migrate status # list applied/pending migrations
|
|
11
|
+
cb-orm validate # check entity schemas against the live DB (read-only)
|
|
11
12
|
cb-orm db push # sync schema in-place (dev only)
|
|
12
13
|
```
|
|
13
14
|
|
|
@@ -57,6 +58,7 @@ export default defineConfig({
|
|
|
57
58
|
"db:run": "cb-orm migrate run",
|
|
58
59
|
"db:rollback": "cb-orm migrate rollback",
|
|
59
60
|
"db:status": "cb-orm migrate status",
|
|
61
|
+
"db:validate": "cb-orm validate",
|
|
60
62
|
"db:push": "cb-orm db push"
|
|
61
63
|
}
|
|
62
64
|
}
|
|
@@ -122,6 +124,19 @@ npx cb-orm migrate status
|
|
|
122
124
|
# ○ 20260423120000_add_role_column.ts
|
|
123
125
|
```
|
|
124
126
|
|
|
127
|
+
### `validate`
|
|
128
|
+
|
|
129
|
+
Checks every configured entity against the live database without applying
|
|
130
|
+
changes, writing migration files, or updating snapshots. It exits with status
|
|
131
|
+
`1` when a table is missing or a schema diff is detected.
|
|
132
|
+
|
|
133
|
+
```sh
|
|
134
|
+
npx cb-orm validate
|
|
135
|
+
# Schema is in sync (4 table(s) checked).
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Use this in CI before deploys when you want to fail fast on schema drift.
|
|
139
|
+
|
|
125
140
|
### `db push`
|
|
126
141
|
|
|
127
142
|
Applies all schema changes directly to the database **without** writing a
|
|
@@ -160,6 +175,7 @@ The CLI delegates all schema intelligence to `@cleverbrush/knex-schema`:
|
|
|
160
175
|
| Diff schema vs DB | `diffSchema(schema, dbState)` |
|
|
161
176
|
| Generate ALTER TABLE source | `generateMigration(diff, tableName)` |
|
|
162
177
|
| Apply diff without file | `applyDiff(knex, diff, tableName)` |
|
|
178
|
+
| Validate without changes | `validateEntitiesAgainstDatabase(knex, entities)` |
|
|
163
179
|
| Polymorphic variant tables | `getPolymorphicVariantSchemas(schema)` |
|
|
164
180
|
|
|
165
181
|
`tsx` is used to load `db.config.ts` at runtime by registering the
|
package/dist/bin.js
CHANGED
|
@@ -66,11 +66,16 @@ async function run(argv) {
|
|
|
66
66
|
console.log(_pkg.version ?? "unknown");
|
|
67
67
|
return;
|
|
68
68
|
}
|
|
69
|
-
const
|
|
69
|
+
const flagArgs = sub?.startsWith("--") ? [sub, ...rest] : rest;
|
|
70
|
+
const flags = parseFlags(flagArgs);
|
|
70
71
|
const configPath = flags["--config"];
|
|
71
72
|
let loadedConfig;
|
|
72
73
|
try {
|
|
73
|
-
if (cmd === "
|
|
74
|
+
if (cmd === "validate") {
|
|
75
|
+
loadedConfig = await loadConfig(configPath);
|
|
76
|
+
const { validate } = await import("./validate-KJTRW3BY.js");
|
|
77
|
+
await validate(loadedConfig);
|
|
78
|
+
} else if (cmd === "migrate") {
|
|
74
79
|
switch (sub) {
|
|
75
80
|
case "generate": {
|
|
76
81
|
let name = "migration";
|
|
@@ -180,6 +185,7 @@ COMMANDS
|
|
|
180
185
|
migrate run Apply pending migrations (knex.migrate.latest)
|
|
181
186
|
migrate rollback Roll back last batch (knex.migrate.rollback)
|
|
182
187
|
migrate status List applied and pending migrations
|
|
188
|
+
validate Check entity schemas against the live DB (read-only)
|
|
183
189
|
db push Sync schema to DB in-place (dev only \u2014 no migration file)
|
|
184
190
|
|
|
185
191
|
OPTIONS
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { OrmCliConfig } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Validate every configured entity against the live database.
|
|
4
|
+
*
|
|
5
|
+
* Exits with status 1 when a table is missing or schema drift is detected.
|
|
6
|
+
*/
|
|
7
|
+
export declare function validate(config: OrmCliConfig): Promise<void>;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/commands/validate.ts
|
|
4
|
+
import { validateEntitiesAgainstDatabase } from "@cleverbrush/knex-schema";
|
|
5
|
+
async function validate(config) {
|
|
6
|
+
const result = await validateEntitiesAgainstDatabase(
|
|
7
|
+
config.knex,
|
|
8
|
+
Object.values(config.entities)
|
|
9
|
+
);
|
|
10
|
+
if (result.valid) {
|
|
11
|
+
console.log(
|
|
12
|
+
`Schema is in sync (${result.checkedTables.length} table(s) checked).`
|
|
13
|
+
);
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
console.error("Schema drift detected:");
|
|
17
|
+
for (const issue of result.issues) {
|
|
18
|
+
if (issue.type === "missing-table") {
|
|
19
|
+
console.error(` - Missing table: ${issue.tableName}`);
|
|
20
|
+
} else {
|
|
21
|
+
console.error(
|
|
22
|
+
` - Drift in ${issue.tableName}: ${summarizeDiff(issue.diff)}`
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
process.exit(1);
|
|
27
|
+
}
|
|
28
|
+
function summarizeDiff(diff) {
|
|
29
|
+
const parts = [
|
|
30
|
+
formatCount(diff.addColumns.length, "column to add", "columns to add"),
|
|
31
|
+
formatCount(
|
|
32
|
+
diff.dropColumns.length,
|
|
33
|
+
"column to drop",
|
|
34
|
+
"columns to drop"
|
|
35
|
+
),
|
|
36
|
+
formatCount(
|
|
37
|
+
diff.alterColumns.length,
|
|
38
|
+
"column to alter",
|
|
39
|
+
"columns to alter"
|
|
40
|
+
),
|
|
41
|
+
formatCount(diff.addIndexes.length, "index to add", "indexes to add"),
|
|
42
|
+
formatCount(
|
|
43
|
+
diff.dropIndexes.length,
|
|
44
|
+
"index to drop",
|
|
45
|
+
"indexes to drop"
|
|
46
|
+
),
|
|
47
|
+
formatCount(
|
|
48
|
+
diff.addForeignKeys.length,
|
|
49
|
+
"foreign key to add",
|
|
50
|
+
"foreign keys to add"
|
|
51
|
+
),
|
|
52
|
+
formatCount(
|
|
53
|
+
diff.dropForeignKeys.length,
|
|
54
|
+
"foreign key to drop",
|
|
55
|
+
"foreign keys to drop"
|
|
56
|
+
)
|
|
57
|
+
].filter((part) => part !== null);
|
|
58
|
+
return parts.length > 0 ? parts.join(", ") : "unknown drift";
|
|
59
|
+
}
|
|
60
|
+
function formatCount(count, singular, plural) {
|
|
61
|
+
if (count === 0) return null;
|
|
62
|
+
return `${count} ${count === 1 ? singular : plural}`;
|
|
63
|
+
}
|
|
64
|
+
export {
|
|
65
|
+
validate
|
|
66
|
+
};
|
package/package.json
CHANGED