Open History to see the last commit
1import { randomUUID } from 'node:crypto'
2import { open, readFile, writeFile } from 'node:fs/promises'
3import { createNeonClient } from '@neon/sdk'
4import pg from 'pg'
5import { migrate } from '../dist/migrations.js'
6import { beginRecovery, configureReclamation, finishRecovery, inspectInstallation, verifyRecovery } from '../dist/operations.js'
7import { createS3ChunkStorage } from '../dist/s3.js'
8
9// Test setup only. Refuse a serving deployment or an unrecorded branch. The
10// ordinary example has no route that enables or changes collection.
11const env = process.env, stateFile = env.NEON_TEST_STATE_FILE ?? '.neon-test-state.json'
12const functionFile = env.NEON_TEST_FUNCTION_ENV_FILE ?? '.env.neon.function'
13const state = JSON.parse(await readFile(stateFile, 'utf8'))
14if (!env.NEON_API_KEY || !env.TEST_DATABASE_URL_UNPOOLED || state.projectId !== env.NEON_PROJECT_ID || state.branchId !== env.NEON_TEST_BRANCH_ID || !state.name.startsWith('ds-test-')) throw new Error('Recorded isolated Neon environment required')
15const neon = createNeonClient({ apiKey: env.NEON_API_KEY, throwOnError: true })
16const branch = await neon.branches.get({ projectId: state.projectId, branchId: state.branchId })
17if (branch.default || branch.protected || branch.name !== state.name || (await neon.functions.list({ projectId: state.projectId, branchId: state.branchId }).all()).length
18 || (await neon.triggers.list({ projectId: state.projectId, branchId: state.branchId })).length) throw new Error('Reclamation test setup requires a fresh stopped branch without Functions or triggers')
19const schema = `ds_test_${randomUUID().replaceAll('-', '')}`
20const handle = await open(functionFile, 'wx', 0o600)
21try { await handle.writeFile(`TEST_FUNCTION_BRANCH_ID=${state.branchId}\nTEST_FUNCTION_SCHEMA=${schema}\n`); await handle.sync() }
22finally { await handle.close() }
23const pool = new pg.Pool({ connectionString: env.TEST_DATABASE_URL_UNPOOLED, max: 1, connectionTimeoutMillis: 20_000 })
24const objects = createS3ChunkStorage({ endpoint: env.AWS_ENDPOINT_URL_S3, region: env.AWS_REGION, bucket: env.NEON_TEST_BUCKET })
25try {
26 await migrate(pool, { schema, environmentId: state.branchId, storageIdentity: objects.identity })
27 const before = await inspectInstallation(pool, { schema }), operationId = randomUUID()
28 const operation = { schema, operationId, expectedInstallationId: before.id, expectedServingEpoch: before.serving_epoch,
29 environmentId: state.branchId, storageIdentity: objects.identity }
30 await writeFile(`${stateFile}.collection.json`, JSON.stringify(operation), { mode: 0o600, flag: 'wx' })
31 const { token } = await beginRecovery(pool, operation)
32 await configureReclamation(pool, { schema, token, enabled: true })
33 await verifyRecovery(pool, { schema, token, objects })
34 await finishRecovery(pool, { schema, token })
35 console.log('Fresh isolated schema ready: collection enabled; historical object recovery is provider-owned.')
36} finally { objects.close(); await pool.end() }