@plutocms/supabase 0.0.1-alpha.7 → 0.0.1-alpha.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@plutocms/supabase",
3
3
  "type": "module",
4
- "version": "0.0.1-alpha.7",
4
+ "version": "0.0.1-alpha.8",
5
5
  "main": "./nuxt.config.ts",
6
6
  "scripts": {
7
7
  "build": "nuxt build",
package/public/schema.sql CHANGED
@@ -1,14 +1,20 @@
1
1
  -- Supabase Schema SQL
2
2
  -- Settings
3
3
  --- Create enum type for settings
4
- create type TSettings as enum (
5
- 'website_title',
6
- 'website_url',
7
- 'website_description'
8
- );
4
+ do $$
5
+ begin
6
+ if not exists (select 1 from pg_type where typname = 'tsettings') then
7
+ create type TSettings as enum (
8
+ 'website_title',
9
+ 'website_url',
10
+ 'website_description'
11
+ );
12
+ end if;
13
+ end
14
+ $$;
9
15
 
10
16
  --- Create settings table
11
- create table public.settings (
17
+ create table if not exists public.settings (
12
18
  id bigint generated by default as identity not null,
13
19
  setting_name public.TSettings not null,
14
20
  setting_value text not null,
@@ -20,45 +26,99 @@ create table public.settings (
20
26
  insert into public.settings (setting_name, setting_value) values
21
27
  ('website_title', 'My Website'),
22
28
  ('website_url', 'https://mywebsite.test'),
23
- ('website_description', 'This is my website.');
29
+ ('website_description', 'This is my website.')
30
+ on conflict (setting_name) do nothing;
24
31
 
25
32
  alter table public.settings enable row level security;
26
33
 
27
34
  -- Policies for settings table
28
- create policy "Enable insert for authenticated users only"
29
- on public.settings
30
- to authenticated, dashboard_user
31
- with check (true);
35
+ do $$
36
+ begin
37
+ if not exists (
38
+ select 1 from pg_policies where tablename = 'settings' and policyname = 'Enable insert for authenticated users only'
39
+ ) then
40
+ create policy "Enable insert for authenticated users only"
41
+ on public.settings
42
+ for insert
43
+ to authenticated, dashboard_user
44
+ with check (true);
45
+ end if;
46
+ end
47
+ $$;
48
+
49
+ do $$
50
+ begin
51
+ if not exists (
52
+ select 1 from pg_policies where tablename = 'settings' and policyname = 'Enable read access for authenticated users on settings'
53
+ ) then
54
+ create policy "Enable read access for authenticated users on settings"
55
+ on public.settings
56
+ for select
57
+ to authenticated, dashboard_user
58
+ using (true);
59
+ end if;
60
+ end
61
+ $$;
62
+
63
+ do $$
64
+ begin
65
+ if not exists (
66
+ select 1 from pg_policies where tablename = 'settings' and policyname = 'Enable update for authenticated users on settings'
67
+ ) then
68
+ create policy "Enable update for authenticated users on settings"
69
+ on public.settings
70
+ for update
71
+ to authenticated, dashboard_user
72
+ using (true)
73
+ with check (true);
74
+ end if;
75
+ end
76
+ $$;
32
77
 
33
78
  --- Create enum type for healthcheck names
34
- create type Healthcheck as enum (
35
- 'first_setup'
36
- );
79
+ do $$
80
+ begin
81
+ if not exists (select 1 from pg_type where typname = 'thealthcheck') then
82
+ create type THealthcheck as enum (
83
+ 'first_setup'
84
+ );
85
+ end if;
86
+ end
87
+ $$;
37
88
 
38
89
  --- Create healthcheck table (name/value format like settings)
39
- create table public.healthcheck (
90
+ create table if not exists public.healthcheck (
40
91
  id bigint generated by default as identity not null,
41
- check_name public.Healthcheck not null,
42
- check_value text not null,
92
+ config_name public.THealthcheck not null,
93
+ config_value text not null,
43
94
  constraint healthcheck_pkey primary key (id),
44
- constraint healthcheck_check_name_key unique (check_name)
95
+ constraint healthcheck_config_name_key unique (config_name)
45
96
  ) TABLESPACE pg_default;
46
97
 
47
98
  --- Insert default healthcheck values
48
- insert into public.healthcheck (check_name, check_value) values
49
- ('first_setup', 'true');
99
+ insert into public.healthcheck (config_name, config_value) values
100
+ ('first_setup', 'true')
101
+ on conflict (config_name) do nothing;
50
102
 
51
103
  alter table public.healthcheck enable row level security;
52
104
 
53
105
  -- Allow read access to healthcheck for public and dashboard_user (no login required)
54
- create policy "Enable read access for healthcheck to public and dashboard_user"
55
- on public.healthcheck
56
- for select
57
- to public, dashboard_user
58
- using (true);
106
+ do $$
107
+ begin
108
+ if not exists (
109
+ select 1 from pg_policies where tablename = 'healthcheck' and policyname = 'Enable read access for healthcheck to public and dashboard_user'
110
+ ) then
111
+ create policy "Enable read access for healthcheck to public and dashboard_user"
112
+ on public.healthcheck
113
+ for select
114
+ to public, dashboard_user
115
+ using (true);
116
+ end if;
117
+ end
118
+ $$;
59
119
 
60
120
  -- Profiles
61
- create table profiles (
121
+ create table if not exists public.profiles (
62
122
  id uuid references auth.users on delete cascade not null primary key,
63
123
  updated_at timestamp with time zone,
64
124
  first_name text,
@@ -68,13 +128,22 @@ create table profiles (
68
128
 
69
129
  alter table public.profiles enable row level security;
70
130
 
71
- create policy "Enable read access for authenticated users"
72
- on public.profiles
73
- to authenticated, dashboard_user
74
- for select using (true);
131
+ do $$
132
+ begin
133
+ if not exists (
134
+ select 1 from pg_policies where tablename = 'profiles' and policyname = 'Enable read access for authenticated users'
135
+ ) then
136
+ create policy "Enable read access for authenticated users"
137
+ on public.profiles
138
+ for select
139
+ to authenticated, dashboard_user
140
+ using (true);
141
+ end if;
142
+ end
143
+ $$;
75
144
 
76
145
  --- inserts a row into public.profiles
77
- create function public.handle_new_user()
146
+ create or replace function public.handle_new_user()
78
147
  returns trigger
79
148
  language plpgsql
80
149
  security definer set search_path = ''
@@ -86,6 +155,14 @@ begin
86
155
  end;
87
156
  $$;
88
157
  --- trigger the function every time a user is created
89
- create trigger on_auth_user_created
90
- after insert on auth.users
91
- for each row execute procedure public.handle_new_user();
158
+ do $$
159
+ begin
160
+ if not exists (
161
+ select 1 from pg_trigger where tgname = 'on_auth_user_created'
162
+ ) then
163
+ create trigger on_auth_user_created
164
+ after insert on auth.users
165
+ for each row execute procedure public.handle_new_user();
166
+ end if;
167
+ end
168
+ $$;
@@ -7,7 +7,7 @@ interface Payload {
7
7
 
8
8
  /**
9
9
  * Splits a SQL string into individual statements, correctly handling
10
- * $$ dollar-quoted blocks (used in functions, triggers, DO blocks).
10
+ * $$ dollar-quoted blocks, single-quoted strings, and -- comments.
11
11
  */
12
12
  function splitStatements(sql: string): string[] {
13
13
  const statements: string[] = []
@@ -32,6 +32,28 @@ function splitStatements(sql: string): string[] {
32
32
  continue
33
33
  }
34
34
 
35
+ // Check for single-quoted strings (handle '' escapes)
36
+ if (sql[i] === `'`) {
37
+ current += sql[i]
38
+ i++
39
+ while (i < sql.length) {
40
+ if (sql[i] === `'` && sql[i + 1] === `'`) {
41
+ // Escaped quote
42
+ current += `''`
43
+ i += 2
44
+ continue
45
+ }
46
+ if (sql[i] === `'`) {
47
+ current += sql[i]
48
+ i++
49
+ break
50
+ }
51
+ current += sql[i]
52
+ i++
53
+ }
54
+ continue
55
+ }
56
+
35
57
  // Check for single-line comments
36
58
  if (sql[i] === '-' && sql[i + 1] === '-') {
37
59
  while (i < sql.length && sql[i] !== '\n') {
@@ -99,8 +121,8 @@ export default defineEventHandler(async (event) => {
99
121
  // Set the first_setup flag in the healthcheck table to false
100
122
  await sql`
101
123
  UPDATE healthcheck
102
- SET check_value = 'false'
103
- WHERE check_name = 'first_setup';
124
+ SET config_value = 'false'
125
+ WHERE config_name = 'first_setup';
104
126
  `
105
127
 
106
128
  return {