tabi-memo / backend

0 stars 0 forks source link

Link `public.users` and `auth.users` when seeding & replace serial based id with uuid based one #14

Closed Yo-mah-Ya closed 10 months ago

Yo-mah-Ya commented 10 months ago
Yo-mah-Ya commented 10 months ago
Migration scripts which generates supabase/seed/data/.ts. ```ts import { createServerClient } from "@supabase/ssr"; import { writeFile } from "fs/promises"; const assertGetEnvValueFrom = (key: string): string => { const value = process.env[key]; if (typeof value !== "string") { throw new Error(`Environment value for ${key} is not set`); } return value; }; const supabaseWithPublicSchema = createServerClient( assertGetEnvValueFrom("SUPABASE_URL"), assertGetEnvValueFrom("SUPABASE_ANON_KEY"), { cookies: {}, db: { schema: "public" } }, ); export const omit = , K extends keyof T>( obj: T, properties: readonly K[], ): Omit => { const set = new Set(properties as readonly string[]); return !Object.keys(obj).length ? obj : (Object.entries(obj).reduce( (acc, [name, value]) => { if (!set.has(name)) { acc[name] = value; } return acc; }, {} as Record, ) as Omit); }; type BaseColumns = { id: number; uuid: string }; (async () => { await Promise.all( ( [ "users", "trips", "activity", "tags", "trip_tags", "invitations", ] as const ).map(async (table) => { const data = await Promise.all( (await supabaseWithPublicSchema.from(table).select()).data?.map( async (record: BaseColumns) => { const d = omit(record, ["uuid"]); if (table === "activity") { return { ...d, id: record.uuid, trip_id: ( ( await supabaseWithPublicSchema .from("trips") .select("uuid") .eq( "id", (record as BaseColumns & { trip_id: number }).trip_id, ) .limit(1) .single() ).data as { uuid: string } ).uuid, }; } else if (table === "invitations") { return { ...d, id: record.uuid, trip_id: ( ( await supabaseWithPublicSchema .from("trips") .select("uuid") .eq( "id", (record as BaseColumns & { trip_id: number }).trip_id, ) .limit(1) .single() ).data as { uuid: string } ).uuid, invited_by_user_id: ( ( await supabaseWithPublicSchema .from("users") .select("uuid") .eq( "id", (record as BaseColumns & { invited_by_user_id: number }) .invited_by_user_id, ) .limit(1) .single() ).data as { uuid: string } ).uuid, invitee_user_id: ( ( await supabaseWithPublicSchema .from("users") .select("uuid") .eq( "id", (record as BaseColumns & { invitee_user_id: number }) .invitee_user_id, ) .limit(1) .single() ).data as { uuid: string } ).uuid, }; } else if (table === "trip_tags") { return { ...d, id: record.uuid, trip_id: ( ( await supabaseWithPublicSchema .from("trips") .select("uuid") .eq( "id", (record as BaseColumns & { trip_id: number }).trip_id, ) .limit(1) .single() ).data as { uuid: string } ).uuid, tag_id: ( ( await supabaseWithPublicSchema .from("tags") .select("uuid") .eq( "id", (record as BaseColumns & { tag_id: number }).tag_id, ) .limit(1) .single() ).data as { uuid: string } ).uuid, }; } else if (table === "trips") { return { ...d, id: record.uuid, user_id: ( ( await supabaseWithPublicSchema .from("users") .select("uuid") .eq( "id", (record as BaseColumns & { user_id: number }).user_id, ) .limit(1) .single() ).data as { uuid: string } ).uuid, }; } return { ...d, id: record.uuid, }; }, ) ?? [], ); return writeFile(`${__dirname}/new/${table}.ts`, JSON.stringify(data)); }), ); })().catch(console.error); ```