feat(supabase): sheet asset Storage bucket + read policy through the sheet, shim storage tables, seeds in the PGlite harness, upload script
This commit is contained in:
1 parent
12f0b21fdf
commit
f4682cdba5
10 files changed
+294
-7
No files matched your search
@@ -16,7 +16,8 @@
|
||||
"test": "vitest",
|
||||
"test:run": "vitest run",
|
||||
"lint": "eslint . --max-warnings 0",
|
||||
"build": "tsc -p tsconfig.json --noEmit"
|
||||
"build": "tsc -p tsconfig.json --noEmit",
|
||||
"assets:upload": "node scripts/upload-sheet-assets.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"@faunosteo/domain": "workspace:*",
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
/**
|
||||
* Uploads the SVG assets of a generated sheet seed into the Storage bucket
|
||||
* anatomical-sheet-assets, content-addressed (<sha256>.svg).
|
||||
*
|
||||
* Operator tool: needs the service key, which never ships to the browser.
|
||||
* Reads SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY from the environment
|
||||
* (local stack: `pnpm supabase status` prints them). Idempotent: an object
|
||||
* whose sha256 already exists is skipped, a mismatching upload is refused.
|
||||
*
|
||||
* pnpm assets:upload supabase/seed/anatomical-sheets/equine/v1
|
||||
*/
|
||||
import { createHash } from 'node:crypto'
|
||||
import { readFile } from 'node:fs/promises'
|
||||
import { join, resolve } from 'node:path'
|
||||
import { createClient } from '@supabase/supabase-js'
|
||||
|
||||
const BUCKET = 'anatomical-sheet-assets'
|
||||
|
||||
interface AssetEntry {
|
||||
file: string
|
||||
sha256: string
|
||||
byte_size: number
|
||||
}
|
||||
|
||||
function requireEnv(name: string): string {
|
||||
const value = process.env[name]
|
||||
if (!value) throw new Error(`${name} is required`)
|
||||
return value
|
||||
}
|
||||
|
||||
async function readManifest(dir: string): Promise<AssetEntry[]> {
|
||||
const raw = await readFile(join(dir, 'assets.json'), 'utf8')
|
||||
return JSON.parse(raw) as AssetEntry[]
|
||||
}
|
||||
|
||||
/** The bytes on disk must still match what the seed recorded. */
|
||||
async function readVerified(dir: string, entry: AssetEntry): Promise<Buffer> {
|
||||
const bytes = await readFile(join(dir, 'assets', entry.file))
|
||||
const sha256 = createHash('sha256').update(bytes).digest('hex')
|
||||
if (sha256 !== entry.sha256 || bytes.length !== entry.byte_size) {
|
||||
throw new Error(`${entry.file}: bytes differ from assets.json`)
|
||||
}
|
||||
return bytes
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const dir = resolve(process.argv[2] ?? '.')
|
||||
const client = createClient(
|
||||
requireEnv('SUPABASE_URL'),
|
||||
requireEnv('SUPABASE_SERVICE_ROLE_KEY'),
|
||||
{ auth: { persistSession: false } },
|
||||
)
|
||||
for (const entry of await readManifest(dir)) {
|
||||
const key = `${entry.sha256}.svg`
|
||||
const bytes = await readVerified(dir, entry)
|
||||
const { error } = await client.storage
|
||||
.from(BUCKET)
|
||||
.upload(key, bytes, { contentType: 'image/svg+xml', upsert: false })
|
||||
if (error && !/already exists/i.test(error.message)) {
|
||||
throw new Error(`${key}: ${error.message}`)
|
||||
}
|
||||
console.log(`${error ? 'kept' : 'uploaded'} ${key} (${entry.file})`)
|
||||
}
|
||||
}
|
||||
|
||||
main().catch((error: unknown) => {
|
||||
console.error(error instanceof Error ? error.message : error)
|
||||
process.exit(1)
|
||||
})
|
||||
@@ -1,6 +1,8 @@
|
||||
/**
|
||||
* PGlite harness: a real PostgreSQL (WASM) that applies the Supabase shim,
|
||||
* then every migration under supabase/migrations, in filename order.
|
||||
* then every migration under supabase/migrations, in filename order, then
|
||||
* the versioned seeds under supabase/seed (same files `supabase db reset`
|
||||
* applies through config.toml sql_paths).
|
||||
* Lets the pgTAP suite and the schema checks run without Docker. The same
|
||||
* SQL runs unchanged under `supabase test db` on the real stack.
|
||||
*/
|
||||
@@ -15,6 +17,8 @@ export const repoRoot = resolve(here, '../../../..')
|
||||
export const supabaseDir = join(repoRoot, 'supabase')
|
||||
export const migrationsDir = join(supabaseDir, 'migrations')
|
||||
export const pgTapTestsDir = join(supabaseDir, 'tests')
|
||||
/** Versioned seeds (reference data such as the official sheets); seed.sql itself needs the real auth schema and stays out. */
|
||||
export const seedDir = join(supabaseDir, 'seed')
|
||||
|
||||
async function listSqlFiles(dir: string): Promise<string[]> {
|
||||
const entries = await readdir(dir)
|
||||
@@ -29,6 +33,12 @@ export async function listPgTapTests(): Promise<string[]> {
|
||||
return listSqlFiles(pgTapTestsDir)
|
||||
}
|
||||
|
||||
/** Every .sql under supabase/seed, recursively, in path order. */
|
||||
export async function listSeeds(): Promise<string[]> {
|
||||
const entries = await readdir(seedDir, { recursive: true }).catch(() => [])
|
||||
return entries.filter((f) => f.endsWith('.sql')).sort()
|
||||
}
|
||||
|
||||
/** Boots PGlite, installs the shim and applies all migrations. */
|
||||
export async function createMigratedDatabase(): Promise<PGlite> {
|
||||
const db = await PGlite.create({ extensions: { pgtap } })
|
||||
@@ -39,6 +49,9 @@ export async function createMigratedDatabase(): Promise<PGlite> {
|
||||
for (const file of await listMigrations()) {
|
||||
await db.exec(await readFile(join(migrationsDir, file), 'utf8'))
|
||||
}
|
||||
for (const file of await listSeeds()) {
|
||||
await db.exec(await readFile(join(seedDir, file), 'utf8'))
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
||||
|
||||
@@ -73,3 +73,35 @@ grant usage on schema extensions to anon, authenticated, service_role;
|
||||
alter default privileges in schema public grant all on tables to anon, authenticated, service_role;
|
||||
alter default privileges in schema public grant all on functions to anon, authenticated, service_role;
|
||||
alter default privileges in schema public grant all on sequences to anon, authenticated, service_role;
|
||||
|
||||
-- Storage schema stand-in (buckets + objects only, the columns the
|
||||
-- migrations and tests touch). The real stack ships the full schema owned by
|
||||
-- supabase_storage_admin; policies declared on storage.objects apply there.
|
||||
create schema storage;
|
||||
grant usage on schema storage to anon, authenticated, service_role;
|
||||
|
||||
create table storage.buckets (
|
||||
id text primary key,
|
||||
name text not null,
|
||||
owner uuid,
|
||||
public boolean default false,
|
||||
file_size_limit bigint,
|
||||
allowed_mime_types text[],
|
||||
created_at timestamptz default now(),
|
||||
updated_at timestamptz default now()
|
||||
);
|
||||
|
||||
create table storage.objects (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
bucket_id text references storage.buckets (id),
|
||||
name text,
|
||||
owner uuid,
|
||||
metadata jsonb,
|
||||
created_at timestamptz default now(),
|
||||
updated_at timestamptz default now(),
|
||||
last_accessed_at timestamptz default now()
|
||||
);
|
||||
|
||||
alter table storage.objects enable row level security;
|
||||
grant select on storage.buckets to anon, authenticated, service_role;
|
||||
grant select, insert, update, delete on storage.objects to anon, authenticated, service_role;
|
||||
@@ -16,6 +16,6 @@
|
||||
"types": ["node", "vitest/globals"],
|
||||
"noEmit": true
|
||||
},
|
||||
"include": ["src", "tests"],
|
||||
"include": ["src", "tests", "scripts"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
Reference in new issue
Block a user