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

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.6",
4
+ "version": "0.0.1-alpha.7",
5
5
  "main": "./nuxt.config.ts",
6
6
  "scripts": {
7
7
  "build": "nuxt build",
package/public/schema.sql CHANGED
@@ -1,17 +1,11 @@
1
1
  -- Supabase Schema SQL
2
2
  -- Settings
3
3
  --- Create enum type for settings
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
- $$;
4
+ create type TSettings as enum (
5
+ 'website_title',
6
+ 'website_url',
7
+ 'website_description'
8
+ );
15
9
 
16
10
  --- Create settings table
17
11
  create table public.settings (
@@ -37,15 +31,9 @@ to authenticated, dashboard_user
37
31
  with check (true);
38
32
 
39
33
  --- Create enum type for healthcheck names
40
- do $$
41
- begin
42
- if not exists (select 1 from pg_type where typname = 'thealthcheck') then
43
- create type Healthcheck as enum (
44
- 'first_setup'
45
- );
46
- end if;
47
- end
48
- $$;
34
+ create type Healthcheck as enum (
35
+ 'first_setup'
36
+ );
49
37
 
50
38
  --- Create healthcheck table (name/value format like settings)
51
39
  create table public.healthcheck (
@@ -5,6 +5,66 @@ 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 (used in functions, triggers, DO blocks).
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-line comments
36
+ if (sql[i] === '-' && sql[i + 1] === '-') {
37
+ while (i < sql.length && sql[i] !== '\n') {
38
+ current += sql[i]
39
+ i++
40
+ }
41
+ continue
42
+ }
43
+
44
+ // Statement terminator
45
+ if (sql[i] === ';') {
46
+ current += ';'
47
+ const trimmed = current.trim()
48
+ if (trimmed && trimmed !== ';') {
49
+ statements.push(trimmed)
50
+ }
51
+ current = ''
52
+ i++
53
+ continue
54
+ }
55
+
56
+ current += sql[i]
57
+ i++
58
+ }
59
+
60
+ const trimmed = current.trim()
61
+ if (trimmed && trimmed !== ';') {
62
+ statements.push(trimmed)
63
+ }
64
+
65
+ return statements
66
+ }
67
+
8
68
  export default defineEventHandler(async (event) => {
9
69
  const body = await readBody<Payload>(event)
10
70
 
@@ -30,7 +90,11 @@ export default defineEventHandler(async (event) => {
30
90
  const sql = postgres(connectionString)
31
91
 
32
92
  try {
33
- await sql.unsafe(schema)
93
+ const statements = splitStatements(schema)
94
+
95
+ for (const statement of statements) {
96
+ await sql.unsafe(statement)
97
+ }
34
98
 
35
99
  // Set the first_setup flag in the healthcheck table to false
36
100
  await sql`