buzzthedev

Phantom SQL Script

Apr 10th, 2025 (edited)
600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. create extension if not exists pgcrypto;
  2.  
  3. create table if not exists public.users (
  4. created_at timestamp with time zone not null default now(),
  5. settings jsonb null default '{}'::jsonb,
  6. id uuid not null default auth.uid(),
  7. theme jsonb null,
  8. constraint users_pkey primary key (id),
  9. constraint users_id_key unique (id)
  10. ) tablespace pg_default;
  11.  
  12. create table if not exists public.customers (
  13. id uuid not null default gen_random_uuid(),
  14. created_at timestamp with time zone not null default now(),
  15. name text null default 'User',
  16. user_id uuid null,
  17. discord_id text null,
  18. constraint customers_pkey primary key (id),
  19. constraint customers_discord_id_key unique (discord_id),
  20. constraint customers_user_id_fkey foreign key (user_id) references public.users (id) on update cascade on delete cascade
  21. ) tablespace pg_default;
  22.  
  23. create table if not exists public.products (
  24. id uuid not null default gen_random_uuid(),
  25. created_at timestamp with time zone not null default now(),
  26. name text null default 'New Product',
  27. image_url text null,
  28. version text null default 'v1',
  29. price double precision null default 0,
  30. enabled boolean null default true,
  31. user_id uuid null default auth.uid(),
  32. constraint products_pkey primary key (id),
  33. constraint products_user_id_fkey foreign key (user_id) references public.users (id) on update cascade on delete cascade
  34. ) tablespace pg_default;
  35.  
  36. create table if not exists public.licenses (
  37. id bigint generated by default as identity not null,
  38. customer uuid null,
  39. product uuid null,
  40. issue_date timestamp with time zone null default now(),
  41. expiration timestamp with time zone null,
  42. license_key text null,
  43. user_id uuid null,
  44. updated_at timestamp with time zone null,
  45. status text null default 'active',
  46. used_ips text[] null default '{}'::text[],
  47. whitelisted_ips text[] null default '{}'::text[],
  48. ip_whitelist_enabled boolean null default false,
  49. max_ips smallint null default 1,
  50. custom_data jsonb null default '{}'::jsonb,
  51. constraint licenses_pkey primary key (id),
  52. constraint licenses_id_key unique (id),
  53. constraint licenses_license_key_key unique (license_key),
  54. constraint licenses_customer_fkey foreign key (customer) references public.customers (id) on update cascade on delete cascade,
  55. constraint licenses_product_fkey foreign key (product) references public.products (id) on update cascade on delete cascade,
  56. constraint licenses_user_id_fkey foreign key (user_id) references public.users (id) on update cascade on delete cascade,
  57. constraint licenses_check check (true)
  58. ) tablespace pg_default;
  59.  
  60. create table if not exists public.logs (
  61. id bigint generated by default as identity not null,
  62. created_at timestamp with time zone not null default now(),
  63. license_key text null,
  64. ip_address text null,
  65. metadata jsonb null default '{}'::jsonb,
  66. status text null default 'Unknown',
  67. user_id uuid null default auth.uid(),
  68. constraint logs_pkey primary key (id),
  69. constraint logs_id_key unique (id),
  70. constraint logs_license_key_fkey foreign key (license_key) references public.licenses (license_key) on update cascade on delete cascade,
  71. constraint logs_user_id_fkey foreign key (user_id) references public.users (id) on update cascade on delete cascade
  72. ) tablespace pg_default;
  73.  
  74. alter table public.customers
  75. add column if not exists auth_user_id uuid unique,
  76. add column if not exists client_email text unique,
  77. add column if not exists client_password text,
  78. add column if not exists metadata jsonb;
  79.  
  80. alter table public.products
  81. add column if not exists metadata jsonb;
  82.  
  83. alter table public.licenses
  84. add column if not exists max_hwids int4,
  85. add column if not exists used_hwids text[];
  86.  
  87. create index if not exists customers_auth_user_id_idx on public.customers(auth_user_id);
  88. create index if not exists customers_client_email_idx on public.customers(client_email);
  89.  
  90. update public.users
  91. set settings = coalesce(settings, '{}'::jsonb) || jsonb_build_object('client_email_domain', 'buzz.dev')
  92. where (settings->>'client_email_domain') is null;
  93.  
  94. do $$
  95. begin
  96. if not exists (
  97. select 1 from pg_publication_tables
  98. where pubname = 'supabase_realtime'
  99. and schemaname = 'public'
  100. and tablename = 'logs'
  101. ) then
  102. alter publication supabase_realtime add table public.logs;
  103. end if;
  104. end
  105. $$;
  106.  
  107. alter table public.users enable row level security;
  108. alter table public.licenses enable row level security;
  109. alter table public.logs enable row level security;
  110. alter table public.products enable row level security;
  111. alter table public.customers enable row level security;
  112.  
  113. drop policy if exists "Users can access their own record" on public.users;
  114. create policy "Users can access their own record"
  115. on public.users
  116. for all
  117. using (auth.uid() = id);
  118.  
  119. drop policy if exists "Users can select their own licenses" on public.licenses;
  120. create policy "Users can select their own licenses"
  121. on public.licenses
  122. for select
  123. using (auth.uid() = customer);
  124.  
  125. drop policy if exists "Users can select their own products" on public.products;
  126. create policy "Users can select their own products"
  127. on public.products
  128. for all
  129. using (auth.uid() = user_id);
  130.  
  131. drop policy if exists "Users can view their own logs" on public.logs;
  132. create policy "Users can view their own logs"
  133. on public.logs
  134. for select
  135. using (auth.uid() = user_id);
  136.  
  137. drop policy if exists "Users can access their own customer record" on public.customers;
  138. create policy "Users can access their own customer record"
  139. on public.customers
  140. for all
  141. using (auth.uid() = user_id);
  142.  
Advertisement
Add Comment
Please, Sign In to add comment