Advertisement
buzzthedev

Untitled

Apr 10th, 2025 (edited)
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. create table public.users (
  2. created_at timestamp with time zone not null default now(),
  3. settings jsonb null default '{}'::jsonb,
  4. id uuid not null default auth.uid(),
  5. theme jsonb null,
  6. constraint users_pkey primary key (id),
  7. constraint users_id_key unique (id)
  8. ) TABLESPACE pg_default;
  9.  
  10. create table public.customers (
  11. id uuid not null default gen_random_uuid (),
  12. created_at timestamp with time zone not null default now(),
  13. name text null default 'User'::text,
  14. user_id uuid null,
  15. discord_id text null,
  16. constraint customers_pkey primary key (id),
  17. constraint customers_discord_id_key unique (discord_id),
  18. constraint customers_user_id_fkey foreign KEY (user_id) references users (id) on update CASCADE on delete CASCADE
  19. ) TABLESPACE pg_default;
  20.  
  21. create table public.products (
  22. id uuid not null default gen_random_uuid (),
  23. created_at timestamp with time zone not null default now(),
  24. name text null default 'New Product'::text,
  25. image_url text null,
  26. version text null default 'v1'::text,
  27. price double precision null default '0'::double precision,
  28. enabled boolean null default true,
  29. user_id uuid null default auth.uid(),
  30. constraint products_pkey primary key (id),
  31. constraint products_user_id_fkey foreign KEY (user_id) references users (id) on update CASCADE on delete CASCADE
  32. ) TABLESPACE pg_default;
  33.  
  34. create table public.licenses (
  35. id bigint generated by default as identity not null,
  36. customer uuid null,
  37. product uuid null,
  38. issue_date timestamp with time zone null default now(),
  39. expiration timestamp with time zone null,
  40. license_key text null,
  41. user_id uuid null,
  42. updated_at timestamp with time zone null,
  43. status text null default 'active'::text,
  44. used_ips text[] null default '{}'::text[],
  45. max_ips smallint null default '1'::smallint,
  46. constraint licenses_pkey primary key (id),
  47. constraint licenses_id_key unique (id),
  48. constraint licenses_license_key_key unique (license_key),
  49. constraint licenses_customer_fkey foreign KEY (customer) references customers (id) on update CASCADE on delete CASCADE,
  50. constraint licenses_product_fkey foreign KEY (product) references products (id) on update CASCADE on delete CASCADE,
  51. constraint licenses_user_id_fkey foreign KEY (user_id) references users (id) on update CASCADE on delete CASCADE,
  52. constraint licenses_check check (true)
  53. ) TABLESPACE pg_default;
  54.  
  55. create table public.logs (
  56. id bigint generated by default as identity not null,
  57. created_at timestamp with time zone not null default now(),
  58. license_key text null,
  59. ip_address text null,
  60. metadata jsonb null default '{}'::jsonb,
  61. status text null default 'Unknown'::text,
  62. user_id uuid null default auth.uid(),
  63. constraint logs_pkey primary key (id),
  64. constraint logs_id_key unique (id),
  65. constraint logs_license_key_fkey foreign KEY (license_key) references licenses (license_key) on update CASCADE on delete CASCADE,
  66. constraint logs_user_id_fkey foreign KEY (user_id) references users (id) on update CASCADE on delete CASCADE
  67. ) TABLESPACE pg_default;
  68.  
  69. alter publication supabase_realtime add table public.logs;
  70.  
  71. alter table public.users enable row level security;
  72. alter table public.licenses enable row level security;
  73. alter table public.logs enable row level security;
  74. alter table public.products enable row level security;
  75. alter table public.customers enable row level security;
  76.  
  77.  
  78. create policy "Users can access their own record"
  79. on public.users
  80. for all
  81. using (auth.uid() = id);
  82.  
  83. create policy "Users can select their own licenses"
  84. on public.licenses
  85. for select
  86. using (auth.uid() = customer);
  87.  
  88. create policy "Users can select their own products"
  89. on public.products
  90. for all
  91. using (auth.uid() = user_id);
  92.  
  93. create policy "Users can view their own logs"
  94. on public.logs
  95. for select
  96. using (auth.uid() = user_id);
  97.  
  98. create policy "Users can access their own customer record"
  99. on public.customers
  100. for all
  101. using (auth.uid() = user_id);
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement