OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
ralph(M15-204): code-migrations leaves core for packages/db and runs as a script, copying the four constants it needs rather than importing them (Carl's ruling, ADR-022)
ralphd8e50c2fb62f
diff --git a/apps/api/Dockerfile b/apps/api/Dockerfileindex 0791156ac..ee08bd466 100644--- a/apps/api/Dockerfile+++ b/apps/api/Dockerfile@@ -143,10 +143,15 @@ COPY --from=prod-deps --chown=bun:bun /app/tooling/typescript ./tooling/typescri# `cd /app/packages/db && bunx [email protected] migrate deploy`, so the schema and# the migrations directory are load-bearing image content, not source-tree# detail. Assert them at build time — the alternative is a self-hoster finding-# out at container start. (`packages/core/scripts/migrate-code.ts` is the code-# migrations' entrypoint and is asserted for the same reason.)+# out at container start. The code migrations' entrypoint is asserted for the+# same reason: it moved to `packages/db/scripts/migrate-code.ts` in M15-204,+# and `packages/core/scripts/migrate-code.ts` — which the shipped compose files+# still name — now forwards to it, so both are load-bearing until those files+# are updated.RUN test -f /app/packages/db/prisma/schema.prisma && \test -d /app/packages/db/prisma/migrations && \+ test -f /app/packages/db/scripts/migrate-code.ts && \+ test -d /app/packages/db/src/code-migrations && \test -f /app/packages/core/scripts/migrate-code.tsUSER bundiff --git a/docs/BUN_INSTALL_RECIPE.md b/docs/BUN_INSTALL_RECIPE.mdindex 5b941a3b2..17c207fc8 100644--- a/docs/BUN_INSTALL_RECIPE.md+++ b/docs/BUN_INSTALL_RECIPE.md@@ -366,6 +366,12 @@ must be right first.|---|---|---|| `migrate:deploy:code` | `pnpm with-env jiti ./src/code-migrations/migrate.ts` | `bun run with-env bun ./src/code-migrations/migrate.ts` — ADR-019 row 7b already replaces `jiti` with bun as the TS runner |+> **Superseded, M15-204:** `code-migrations` left core for `@openpanel/db`+> (ADR-022, Carl's rulings), so `packages/core` no longer declares+> `migrate:deploy:code` at all and `packages/db`'s own entry below runs+> `bun run with-env bun ./scripts/migrate-code.ts` directly instead of+> filtering back to core. The rows are kept as the P13 record.+### `packages/db/package.json`| script | today | bun |diff --git a/docs/DOCKER_IMAGES.md b/docs/DOCKER_IMAGES.mdindex 506dde478..7cbec6aa7 100644--- a/docs/DOCKER_IMAGES.md+++ b/docs/DOCKER_IMAGES.md@@ -127,7 +127,15 @@ cd /app/packages/core && bun scripts/migrate-code.ts- **The version is pinned.** A bare `bunx prisma` fetches latest, which candrift from the generated client and the schema the image ships. The repo ison `prisma 6.14.0`.-- `packages/db/prisma/schema.prisma`, `packages/db/prisma/migrations` and+- **The code migrations moved to `@openpanel/db` in M15-204** (ADR-022, Carl's+ rulings: they run as a script with direct database access and no services).+ The real entrypoint is now `packages/db/scripts/migrate-code.ts`, and+ `packages/core/scripts/migrate-code.ts` is a one-line forwarder to it, kept+ so the command above — which the three shipped compose files still spell+ against `packages/core` — keeps working. The forwarder goes away in the task+ that is allowed to edit `self-hosting/*` and `.github/smoke/*`.+- `packages/db/prisma/schema.prisma`, `packages/db/prisma/migrations`,+ `packages/db/scripts/migrate-code.ts`, `packages/db/src/code-migrations` and`packages/core/scripts/migrate-code.ts` are asserted at build time by a `RUNtest` in the runtime stage. The whole command depends on them, and thealternative is a self-hoster finding out at container start.diff --git a/docs/OPS_DEPLOY_MIGRATION.md b/docs/OPS_DEPLOY_MIGRATION.mdindex 65d43ecdf..a9151e1ca 100644--- a/docs/OPS_DEPLOY_MIGRATION.md+++ b/docs/OPS_DEPLOY_MIGRATION.md@@ -11,7 +11,9 @@> any more. The shipped commands are now> `cd /app/packages/db && bunx [email protected] migrate deploy` and> `cd /app/packages/core && bun scripts/migrate-code.ts`; see-> `docs/DOCKER_IMAGES.md`. Everything else in this file stands.+> `docs/DOCKER_IMAGES.md`. Since M15-204 that second path is a forwarder — the+> migrations themselves live in `packages/db/src/code-migrations`, run by+> `packages/db/scripts/migrate-code.ts`. Everything else in this file stands.## What changed in the repodiff --git a/packages/core/package.json b/packages/core/package.jsonindex 8a1ee6288..42d92579f 100644--- a/packages/core/package.json+++ b/packages/core/package.json@@ -15,7 +15,6 @@"gen:asns": "bun scripts/get-datacenter-asns.ts","gen:bots": "bun scripts/get-bots.ts && biome format --write ./src/modules/ingest/src/bots/bots.ts","gen:referrers": "bun scripts/get-referrers.ts && biome format --write ./src/shared/referrers/index.ts",- "migrate:deploy:code": "bun run with-env bun ./scripts/migrate-code.ts","with-env": "dotenv -e ../../.env -c --"},"dependencies": {diff --git a/packages/core/scripts/migrate-code.ts b/packages/core/scripts/migrate-code.tsindex d96eeddfd..ddf9a900b 100644--- a/packages/core/scripts/migrate-code.ts+++ b/packages/core/scripts/migrate-code.ts@@ -1,26 +1,11 @@-// The shell for `src/code-migrations`. It reads the environment, the runner-// does the work — the same split `apps/api/src/main.ts` and `config/env.ts`-// have, and what keeps `packages/core/src` free of `process.env` (ADR-022 R7).+// Compatibility entrypoint. `code-migrations` moved to @openpanel/db in+// M15-204 (ADR-022, Carl's rulings), but the shipped compose files and the+// Coolify template still run `cd /app/packages/core && bun+// scripts/migrate-code.ts`, and those files are outside that task's scope.+// This forwards so a self-hoster pulling the new image keeps migrating.//-// `code-migrations` is not a conformance target (ADR-022, Carl's rulings): it-// runs as a script with direct database access and no services, and it leaves-// core for `packages/db` in its own task. This file is the seam that makes it-// runnable without a `process.env` read inside `src/`.+// Delete this the moment self-hosting/docker-compose.template.yml,+// self-hosting/coolify.yml and .github/smoke/docker-compose.yml name+// `packages/db` instead.-import type { CodeMigrationEnv } from '../src/code-migrations/helpers';-import { runCodeMigrations } from '../src/code-migrations/migrate';--const TRUE_VALUES = new Set(['true', '1']);--function isEnabled(value: string | undefined): boolean {- return value !== undefined && TRUE_VALUES.has(value.trim());-}--const env: CodeMigrationEnv = {- clickhouseCluster: isEnabled(process.env.CLICKHOUSE_CLUSTER),- selfHosted: isEnabled(process.env.SELF_HOSTED),- databaseUrl: process.env.DATABASE_URL,- clickhouseUrl: process.env.CLICKHOUSE_URL,-};--await runCodeMigrations(env);+import '@openpanel/db/scripts/migrate-code';diff --git a/packages/core/src/code-migrations/6-add-revenue-column.ts b/packages/core/src/code-migrations/6-add-revenue-column.tsdeleted file mode 100644index dbfff35a2..000000000--- a/packages/core/src/code-migrations/6-add-revenue-column.ts+++ /dev/null@@ -1,36 +0,0 @@-import fs from 'node:fs';-import path from 'node:path';-import {- addColumns,- runClickhouseMigrationCommands,-} from '@openpanel/db/src/clickhouse/migration';-import { type CodeMigrationEnv, getIsCluster } from './helpers';--export async function up(env: CodeMigrationEnv) {- const isClustered = getIsCluster(env);-- const sqls: string[] = [- ...addColumns(- 'events',- ['`revenue` UInt64 AFTER `referrer_type`'],- isClustered- ),- ];-- fs.writeFileSync(- path.join(__filename.replace('.ts', '.sql')),- sqls- .map((sql) =>- sql- .trim()- .replace(/;$/, '')- .replace(/\n{2,}/g, '\n')- .concat(';')- )- .join('\n\n---\n\n')- );-- if (!process.argv.includes('--dry')) {- await runClickhouseMigrationCommands(sqls);- }-}diff --git a/packages/core/src/code-migrations/helpers.ts b/packages/core/src/code-migrations/helpers.tsdeleted file mode 100644index c0b41d304..000000000--- a/packages/core/src/code-migrations/helpers.ts+++ /dev/null@@ -1,43 +0,0 @@-export function printBoxMessage(title: string, lines: (string | unknown)[]) {- console.log('┌──┐');- console.log('│');- if (title) {- console.log(`│ ${title}`);- if (lines.length) {- console.log('│');- }- }- lines.forEach((line) => {- console.log(`│ ${line}`);- });- console.log('│');- console.log('└──┘');-}--/**- * The environment this script needs, read by its shell- * (`packages/core/scripts/migrate-code.ts`) rather than here: nothing under- * `packages/core/src` reads `process.env` (ADR-022 R7).- */-export interface CodeMigrationEnv {- clickhouseCluster: boolean;- selfHosted: boolean;- databaseUrl: string | undefined;- clickhouseUrl: string | undefined;-}--export function getIsCluster(env: CodeMigrationEnv) {- return process.argv.includes('--cluster') || env.clickhouseCluster;-}--export function getIsSelfHosting(env: CodeMigrationEnv) {- return env.selfHosted;-}--export function getIsDry() {- return process.argv.includes('--dry');-}--export function getShouldIgnoreRecord() {- return process.argv.includes('--no-record');-}diff --git a/packages/core/src/modules/chart/src/field-resolution.ts b/packages/core/src/modules/chart/src/field-resolution.tsindex a715dd5c6..c4cea0f10 100644--- a/packages/core/src/modules/chart/src/field-resolution.ts+++ b/packages/core/src/modules/chart/src/field-resolution.ts@@ -35,7 +35,8 @@ export const CHART_TABLE = {} as const;// Top-level columns on the events table. Derived from the migration in-// code-migrations/3-init-ch.ts (+ revenue added in 6-add-revenue-column.ts).+// @openpanel/db's code-migrations/3-init-ch.ts (+ revenue added in+// 6-add-revenue-column.ts).// Used to distinguish real columns from property keys and reject unknown// identifiers before they reach ClickHouse.export const EVENT_TOP_LEVEL_COLUMNS = new Set<string>([diff --git a/packages/core/src/modules/organization/organization.constants.ts b/packages/core/src/modules/organization/organization.constants.tsindex 23dabed56..0b6e61454 100644--- a/packages/core/src/modules/organization/organization.constants.ts+++ b/packages/core/src/modules/organization/organization.constants.ts@@ -1,9 +1,10 @@// Moved from packages/validation/src/index.ts (M11-006, ADR-008's module map:// organization owns "C" for the member/invite/organization vocabulary).// `zProjectAccessGrant` is a per-project grant but every consumer is-// organization-shaped (organization.rpc.ts's invite/access mutations,-// code-migrations/20-invite-project-access-levels.ts), so it lives here and-// `zInviteUser` embeds it without a cross-file constants import.+// organization-shaped (organization.rpc.ts's invite/access mutations), so it+// lives here and `zInviteUser` embeds it without a cross-file constants+// import. @openpanel/db's `code-migrations/constants.ts` keeps its own frozen+// copy of the schema (ADR-022, Carl's rulings) — it is not a consumer.//// Isomorphic by the AGENTS.md rule: zod and nothing else.diff --git a/packages/core/src/shared/ch-tables.ts b/packages/core/src/shared/ch-tables.tsindex 66c3f76cb..ed3527202 100644--- a/packages/core/src/shared/ch-tables.ts+++ b/packages/core/src/shared/ch-tables.ts@@ -2,8 +2,8 @@// cluster helpers that derive from them.//// M10-009: these live in `@openpanel/db/src/clickhouse/client.ts` too, and-// that module is where the migrations (`code-migrations/`) still take them-// from — but importing it constructs a real ClickHouse client and a real pino+// that module is where the migrations (`@openpanel/db`'s `code-migrations/`,+// M15-204) still take them from — but importing it constructs a real ClickHouse client and a real pino// logger at module load, which is the cost every `load*()` lazy loader in this// package existed to defer, and it is a value import of `@openpanel/db` from// core, which `core-uses-ctx-not-db-internals` now forbids. Core takes thediff --git a/packages/db/package.json b/packages/db/package.jsonindex 436664337..05eba38bf 100644--- a/packages/db/package.json+++ b/packages/db/package.json@@ -6,7 +6,7 @@"scripts": {"codegen": "bun run with-env prisma generate && bun prisma/prisma-json-types.ts","migrate": "bun run with-env prisma migrate dev",- "migrate:deploy:code": "bun run --filter @openpanel/core migrate:deploy:code",+ "migrate:deploy:code": "bun run with-env bun ./scripts/migrate-code.ts","migrate:deploy:db": "bun run with-env prisma migrate deploy","migrate:deploy": "bun run migrate:deploy:db && bun run migrate:deploy:code","duplicate-events": "bun run with-env bun ./scripts/find-duplicate-events.ts",diff --git a/packages/db/scripts/migrate-code.ts b/packages/db/scripts/migrate-code.tsnew file mode 100644index 000000000..c1c4fc50c--- /dev/null+++ b/packages/db/scripts/migrate-code.ts@@ -0,0 +1,27 @@+// The shell for `src/code-migrations`. It reads the environment, the runner+// does the work — the same split `apps/api/src/main.ts` and `config/env.ts`+// have, and what keeps every migration file free of `process.env`+// (ADR-022 R7, which travelled with the directory in M15-204).+//+// `code-migrations` is not a conformance target (ADR-022, Carl's rulings): it+// lives in @openpanel/db, runs as a script with direct database access and no+// services, and copies the vocabulary it needs from core rather than importing+// it — isolation beats reuse.++import type { CodeMigrationEnv } from '../src/code-migrations/helpers';+import { runCodeMigrations } from '../src/code-migrations/migrate';++const TRUE_VALUES = new Set(['true', '1']);++function isEnabled(value: string | undefined): boolean {+ return value !== undefined && TRUE_VALUES.has(value.trim());+}++const env: CodeMigrationEnv = {+ clickhouseCluster: isEnabled(process.env.CLICKHOUSE_CLUSTER),+ selfHosted: isEnabled(process.env.SELF_HOSTED),+ databaseUrl: process.env.DATABASE_URL,+ clickhouseUrl: process.env.CLICKHOUSE_URL,+};++await runCodeMigrations(env);diff --git a/packages/db/src/clickhouse/migration.ts b/packages/db/src/clickhouse/migration.tsindex 2f1f7a395..270a1b541 100644--- a/packages/db/src/clickhouse/migration.ts+++ b/packages/db/src/clickhouse/migration.ts@@ -547,12 +547,14 @@ export async function runClickhouseMigrationCommands(sqls: string[]) {throw e;} finally {- // Clean up event listeners. Cast needed under core's bun-types-only- // `types` array (M8-005 first pulled this file into that compilation via- // code-migrations/): bun-types 1.4.0's `Process.off` override only- // declares its own "memoryPressure" overload, so the plain- // @types/node Signals overload TS picks under packages/db's own tsconfig- // isn't visible there.+ // Clean up event listeners. bun-types 1.4.0's `Process.off` override+ // declares only its own "memoryPressure" overload and it wins over the+ // @types/node Signals overload, so the cast is what keeps this compiling.+ // M8-005 first hit this via `code-migrations/`, which pulled the file into+ // core's bun-types-only compilation; M15-204 moved that directory into this+ // package and the edge is gone, but `bun-types` leads packages/db's own+ // `types` array too — verified 2026-09-11 by dropping the cast and running+ // `tsc --noEmit` here (TS2345 on both lines).const emitter = process as unknown as EventEmitter;emitter.off('SIGTERM', handleSigterm);emitter.off('SIGINT', handleSigint);diff --git a/packages/core/src/code-migrations/1-settings.ts b/packages/db/src/code-migrations/1-settings.tssimilarity index 100%rename from packages/core/src/code-migrations/1-settings.tsrename to packages/db/src/code-migrations/1-settings.tsdiff --git a/packages/core/src/code-migrations/10-add-session-replay.ts b/packages/db/src/code-migrations/10-add-session-replay.tssimilarity index 70%rename from packages/core/src/code-migrations/10-add-session-replay.tsrename to packages/db/src/code-migrations/10-add-session-replay.tsindex 6b2f2bbd3..d3d0e0c0c 100644--- a/packages/core/src/code-migrations/10-add-session-replay.ts+++ b/packages/db/src/code-migrations/10-add-session-replay.ts@@ -1,12 +1,10 @@-import fs from 'node:fs';-import path from 'node:path';-import { TABLE_NAMES } from '@openpanel/db/src/clickhouse/client';+import { TABLE_NAMES } from '../clickhouse/client';import {createTable,modifyTTL,runClickhouseMigrationCommands,-} from '@openpanel/db/src/clickhouse/migration';-import { type CodeMigrationEnv, getIsCluster } from './helpers';+} from '../clickhouse/migration';+import { type CodeMigrationEnv, getIsCluster, writeSqlDump } from './helpers';export async function up(env: CodeMigrationEnv) {const isClustered = getIsCluster(env);@@ -40,18 +38,7 @@ export async function up(env: CodeMigrationEnv) {}),];- fs.writeFileSync(- path.join(import.meta.filename.replace('.ts', '.sql')),- sqls- .map((sql) =>- sql- .trim()- .replace(/;$/, '')- .replace(/\n{2,}/g, '\n')- .concat(';')- )- .join('\n\n---\n\n')- );+ writeSqlDump(import.meta.url, sqls);if (!process.argv.includes('--dry')) {await runClickhouseMigrationCommands(sqls);diff --git a/packages/core/src/code-migrations/11-add-groups.ts b/packages/db/src/code-migrations/11-add-groups.tssimilarity index 71%rename from packages/core/src/code-migrations/11-add-groups.tsrename to packages/db/src/code-migrations/11-add-groups.tsindex 9580fa0bb..073d55b70 100644--- a/packages/core/src/code-migrations/11-add-groups.ts+++ b/packages/db/src/code-migrations/11-add-groups.ts@@ -1,12 +1,10 @@-import fs from 'node:fs';-import path from 'node:path';-import { TABLE_NAMES } from '@openpanel/db/src/clickhouse/client';+import { TABLE_NAMES } from '../clickhouse/client';import {addColumns,createTable,runClickhouseMigrationCommands,-} from '@openpanel/db/src/clickhouse/migration';-import { type CodeMigrationEnv, getIsCluster } from './helpers';+} from '../clickhouse/migration';+import { type CodeMigrationEnv, getIsCluster, writeSqlDump } from './helpers';export async function up(env: CodeMigrationEnv) {const isClustered = getIsCluster(env);@@ -47,18 +45,7 @@ export async function up(env: CodeMigrationEnv) {}),];- fs.writeFileSync(- path.join(import.meta.filename.replace('.ts', '.sql')),- sqls- .map((sql) =>- sql- .trim()- .replace(/;$/, '')- .replace(/\n{2,}/g, '\n')- .concat(';')- )- .join('\n\n---\n\n')- );+ writeSqlDump(import.meta.url, sqls);if (!process.argv.includes('--dry')) {await runClickhouseMigrationCommands(sqls);diff --git a/packages/core/src/code-migrations/12-add-gsc.ts b/packages/db/src/code-migrations/12-add-gsc.tssimilarity index 82%rename from packages/core/src/code-migrations/12-add-gsc.tsrename to packages/db/src/code-migrations/12-add-gsc.tsindex 33afa98c2..ebd679840 100644--- a/packages/core/src/code-migrations/12-add-gsc.ts+++ b/packages/db/src/code-migrations/12-add-gsc.ts@@ -1,10 +1,8 @@-import fs from 'node:fs';-import path from 'node:path';import {createTable,runClickhouseMigrationCommands,-} from '@openpanel/db/src/clickhouse/migration';-import { type CodeMigrationEnv, getIsCluster } from './helpers';+} from '../clickhouse/migration';+import { type CodeMigrationEnv, getIsCluster, writeSqlDump } from './helpers';export async function up(env: CodeMigrationEnv) {const isClustered = getIsCluster(env);@@ -69,18 +67,7 @@ export async function up(env: CodeMigrationEnv) {}),];- fs.writeFileSync(- path.join(__filename.replace('.ts', '.sql')),- sqls- .map((sql) =>- sql- .trim()- .replace(/;$/, '')- .replace(/\n{2,}/g, '\n')- .concat(';')- )- .join('\n\n---\n\n')- );+ writeSqlDump(import.meta.url, sqls);if (!process.argv.includes('--dry')) {await runClickhouseMigrationCommands(sqls);diff --git a/packages/core/src/code-migrations/13-cohorts.ts b/packages/db/src/code-migrations/13-cohorts.tssimilarity index 90%rename from packages/core/src/code-migrations/13-cohorts.tsrename to packages/db/src/code-migrations/13-cohorts.tsindex fc067a3e6..1e2aec655 100644--- a/packages/core/src/code-migrations/13-cohorts.ts+++ b/packages/db/src/code-migrations/13-cohorts.ts@@ -1,14 +1,12 @@-import fs from 'node:fs';-import path from 'node:path';-import { TABLE_NAMES } from '@openpanel/db/src/clickhouse/client';+import { TABLE_NAMES } from '../clickhouse/client';import {createMaterializedView,createTable,dropTable,getExistingTables,runClickhouseMigrationCommands,-} from '@openpanel/db/src/clickhouse/migration';-import { type CodeMigrationEnv, getIsCluster } from './helpers';+} from '../clickhouse/migration';+import { type CodeMigrationEnv, getIsCluster, writeSqlDump } from './helpers';export async function up(env: CodeMigrationEnv) {const replicatedVersion = '1';@@ -120,18 +118,7 @@ export async function up(env: CodeMigrationEnv) {);}- fs.writeFileSync(- path.join(import.meta.filename.replace('.ts', '.sql')),- sqls- .map((sql) =>- sql- .trim()- .replace(/;$/, '')- .replace(/\n{2,}/g, '\n')- .concat(';')- )- .join('\n\n---\n\n')- );+ writeSqlDump(import.meta.url, sqls);if (!process.argv.includes('--dry')) {await runClickhouseMigrationCommands(sqls);diff --git a/packages/core/src/code-migrations/14-profile-event-property-summary-mv.ts b/packages/db/src/code-migrations/14-profile-event-property-summary-mv.tssimilarity index 83%rename from packages/core/src/code-migrations/14-profile-event-property-summary-mv.tsrename to packages/db/src/code-migrations/14-profile-event-property-summary-mv.tsindex d03901e1a..7a735bffb 100644--- a/packages/core/src/code-migrations/14-profile-event-property-summary-mv.ts+++ b/packages/db/src/code-migrations/14-profile-event-property-summary-mv.ts@@ -1,13 +1,11 @@-import fs from 'node:fs';-import path from 'node:path';-import { TABLE_NAMES } from '@openpanel/db/src/clickhouse/client';+import { TABLE_NAMES } from '../clickhouse/client';import {createMaterializedView,dropTable,getExistingTables,runClickhouseMigrationCommands,-} from '@openpanel/db/src/clickhouse/migration';-import { type CodeMigrationEnv, getIsCluster } from './helpers';+} from '../clickhouse/migration';+import { type CodeMigrationEnv, getIsCluster, writeSqlDump } from './helpers';// Per-property breakdown of profile events, used when cohort event criteria// filter on properties.<key> AND as a future fast path for property breakdowns@@ -67,18 +65,7 @@ export async function up(env: CodeMigrationEnv) {);}- fs.writeFileSync(- path.join(import.meta.filename.replace('.ts', '.sql')),- sqls- .map((sql) =>- sql- .trim()- .replace(/;$/, '')- .replace(/\n{2,}/g, '\n')- .concat(';')- )- .join('\n\n---\n\n')- );+ writeSqlDump(import.meta.url, sqls);if (!process.argv.includes('--dry')) {await runClickhouseMigrationCommands(sqls);diff --git a/packages/core/src/code-migrations/15-backfill-cohort-mvs.ts b/packages/db/src/code-migrations/15-backfill-cohort-mvs.tssimilarity index 98%rename from packages/core/src/code-migrations/15-backfill-cohort-mvs.tsrename to packages/db/src/code-migrations/15-backfill-cohort-mvs.tsindex d0d60f39e..82501c71d 100644--- a/packages/core/src/code-migrations/15-backfill-cohort-mvs.ts+++ b/packages/db/src/code-migrations/15-backfill-cohort-mvs.ts@@ -1,8 +1,8 @@-import { TABLE_NAMES } from '@openpanel/db/src/clickhouse/client';+import { TABLE_NAMES } from '../clickhouse/client';import {chMigrationClient,runClickhouseMigrationCommands,-} from '@openpanel/db/src/clickhouse/migration';+} from '../clickhouse/migration';import { type CodeMigrationEnv, getIsCluster } from './helpers';/**diff --git a/packages/core/src/code-migrations/16-restructure-profiles.ts b/packages/db/src/code-migrations/16-restructure-profiles.tssimilarity index 94%rename from packages/core/src/code-migrations/16-restructure-profiles.tsrename to packages/db/src/code-migrations/16-restructure-profiles.tsindex 1043db439..8a67382fc 100644--- a/packages/core/src/code-migrations/16-restructure-profiles.ts+++ b/packages/db/src/code-migrations/16-restructure-profiles.ts@@ -1,12 +1,10 @@-import fs from 'node:fs';-import path from 'node:path';import {chMigrationClient,createTable,renameTable,runClickhouseMigrationCommands,-} from '@openpanel/db/src/clickhouse/migration';-import { type CodeMigrationEnv, getIsCluster } from './helpers';+} from '../clickhouse/migration';+import { type CodeMigrationEnv, getIsCluster, writeSqlDump } from './helpers';// Restructures the `profiles` table so the column names actually mean what// they say:@@ -191,18 +189,7 @@ export async function up(env: CodeMigrationEnv) {sqls.push(...renameTable({ from: NEW_TABLE, to: 'profiles', isClustered }));}- fs.writeFileSync(- path.join(import.meta.filename.replace('.ts', '.sql')),- sqls- .map((sql) =>- sql- .trim()- .replace(/;$/, '')- .replace(/\n{2,}/g, '\n')- .concat(';')- )- .join('\n\n---\n\n')- );+ writeSqlDump(import.meta.url, sqls);if (!process.argv.includes('--dry')) {await runClickhouseMigrationCommands(sqls);diff --git a/packages/core/src/code-migrations/17-migrate-deprecated-ranges.ts b/packages/db/src/code-migrations/17-migrate-deprecated-ranges.tssimilarity index 90%rename from packages/core/src/code-migrations/17-migrate-deprecated-ranges.tsrename to packages/db/src/code-migrations/17-migrate-deprecated-ranges.tsindex 192218b24..d49e40c4e 100644--- a/packages/core/src/code-migrations/17-migrate-deprecated-ranges.ts+++ b/packages/db/src/code-migrations/17-migrate-deprecated-ranges.ts@@ -1,4 +1,4 @@-import { db } from '@openpanel/db';+import { db } from '../prisma-client';import { printBoxMessage } from './helpers';export async function up() {diff --git a/packages/core/src/code-migrations/18-events-profile-id-index.ts b/packages/db/src/code-migrations/18-events-profile-id-index.tssimilarity index 99%rename from packages/core/src/code-migrations/18-events-profile-id-index.tsrename to packages/db/src/code-migrations/18-events-profile-id-index.tsindex 7ba1473d9..015c00543 100644--- a/packages/core/src/code-migrations/18-events-profile-id-index.ts+++ b/packages/db/src/code-migrations/18-events-profile-id-index.ts@@ -1,7 +1,7 @@import {chMigrationClient,runClickhouseMigrationCommands,-} from '@openpanel/db/src/clickhouse/migration';+} from '../clickhouse/migration';import { type CodeMigrationEnv, getIsCluster } from './helpers';/**diff --git a/packages/core/src/code-migrations/19-event-property-values-projections.ts b/packages/db/src/code-migrations/19-event-property-values-projections.tssimilarity index 98%rename from packages/core/src/code-migrations/19-event-property-values-projections.tsrename to packages/db/src/code-migrations/19-event-property-values-projections.tsindex 0b70d0247..9aa48589b 100644--- a/packages/core/src/code-migrations/19-event-property-values-projections.ts+++ b/packages/db/src/code-migrations/19-event-property-values-projections.ts@@ -1,8 +1,8 @@-import { TABLE_NAMES } from '@openpanel/db/src/clickhouse/client';+import { TABLE_NAMES } from '../clickhouse/client';import {chMigrationClient,runClickhouseMigrationCommands,-} from '@openpanel/db/src/clickhouse/migration';+} from '../clickhouse/migration';import { type CodeMigrationEnv, getIsCluster } from './helpers';/**diff --git a/packages/core/src/code-migrations/2-accounts.ts b/packages/db/src/code-migrations/2-accounts.tssimilarity index 98%rename from packages/core/src/code-migrations/2-accounts.tsrename to packages/db/src/code-migrations/2-accounts.tsindex c95b1b261..ac7dc0512 100644--- a/packages/core/src/code-migrations/2-accounts.ts+++ b/packages/db/src/code-migrations/2-accounts.ts@@ -5,7 +5,7 @@ import { fileURLToPath } from 'node:url';const __filename = fileURLToPath(import.meta.url);const __dirname = dirname(__filename);-import { db } from '@openpanel/db';+import { db } from '../prisma-client';import { printBoxMessage } from './helpers';const simpleCsvParser = (csv: string): Record<string, unknown>[] => {diff --git a/packages/core/src/code-migrations/20-cohort-summary-mv-sort-key.ts b/packages/db/src/code-migrations/20-cohort-summary-mv-sort-key.tssimilarity index 90%rename from packages/core/src/code-migrations/20-cohort-summary-mv-sort-key.tsrename to packages/db/src/code-migrations/20-cohort-summary-mv-sort-key.tsindex 5240f0217..03fcc14f2 100644--- a/packages/core/src/code-migrations/20-cohort-summary-mv-sort-key.ts+++ b/packages/db/src/code-migrations/20-cohort-summary-mv-sort-key.ts@@ -1,12 +1,10 @@-import fs from 'node:fs';-import path from 'node:path';-import { TABLE_NAMES } from '@openpanel/db/src/clickhouse/client';+import { TABLE_NAMES } from '../clickhouse/client';import {createMaterializedView,getExistingTables,runClickhouseMigrationCommands,-} from '@openpanel/db/src/clickhouse/migration';-import { type CodeMigrationEnv, getIsCluster } from './helpers';+} from '../clickhouse/migration';+import { type CodeMigrationEnv, getIsCluster, writeSqlDump } from './helpers';/*** Re-key the cohort summary MVs for the queries that actually read them.@@ -127,18 +125,7 @@ export async function up(env: CodeMigrationEnv) {);}- fs.writeFileSync(- path.join(import.meta.filename.replace('.ts', '.sql')),- sqls- .map((sql) =>- sql- .trim()- .replace(/;$/, '')- .replace(/\n{2,}/g, '\n')- .concat(';')- )- .join('\n\n---\n\n')- );+ writeSqlDump(import.meta.url, sqls);if (process.argv.includes('--dry')) {console.log('🔍 DRY RUN — CREATE statements:');diff --git a/packages/core/src/code-migrations/20-invite-project-access-levels.ts b/packages/db/src/code-migrations/20-invite-project-access-levels.tssimilarity index 97%rename from packages/core/src/code-migrations/20-invite-project-access-levels.tsrename to packages/db/src/code-migrations/20-invite-project-access-levels.tsindex 4da28d023..61516eda0 100644--- a/packages/core/src/code-migrations/20-invite-project-access-levels.ts+++ b/packages/db/src/code-migrations/20-invite-project-access-levels.ts@@ -1,5 +1,5 @@-import { db } from '@openpanel/db';-import { zProjectAccessGrant } from '../modules/organization/organization.constants';+import { db } from '../prisma-client';+import { zProjectAccessGrant } from './constants';import { getIsDry, printBoxMessage } from './helpers';/**diff --git a/packages/core/src/code-migrations/21-backfill-cohort-summary-mvs.ts b/packages/db/src/code-migrations/21-backfill-cohort-summary-mvs.tssimilarity index 98%rename from packages/core/src/code-migrations/21-backfill-cohort-summary-mvs.tsrename to packages/db/src/code-migrations/21-backfill-cohort-summary-mvs.tsindex 598c21c52..a6b74f6b8 100644--- a/packages/core/src/code-migrations/21-backfill-cohort-summary-mvs.ts+++ b/packages/db/src/code-migrations/21-backfill-cohort-summary-mvs.ts@@ -1,8 +1,8 @@-import { TABLE_NAMES } from '@openpanel/db/src/clickhouse/client';+import { TABLE_NAMES } from '../clickhouse/client';import {chMigrationClient,runClickhouseMigrationCommands,-} from '@openpanel/db/src/clickhouse/migration';+} from '../clickhouse/migration';import { type CodeMigrationEnv, getIsCluster } from './helpers';/**@@ -53,7 +53,7 @@ import { type CodeMigrationEnv, getIsCluster } from './helpers';* The same file is the supervised tool. Run it directly to control when* the work happens, or to redo part of it:*- * CLICKHOUSE_URL=... jiti packages/core/src/code-migrations/21-backfill-cohort-summary-mvs.ts+ * CLICKHOUSE_URL=... bun packages/db/src/code-migrations/21-backfill-cohort-summary-mvs.ts** Flags:* --dry Print the plan and the first batch; run nothing.diff --git a/packages/core/src/code-migrations/21-wind-down-onboarding-pointer.ts b/packages/db/src/code-migrations/21-wind-down-onboarding-pointer.tssimilarity index 97%rename from packages/core/src/code-migrations/21-wind-down-onboarding-pointer.tsrename to packages/db/src/code-migrations/21-wind-down-onboarding-pointer.tsindex 6e7c5fadb..fb0acec81 100644--- a/packages/core/src/code-migrations/21-wind-down-onboarding-pointer.ts+++ b/packages/db/src/code-migrations/21-wind-down-onboarding-pointer.ts@@ -15,7 +15,7 @@** pnpm with-env jiti ./src/code-migrations/21-wind-down-onboarding-pointer.ts --dry*/-import { db } from '@openpanel/db/src/prisma-client';+import { db } from '../prisma-client';import { getIsDry, printBoxMessage } from './helpers';const RETIRED_POINTER = 'onboarding-trial-ended';diff --git a/packages/core/src/code-migrations/22-add-events-inserted-at.ts b/packages/db/src/code-migrations/22-add-events-inserted-at.tssimilarity index 84%rename from packages/core/src/code-migrations/22-add-events-inserted-at.tsrename to packages/db/src/code-migrations/22-add-events-inserted-at.tsindex 0cd53b7a6..495de1b52 100644--- a/packages/core/src/code-migrations/22-add-events-inserted-at.ts+++ b/packages/db/src/code-migrations/22-add-events-inserted-at.ts@@ -1,10 +1,8 @@-import fs from 'node:fs';-import path from 'node:path';import {addColumns,runClickhouseMigrationCommands,-} from '@openpanel/db/src/clickhouse/migration';-import { type CodeMigrationEnv, getIsCluster } from './helpers';+} from '../clickhouse/migration';+import { type CodeMigrationEnv, getIsCluster, writeSqlDump } from './helpers';export async function up(env: CodeMigrationEnv) {const isClustered = getIsCluster(env);@@ -48,18 +46,7 @@ export async function up(env: CodeMigrationEnv) {materializeIndexSql,];- fs.writeFileSync(- path.join(__filename.replace('.ts', '.sql')),- sqls- .map((sql) =>- sql- .trim()- .replace(/;$/, '')- .replace(/\n{2,}/g, '\n')- .concat(';')- )- .join('\n\n---\n\n')- );+ writeSqlDump(import.meta.url, sqls);if (!process.argv.includes('--dry')) {await runClickhouseMigrationCommands(sqls);diff --git a/packages/core/src/code-migrations/3-init-ch.ts b/packages/db/src/code-migrations/3-init-ch.tssimilarity index 95%rename from packages/core/src/code-migrations/3-init-ch.tsrename to packages/db/src/code-migrations/3-init-ch.tsindex 6ce128922..2b808f0b8 100644--- a/packages/core/src/code-migrations/3-init-ch.ts+++ b/packages/db/src/code-migrations/3-init-ch.ts@@ -1,11 +1,4 @@-import fs from 'node:fs';-import path, { dirname } from 'node:path';-import { fileURLToPath } from 'node:url';--const __filename = fileURLToPath(import.meta.url);-const __dirname = dirname(__filename);--import { formatClickhouseDate } from '@openpanel/db/src/clickhouse/client';+import { formatClickhouseDate } from '../clickhouse/client';import {createDatabase,createMaterializedView,@@ -15,12 +8,13 @@ import {moveDataBetweenTables,renameTable,runClickhouseMigrationCommands,-} from '@openpanel/db/src/clickhouse/migration';+} from '../clickhouse/migration';import {type CodeMigrationEnv,getIsCluster,getIsSelfHosting,printBoxMessage,+ writeSqlDump,} from './helpers';export async function up(env: CodeMigrationEnv) {@@ -345,18 +339,7 @@ export async function up(env: CodeMigrationEnv) {);}- fs.writeFileSync(- path.join(__dirname, '3-init-ch.sql'),- sqls- .map((sql) =>- sql- .trim()- .replace(/;$/, '')- .replace(/\n{2,}/g, '\n')- .concat(';')- )- .join('\n\n---\n\n')- );+ writeSqlDump(import.meta.url, sqls);printBoxMessage('Will start migration for self-hosting setup.', ['This will move all data from the old tables to the new ones.',diff --git a/packages/core/src/code-migrations/4-add-sessions.ts b/packages/db/src/code-migrations/4-add-sessions.tssimilarity index 93%rename from packages/core/src/code-migrations/4-add-sessions.tsrename to packages/db/src/code-migrations/4-add-sessions.tsindex 1f79e66c8..f4b106454 100644--- a/packages/core/src/code-migrations/4-add-sessions.ts+++ b/packages/db/src/code-migrations/4-add-sessions.ts@@ -1,15 +1,10 @@-import fs from 'node:fs';-import path from 'node:path';-import {- formatClickhouseDate,- TABLE_NAMES,-} from '@openpanel/db/src/clickhouse/client';+import { formatClickhouseDate, TABLE_NAMES } from '../clickhouse/client';import {chMigrationClient,createTable,runClickhouseMigrationCommands,-} from '@openpanel/db/src/clickhouse/migration';-import { type CodeMigrationEnv, getIsCluster } from './helpers';+} from '../clickhouse/migration';+import { type CodeMigrationEnv, getIsCluster, writeSqlDump } from './helpers';export async function up(env: CodeMigrationEnv) {const isClustered = getIsCluster(env);@@ -72,18 +67,7 @@ export async function up(env: CodeMigrationEnv) {sqls.push(...(await createOldSessions()));- fs.writeFileSync(- path.join(__filename.replace('.ts', '.sql')),- sqls- .map((sql) =>- sql- .trim()- .replace(/;$/, '')- .replace(/\n{2,}/g, '\n')- .concat(';')- )- .join('\n\n---\n\n')- );+ writeSqlDump(import.meta.url, sqls);if (!process.argv.includes('--dry')) {await runClickhouseMigrationCommands(sqls);diff --git a/packages/core/src/code-migrations/5-add-imports-table.ts b/packages/db/src/code-migrations/5-add-imports-table.tssimilarity index 85%rename from packages/core/src/code-migrations/5-add-imports-table.tsrename to packages/db/src/code-migrations/5-add-imports-table.tsindex d131c715f..56e1a98fc 100644--- a/packages/core/src/code-migrations/5-add-imports-table.ts+++ b/packages/db/src/code-migrations/5-add-imports-table.ts@@ -1,11 +1,9 @@-import fs from 'node:fs';-import path from 'node:path';import {createTable,modifyTTL,runClickhouseMigrationCommands,-} from '@openpanel/db/src/clickhouse/migration';-import { type CodeMigrationEnv, getIsCluster } from './helpers';+} from '../clickhouse/migration';+import { type CodeMigrationEnv, getIsCluster, writeSqlDump } from './helpers';export async function up(env: CodeMigrationEnv) {const isClustered = getIsCluster(env);@@ -70,18 +68,7 @@ export async function up(env: CodeMigrationEnv) {}));- fs.writeFileSync(- path.join(__filename.replace('.ts', '.sql')),- sqls- .map((sql) =>- sql- .trim()- .replace(/;$/, '')- .replace(/\n{2,}/g, '\n')- .concat(';')- )- .join('\n\n---\n\n')- );+ writeSqlDump(import.meta.url, sqls);if (!process.argv.includes('--dry')) {await runClickhouseMigrationCommands(sqls);diff --git a/packages/db/src/code-migrations/6-add-revenue-column.ts b/packages/db/src/code-migrations/6-add-revenue-column.tsnew file mode 100644index 000000000..266cda680--- /dev/null+++ b/packages/db/src/code-migrations/6-add-revenue-column.ts@@ -0,0 +1,23 @@+import {+ addColumns,+ runClickhouseMigrationCommands,+} from '../clickhouse/migration';+import { type CodeMigrationEnv, getIsCluster, writeSqlDump } from './helpers';++export async function up(env: CodeMigrationEnv) {+ const isClustered = getIsCluster(env);++ const sqls: string[] = [+ ...addColumns(+ 'events',+ ['`revenue` UInt64 AFTER `referrer_type`'],+ isClustered+ ),+ ];++ writeSqlDump(import.meta.url, sqls);++ if (!process.argv.includes('--dry')) {+ await runClickhouseMigrationCommands(sqls);+ }+}diff --git a/packages/core/src/code-migrations/7-migrate-events-to-series.ts b/packages/db/src/code-migrations/7-migrate-events-to-series.tssimilarity index 91%rename from packages/core/src/code-migrations/7-migrate-events-to-series.tsrename to packages/db/src/code-migrations/7-migrate-events-to-series.tsindex cd2eb0774..447efa5e5 100644--- a/packages/core/src/code-migrations/7-migrate-events-to-series.ts+++ b/packages/db/src/code-migrations/7-migrate-events-to-series.ts@@ -1,11 +1,6 @@-import { db } from '@openpanel/db';-import { shortId } from '@openpanel/shared';-import type {- IChartEvent,- IChartEventItem,- IChartFormula,-} from '../modules/report/report.constants';-import { printBoxMessage } from './helpers';+import { db } from '../prisma-client';+import type { IChartEvent, IChartEventItem, IChartFormula } from './constants';+import { printBoxMessage, shortId } from './helpers';export async function up() {printBoxMessage('🔄 Migrating Events to Series Format', []);diff --git a/packages/core/src/code-migrations/8-order-keys.ts b/packages/db/src/code-migrations/8-order-keys.tssimilarity index 95%rename from packages/core/src/code-migrations/8-order-keys.tsrename to packages/db/src/code-migrations/8-order-keys.tsindex 1f1c1feb8..72987e4d9 100644--- a/packages/core/src/code-migrations/8-order-keys.ts+++ b/packages/db/src/code-migrations/8-order-keys.ts@@ -1,13 +1,11 @@-import fs from 'node:fs';-import path from 'node:path';import {chMigrationClient,createTable,moveDataBetweenTables,renameTable,runClickhouseMigrationCommands,-} from '@openpanel/db/src/clickhouse/migration';-import { type CodeMigrationEnv, getIsCluster } from './helpers';+} from '../clickhouse/migration';+import { type CodeMigrationEnv, getIsCluster, writeSqlDump } from './helpers';export async function up(env: CodeMigrationEnv) {const isClustered = getIsCluster(env);@@ -283,18 +281,7 @@ export async function up(env: CodeMigrationEnv) {);}- fs.writeFileSync(- path.join(__filename.replace('.ts', '.sql')),- sqls- .map((sql) =>- sql- .trim()- .replace(/;$/, '')- .replace(/\n{2,}/g, '\n')- .concat(';')- )- .join('\n\n---\n\n')- );+ writeSqlDump(import.meta.url, sqls);if (!process.argv.includes('--dry')) {await runClickhouseMigrationCommands(sqls);diff --git a/packages/core/src/code-migrations/9-migrate-options.ts b/packages/db/src/code-migrations/9-migrate-options.tssimilarity index 95%rename from packages/core/src/code-migrations/9-migrate-options.tsrename to packages/db/src/code-migrations/9-migrate-options.tsindex ad3c0a087..67905e5dc 100644--- a/packages/core/src/code-migrations/9-migrate-options.ts+++ b/packages/db/src/code-migrations/9-migrate-options.ts@@ -1,5 +1,5 @@-import { db } from '@openpanel/db';-import type { IReportOptions } from '../modules/report/report.constants';+import { db } from '../prisma-client';+import type { IReportOptions } from './constants';import { printBoxMessage } from './helpers';export async function up() {diff --git a/packages/db/src/code-migrations/constants.ts b/packages/db/src/code-migrations/constants.tsnew file mode 100644index 000000000..2e6154c20--- /dev/null+++ b/packages/db/src/code-migrations/constants.ts@@ -0,0 +1,98 @@+// The vocabulary these one-shot migrations need, COPIED from packages/core+// rather than imported from it.+//+// ADR-022, Carl's rulings: "I'd rather have code-migrations isolated from+// core, so I'd rather duplicate code for this to be isolated." A migration+// reads rows written by a past version of the app, so it must keep describing+// the shape that existed when it was written — an import of the live+// `report.constants.ts` would silently redefine what migration 7 and 9 mean+// the next time someone edits a chart segment.+//+// Sources at the time of the move (2026-09-11), for anyone diffing:+// packages/core/src/modules/report/report.constants.ts (the chart types)+// packages/core/src/modules/organization/organization.constants.ts+// (zProjectAccessGrant)+// These are frozen. Do not re-sync them with core.++import { z } from 'zod';++/**+ * A per-project grant, as `20-invite-project-access-levels.ts` writes it.+ * Copied verbatim; that migration `.parse()`s with it, so it is a value.+ */+export const zProjectAccessGrant = z.object({+ projectId: z.string(),+ level: z.enum(['read', 'write']),+});++/**+ * The chart vocabulary, spelled as plain types rather than as the zod chain+ * core derives them from: `7-migrate-events-to-series.ts` and+ * `9-migrate-options.ts` use them type-only, and copying the schemas would+ * drag the whole operator/segment/filter enum tree along for no runtime gain.+ */+export interface IChartEventFilter {+ id?: string;+ name: string;+ operator:+ | 'is'+ | 'isNot'+ | 'contains'+ | 'doesNotContain'+ | 'startsWith'+ | 'endsWith'+ | 'regex'+ | 'isNull'+ | 'isNotNull'+ | 'gt'+ | 'lt'+ | 'gte'+ | 'lte'+ | 'inCohort'+ | 'notInCohort';+ value: (string | number | boolean | null)[];+ type?: 'string' | 'number' | 'date' | 'datetime' | 'boolean';+ cohortId?: string;+ cohortIds?: string[];+}++export interface IChartEvent {+ id?: string;+ name: string;+ displayName?: string;+ property?: string;+ segment:+ | 'event'+ | 'user'+ | 'session'+ | 'group'+ | 'user_average'+ | 'one_event_per_user'+ | 'property_sum'+ | 'property_average'+ | 'property_max'+ | 'property_min';+ filters: IChartEventFilter[];+}++export interface IChartFormula {+ id?: string;+ type: 'formula';+ formula: string;+ displayName?: string;+ hideSeries?: string[];+}++export type IChartEventItem = (IChartEvent & { type: 'event' }) | IChartFormula;++export type IReportOptions =+ | { type: 'funnel'; funnelGroup?: string; funnelWindow?: number }+ | { type: 'retention'; criteria?: 'on_or_after' | 'on' }+ | {+ type: 'sankey';+ mode: 'between' | 'after' | 'before';+ steps: number;+ exclude: string[];+ include?: string[];+ }+ | { type: 'histogram'; stacked: boolean };diff --git a/packages/db/src/code-migrations/helpers.ts b/packages/db/src/code-migrations/helpers.tsnew file mode 100644index 000000000..40be65a88--- /dev/null+++ b/packages/db/src/code-migrations/helpers.ts@@ -0,0 +1,92 @@+import fs from 'node:fs';+import { fileURLToPath } from 'node:url';++// `shortId` is COPIED from @openpanel/shared's id.ts rather than imported, on+// the same ruling as constants.ts: code-migrations stays isolated, and a+// one-shot script is not worth a dependency edge. This is nanoid/non-secure's+// alphabet and algorithm at the length `shortId()` uses, so the ids these+// migrations write are the same shape the app writes.+const SHORT_ID_ALPHABET =+ 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict';+const SHORT_ID_ALPHABET_SIZE = 64;+const SHORT_ID_LENGTH = 4;++export function shortId(): string {+ let id = '';+ for (let i = 0; i < SHORT_ID_LENGTH; i++) {+ id += SHORT_ID_ALPHABET[Math.floor(Math.random() * SHORT_ID_ALPHABET_SIZE)];+ }+ return id;+}++export function printBoxMessage(title: string, lines: (string | unknown)[]) {+ console.log('┌──┐');+ console.log('│');+ if (title) {+ console.log(`│ ${title}`);+ if (lines.length) {+ console.log('│');+ }+ }+ lines.forEach((line) => {+ console.log(`│ ${line}`);+ });+ console.log('│');+ console.log('└──┘');+}++/**+ * The environment this script needs, read by its shell+ * (`packages/db/scripts/migrate-code.ts`) rather than here, so no migration+ * file reads `process.env` (ADR-022 R7, which travelled with the move).+ */+export interface CodeMigrationEnv {+ clickhouseCluster: boolean;+ selfHosted: boolean;+ databaseUrl: string | undefined;+ clickhouseUrl: string | undefined;+}++export function getIsCluster(env: CodeMigrationEnv) {+ return process.argv.includes('--cluster') || env.clickhouseCluster;+}++export function getIsSelfHosting(env: CodeMigrationEnv) {+ return env.selfHosted;+}++export function getIsDry() {+ return process.argv.includes('--dry');+}++export function getShouldIgnoreRecord() {+ return process.argv.includes('--no-record');+}++const TRAILING_SEMICOLON = /;$/;+const BLANK_LINE_RUN = /\n{2,}/g;+const STATEMENT_SEPARATOR = '\n\n---\n\n';++/**+ * Dump a migration's ClickHouse statements beside it as `<migration>.sql`, so+ * an operator can read what a run will do. The dumps are build artifacts, not+ * tracked files.+ *+ * Pass `import.meta.url`: it is the one file-location technique that needs+ * neither Bun's ESM `__filename` polyfill nor Node >= 20.11's+ * `import.meta.filename`, both of which this directory used to mix.+ */+export function writeSqlDump(migrationUrl: string, sqls: string[]) {+ fs.writeFileSync(+ fileURLToPath(migrationUrl).replace('.ts', '.sql'),+ sqls+ .map((sql) =>+ sql+ .trim()+ .replace(TRAILING_SEMICOLON, '')+ .replace(BLANK_LINE_RUN, '\n')+ .concat(';')+ )+ .join(STATEMENT_SEPARATOR)+ );+}diff --git a/packages/core/src/code-migrations/migrate.ts b/packages/db/src/code-migrations/migrate.tssimilarity index 84%rename from packages/core/src/code-migrations/migrate.tsrename to packages/db/src/code-migrations/migrate.tsindex df67b1019..533a009b4 100644--- a/packages/core/src/code-migrations/migrate.ts+++ b/packages/db/src/code-migrations/migrate.ts@@ -1,11 +1,7 @@import fs from 'node:fs';import path, { dirname } from 'node:path';import { fileURLToPath } from 'node:url';--const __filename = fileURLToPath(import.meta.url);-const __dirname = dirname(__filename);--import { db } from '@openpanel/db';+import { db } from '../prisma-client';import {type CodeMigrationEnv,getIsCluster,@@ -15,13 +11,15 @@ import {printBoxMessage,} from './helpers';+// The numbered migration files live beside this runner.+const MIGRATIONS_DIR = dirname(fileURLToPath(import.meta.url));+export async function runCodeMigrations(env: CodeMigrationEnv) {const args = process.argv.slice(2);const migration = args.filter((arg) => !arg.startsWith('--'))[0];- const migrationsDir = path.join(__dirname, '..', 'code-migrations');const migrations = fs- .readdirSync(migrationsDir)+ .readdirSync(MIGRATIONS_DIR).filter((file) => {const version = file.split('-')[0];return (@@ -74,7 +72,7 @@ export async function runCodeMigrations(env: CodeMigrationEnv) {}if (migration) {- await runMigration(migrationsDir, migration, env);+ await runMigration(migration, env);} else {for (const file of migrations) {if (finishedMigrations.some((migration) => migration.name === file)) {@@ -82,7 +80,7 @@ export async function runCodeMigrations(env: CodeMigrationEnv) {continue;}- await runMigration(migrationsDir, file, env);+ await runMigration(file, env);}}@@ -90,14 +88,10 @@ export async function runCodeMigrations(env: CodeMigrationEnv) {process.exit(0);}-async function runMigration(- migrationsDir: string,- file: string,- env: CodeMigrationEnv-) {+async function runMigration(file: string, env: CodeMigrationEnv) {printBoxMessage('⚡️ Running Migration ⚡️ ', [`${file}`]);try {- const migration = await import(path.join(migrationsDir, file));+ const migration = await import(path.join(MIGRATIONS_DIR, file));await migration.up(env);if (!(getIsDry() || getShouldIgnoreRecord())) {await db.codeMigration.upsert({