Composes Scribase's own shipped APIs (the `scribase` SDK's imports/migrations/ database/environments resources, plus the GoTrue admin API every self-hosted Supabase migration tool already uses) rather than reinventing a dump format: - supabase: wraps the real POST /v1/imports one-call migration (Postgres, auth.users with hashes, storage). - neon: pg_dump schema+data as plain SQL, submitted through POST .../migrations (environments.connection() is internal-network-only — verified live — so this is the only path that actually reaches a Scribase env from outside); auto-generates owner-based RLS so the real migration lint's BLOCK on RLS-less public tables is satisfied without ever opening a table to every user; branches land in branch_<name> schemas. - clerk: exports users via the real Backend API, creates them via the destination's GoTrue admin API with a one-time password + reset flag (Clerk never exposes a portable hash on any plan). - resend: exports sending domains + broadcasts (as templates) to a portable JSON bundle; honest about Scribase having no write endpoint for either yet. Verified live end-to-end against a real local Supabase-shaped Postgres (Docker, GoTrue-shaped auth.users with a real bcrypt hash) and a real Scribase project (ereboslab/scribase-migrate-verify-internal): schema+RLS applied with 0 lint blocks, data row counts matched via a real HTTP query (2/2 profiles, 3/3 notes), and a real password-grant login returned a real access_token for the migrated user. Full artifacts in test-fixtures/verification-run/. docs/migrate-page.md is a ready-to-paste scribase.com/migrate page.
86 lines
3.6 KiB
JavaScript
86 lines
3.6 KiB
JavaScript
/**
|
|
* Creates (idempotently) a real Scribase project + environment to migrate
|
|
* into. Used both by `scribase-migrate --create-project` for a first-time
|
|
* destination and by this repo's own local verification run.
|
|
*/
|
|
|
|
/**
|
|
* @param {import('scribase').ScribaseClient} client
|
|
* @param {{ organizationId?: string, projectId: string, environmentId?: string, region?: string, postgresMajor?: number, dataMode?: 'schema_only'|'sanitized'|'snapshot', kind?: 'preview'|'staging'|'production', ttlHours?: number }} opts
|
|
*/
|
|
export async function ensureProject(client, opts) {
|
|
const organizationId = opts.organizationId ?? (await firstOrganization(client));
|
|
const projectId = opts.projectId;
|
|
const environmentId = opts.environmentId ?? 'production';
|
|
const region = opts.region ?? 'us-east';
|
|
const postgresMajor = opts.postgresMajor ?? 17;
|
|
const dataMode = opts.dataMode ?? 'snapshot';
|
|
// `production` environments cannot be deleted once created (verified
|
|
// live) — callers doing throwaway verification should pass
|
|
// `kind: 'preview'` explicitly.
|
|
const kind = opts.kind ?? 'production';
|
|
|
|
await client.projects.put(organizationId, projectId, { region, postgres_major: postgresMajor });
|
|
|
|
let environment;
|
|
try {
|
|
environment = await client.environments.get({ organizationId, projectId, environmentId });
|
|
} catch {
|
|
environment = null;
|
|
}
|
|
|
|
if (!environment) {
|
|
const accepted = await client.environments.create({
|
|
organization_id: organizationId,
|
|
project_id: projectId,
|
|
environment_id: environmentId,
|
|
kind,
|
|
data_mode: dataMode,
|
|
region,
|
|
postgres_major: postgresMajor,
|
|
...(opts.ttlHours ? { ttl_hours: opts.ttlHours } : {}),
|
|
});
|
|
await client.operations.wait(accepted.operation.id, organizationId, { timeoutMs: 10 * 60_000 });
|
|
environment = await waitUntilReady(client, { organizationId, projectId, environmentId });
|
|
} else {
|
|
environment = await waitUntilReady(client, { organizationId, projectId, environmentId });
|
|
}
|
|
|
|
const connection = await client.environments.connection({ organizationId, projectId, environmentId });
|
|
const apiKeys = await client.environments.apiKeys({ organizationId, projectId, environmentId });
|
|
|
|
return { organizationId, projectId, environmentId, environment, connection, apiKeys };
|
|
}
|
|
|
|
async function waitUntilReady(client, ref, { timeoutMs = 5 * 60_000, intervalMs = 2000 } = {}) {
|
|
const deadline = Date.now() + timeoutMs;
|
|
for (;;) {
|
|
const env = await client.environments.get(ref);
|
|
if (env.phase === 'ready') return env;
|
|
if (env.phase === 'failed_recoverable' || env.phase === 'deleted') {
|
|
throw new Error(`environment ${ref.environmentId} is ${env.phase}: ${env.last_error?.message ?? 'no detail'}`);
|
|
}
|
|
if (Date.now() > deadline) {
|
|
throw new Error(`environment ${ref.environmentId} did not become ready within ${timeoutMs}ms (phase=${env.phase})`);
|
|
}
|
|
await new Promise((resolve) => setTimeout(resolve, intervalMs));
|
|
}
|
|
}
|
|
|
|
async function firstOrganization(client) {
|
|
const orgs = await client.organizations.list();
|
|
if (!orgs.length) {
|
|
throw new Error('this Scribase account belongs to no organization; create one first (console.scribase.com)');
|
|
}
|
|
return orgs[0].organization_id;
|
|
}
|
|
|
|
/** Tears a verification project down (environment delete; the project spec can stay). */
|
|
export async function deleteEnvironment(client, { organizationId, projectId, environmentId }) {
|
|
const accepted = await client.environments.delete({ organizationId, projectId, environmentId });
|
|
await client.operations.wait(accepted.operation.id, organizationId, {
|
|
timeoutMs: 5 * 60_000,
|
|
throwOnFailure: false,
|
|
});
|
|
}
|