@intelligo-dev/cli 1.0.0-beta.1 → 1.0.0-beta.14
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/NOTICE +6 -0
- package/README.md +102 -0
- package/dist/bin.js +122 -37
- package/dist/bin.js.map +1 -1
- package/dist/commands/add.d.ts +26 -1
- package/dist/commands/add.d.ts.map +1 -1
- package/dist/commands/add.js +70 -4
- package/dist/commands/add.js.map +1 -1
- package/dist/commands/create-flow.d.ts +37 -0
- package/dist/commands/create-flow.d.ts.map +1 -0
- package/dist/commands/create-flow.js +212 -0
- package/dist/commands/create-flow.js.map +1 -0
- package/dist/commands/create.d.ts +27 -6
- package/dist/commands/create.d.ts.map +1 -1
- package/dist/commands/create.js +53 -13
- package/dist/commands/create.js.map +1 -1
- package/dist/commands/doctor.d.ts +14 -3
- package/dist/commands/doctor.d.ts.map +1 -1
- package/dist/commands/doctor.js +295 -37
- package/dist/commands/doctor.js.map +1 -1
- package/dist/commands/migrate-check.d.ts +73 -1
- package/dist/commands/migrate-check.d.ts.map +1 -1
- package/dist/commands/migrate-check.js +130 -5
- package/dist/commands/migrate-check.js.map +1 -1
- package/dist/commands/migrate.d.ts +97 -0
- package/dist/commands/migrate.d.ts.map +1 -0
- package/dist/commands/migrate.js +141 -0
- package/dist/commands/migrate.js.map +1 -0
- package/dist/commands/sync-messages.d.ts +37 -0
- package/dist/commands/sync-messages.d.ts.map +1 -0
- package/dist/commands/sync-messages.js +88 -0
- package/dist/commands/sync-messages.js.map +1 -0
- package/dist/commands/sync-scaffold.d.ts +36 -0
- package/dist/commands/sync-scaffold.d.ts.map +1 -0
- package/dist/commands/sync-scaffold.js +53 -0
- package/dist/commands/sync-scaffold.js.map +1 -0
- package/dist/commands/sync.d.ts +103 -0
- package/dist/commands/sync.d.ts.map +1 -0
- package/dist/commands/sync.js +361 -0
- package/dist/commands/sync.js.map +1 -0
- package/dist/commands/upgrade-check.d.ts +5 -5
- package/dist/commands/upgrade-check.d.ts.map +1 -1
- package/dist/commands/upgrade-check.js +22 -6
- package/dist/commands/upgrade-check.js.map +1 -1
- package/dist/env-files.d.ts +12 -0
- package/dist/env-files.d.ts.map +1 -0
- package/dist/env-files.js +29 -0
- package/dist/env-files.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -1
- package/dist/manifest.d.ts +36 -15
- package/dist/manifest.d.ts.map +1 -1
- package/dist/manifest.js +32 -16
- package/dist/manifest.js.map +1 -1
- package/dist/migrations.d.ts +3 -6
- package/dist/migrations.d.ts.map +1 -1
- package/dist/migrations.js +3 -6
- package/dist/migrations.js.map +1 -1
- package/dist/model-catalogue.d.ts +14 -0
- package/dist/model-catalogue.d.ts.map +1 -0
- package/dist/model-catalogue.js +34 -0
- package/dist/model-catalogue.js.map +1 -0
- package/dist/module-exports.d.ts +63 -0
- package/dist/module-exports.d.ts.map +1 -0
- package/dist/module-exports.js +231 -0
- package/dist/module-exports.js.map +1 -0
- package/dist/registry-bundle.d.ts +43 -0
- package/dist/registry-bundle.d.ts.map +1 -0
- package/dist/registry-bundle.js +74 -0
- package/dist/registry-bundle.js.map +1 -0
- package/dist/registry-items.d.ts +84 -0
- package/dist/registry-items.d.ts.map +1 -0
- package/dist/registry-items.js +187 -0
- package/dist/registry-items.js.map +1 -0
- package/package.json +36 -10
- package/src/bin.ts +316 -0
- package/src/commands/add.ts +255 -0
- package/src/commands/create-flow.ts +284 -0
- package/src/commands/create.ts +149 -0
- package/src/commands/doctor.ts +487 -0
- package/src/commands/migrate-check.ts +298 -0
- package/src/commands/migrate.ts +228 -0
- package/src/commands/sync-messages.ts +114 -0
- package/src/commands/sync-scaffold.ts +83 -0
- package/src/commands/sync.ts +534 -0
- package/src/commands/upgrade-check.ts +160 -0
- package/src/env-files.ts +31 -0
- package/src/index.ts +39 -0
- package/src/manifest.ts +147 -0
- package/src/migrations-dir.ts +25 -0
- package/src/migrations.ts +133 -0
- package/src/model-catalogue.ts +35 -0
- package/src/module-exports.ts +266 -0
- package/src/registry-bundle.ts +103 -0
- package/src/registry-items.ts +237 -0
- package/templates/admin-page/admin-page.tsx.tpl +2 -3
- package/templates/app-scaffold/assistant-route.ts.tpl +2 -2
- package/templates/app-scaffold/auth-route.ts.tpl +10 -0
- package/templates/app-scaffold/components.json.tpl +6 -2
- package/templates/app-scaffold/db-schema.ts.tpl +29 -0
- package/templates/app-scaffold/drizzle-journal.json.tpl +5 -0
- package/templates/app-scaffold/drizzle.config.ts.tpl +45 -0
- package/templates/app-scaffold/env.example.tpl +65 -1
- package/templates/app-scaffold/globals.css.tpl +172 -281
- package/templates/app-scaffold/i18n-request.ts.tpl +25 -0
- package/templates/app-scaffold/instrumentation.ts.tpl +21 -0
- package/templates/app-scaffold/intelligo.ts.tpl +95 -41
- package/templates/app-scaffold/layout.tsx.tpl +18 -1
- package/templates/app-scaffold/lib-utils.ts.tpl +1 -6
- package/templates/app-scaffold/next.config.mjs.tpl +2 -0
- package/templates/app-scaffold/package.json.tpl +31 -17
- package/templates/app-scaffold/page.tsx.tpl +16 -0
- package/templates/app-scaffold/plans.ts.tpl +32 -13
- package/templates/app-scaffold/{middleware.ts.tpl → proxy.ts.tpl} +6 -0
- package/templates/app-scaffold/sonner.tsx.tpl +42 -20
- package/templates/app-scaffold/stripe-webhook-route.ts.tpl +24 -0
- package/templates/app-scaffold/theme-provider.tsx.tpl +6 -4
- package/templates/app-scaffold/tsconfig.json.tpl +20 -4
- package/templates/app-scaffold/use-mobile.ts.tpl +19 -0
- package/templates/app-scaffold/workspace-bootstrap.ts.tpl +30 -0
- package/templates/maintenance/maintenance-route.ts.tpl +119 -0
- package/templates/manifest.json +75 -17
- package/templates/registry/ai-agent-activity.json +23 -0
- package/templates/registry/ai-agent-progress.json +21 -0
- package/templates/registry/ai-approval-card.json +27 -0
- package/templates/registry/ai-artifact.json +22 -0
- package/templates/registry/ai-branch.json +21 -0
- package/templates/registry/ai-citations.json +24 -0
- package/templates/registry/ai-code-block.json +23 -0
- package/templates/registry/ai-composer-menu.json +19 -0
- package/templates/registry/ai-file-diff.json +25 -0
- package/templates/registry/ai-image-generation.json +23 -0
- package/templates/registry/ai-markdown.json +23 -0
- package/templates/registry/ai-message-bubble.json +23 -0
- package/templates/registry/ai-message-scroller.json +22 -0
- package/templates/registry/ai-message.json +21 -0
- package/templates/registry/ai-motion.json +19 -0
- package/templates/registry/ai-prompt-input.json +28 -0
- package/templates/registry/ai-reasoning-text.json +22 -0
- package/templates/registry/ai-reasoning.json +22 -0
- package/templates/registry/ai-shimmer-text.json +19 -0
- package/templates/registry/ai-sidebar.json +25 -0
- package/templates/registry/ai-speech-input.json +21 -0
- package/templates/registry/ai-streaming-response.json +24 -0
- package/templates/registry/ai-suggestion.json +19 -0
- package/templates/registry/ai-todo-list.json +22 -0
- package/templates/registry/ai-tool-approval.json +26 -0
- package/templates/registry/ai-tool-result.json +25 -0
- package/templates/registry/alert-dialog.json +23 -0
- package/templates/registry/animated-list.json +21 -0
- package/templates/registry/app-shell.json +93 -0
- package/templates/registry/artifacts.json +82 -0
- package/templates/registry/attachment.json +22 -0
- package/templates/registry/auth-email-verification.json +39 -0
- package/templates/registry/auth-login.json +72 -0
- package/templates/registry/auth-password-reset.json +53 -0
- package/templates/registry/auth-signup.json +41 -0
- package/templates/registry/billing-settings.json +60 -0
- package/templates/registry/button.json +22 -0
- package/templates/registry/chat-eve.json +19 -0
- package/templates/registry/chat-panel.json +30 -0
- package/templates/registry/chat-share.json +42 -0
- package/templates/registry/chat-widget.json +34 -0
- package/templates/registry/chat.json +326 -0
- package/templates/registry/checkbox.json +22 -0
- package/templates/registry/checkout.json +40 -0
- package/templates/registry/collapsible.json +19 -0
- package/templates/registry/command.json +23 -0
- package/templates/registry/copy-button.json +21 -0
- package/templates/registry/dashboard.json +69 -0
- package/templates/registry/dialog.json +24 -0
- package/templates/registry/document-viewer.json +22 -0
- package/templates/registry/dropdown-menu.json +23 -0
- package/templates/registry/expandable-tabs.json +22 -0
- package/templates/registry/feature-gating.json +73 -0
- package/templates/registry/file-upload.json +24 -0
- package/templates/registry/hold-action-button.json +22 -0
- package/templates/registry/input-group.json +23 -0
- package/templates/registry/input.json +19 -0
- package/templates/registry/intelligo.json +187 -0
- package/templates/registry/invitation-accept.json +64 -0
- package/templates/registry/language-switcher.json +29 -0
- package/templates/registry/morphing-modal.json +24 -0
- package/templates/registry/notification-stack.json +24 -0
- package/templates/registry/notifications.json +87 -0
- package/templates/registry/onboarding.json +83 -0
- package/templates/registry/otp-input.json +22 -0
- package/templates/registry/page-header.json +15 -0
- package/templates/registry/payment-poll.json +52 -0
- package/templates/registry/popover-morph.json +22 -0
- package/templates/registry/popover.json +22 -0
- package/templates/registry/pricing.json +111 -0
- package/templates/registry/privacy-settings.json +65 -0
- package/templates/registry/profile-settings.json +68 -0
- package/templates/registry/progress.json +19 -0
- package/templates/registry/radio-group.json +22 -0
- package/templates/registry/registry.json +2945 -0
- package/templates/registry/route-error.json +65 -0
- package/templates/registry/select-morph.json +23 -0
- package/templates/registry/select.json +23 -0
- package/templates/registry/settings-shell.json +47 -0
- package/templates/registry/sheet.json +24 -0
- package/templates/registry/sidebar.json +28 -0
- package/templates/registry/smoke.json +34 -0
- package/templates/registry/spinner.json +16 -0
- package/templates/registry/stat-card.json +18 -0
- package/templates/registry/status-badge.json +18 -0
- package/templates/registry/switch.json +20 -0
- package/templates/registry/tabs.json +23 -0
- package/templates/registry/team-settings.json +97 -0
- package/templates/registry/textarea.json +16 -0
- package/templates/registry/tooltip.json +22 -0
- package/templates/registry/trial-banner.json +43 -0
- package/templates/registry/usage.json +84 -0
- package/templates/registry/workspace-settings.json +71 -0
- package/templates/registry-items.json +116 -0
- package/templates/registry-requires.json +180 -0
- package/templates/usage-page/usage-page.tsx.tpl +23 -8
- package/templates/vitest/server-only.ts.tpl +7 -0
- package/templates/vitest/vitest.config.ts.tpl +37 -0
- package/templates/billing-page/billing-page.tsx.tpl +0 -72
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `intelligo migrate --check`
|
|
3
|
+
*
|
|
4
|
+
* Answers "would deploying this code against that database work?"
|
|
5
|
+
* without changing anything. Two failure modes matter and neither is
|
|
6
|
+
* visible from the code alone:
|
|
7
|
+
*
|
|
8
|
+
* - migrations the database has not applied yet (deploying now runs
|
|
9
|
+
* code against an older schema);
|
|
10
|
+
* - migrations the database has applied that this checkout does not
|
|
11
|
+
* contain (the database is ahead — usually a rollback in progress).
|
|
12
|
+
*
|
|
13
|
+
* Drizzle records applied migrations in `drizzle.__drizzle_migrations`
|
|
14
|
+
* by content hash. A database provisioned with `db:push` has the
|
|
15
|
+
* schema but no rows there at all, which this reports distinctly:
|
|
16
|
+
* "unmanaged" is a different problem from "behind", and baselining is
|
|
17
|
+
* the fix (see the migrations README).
|
|
18
|
+
*
|
|
19
|
+
* The framework's chain is one baseline. A database that ran the pre-1.0
|
|
20
|
+
* chain holds its hashes, which `legacy-chain.json` (next to the
|
|
21
|
+
* journal) lists: they are reported as `legacy`, not as unknown, and a
|
|
22
|
+
* database holding all of them is `adoptable` — its schema is the
|
|
23
|
+
* baseline's, so `migrate` records the baseline without running it.
|
|
24
|
+
*
|
|
25
|
+
* The exit code is 1 whenever anything is pending or the database is
|
|
26
|
+
* ahead, which a brand-new database and a stale one share. A deploy
|
|
27
|
+
* gate that must tell them apart reads `migrate --check --json`: one
|
|
28
|
+
* JSON object on stdout, same exit code, whose `state` is
|
|
29
|
+
*
|
|
30
|
+
* - `up_to_date` — every migration is applied;
|
|
31
|
+
* - `pending` — a migrated database is behind this checkout;
|
|
32
|
+
* - `fresh` — no migration records and none of the framework's
|
|
33
|
+
* tables: an empty database, `migrate` applies the chain;
|
|
34
|
+
* - `unmanaged` — the tables exist with no records (`db:push`);
|
|
35
|
+
* - `ahead` — applied migrations this checkout does not contain;
|
|
36
|
+
* - `legacy` — the pre-1.0 chain; `adoptable` says whether
|
|
37
|
+
* `migrate` can take it over.
|
|
38
|
+
*
|
|
39
|
+
* Beside `state` it carries `exitCode`, `chain`, `applied`, `pending`,
|
|
40
|
+
* `unknown`, `legacy`, `legacyMissing` and `adoptable`.
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
import { createHash } from "node:crypto";
|
|
44
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
45
|
+
import path from "node:path";
|
|
46
|
+
|
|
47
|
+
import { readMigrationChain } from "../migrations.js";
|
|
48
|
+
|
|
49
|
+
export type MigrateCheckResult = {
|
|
50
|
+
/** Journal tags this checkout knows about, in order. */
|
|
51
|
+
chain: string[];
|
|
52
|
+
/** Applied according to the database. */
|
|
53
|
+
applied: string[];
|
|
54
|
+
/** In the chain but not applied — a deploy would run against an older schema. */
|
|
55
|
+
pending: string[];
|
|
56
|
+
/** Applied but absent from this checkout — the database is ahead. */
|
|
57
|
+
unknown: string[];
|
|
58
|
+
/**
|
|
59
|
+
* True when the migrations table is empty or missing while the
|
|
60
|
+
* schema exists — a push-provisioned database that needs baselining.
|
|
61
|
+
*/
|
|
62
|
+
unmanaged: boolean;
|
|
63
|
+
/** Pre-1.0 migrations the database ran, by tag. */
|
|
64
|
+
legacy: string[];
|
|
65
|
+
/** Length of the pre-1.0 chain (0 when this checkout ships none). */
|
|
66
|
+
legacyChainLength: number;
|
|
67
|
+
/**
|
|
68
|
+
* Pre-1.0 migrations the database has not run, by tag, in chain
|
|
69
|
+
* order — empty unless it ran some of them.
|
|
70
|
+
*/
|
|
71
|
+
legacyMissing: string[];
|
|
72
|
+
/**
|
|
73
|
+
* The database ran the whole pre-1.0 chain and not the baseline:
|
|
74
|
+
* `migrate` records the baseline as applied without running it.
|
|
75
|
+
*/
|
|
76
|
+
adoptable: boolean;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/** The pre-1.0 chain's tags and hashes, if this checkout ships them. */
|
|
80
|
+
export function readLegacyChain(
|
|
81
|
+
migrationsDir: string
|
|
82
|
+
): Array<{ tag: string; hash: string; alternates?: string[] }> {
|
|
83
|
+
const file = path.join(migrationsDir, "legacy-chain.json");
|
|
84
|
+
if (!existsSync(file)) return [];
|
|
85
|
+
const parsed = JSON.parse(readFileSync(file, "utf8")) as {
|
|
86
|
+
entries?: Array<{ tag: string; hash: string; alternates?: string[] }>;
|
|
87
|
+
};
|
|
88
|
+
return parsed.entries ?? [];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Drizzle hashes the file contents with sha256. */
|
|
92
|
+
export function hashMigration(sql: string): string {
|
|
93
|
+
return createHash("sha256").update(sql).digest("hex");
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
type QueryFn = (sql: string) => Promise<Array<{ hash: string }>>;
|
|
97
|
+
|
|
98
|
+
export async function migrateCheck(
|
|
99
|
+
migrationsDir: string,
|
|
100
|
+
query: QueryFn
|
|
101
|
+
): Promise<MigrateCheckResult> {
|
|
102
|
+
const chain = readMigrationChain(migrationsDir);
|
|
103
|
+
|
|
104
|
+
const hashToTag = new Map<string, string>();
|
|
105
|
+
for (const tag of chain.journalTags) {
|
|
106
|
+
const file = path.join(migrationsDir, `${tag}.sql`);
|
|
107
|
+
try {
|
|
108
|
+
hashToTag.set(hashMigration(readFileSync(file, "utf8")), tag);
|
|
109
|
+
} catch {
|
|
110
|
+
// readMigrationChain already reports files the journal names but
|
|
111
|
+
// that are not on disk; nothing to hash here.
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
let rows: Array<{ hash: string }> = [];
|
|
116
|
+
let tableMissing = false;
|
|
117
|
+
try {
|
|
118
|
+
rows = await query(
|
|
119
|
+
`SELECT hash FROM drizzle.__drizzle_migrations ORDER BY created_at`
|
|
120
|
+
);
|
|
121
|
+
} catch {
|
|
122
|
+
tableMissing = true;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const legacyChain = readLegacyChain(migrationsDir);
|
|
126
|
+
// A migration can be recorded under more than one hash: npm's
|
|
127
|
+
// 1.0.0-beta.6 shipped different bytes for three of them.
|
|
128
|
+
const legacyByHash = new Map(
|
|
129
|
+
legacyChain.flatMap((e) =>
|
|
130
|
+
[e.hash, ...(e.alternates ?? [])].map((h) => [h, e.tag] as const)
|
|
131
|
+
)
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
const appliedHashes = new Set(rows.map((r) => r.hash));
|
|
135
|
+
const applied: string[] = [];
|
|
136
|
+
const unknown: string[] = [];
|
|
137
|
+
const legacy: string[] = [];
|
|
138
|
+
for (const hash of appliedHashes) {
|
|
139
|
+
const tag = hashToTag.get(hash);
|
|
140
|
+
const legacyTag = legacyByHash.get(hash);
|
|
141
|
+
if (tag) applied.push(tag);
|
|
142
|
+
else if (legacyTag) legacy.push(legacyTag);
|
|
143
|
+
else unknown.push(hash.slice(0, 12));
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const appliedTags = new Set(applied);
|
|
147
|
+
const pending = chain.journalTags.filter((t) => !appliedTags.has(t));
|
|
148
|
+
const baseline = chain.journalTags[0];
|
|
149
|
+
|
|
150
|
+
return {
|
|
151
|
+
chain: chain.journalTags,
|
|
152
|
+
applied,
|
|
153
|
+
pending,
|
|
154
|
+
unknown,
|
|
155
|
+
unmanaged: tableMissing || appliedHashes.size === 0,
|
|
156
|
+
legacy,
|
|
157
|
+
legacyChainLength: legacyChain.length,
|
|
158
|
+
legacyMissing:
|
|
159
|
+
legacy.length > 0
|
|
160
|
+
? legacyChain.map((e) => e.tag).filter((t) => !legacy.includes(t))
|
|
161
|
+
: [],
|
|
162
|
+
adoptable:
|
|
163
|
+
legacyChain.length > 0 &&
|
|
164
|
+
legacy.length === legacyChain.length &&
|
|
165
|
+
baseline !== undefined &&
|
|
166
|
+
!appliedTags.has(baseline),
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* `schemaExists` is whether the framework's tables are in the database
|
|
172
|
+
* (`SCHEMA_PROBE_SQL`). When the caller did not probe, a database with
|
|
173
|
+
* no records gets the cautious `db:push` wording.
|
|
174
|
+
*/
|
|
175
|
+
export function formatMigrateCheck(
|
|
176
|
+
r: MigrateCheckResult,
|
|
177
|
+
schemaExists?: boolean
|
|
178
|
+
): string {
|
|
179
|
+
const lines: string[] = [];
|
|
180
|
+
|
|
181
|
+
if (r.unmanaged && schemaExists === false) {
|
|
182
|
+
lines.push(
|
|
183
|
+
`! This database is empty: no migration records and none of the ` +
|
|
184
|
+
`framework's tables. \`intelligo migrate\` applies all ` +
|
|
185
|
+
`${r.chain.length} migration(s).`
|
|
186
|
+
);
|
|
187
|
+
} else if (r.unmanaged) {
|
|
188
|
+
lines.push(
|
|
189
|
+
`! This database has no migration records. If it was provisioned with ` +
|
|
190
|
+
`db:push, baseline it before running migrate — otherwise migrate will ` +
|
|
191
|
+
`try to apply all ${r.chain.length} migrations. See README.md in ` +
|
|
192
|
+
`@intelligo-dev/core's src/db/migrations.`
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (r.adoptable) {
|
|
197
|
+
lines.push(
|
|
198
|
+
`! This database ran the framework's pre-1.0 migration chain. migrate ` +
|
|
199
|
+
`records ${r.chain[0]} as applied without running it (the schema is ` +
|
|
200
|
+
`already there), then applies what follows. Tables the old chain ` +
|
|
201
|
+
`created that the framework no longer owns are left untouched.`
|
|
202
|
+
);
|
|
203
|
+
} else if (isPartialLegacy(r)) {
|
|
204
|
+
lines.push(`✗ ${partialLegacyMessage(r)}`);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (r.unknown.length > 0) {
|
|
208
|
+
lines.push(
|
|
209
|
+
`✗ Database is ahead: ${r.unknown.length} applied migration(s) are not in ` +
|
|
210
|
+
`this checkout (${r.unknown.join(", ")})`
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (r.pending.length > 0) {
|
|
215
|
+
lines.push(
|
|
216
|
+
`✗ ${r.pending.length} migration(s) pending: ${r.pending.join(", ")}`
|
|
217
|
+
);
|
|
218
|
+
} else if (!r.unmanaged) {
|
|
219
|
+
lines.push(`✓ Up to date (${r.applied.length}/${r.chain.length} applied)`);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return lines.join("\n");
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** Non-zero when deploying this code would run against a stale schema. */
|
|
226
|
+
export function migrateCheckExitCode(r: MigrateCheckResult): number {
|
|
227
|
+
return r.pending.length > 0 || r.unknown.length > 0 ? 1 : 0;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** A partial pre-1.0 chain: neither adoptable nor migratable from here. */
|
|
231
|
+
export function isPartialLegacy(r: MigrateCheckResult): boolean {
|
|
232
|
+
return r.legacy.length > 0 && r.legacy.length < r.legacyChainLength;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* The last pre-1.0 migration any published `@intelligo-dev/core`
|
|
237
|
+
* carries: 1.0.0-beta.6 shipped the chain through it, and the versions
|
|
238
|
+
* that carried the rest were never published.
|
|
239
|
+
*/
|
|
240
|
+
export const LAST_PUBLISHED_LEGACY_TAG = "0042_sessions_active_organization_id";
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Why a partial pre-1.0 chain is refused and where the way forward is
|
|
244
|
+
* written down. No published version finishes the chain, so the
|
|
245
|
+
* message names what is missing and points at the README's options
|
|
246
|
+
* instead of at a version to install.
|
|
247
|
+
*/
|
|
248
|
+
export function partialLegacyMessage(r: MigrateCheckResult): string {
|
|
249
|
+
const shown = r.legacyMissing.slice(0, 6);
|
|
250
|
+
const more = r.legacyMissing.length - shown.length;
|
|
251
|
+
return (
|
|
252
|
+
`This database ran ${r.legacy.length} of the ${r.legacyChainLength} ` +
|
|
253
|
+
`pre-1.0 migrations; it has not run ${shown.join(", ")}` +
|
|
254
|
+
`${more > 0 ? ` and ${more} more` : ""}. No published ` +
|
|
255
|
+
`@intelligo-dev/core finishes that chain: 1.0.0-beta.6 ships it ` +
|
|
256
|
+
`through ${LAST_PUBLISHED_LEGACY_TAG}, and the versions that carried ` +
|
|
257
|
+
`the rest never reached npm. Apply the missing migrations' SQL from ` +
|
|
258
|
+
`your own copy of it, or bring the schema to the baseline and record ` +
|
|
259
|
+
`it as applied — see "Databases from before 1.0" in README.md in ` +
|
|
260
|
+
`@intelligo-dev/core's src/db/migrations.`
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export type MigrateState =
|
|
265
|
+
"up_to_date" | "pending" | "fresh" | "ahead" | "unmanaged" | "legacy";
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* One word for what the check found, most urgent first: a database that
|
|
269
|
+
* is ahead or on the pre-1.0 chain is that before it is anything else.
|
|
270
|
+
* `schemaExists` separates an empty database from a push-provisioned one.
|
|
271
|
+
*/
|
|
272
|
+
export function migrateState(
|
|
273
|
+
r: MigrateCheckResult,
|
|
274
|
+
schemaExists: boolean
|
|
275
|
+
): MigrateState {
|
|
276
|
+
if (r.unknown.length > 0) return "ahead";
|
|
277
|
+
if (r.legacy.length > 0) return "legacy";
|
|
278
|
+
if (r.unmanaged) return schemaExists ? "unmanaged" : "fresh";
|
|
279
|
+
return r.pending.length > 0 ? "pending" : "up_to_date";
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** The `--json` report: `state` first, then the detail behind it. */
|
|
283
|
+
export function formatMigrateCheckJson(
|
|
284
|
+
r: MigrateCheckResult,
|
|
285
|
+
schemaExists: boolean
|
|
286
|
+
): string {
|
|
287
|
+
return JSON.stringify({
|
|
288
|
+
state: migrateState(r, schemaExists),
|
|
289
|
+
exitCode: migrateCheckExitCode(r),
|
|
290
|
+
chain: r.chain,
|
|
291
|
+
applied: r.applied,
|
|
292
|
+
pending: r.pending,
|
|
293
|
+
unknown: r.unknown,
|
|
294
|
+
legacy: r.legacy,
|
|
295
|
+
legacyMissing: r.legacyMissing,
|
|
296
|
+
adoptable: r.adoptable,
|
|
297
|
+
});
|
|
298
|
+
}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `intelligo migrate` — apply the framework's migration chain.
|
|
3
|
+
*
|
|
4
|
+
* A consumer application has two migration chains in one database:
|
|
5
|
+
* the framework's, shipped inside `@intelligo-dev/core` (its `.sql`
|
|
6
|
+
* files and journal are in the package's `files`), and its own, which
|
|
7
|
+
* drizzle-kit generates from the tables the application owns. They
|
|
8
|
+
* must not share a journal — drizzle-kit applies by timestamp, so a
|
|
9
|
+
* framework migration published after the consumer generated one of
|
|
10
|
+
* theirs would be silently skipped — and a consumer cannot write into
|
|
11
|
+
* `node_modules` anyway. So the framework chain is applied by this
|
|
12
|
+
* command, into drizzle's default `drizzle.__drizzle_migrations`
|
|
13
|
+
* table, and the consumer's chain by `drizzle-kit migrate` from their
|
|
14
|
+
* own `drizzle.config.ts`, into a table of its own (the scaffold sets
|
|
15
|
+
* `migrations.table` to `__app_migrations`).
|
|
16
|
+
*
|
|
17
|
+
* Migrations are selected by content hash (what `migrate --check`
|
|
18
|
+
* compares), not by drizzle's journal-timestamp rule, and are recorded
|
|
19
|
+
* in the same table drizzle's migrator writes — see
|
|
20
|
+
* `readPendingMigrations` for why.
|
|
21
|
+
*
|
|
22
|
+
* The one thing this refuses to do is guess. A database with the
|
|
23
|
+
* framework's tables but no migration records was provisioned with
|
|
24
|
+
* `db:push`; applying the whole chain to it would fail part-way (not
|
|
25
|
+
* every migration is `IF NOT EXISTS`-guarded) and leave the records
|
|
26
|
+
* half-written. Baselining is the fix, and it is deliberately a manual
|
|
27
|
+
* step — see `packages/core/src/db/migrations/README.md`.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import { readFileSync } from "node:fs";
|
|
31
|
+
import path from "node:path";
|
|
32
|
+
|
|
33
|
+
import {
|
|
34
|
+
hashMigration,
|
|
35
|
+
isPartialLegacy,
|
|
36
|
+
migrateCheck,
|
|
37
|
+
partialLegacyMessage,
|
|
38
|
+
type MigrateCheckResult,
|
|
39
|
+
} from "./migrate-check.js";
|
|
40
|
+
|
|
41
|
+
export type ApplyDecision =
|
|
42
|
+
| {
|
|
43
|
+
action: "apply";
|
|
44
|
+
pending: string[];
|
|
45
|
+
/** Recorded as applied without running: the schema is already there. */
|
|
46
|
+
adopted: string[];
|
|
47
|
+
}
|
|
48
|
+
| { action: "noop" }
|
|
49
|
+
| { action: "refuse"; reason: string };
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* What to do with a database, given the check result and whether the
|
|
53
|
+
* framework's schema already exists in it. Pure, so the three states
|
|
54
|
+
* that matter — empty, push-provisioned, behind — are unit-testable
|
|
55
|
+
* without a database.
|
|
56
|
+
*/
|
|
57
|
+
export function decideApply(
|
|
58
|
+
check: MigrateCheckResult,
|
|
59
|
+
schemaExists: boolean
|
|
60
|
+
): ApplyDecision {
|
|
61
|
+
if (check.unknown.length > 0) {
|
|
62
|
+
return {
|
|
63
|
+
action: "refuse",
|
|
64
|
+
reason:
|
|
65
|
+
`Database is ahead: ${check.unknown.length} applied migration(s) are ` +
|
|
66
|
+
`not in this checkout (${check.unknown.join(", ")}). Upgrade ` +
|
|
67
|
+
`@intelligo-dev/core before migrating, or restore the database.`,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (isPartialLegacy(check)) {
|
|
72
|
+
return { action: "refuse", reason: partialLegacyMessage(check) };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (check.adoptable) {
|
|
76
|
+
const [baseline, ...rest] = check.pending;
|
|
77
|
+
return {
|
|
78
|
+
action: "apply",
|
|
79
|
+
adopted: baseline ? [baseline] : [],
|
|
80
|
+
pending: rest,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (check.unmanaged && schemaExists) {
|
|
85
|
+
return {
|
|
86
|
+
action: "refuse",
|
|
87
|
+
reason:
|
|
88
|
+
`This database has the framework's tables but no migration records — ` +
|
|
89
|
+
`it was provisioned with db:push. Baseline it before running migrate, ` +
|
|
90
|
+
`otherwise all ${check.chain.length} migrations would be applied to ` +
|
|
91
|
+
`tables that already exist. See README.md in @intelligo-dev/core's ` +
|
|
92
|
+
`src/db/migrations.`,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (check.pending.length === 0) return { action: "noop" };
|
|
97
|
+
|
|
98
|
+
return { action: "apply", pending: check.pending, adopted: [] };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
type QueryFn = (sql: string) => Promise<Array<Record<string, unknown>>>;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* One migration ready to run: its statements and the journal entry it
|
|
105
|
+
* will be recorded under. `createdAt` is the journal's `when`, which is
|
|
106
|
+
* what drizzle-kit writes to `created_at` too, so records made here and
|
|
107
|
+
* records made by drizzle's own migrator are indistinguishable.
|
|
108
|
+
*/
|
|
109
|
+
export type PendingMigration = {
|
|
110
|
+
tag: string;
|
|
111
|
+
hash: string;
|
|
112
|
+
createdAt: number;
|
|
113
|
+
statements: string[];
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Read the pending migrations off disk, in journal order.
|
|
118
|
+
*
|
|
119
|
+
* The chain is applied by content hash — the same key `migrate --check`
|
|
120
|
+
* compares — and not by drizzle's rule of "every entry whose journal
|
|
121
|
+
* timestamp is greater than the last applied row's". That rule depends
|
|
122
|
+
* on `when` values increasing monotonically, which a hand-numbered
|
|
123
|
+
* journal does not guarantee: an entry numbered below an applied one is
|
|
124
|
+
* skipped silently while the check reports it pending. Selecting by hash makes "pending" mean exactly what the check says.
|
|
125
|
+
*/
|
|
126
|
+
export function readPendingMigrations(
|
|
127
|
+
migrationsDir: string,
|
|
128
|
+
pendingTags: string[]
|
|
129
|
+
): PendingMigration[] {
|
|
130
|
+
const journal = JSON.parse(
|
|
131
|
+
readFileSync(path.join(migrationsDir, "meta", "_journal.json"), "utf8")
|
|
132
|
+
) as {
|
|
133
|
+
entries?: Array<{ tag: string; when: number; breakpoints?: boolean }>;
|
|
134
|
+
};
|
|
135
|
+
const byTag = new Map((journal.entries ?? []).map((e) => [e.tag, e]));
|
|
136
|
+
|
|
137
|
+
return pendingTags.map((tag) => {
|
|
138
|
+
const entry = byTag.get(tag);
|
|
139
|
+
if (!entry) throw new Error(`${tag} is pending but not in the journal`);
|
|
140
|
+
const sql = readFileSync(path.join(migrationsDir, `${tag}.sql`), "utf8");
|
|
141
|
+
const statements = (
|
|
142
|
+
entry.breakpoints === false
|
|
143
|
+
? [sql]
|
|
144
|
+
: sql.split("--> statement-breakpoint")
|
|
145
|
+
)
|
|
146
|
+
.map((s) => s.trim())
|
|
147
|
+
.filter((s) => s.length > 0);
|
|
148
|
+
return { tag, hash: hashMigration(sql), createdAt: entry.when, statements };
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** The records table drizzle's migrator uses, created the way it creates it. */
|
|
153
|
+
export const MIGRATIONS_TABLE_SQL = [
|
|
154
|
+
`CREATE SCHEMA IF NOT EXISTS "drizzle"`,
|
|
155
|
+
`CREATE TABLE IF NOT EXISTS "drizzle"."__drizzle_migrations" (id SERIAL PRIMARY KEY, hash text NOT NULL, created_at bigint)`,
|
|
156
|
+
];
|
|
157
|
+
|
|
158
|
+
export type ApplyMigrationsOptions = {
|
|
159
|
+
migrationsDir: string;
|
|
160
|
+
/** Runs a read-only query and returns its rows. */
|
|
161
|
+
query: QueryFn;
|
|
162
|
+
/**
|
|
163
|
+
* Applies the given migrations in order, all in one transaction,
|
|
164
|
+
* recording each in `drizzle.__drizzle_migrations` as it goes.
|
|
165
|
+
*/
|
|
166
|
+
run: (pending: PendingMigration[]) => Promise<void>;
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
export type ApplyMigrationsResult = ApplyDecision & {
|
|
170
|
+
/** After the run, for the summary line and for `noop` detection. */
|
|
171
|
+
chainLength: number;
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* `users` is the first table the framework creates (auth), so its
|
|
176
|
+
* presence with no migration records is the push-provisioned signature.
|
|
177
|
+
*/
|
|
178
|
+
export const SCHEMA_PROBE_SQL = `SELECT to_regclass('public.users') AS rel`;
|
|
179
|
+
|
|
180
|
+
export async function applyMigrations(
|
|
181
|
+
options: ApplyMigrationsOptions
|
|
182
|
+
): Promise<ApplyMigrationsResult> {
|
|
183
|
+
const check = await migrateCheck(
|
|
184
|
+
options.migrationsDir,
|
|
185
|
+
async (sql) => (await options.query(sql)) as Array<{ hash: string }>
|
|
186
|
+
);
|
|
187
|
+
|
|
188
|
+
const probe = await options.query(SCHEMA_PROBE_SQL);
|
|
189
|
+
const schemaExists = probe.length > 0 && probe[0]!.rel != null;
|
|
190
|
+
|
|
191
|
+
const decision = decideApply(check, schemaExists);
|
|
192
|
+
if (decision.action === "apply") {
|
|
193
|
+
// An adopted migration is recorded with no statements: its schema is
|
|
194
|
+
// already in the database.
|
|
195
|
+
const adopted = readPendingMigrations(
|
|
196
|
+
options.migrationsDir,
|
|
197
|
+
decision.adopted
|
|
198
|
+
).map((m) => ({ ...m, statements: [] }));
|
|
199
|
+
await options.run([
|
|
200
|
+
...adopted,
|
|
201
|
+
...readPendingMigrations(options.migrationsDir, decision.pending),
|
|
202
|
+
]);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return { ...decision, chainLength: check.chain.length };
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export function formatApplyResult(r: ApplyMigrationsResult): string {
|
|
209
|
+
switch (r.action) {
|
|
210
|
+
case "refuse":
|
|
211
|
+
return `✗ ${r.reason}`;
|
|
212
|
+
case "noop":
|
|
213
|
+
return `✓ Nothing to apply (${r.chainLength}/${r.chainLength} applied)`;
|
|
214
|
+
case "apply":
|
|
215
|
+
return [
|
|
216
|
+
...r.adopted.map(
|
|
217
|
+
(tag) =>
|
|
218
|
+
`✓ Adopted ${tag}: this database ran the pre-1.0 chain, so its schema is already there`
|
|
219
|
+
),
|
|
220
|
+
`✓ Applied ${r.pending.length} migration(s):`,
|
|
221
|
+
...r.pending.map((tag) => ` ${tag}`),
|
|
222
|
+
].join("\n");
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export function applyExitCode(r: ApplyMigrationsResult): number {
|
|
227
|
+
return r.action === "refuse" ? 1 : 0;
|
|
228
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Message files under `intelligo sync`: the registry's keys an app's
|
|
3
|
+
* copy lacks, the merge that keeps the app's values, and the other
|
|
4
|
+
* locales checked against the app's English copy.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
|
|
10
|
+
export type Json = Record<string, unknown>;
|
|
11
|
+
|
|
12
|
+
const isObject = (v: unknown): v is Json =>
|
|
13
|
+
typeof v === "object" && v !== null && !Array.isArray(v);
|
|
14
|
+
|
|
15
|
+
/** Dotted paths of every leaf in `registry` that `app` lacks. */
|
|
16
|
+
export function missingMessageKeys(
|
|
17
|
+
registry: Json,
|
|
18
|
+
app: Json,
|
|
19
|
+
prefix = ""
|
|
20
|
+
): string[] {
|
|
21
|
+
return Object.entries(registry).flatMap(([key, value]) => {
|
|
22
|
+
const at = prefix ? `${prefix}.${key}` : key;
|
|
23
|
+
if (!(key in app)) return [at];
|
|
24
|
+
const mine = app[key];
|
|
25
|
+
return isObject(value) && isObject(mine)
|
|
26
|
+
? missingMessageKeys(value, mine, at)
|
|
27
|
+
: [];
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The registry's messages with the app's copy laid over them: every key
|
|
33
|
+
* the registry has is present, every value the app set wins, and keys
|
|
34
|
+
* only the app has (its own nav entries, product copy) are kept.
|
|
35
|
+
*/
|
|
36
|
+
export function mergeMessages(registry: Json, app: Json): Json {
|
|
37
|
+
const out: Json = {};
|
|
38
|
+
for (const [key, value] of Object.entries(registry)) {
|
|
39
|
+
const mine = app[key];
|
|
40
|
+
out[key] =
|
|
41
|
+
key in app
|
|
42
|
+
? isObject(value) && isObject(mine)
|
|
43
|
+
? mergeMessages(value, mine)
|
|
44
|
+
: mine
|
|
45
|
+
: value;
|
|
46
|
+
}
|
|
47
|
+
for (const [key, value] of Object.entries(app)) {
|
|
48
|
+
if (!(key in out)) out[key] = value;
|
|
49
|
+
}
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export const isMessages = (target: string) =>
|
|
54
|
+
target.startsWith("messages/") && target.endsWith(".json");
|
|
55
|
+
|
|
56
|
+
/** The locale the registry ships messages in. */
|
|
57
|
+
const SOURCE_LOCALE = "en";
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The app's locales, from `locales: [...]` in i18n/routing.ts — the
|
|
61
|
+
* source locale alone when there is no such file or list.
|
|
62
|
+
*/
|
|
63
|
+
export function appLocales(appRoot: string): string[] {
|
|
64
|
+
const file = path.join(appRoot, "i18n", "routing.ts");
|
|
65
|
+
if (!existsSync(file)) return [SOURCE_LOCALE];
|
|
66
|
+
const list = /\blocales\s*:\s*\[([^\]]*)\]/.exec(readFileSync(file, "utf8"));
|
|
67
|
+
const locales = [...(list?.[1] ?? "").matchAll(/["'`]([^"'`]+)["'`]/g)].map(
|
|
68
|
+
(m) => m[1]!
|
|
69
|
+
);
|
|
70
|
+
return locales.length > 0 ? locales : [SOURCE_LOCALE];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export type LocaleEntry = {
|
|
74
|
+
item: string;
|
|
75
|
+
path: string;
|
|
76
|
+
state: "current" | "locale-behind";
|
|
77
|
+
missingKeys?: string[];
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Every other locale's copy of the namespaces the registry ships,
|
|
82
|
+
* against the app's English file on disk — the merged copy, product
|
|
83
|
+
* keys included, which is what the other locales translate. A missing
|
|
84
|
+
* file lacks every key.
|
|
85
|
+
*/
|
|
86
|
+
export function localeEntries(
|
|
87
|
+
appRoot: string,
|
|
88
|
+
files: readonly { item: string; target: string }[]
|
|
89
|
+
): LocaleEntry[] {
|
|
90
|
+
const others = appLocales(appRoot).filter((l) => l !== SOURCE_LOCALE);
|
|
91
|
+
if (others.length === 0) return [];
|
|
92
|
+
const prefix = `messages/${SOURCE_LOCALE}/`;
|
|
93
|
+
const entries: LocaleEntry[] = [];
|
|
94
|
+
for (const { item, target } of files) {
|
|
95
|
+
if (!target.startsWith(prefix) || !target.endsWith(".json")) continue;
|
|
96
|
+
const english = path.join(appRoot, target);
|
|
97
|
+
if (!existsSync(english)) continue;
|
|
98
|
+
const source = JSON.parse(readFileSync(english, "utf8")) as Json;
|
|
99
|
+
for (const locale of others) {
|
|
100
|
+
const rel = `messages/${locale}/${target.slice(prefix.length)}`;
|
|
101
|
+
const abs = path.join(appRoot, rel);
|
|
102
|
+
const missingKeys = missingMessageKeys(
|
|
103
|
+
source,
|
|
104
|
+
existsSync(abs) ? (JSON.parse(readFileSync(abs, "utf8")) as Json) : {}
|
|
105
|
+
);
|
|
106
|
+
entries.push(
|
|
107
|
+
missingKeys.length > 0
|
|
108
|
+
? { item, path: rel, state: "locale-behind", missingKeys }
|
|
109
|
+
: { item, path: rel, state: "current" }
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return entries;
|
|
114
|
+
}
|