@plutocms/supabase 0.0.1-alpha.6 → 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 +1 -1
- package/public/schema.sql +92 -27
- package/server/api/setup/create.post.ts +89 -3
package/package.json
CHANGED
package/public/schema.sql
CHANGED
|
@@ -14,7 +14,7 @@ end
|
|
|
14
14
|
$$;
|
|
15
15
|
|
|
16
16
|
--- Create settings table
|
|
17
|
-
create table public.settings (
|
|
17
|
+
create table if not exists public.settings (
|
|
18
18
|
id bigint generated by default as identity not null,
|
|
19
19
|
setting_name public.TSettings not null,
|
|
20
20
|
setting_value text not null,
|
|
@@ -26,21 +26,60 @@ create table public.settings (
|
|
|
26
26
|
insert into public.settings (setting_name, setting_value) values
|
|
27
27
|
('website_title', 'My Website'),
|
|
28
28
|
('website_url', 'https://mywebsite.test'),
|
|
29
|
-
('website_description', 'This is my website.')
|
|
29
|
+
('website_description', 'This is my website.')
|
|
30
|
+
on conflict (setting_name) do nothing;
|
|
30
31
|
|
|
31
32
|
alter table public.settings enable row level security;
|
|
32
33
|
|
|
33
34
|
-- Policies for settings table
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
+
$$;
|
|
38
77
|
|
|
39
78
|
--- Create enum type for healthcheck names
|
|
40
79
|
do $$
|
|
41
80
|
begin
|
|
42
81
|
if not exists (select 1 from pg_type where typname = 'thealthcheck') then
|
|
43
|
-
create type
|
|
82
|
+
create type THealthcheck as enum (
|
|
44
83
|
'first_setup'
|
|
45
84
|
);
|
|
46
85
|
end if;
|
|
@@ -48,29 +87,38 @@ end
|
|
|
48
87
|
$$;
|
|
49
88
|
|
|
50
89
|
--- Create healthcheck table (name/value format like settings)
|
|
51
|
-
create table public.healthcheck (
|
|
90
|
+
create table if not exists public.healthcheck (
|
|
52
91
|
id bigint generated by default as identity not null,
|
|
53
|
-
|
|
54
|
-
|
|
92
|
+
config_name public.THealthcheck not null,
|
|
93
|
+
config_value text not null,
|
|
55
94
|
constraint healthcheck_pkey primary key (id),
|
|
56
|
-
constraint
|
|
95
|
+
constraint healthcheck_config_name_key unique (config_name)
|
|
57
96
|
) TABLESPACE pg_default;
|
|
58
97
|
|
|
59
98
|
--- Insert default healthcheck values
|
|
60
|
-
insert into public.healthcheck (
|
|
61
|
-
('first_setup', 'true')
|
|
99
|
+
insert into public.healthcheck (config_name, config_value) values
|
|
100
|
+
('first_setup', 'true')
|
|
101
|
+
on conflict (config_name) do nothing;
|
|
62
102
|
|
|
63
103
|
alter table public.healthcheck enable row level security;
|
|
64
104
|
|
|
65
105
|
-- Allow read access to healthcheck for public and dashboard_user (no login required)
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
to public
|
|
70
|
-
|
|
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
|
+
$$;
|
|
71
119
|
|
|
72
120
|
-- Profiles
|
|
73
|
-
create table profiles (
|
|
121
|
+
create table if not exists public.profiles (
|
|
74
122
|
id uuid references auth.users on delete cascade not null primary key,
|
|
75
123
|
updated_at timestamp with time zone,
|
|
76
124
|
first_name text,
|
|
@@ -80,13 +128,22 @@ create table profiles (
|
|
|
80
128
|
|
|
81
129
|
alter table public.profiles enable row level security;
|
|
82
130
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
for
|
|
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
|
+
$$;
|
|
87
144
|
|
|
88
145
|
--- inserts a row into public.profiles
|
|
89
|
-
create function public.handle_new_user()
|
|
146
|
+
create or replace function public.handle_new_user()
|
|
90
147
|
returns trigger
|
|
91
148
|
language plpgsql
|
|
92
149
|
security definer set search_path = ''
|
|
@@ -98,6 +155,14 @@ begin
|
|
|
98
155
|
end;
|
|
99
156
|
$$;
|
|
100
157
|
--- trigger the function every time a user is created
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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
|
+
$$;
|
|
@@ -5,6 +5,88 @@ interface Payload {
|
|
|
5
5
|
connectionString: string
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Splits a SQL string into individual statements, correctly handling
|
|
10
|
+
* $$ dollar-quoted blocks, single-quoted strings, and -- comments.
|
|
11
|
+
*/
|
|
12
|
+
function splitStatements(sql: string): string[] {
|
|
13
|
+
const statements: string[] = []
|
|
14
|
+
let current = ''
|
|
15
|
+
let i = 0
|
|
16
|
+
|
|
17
|
+
while (i < sql.length) {
|
|
18
|
+
// Check for dollar-quoting ($$)
|
|
19
|
+
if (sql[i] === '$' && sql[i + 1] === '$') {
|
|
20
|
+
current += '$$'
|
|
21
|
+
i += 2
|
|
22
|
+
// Read until closing $$
|
|
23
|
+
while (i < sql.length) {
|
|
24
|
+
if (sql[i] === '$' && sql[i + 1] === '$') {
|
|
25
|
+
current += '$$'
|
|
26
|
+
i += 2
|
|
27
|
+
break
|
|
28
|
+
}
|
|
29
|
+
current += sql[i]
|
|
30
|
+
i++
|
|
31
|
+
}
|
|
32
|
+
continue
|
|
33
|
+
}
|
|
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
|
+
|
|
57
|
+
// Check for single-line comments
|
|
58
|
+
if (sql[i] === '-' && sql[i + 1] === '-') {
|
|
59
|
+
while (i < sql.length && sql[i] !== '\n') {
|
|
60
|
+
current += sql[i]
|
|
61
|
+
i++
|
|
62
|
+
}
|
|
63
|
+
continue
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Statement terminator
|
|
67
|
+
if (sql[i] === ';') {
|
|
68
|
+
current += ';'
|
|
69
|
+
const trimmed = current.trim()
|
|
70
|
+
if (trimmed && trimmed !== ';') {
|
|
71
|
+
statements.push(trimmed)
|
|
72
|
+
}
|
|
73
|
+
current = ''
|
|
74
|
+
i++
|
|
75
|
+
continue
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
current += sql[i]
|
|
79
|
+
i++
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const trimmed = current.trim()
|
|
83
|
+
if (trimmed && trimmed !== ';') {
|
|
84
|
+
statements.push(trimmed)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return statements
|
|
88
|
+
}
|
|
89
|
+
|
|
8
90
|
export default defineEventHandler(async (event) => {
|
|
9
91
|
const body = await readBody<Payload>(event)
|
|
10
92
|
|
|
@@ -30,13 +112,17 @@ export default defineEventHandler(async (event) => {
|
|
|
30
112
|
const sql = postgres(connectionString)
|
|
31
113
|
|
|
32
114
|
try {
|
|
33
|
-
|
|
115
|
+
const statements = splitStatements(schema)
|
|
116
|
+
|
|
117
|
+
for (const statement of statements) {
|
|
118
|
+
await sql.unsafe(statement)
|
|
119
|
+
}
|
|
34
120
|
|
|
35
121
|
// Set the first_setup flag in the healthcheck table to false
|
|
36
122
|
await sql`
|
|
37
123
|
UPDATE healthcheck
|
|
38
|
-
SET
|
|
39
|
-
WHERE
|
|
124
|
+
SET config_value = 'false'
|
|
125
|
+
WHERE config_name = 'first_setup';
|
|
40
126
|
`
|
|
41
127
|
|
|
42
128
|
return {
|