diff --git a/drizzle.config.ts b/drizzle.config.ts index bce5f38..d94979e 100644 --- a/drizzle.config.ts +++ b/drizzle.config.ts @@ -1,5 +1,5 @@ import { defineConfig } from 'drizzle-kit'; -console.log(process.env.DATABASE_URL) + export default defineConfig({ schema: './src/integrations/drizzle/db/schema.ts', out: './src/integrations/supabase/migrations', diff --git a/src/integrations/drizzle/db/schema.ts b/src/integrations/drizzle/db/schema.ts index e10f8ba..712a4a0 100644 --- a/src/integrations/drizzle/db/schema.ts +++ b/src/integrations/drizzle/db/schema.ts @@ -1,6 +1,44 @@ -import { pgTable, serial, text, varchar } from "drizzle-orm/pg-core"; + + + +import { sql } from 'drizzle-orm'; +import { foreignKey, pgPolicy, pgTable, serial, text, uuid, varchar } from 'drizzle-orm/pg-core'; +import { authenticatedRole, authUsers } from 'drizzle-orm/supabase'; + export const users = pgTable('demo', { id: serial('id').primaryKey(), fullName: text('full_name'), - phone: varchar('phone', { length: 256 }), -}); \ No newline at end of file + phone: varchar('phone', { length: 256 }) +}); + +export const profiles = pgTable( + 'profiles', + { + id: uuid('id').notNull().primaryKey(), + firstName: text('first_name'), + lastName: text('last_name'), + }, + (table) => [ + foreignKey({ + columns: [table.id], + foreignColumns: [authUsers.id], + name: 'profiles_id_fkey', + }).onDelete('cascade'), + pgPolicy('select-own-profile', { + for: 'select', + to: authenticatedRole, + using: sql`${table.id} = auth.uid()`, + }), + pgPolicy('update-own-profile', { + for: 'update', + to: authenticatedRole, + using: sql`${table.id} = auth.uid()`, + withCheck: sql`${table.id} = auth.uid()`, + }), + pgPolicy('insert-profile', { + for: 'insert', + to: authenticatedRole, + withCheck: sql`${table.id} = auth.uid()`, + }) + ] +).enableRLS(); diff --git a/src/integrations/supabase/migrations/0000_talented_doorman.sql b/src/integrations/supabase/migrations/0000_talented_doorman.sql index 0c9036c..36609bf 100644 --- a/src/integrations/supabase/migrations/0000_talented_doorman.sql +++ b/src/integrations/supabase/migrations/0000_talented_doorman.sql @@ -1,4 +1,4 @@ -CREATE TABLE "demo" ( +IF NOT EXISTS CREATE TABLE "demo" ( "id" serial PRIMARY KEY NOT NULL, "full_name" text, "phone" varchar(256) diff --git a/src/integrations/supabase/migrations/0001_nice_gargoyle.sql b/src/integrations/supabase/migrations/0001_nice_gargoyle.sql new file mode 100644 index 0000000..df3af4b --- /dev/null +++ b/src/integrations/supabase/migrations/0001_nice_gargoyle.sql @@ -0,0 +1,11 @@ +CREATE TABLE "profiles" ( + "id" uuid PRIMARY KEY NOT NULL, + "first_name" text, + "last_name" text +); +--> statement-breakpoint +ALTER TABLE "profiles" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint +ALTER TABLE "profiles" ADD CONSTRAINT "profiles_id_fkey" FOREIGN KEY ("id") REFERENCES "auth"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint +CREATE POLICY "select-own-profile" ON "profiles" AS PERMISSIVE FOR SELECT TO "authenticated" USING ("profiles"."id" = auth.uid());--> statement-breakpoint +CREATE POLICY "update-own-profile" ON "profiles" AS PERMISSIVE FOR UPDATE TO "authenticated" USING ("profiles"."id" = auth.uid()) WITH CHECK ("profiles"."id" = auth.uid());--> statement-breakpoint +CREATE POLICY "insert-profile" ON "profiles" AS PERMISSIVE FOR INSERT TO "authenticated" WITH CHECK ("profiles"."id" = auth.uid()); \ No newline at end of file diff --git a/src/integrations/supabase/migrations/meta/0001_snapshot.json b/src/integrations/supabase/migrations/meta/0001_snapshot.json new file mode 100644 index 0000000..c313063 --- /dev/null +++ b/src/integrations/supabase/migrations/meta/0001_snapshot.json @@ -0,0 +1,125 @@ +{ + "id": "7d0d4272-65ba-45cf-9dd3-a5e2008d3744", + "prevId": "c266fe94-b863-4b6c-930c-44af8af68c1a", + "version": "7", + "dialect": "postgresql", + "tables": { + "public.profiles": { + "name": "profiles", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true + }, + "first_name": { + "name": "first_name", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "last_name": { + "name": "last_name", + "type": "text", + "primaryKey": false, + "notNull": false + } + }, + "indexes": {}, + "foreignKeys": { + "profiles_id_fkey": { + "name": "profiles_id_fkey", + "tableFrom": "profiles", + "tableTo": "users", + "schemaTo": "auth", + "columnsFrom": [ + "id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": { + "select-own-profile": { + "name": "select-own-profile", + "as": "PERMISSIVE", + "for": "SELECT", + "to": [ + "authenticated" + ], + "using": "\"profiles\".\"id\" = auth.uid()" + }, + "update-own-profile": { + "name": "update-own-profile", + "as": "PERMISSIVE", + "for": "UPDATE", + "to": [ + "authenticated" + ], + "using": "\"profiles\".\"id\" = auth.uid()", + "withCheck": "\"profiles\".\"id\" = auth.uid()" + }, + "insert-profile": { + "name": "insert-profile", + "as": "PERMISSIVE", + "for": "INSERT", + "to": [ + "authenticated" + ], + "withCheck": "\"profiles\".\"id\" = auth.uid()" + } + }, + "checkConstraints": {}, + "isRLSEnabled": true + }, + "public.demo": { + "name": "demo", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "full_name": { + "name": "full_name", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "phone": { + "name": "phone", + "type": "varchar(256)", + "primaryKey": false, + "notNull": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + } + }, + "enums": {}, + "schemas": {}, + "sequences": {}, + "roles": {}, + "policies": {}, + "views": {}, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} \ No newline at end of file diff --git a/src/integrations/supabase/migrations/meta/_journal.json b/src/integrations/supabase/migrations/meta/_journal.json index ea38f8f..8953307 100644 --- a/src/integrations/supabase/migrations/meta/_journal.json +++ b/src/integrations/supabase/migrations/meta/_journal.json @@ -8,6 +8,13 @@ "when": 1754932713212, "tag": "0000_talented_doorman", "breakpoints": true + }, + { + "idx": 1, + "version": "7", + "when": 1755013739316, + "tag": "0001_nice_gargoyle", + "breakpoints": true } ] } \ No newline at end of file