Files
zeekay 10e6fec4c0 debrand: the brand token is INSIGHTS_, and a wrong one stops the container
The rule table mapped HOG_ -> FN_ ahead of any POSTHOG rule, so POSTHOG_FOO
split into POST + HOG_ and came out POSTFN_FOO. Nothing sets that. The live
pods set INSIGHTS_SKIP_MIGRATION_CHECKS, which bin/migrate-check reads on the
container entrypoint, so the misspelling does not degrade a feature -- it runs
migrate --check against a database whose history this tree does not share and
the container never starts.

Both replacements are measured against the shipped tree rather than chosen:
every HOG_ name with a counterpart there is spelled INSIGHTS_ (HOG_QL ->
INSIGHTS_QL, HOG_FUNCTION_TEMPLATES -> INSIGHTS_FUNCTION_TEMPLATES, 12 of 12
determinate cases), and FN_ appears in it nowhere. Upstream carries 95 HOG_
tokens and zero FN_ ones, so repairing FN_ back out is exactly the inverse of
the bad rule and cannot collide with an upstream name.

The rule is fixed for the next convergence, and the tree it already produced is
repaired in the same commit -- the codemod is idempotent against its own output,
so it cannot undo this itself.
2026-08-01 17:42:28 -07:00

73 lines
2.8 KiB
JavaScript
Executable File

#!/usr/bin/env node
const { Client } = require('pg')
const fs = require('fs')
const path = require('path')
async function fetchInsightsKey() {
const client = new Client({
host: process.env.PGHOST || 'localhost',
port: parseInt(process.env.PGPORT || '5432'),
database: process.env.PGDATABASE || 'insights',
user: process.env.PGUSER || 'insights',
password: process.env.PGPASSWORD || 'insights',
connectionTimeoutMillis: 3000, // A brief timeout to avoid waiting forever
})
let teamId, teamApiToken, teamCreatedAt
try {
await client.connect()
// Query for the latest team's API token (or you can specify a team_id)
const result = await client.query(
process.env.DEMO_TEAM_ID
? 'SELECT id, api_token, created_at FROM insights_team WHERE id = $1'
: 'SELECT id, api_token, created_at FROM insights_team ORDER BY created_at DESC LIMIT 1',
process.env.DEMO_TEAM_ID ? [process.env.DEMO_TEAM_ID] : []
)
if (result.rows.length === 0) {
console.warn(
`⚠ ${process.env.DEMO_TEAM_ID ? `No team found with ID ${process.env.DEMO_TEAM_ID}` : 'No team found'}`
)
console.warn(' Continuing without Insights API key...\n')
return
}
teamId = result.rows[0].id
teamApiToken = result.rows[0].api_token
teamCreatedAt = result.rows[0].created_at
} catch (error) {
console.warn('⚠ Error fetching Insights key:', error.message)
console.warn(' Continuing without Insights API key...\n')
return
} finally {
await client.end()
}
const envPath = path.join(__dirname, '..', '.env.local')
// Check if .env.local already exists with a valid key
if (fs.existsSync(envPath) && !process.env.FORCE_FETCH_KEY) {
const envContent = fs.readFileSync(envPath, 'utf-8')
const keyMatch = envContent.match(/NEXT_PUBLIC_INSIGHTS_KEY=(.+) # (.+)/)
if (keyMatch && keyMatch[1] && keyMatch[1].trim() && keyMatch[2].trim() === teamCreatedAt.toISOString()) {
console.info(
`✓ Insights API key already exists in .env.local for team created at ${teamCreatedAt.toISOString()}`
)
console.info(' `rm tools/hedgebox-dummy/.env.local` or `export FORCE_FETCH_KEY=1` to fetch anew\n')
return
}
}
// Write to .env.local file
fs.writeFileSync(
path.join(__dirname, '..', '.env.local'),
`NEXT_PUBLIC_INSIGHTS_KEY=${teamApiToken} # ${teamCreatedAt.toISOString()}\n`
)
console.info(`✓ Insights API key fetched and written to .env.local`)
console.info(` Team ID: ${teamId}`)
console.info(` API token: ${teamApiToken}\n`)
}
fetchInsightsKey()