Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

2.5 KB
Raw Blame
import {
  addColumns,
  runClickhouseMigrationCommands,
} from '../clickhouse/migration';
import { type CodeMigrationEnv, getIsCluster, writeSqlDump } from './helpers';

export async function up(env: CodeMigrationEnv) {
  const isClustered = getIsCluster(env);

  // `inserted_at` is the ingestion (CH-insert) time, used as the cursor for the
  // object-store export job. New rows set it explicitly at insert time (see
  // createEvent / moveImportsToProduction). The DEFAULT is `created_at` — NOT
  // `now()` — on purpose: a DEFAULT now() on a column added to an existing
  // MergeTree is evaluated lazily at read time for pre-migration parts, so old
  // rows would read as "just inserted" on every scan and never settle. Defaulting
  // to the existing `created_at` column is deterministic and needs no part
  // rewrite, so historical rows keep a stable, in-the-past inserted_at.
  // `inserted_at` is not part of the table's ORDER BY, so the windowed export
  // query (WHERE inserted_at > cursor) can't use the primary index. A minmax
  // skip index lets ClickHouse skip granules whose inserted_at range is entirely
  // below the cursor. It only needs to exist on the local MergeTree (the
  // `_replicated` table when clustered); the distributed table has no data.
  const indexName = 'idx_inserted_at';
  const indexExpr = `ADD INDEX IF NOT EXISTS ${indexName} inserted_at TYPE minmax GRANULARITY 1`;
  const indexSql = isClustered
    ? `ALTER TABLE events_replicated ON CLUSTER '{cluster}' ${indexExpr}`
    : `ALTER TABLE events ${indexExpr}`;

  // ADD INDEX is metadata-only: it covers newly written parts, so without this
  // the export scan keeps reading every historical granule until unrelated
  // merges happen to rewrite them. MATERIALIZE INDEX submits an async mutation
  // that backfills the existing parts (same pattern as
  // 18-events-profile-id-index.ts); it returns immediately and the mutation
  // progresses in the background.
  const materializeIndexSql = isClustered
    ? `ALTER TABLE events_replicated ON CLUSTER '{cluster}' MATERIALIZE INDEX ${indexName}`
    : `ALTER TABLE events MATERIALIZE INDEX ${indexName}`;

  const sqls: string[] = [
    ...addColumns(
      'events',
      ['`inserted_at` DateTime64(3) DEFAULT created_at AFTER `imported_at`'],
      isClustered
    ),
    indexSql,
    materializeIndexSql,
  ];

  writeSqlDump(import.meta.url, sqls);

  if (!process.argv.includes('--dry')) {
    await runClickhouseMigrationCommands(sqls);
  }
}