import { readFile, readdir } from "node:fs/promises"; import { PGlite } from "../.data/sql-verification/node_modules/@electric-sql/pglite/dist/index.js"; const db = new PGlite(); try { await db.exec(`create role anon; create role authenticated; create role service_role bypassrls; create schema auth; create table auth.users(id uuid primary key); create function auth.uid() returns uuid language sql as $$ select null::uuid $$; create schema storage; create table storage.buckets(id text primary key,name text,public boolean,file_size_limit bigint,allowed_mime_types text[]); create table storage.objects(id uuid primary key default gen_random_uuid(),bucket_id text,name text); create function storage.foldername(name text) returns text[] language sql as $$ select string_to_array(name, '/') $$;`); for (const file of (await readdir("supabase/migrations")) .filter((f) => f.endsWith(".sql")) .sort()) await db.exec(await readFile(`supabase/migrations/${file}`, "utf8")); await db.exec(`do $$ declare owner uuid:=gen_random_uuid(); other uuid:=gen_random_uuid(); root uuid; child uuid; m jsonb; old_revision uuid; begin insert into auth.users(id) values(owner),(other); insert into public.profiles(id,display_name) values(owner,'Owner'),(other,'Other') on conflict(id) do nothing; m:=jsonb_build_object('name','Test map','description','test','source','plugin','serverName','Test server','serverAddress','one.example', 'bounds',jsonb_build_object('minX',0,'minZ',0,'maxX',64,'maxZ',64), 'viewpoints',(select jsonb_agg(jsonb_build_object('id','p'||n,'x',n,'y',65,'z',n)) from generate_series(1,5) n)); root:=public.create_upload(owner,m); perform public.set_contribution_server(root,owner,'ONE.example:25565, two.example:25566'); perform public.assert_capture_server((select contribution_server_address from public.datasets where id=root),'two.example:25566'); begin perform public.assert_capture_server((select contribution_server_address from public.datasets where id=root),'wrong.example'); raise exception 'Mismatch accepted'; exception when others then if sqlerrm not like 'Wrong capture server:%' then raise; end if; end; update public.dataset_inputs set validated=true where dataset_id=root; insert into public.revisions(dataset_id,manifest,asset_root) values(root,m,root::text) returning id into old_revision; update public.datasets set state='published',revision_id=old_revision,update_state='draft' where id=root; begin perform public.publish_owner_update(root,other); raise exception 'Non-owner accepted'; exception when others then if sqlerrm <> 'Map owner permission required' then raise; end if; end; perform public.publish_owner_update(root,owner); if (select revision_id=old_revision or update_state is not null from public.datasets where id=root) then raise exception 'Owner update failed'; end if; old_revision:=(select revision_id from public.datasets where id=root); child:=public.create_upload(owner,m||jsonb_build_object('contributionTo',root,'serverAddress','two.example:25566','viewpoints',jsonb_build_array(jsonb_build_object('id','new','x',10,'y',65,'z',10)))); update public.datasets set state='draft' where id=child; update public.dataset_inputs set validated=true where dataset_id=child; update public.datasets set update_state='draft' where id=root; begin perform public.publish_own_contribution(child,owner); raise exception 'Unpublished changes were overwritten'; exception when others then if sqlerrm <> 'Publish or discard the existing map draft first' then raise; end if; end; update public.datasets set update_state=null where id=root; perform public.publish_own_contribution(child,owner); if (select revision_id=old_revision or panorama_count<>6 from public.datasets where id=root) then raise exception 'One-click contribution failed'; end if; if (select contribution_status from public.datasets where id=child)<>'accepted' then raise exception 'Owner contribution was not accepted'; end if; if has_function_privilege('authenticated','public.publish_owner_update(uuid,uuid)','execute') then raise exception 'Public RPC exposed'; end if; end $$;`); console.log( "Map update SQL passed: aliases, mismatch rejection, owner authorization and immutable revision.", ); } catch (error) { console.error(error.message); process.exitCode = 1; } finally { await db.close(); }