Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 95 additions & 25 deletions scripts/backfill_manifest_file_sizes.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,16 @@
* bun scripts/backfill_manifest_file_sizes.mjs --all --apply
*/

import { mkdirSync, writeFileSync } from 'node:fs'
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import { dirname, resolve } from 'node:path'
import process from 'node:process'
import { fileURLToPath } from 'node:url'
import { S3Client } from '@bradenmacdonald/s3-lite-client'
import { config } from 'dotenv'
import { parse } from 'dotenv'
import pg from 'pg'

const __dirname = dirname(fileURLToPath(import.meta.url))

for (const envPath of [
'../.env',
'../.env.local',
'../internal/cloudflare/.env.prod',
'../internal/cloudflare/.env.local',
]) {
config({ path: resolve(__dirname, envPath), override: true, quiet: true })
}

const DB_URL_ENV_KEYS = [
'MAIN_SUPABASE_DB_URL',
'DATABASE_URL',
'POSTGRES_URL',
'SUPABASE_DB_URL',
'SUPABASE_DB_DIRECT_URL',
'DIRECT_URL',
]

function hasFlag(name) {
return process.argv.includes(name)
}
Expand All @@ -64,6 +46,55 @@ function getArgValue(name) {
return undefined
}

function getTarget() {
if (hasFlag('--help') || hasFlag('-h'))
return 'prod'
const target = getArgValue('--target') ?? (hasFlag('--local') ? 'local' : 'prod')
if (target !== 'prod' && target !== 'local')
throw new Error('--target must be "prod" or "local"')
return target
}

const target = getTarget()
const sharedEnvPaths = [
'../.env',
]
const targetEnvPaths = target === 'prod'
? [
'../internal/cloudflare/.env.prod',
]
: [
'../.env.local',
'../internal/cloudflare/.env.local',
]

function loadEnvFiles(envPaths) {
const loaded = {}
for (const envPath of envPaths) {
const resolvedPath = resolve(__dirname, envPath)
if (!existsSync(resolvedPath))
continue
Object.assign(loaded, parse(readFileSync(resolvedPath)))
}
return loaded
}

const targetEnv = loadEnvFiles(targetEnvPaths)
const runtimeEnv = loadEnvFiles([...sharedEnvPaths, ...targetEnvPaths])

for (const [key, value] of Object.entries(runtimeEnv)) {
process.env[key] = value
}

const DB_URL_ENV_KEYS = [
'MAIN_SUPABASE_DB_URL',
'DATABASE_URL',
'POSTGRES_URL',
'SUPABASE_DB_URL',
'SUPABASE_DB_DIRECT_URL',
'DIRECT_URL',
]
Comment thread
coderabbitai[bot] marked this conversation as resolved.

function getNumberArg(name, fallback) {
const value = getArgValue(name)
if (value === undefined)
Expand All @@ -74,13 +105,46 @@ function getNumberArg(name, fallback) {
return parsed
}

function getDatabaseUrl() {
function getDatabaseUrl(databaseEnv) {
for (const key of DB_URL_ENV_KEYS) {
const value = process.env[key]
const value = databaseEnv[key]
if (value)
return value
}
throw new Error(`Missing Postgres URL. Set one of: ${DB_URL_ENV_KEYS.join(', ')}`)
throw new Error(`Missing Postgres URL in ${targetEnvPaths.join(', ')}. Set one of: ${DB_URL_ENV_KEYS.join(', ')}`)
}

function isLocalDatabaseUrl(databaseUrl) {
try {
let { hostname } = new URL(databaseUrl)
if (hostname.startsWith('[') && hostname.endsWith(']'))
hostname = hostname.slice(1, -1)
return ['127.0.0.1', 'localhost', '::1'].includes(hostname)
}
catch {
return databaseUrl.includes('127.0.0.1')
|| databaseUrl.includes('localhost')
|| databaseUrl.includes('::1')
|| databaseUrl.includes('[::1]')
}
}

function getSafeDatabaseUrl() {
const databaseUrl = getDatabaseUrl(targetEnv)
if (target === 'prod' && isLocalDatabaseUrl(databaseUrl)) {
throw new Error('Refusing to use a local Postgres URL for the default prod target. Pass --target=local only when you intentionally want local.')
}
return databaseUrl
}

function describeDatabaseUrl(databaseUrl) {
try {
const { host } = new URL(databaseUrl)
return host
}
catch {
return 'unknown host'
}
}

function getRequiredEnv(name) {
Expand Down Expand Up @@ -257,6 +321,8 @@ Options:
--batch-size DB page size. Default: 500.
--concurrency Storage HEAD/RANGE concurrency. Default: 20.
--include-deleted Include deleted bundles.
--target prod|local Env target. Default: prod.
--local Alias for --target=local.
--verbose Print every checked row.
`)
return
Expand All @@ -278,9 +344,12 @@ Options:
if (appVersionIdRaw && (!Number.isFinite(appVersionId) || appVersionId <= 0))
throw new Error('--app-version-id must be a positive integer')

const databaseUrl = getSafeDatabaseUrl()
console.log(`Using ${target} database target: ${describeDatabaseUrl(databaseUrl)}`)

const pool = new pg.Pool({
connectionString: getDatabaseUrl(),
ssl: { rejectUnauthorized: false },
connectionString: databaseUrl,
ssl: target === 'prod' ? { rejectUnauthorized: false } : false,
})
const s3 = new S3Client({
accessKey: getRequiredEnv('S3_ACCESS_KEY_ID'),
Expand All @@ -301,6 +370,7 @@ Options:
includeDeleted,
missingSize: 0,
scannedAt: new Date().toISOString(),
target,
unchanged: 0,
}

Expand Down
12 changes: 9 additions & 3 deletions supabase/functions/_backend/triggers/on_manifest_create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ const SIZE_RETRY_DELAY_MS = 500
const MANIFEST_UPDATE_RETRY_ATTEMPTS = 3
const MANIFEST_UPDATE_RETRY_DELAY_MS = 300

function getQueueLogMetadata(c: Context) {
interface QueueLogMetadata {
queueName: string | null
queueMsgId: string | null
queueReadCount: string | null
cfId: string | null
}

function getQueueLogMetadata(c: Context): QueueLogMetadata {
return {
queueName: c.req.header('x-capgo-queue-name') ?? null,
queueMsgId: c.req.header('x-capgo-queue-msg-id') ?? null,
Expand Down Expand Up @@ -79,8 +86,7 @@ async function runManifestUpdateWithRetry(
}
}

async function updateManifestSize(c: Context, record: Database['public']['Tables']['manifest']['Row']) {
const queue = getQueueLogMetadata(c)
export async function updateManifestSize(c: Context, record: Database['public']['Tables']['manifest']['Row'], queue = getQueueLogMetadata(c)) {
if (!record.s3_path) {
cloudlog({ requestId: c.get('requestId'), message: 'No s3 path', id: record.id, app_version_id: record.app_version_id, file_name: record.file_name, queue })
throw simpleError('no_s3_path', 'No s3 path', { record })
Expand Down
Loading
Loading