ktx/examples/postgres-historic/scripts/smoke.sh

137 lines
4.9 KiB
Bash
Raw Permalink Normal View History

2026-05-10 23:12:26 +02:00
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
EXAMPLE_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
2026-05-10 23:51:24 +02:00
KTX_ROOT="$(cd "$EXAMPLE_DIR/../.." && pwd)"
2026-05-10 23:12:26 +02:00
COMPOSE_FILE="$EXAMPLE_DIR/docker-compose.yml"
2026-05-10 23:51:24 +02:00
PROJECT_PARENT="${KTX_POSTGRES_HISTORIC_PROJECT_PARENT:-$(mktemp -d)}"
PROJECT_DIR="$PROJECT_PARENT/postgres-historic-ktx"
KTX_BIN="$KTX_ROOT/packages/cli/dist/bin.js"
2026-05-11 12:51:24 +02:00
export KTX_RUNTIME_ROOT="$PROJECT_PARENT/managed-runtime"
unset KTX_DAEMON_URL
unset KTX_SQL_ANALYSIS_URL
2026-05-10 23:12:26 +02:00
cleanup() {
2026-05-11 12:51:24 +02:00
if [[ -f "$KTX_BIN" ]]; then
node "$KTX_BIN" runtime stop >/dev/null 2>&1 || true
2026-05-10 23:12:26 +02:00
fi
2026-05-10 23:51:24 +02:00
if [[ "${KTX_POSTGRES_HISTORIC_KEEP_DOCKER:-0}" != "1" ]]; then
2026-05-10 23:12:26 +02:00
docker compose -f "$COMPOSE_FILE" down -v >/dev/null 2>&1 || true
fi
}
trap cleanup EXIT
latest_manifest() {
find "$PROJECT_DIR/raw-sources/warehouse/historic-sql" -name manifest.json | sort | tail -n 1
}
assert_manifest() {
local manifest_path="$1"
local expected_first_run="$2"
node - "$manifest_path" "$expected_first_run" <<'NODE'
const { readFileSync } = require('node:fs');
const manifestPath = process.argv[2];
const expectedFirstRun = process.argv[3] === 'true';
const manifest = JSON.parse(readFileSync(manifestPath, 'utf8'));
if (manifest.dialect !== 'postgres') throw new Error(`Expected dialect postgres, got ${manifest.dialect}`);
if (manifest.degraded !== true) throw new Error('Expected degraded:true for Postgres PGSS v1');
if (manifest.baselineFirstRun !== expectedFirstRun) {
throw new Error(`Expected baselineFirstRun:${expectedFirstRun}, got ${manifest.baselineFirstRun}`);
}
if (!manifest.pgServerVersion) throw new Error('Expected pgServerVersion');
if (!manifest.statsResetAt) throw new Error('Expected statsResetAt');
if (!Array.isArray(manifest.templates) || manifest.templates.length === 0) {
throw new Error('Expected at least one staged historic-SQL template');
}
NODE
}
run_historic_stage_only() {
local job_id="$1"
2026-05-10 23:51:24 +02:00
node - "$KTX_ROOT" "$PROJECT_DIR" "$job_id" <<'NODE'
2026-05-10 23:12:26 +02:00
const { join } = await import('node:path');
2026-05-10 23:51:24 +02:00
const ktxRoot = process.argv[2];
2026-05-10 23:12:26 +02:00
const projectDir = process.argv[3];
const jobId = process.argv[4];
2026-05-10 23:51:24 +02:00
const { loadKtxProject } = await import(join(ktxRoot, 'packages/context/dist/project/index.js'));
const { runLocalStageOnlyIngest } = await import(join(ktxRoot, 'packages/context/dist/ingest/index.js'));
const { createKtxCliLocalIngestAdapters } = await import(join(ktxRoot, 'packages/cli/dist/local-adapters.js'));
2026-05-11 12:51:24 +02:00
const { getKtxCliPackageInfo } = await import(join(ktxRoot, 'packages/cli/dist/index.js'));
2026-05-10 23:12:26 +02:00
2026-05-10 23:51:24 +02:00
const project = await loadKtxProject({ projectDir });
2026-05-11 12:51:24 +02:00
const cliVersion = getKtxCliPackageInfo().version;
const managedRuntimeIo = { stdout: process.stdout, stderr: process.stderr };
const adapters = createKtxCliLocalIngestAdapters(project, {
historicSqlConnectionId: 'warehouse',
managedDaemon: {
cliVersion,
installPolicy: 'auto',
io: managedRuntimeIo,
},
});
2026-05-10 23:12:26 +02:00
const adapter = adapters.find((candidate) => candidate.source === 'historic-sql');
if (!adapter) throw new Error('historic-sql adapter was not registered for local run');
const record = await runLocalStageOnlyIngest({
project,
adapters,
adapter: 'historic-sql',
connectionId: 'warehouse',
trigger: 'manual_resync',
jobId,
});
await adapter.onPullSucceeded?.({
connectionId: 'warehouse',
sourceKey: 'historic-sql',
syncId: record.syncId,
trigger: 'manual_resync',
completedAt: new Date(record.completedAt),
2026-05-10 23:51:24 +02:00
stagedDir: join(project.projectDir, '.ktx/cache/local-ingest', jobId, 'staged'),
2026-05-10 23:12:26 +02:00
});
console.log(record.syncId);
NODE
}
2026-05-10 23:51:24 +02:00
cd "$KTX_ROOT"
pnpm --filter @ktx/context run build
pnpm --filter @ktx/cli run build
2026-05-10 23:12:26 +02:00
docker compose -f "$COMPOSE_FILE" up -d --wait
"$EXAMPLE_DIR/scripts/generate-workload.sh" base
2026-05-10 23:51:24 +02:00
export WAREHOUSE_DATABASE_URL="${WAREHOUSE_DATABASE_URL:-postgresql://ktx_reader:ktx_reader@127.0.0.1:55432/analytics}" # pragma: allowlist secret
node "$KTX_BIN" --project-dir "$PROJECT_DIR" setup \
2026-05-10 23:12:26 +02:00
--new \
--skip-agents \
--skip-llm \
--skip-embeddings \
--skip-sources \
--database postgres \
--new-database-connection-id warehouse \
--database-url env:WAREHOUSE_DATABASE_URL \
--database-schema public \
--enable-historic-sql \
--historic-sql-min-calls 2 \
--yes \
--no-input
run_historic_stage_only "historic-first-$$"
FIRST_MANIFEST="$(latest_manifest)"
assert_manifest "$FIRST_MANIFEST" true
"$EXAMPLE_DIR/scripts/generate-workload.sh" extra
run_historic_stage_only "historic-second-$$"
SECOND_MANIFEST="$(latest_manifest)"
assert_manifest "$SECOND_MANIFEST" false
docker compose -f "$COMPOSE_FILE" exec -T postgres \
psql -U postgres -d analytics -v ON_ERROR_STOP=1 -c "SELECT pg_stat_statements_reset();" >/dev/null
"$EXAMPLE_DIR/scripts/generate-workload.sh" extra
run_historic_stage_only "historic-reset-$$"
RESET_MANIFEST="$(latest_manifest)"
assert_manifest "$RESET_MANIFEST" true
echo "Postgres historic SQL smoke passed"
echo "Project dir: $PROJECT_DIR"