@fayz-ai/plugin-forms 0.1.5 → 0.8.0

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.
@@ -2,16 +2,16 @@
2
2
  -- Custom Forms Plugin — Base Tables
3
3
  -- ============================================================
4
4
 
5
- -- frm_templates: form template definitions (versioned)
6
- CREATE TABLE IF NOT EXISTS public.frm_templates (
5
+ -- plg_forms_templates: form template definitions (versioned)
6
+ CREATE TABLE IF NOT EXISTS public.plg_forms_templates (
7
7
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
8
- tenant_id uuid NOT NULL REFERENCES saas_core.tenants(id) ON DELETE CASCADE,
8
+ tenant_id uuid NOT NULL REFERENCES public.tenants(id) ON DELETE CASCADE,
9
9
  name text NOT NULL,
10
10
  description text,
11
11
  category text NOT NULL DEFAULT 'general',
12
12
  version integer NOT NULL DEFAULT 1,
13
13
  is_current boolean NOT NULL DEFAULT true,
14
- parent_id uuid REFERENCES public.frm_templates(id),
14
+ parent_id uuid REFERENCES public.plg_forms_templates(id),
15
15
  schema jsonb NOT NULL DEFAULT '{"fields":[],"layout":{"columns":12}}',
16
16
  specialty text,
17
17
  tags text[] DEFAULT '{}',
@@ -24,37 +24,41 @@ CREATE TABLE IF NOT EXISTS public.frm_templates (
24
24
  updated_at timestamptz NOT NULL DEFAULT now()
25
25
  );
26
26
 
27
- ALTER TABLE public.frm_templates ENABLE ROW LEVEL SECURITY;
27
+ ALTER TABLE public.plg_forms_templates ENABLE ROW LEVEL SECURITY;
28
28
 
29
- CREATE INDEX IF NOT EXISTS idx_frm_templates_tenant
30
- ON public.frm_templates(tenant_id);
31
- CREATE INDEX IF NOT EXISTS idx_frm_templates_parent
32
- ON public.frm_templates(parent_id);
33
- CREATE INDEX IF NOT EXISTS idx_frm_templates_category
34
- ON public.frm_templates(tenant_id, category);
35
- CREATE INDEX IF NOT EXISTS idx_frm_templates_current
36
- ON public.frm_templates(tenant_id, is_current, is_active)
29
+ CREATE INDEX IF NOT EXISTS idx_plg_forms_templates_tenant
30
+ ON public.plg_forms_templates(tenant_id);
31
+ CREATE INDEX IF NOT EXISTS idx_plg_forms_templates_parent
32
+ ON public.plg_forms_templates(parent_id);
33
+ CREATE INDEX IF NOT EXISTS idx_plg_forms_templates_category
34
+ ON public.plg_forms_templates(tenant_id, category);
35
+ CREATE INDEX IF NOT EXISTS idx_plg_forms_templates_current
36
+ ON public.plg_forms_templates(tenant_id, is_current, is_active)
37
37
  WHERE is_current = true AND is_active = true AND is_deleted = false;
38
38
 
39
- CREATE POLICY "frm_templates_select" ON public.frm_templates
39
+ DROP POLICY IF EXISTS "plg_forms_templates_select" ON public.plg_forms_templates;
40
+ CREATE POLICY "plg_forms_templates_select" ON public.plg_forms_templates
40
41
  FOR SELECT TO authenticated
41
42
  USING (tenant_id IN (SELECT public.user_tenant_ids()));
42
- CREATE POLICY "frm_templates_insert" ON public.frm_templates
43
+ DROP POLICY IF EXISTS "plg_forms_templates_insert" ON public.plg_forms_templates;
44
+ CREATE POLICY "plg_forms_templates_insert" ON public.plg_forms_templates
43
45
  FOR INSERT TO authenticated
44
46
  WITH CHECK (tenant_id IN (SELECT public.user_tenant_ids()));
45
- CREATE POLICY "frm_templates_update" ON public.frm_templates
47
+ DROP POLICY IF EXISTS "plg_forms_templates_update" ON public.plg_forms_templates;
48
+ CREATE POLICY "plg_forms_templates_update" ON public.plg_forms_templates
46
49
  FOR UPDATE TO authenticated
47
50
  USING (tenant_id IN (SELECT public.user_tenant_ids()));
48
- CREATE POLICY "frm_templates_delete" ON public.frm_templates
51
+ DROP POLICY IF EXISTS "plg_forms_templates_delete" ON public.plg_forms_templates;
52
+ CREATE POLICY "plg_forms_templates_delete" ON public.plg_forms_templates
49
53
  FOR DELETE TO authenticated
50
54
  USING (tenant_id IN (SELECT public.user_tenant_ids()));
51
55
 
52
- -- frm_documents: filled form instances
53
- CREATE TABLE IF NOT EXISTS public.frm_documents (
56
+ -- plg_forms_documents: filled form instances
57
+ CREATE TABLE IF NOT EXISTS public.plg_forms_documents (
54
58
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
55
- tenant_id uuid NOT NULL REFERENCES saas_core.tenants(id) ON DELETE CASCADE,
56
- template_id uuid NOT NULL REFERENCES public.frm_templates(id),
57
- person_id uuid REFERENCES saas_core.persons(id) ON DELETE SET NULL,
59
+ tenant_id uuid NOT NULL REFERENCES public.tenants(id) ON DELETE CASCADE,
60
+ template_id uuid NOT NULL REFERENCES public.plg_forms_templates(id),
61
+ person_id uuid REFERENCES public.people(id) ON DELETE SET NULL,
58
62
  title text,
59
63
  data jsonb NOT NULL DEFAULT '{}',
60
64
  status text NOT NULL DEFAULT 'draft',
@@ -69,35 +73,51 @@ CREATE TABLE IF NOT EXISTS public.frm_documents (
69
73
  updated_at timestamptz NOT NULL DEFAULT now()
70
74
  );
71
75
 
72
- ALTER TABLE public.frm_documents ENABLE ROW LEVEL SECURITY;
76
+ ALTER TABLE public.plg_forms_documents ENABLE ROW LEVEL SECURITY;
73
77
 
74
- CREATE INDEX IF NOT EXISTS idx_frm_documents_tenant
75
- ON public.frm_documents(tenant_id);
76
- CREATE INDEX IF NOT EXISTS idx_frm_documents_person
77
- ON public.frm_documents(person_id);
78
- CREATE INDEX IF NOT EXISTS idx_frm_documents_template
79
- ON public.frm_documents(template_id);
80
- CREATE INDEX IF NOT EXISTS idx_frm_documents_status
81
- ON public.frm_documents(tenant_id, status);
78
+ CREATE INDEX IF NOT EXISTS idx_plg_forms_documents_tenant
79
+ ON public.plg_forms_documents(tenant_id);
80
+ -- person_id/status only exist in the pre-archetype shape this file creates;
81
+ -- converted pools (salon) already carry the archetype extension shape
82
+ -- (document_id PK, no person_id) — 002 owns that shape, so guard these.
83
+ DO $$
84
+ BEGIN
85
+ IF EXISTS (
86
+ SELECT 1 FROM information_schema.columns
87
+ WHERE table_schema = 'public' AND table_name = 'plg_forms_documents'
88
+ AND column_name = 'person_id'
89
+ ) THEN
90
+ EXECUTE 'CREATE INDEX IF NOT EXISTS idx_plg_forms_documents_person
91
+ ON public.plg_forms_documents(person_id)';
92
+ EXECUTE 'CREATE INDEX IF NOT EXISTS idx_plg_forms_documents_status
93
+ ON public.plg_forms_documents(tenant_id, status)';
94
+ END IF;
95
+ END $$;
96
+ CREATE INDEX IF NOT EXISTS idx_plg_forms_documents_template
97
+ ON public.plg_forms_documents(template_id);
82
98
 
83
- CREATE POLICY "frm_documents_select" ON public.frm_documents
99
+ DROP POLICY IF EXISTS "plg_forms_documents_select" ON public.plg_forms_documents;
100
+ CREATE POLICY "plg_forms_documents_select" ON public.plg_forms_documents
84
101
  FOR SELECT TO authenticated
85
102
  USING (tenant_id IN (SELECT public.user_tenant_ids()));
86
- CREATE POLICY "frm_documents_insert" ON public.frm_documents
103
+ DROP POLICY IF EXISTS "plg_forms_documents_insert" ON public.plg_forms_documents;
104
+ CREATE POLICY "plg_forms_documents_insert" ON public.plg_forms_documents
87
105
  FOR INSERT TO authenticated
88
106
  WITH CHECK (tenant_id IN (SELECT public.user_tenant_ids()));
89
- CREATE POLICY "frm_documents_update" ON public.frm_documents
107
+ DROP POLICY IF EXISTS "plg_forms_documents_update" ON public.plg_forms_documents;
108
+ CREATE POLICY "plg_forms_documents_update" ON public.plg_forms_documents
90
109
  FOR UPDATE TO authenticated
91
110
  USING (tenant_id IN (SELECT public.user_tenant_ids()));
92
- CREATE POLICY "frm_documents_delete" ON public.frm_documents
111
+ DROP POLICY IF EXISTS "plg_forms_documents_delete" ON public.plg_forms_documents;
112
+ CREATE POLICY "plg_forms_documents_delete" ON public.plg_forms_documents
93
113
  FOR DELETE TO authenticated
94
114
  USING (tenant_id IN (SELECT public.user_tenant_ids()));
95
115
 
96
- -- frm_document_files: file attachments for image/gallery/drawing fields
97
- CREATE TABLE IF NOT EXISTS public.frm_document_files (
116
+ -- plg_forms_document_files: file attachments for image/gallery/drawing fields
117
+ CREATE TABLE IF NOT EXISTS public.plg_forms_document_files (
98
118
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
99
- tenant_id uuid NOT NULL REFERENCES saas_core.tenants(id) ON DELETE CASCADE,
100
- document_id uuid NOT NULL REFERENCES public.frm_documents(id) ON DELETE CASCADE,
119
+ tenant_id uuid NOT NULL REFERENCES public.tenants(id) ON DELETE CASCADE,
120
+ document_id uuid NOT NULL REFERENCES public.plg_forms_documents(id) ON DELETE CASCADE,
101
121
  field_key text NOT NULL,
102
122
  file_url text NOT NULL,
103
123
  file_name text,
@@ -108,31 +128,47 @@ CREATE TABLE IF NOT EXISTS public.frm_document_files (
108
128
  created_at timestamptz NOT NULL DEFAULT now()
109
129
  );
110
130
 
111
- ALTER TABLE public.frm_document_files ENABLE ROW LEVEL SECURITY;
131
+ ALTER TABLE public.plg_forms_document_files ENABLE ROW LEVEL SECURITY;
112
132
 
113
- CREATE INDEX IF NOT EXISTS idx_frm_document_files_document
114
- ON public.frm_document_files(document_id);
133
+ CREATE INDEX IF NOT EXISTS idx_plg_forms_document_files_document
134
+ ON public.plg_forms_document_files(document_id);
115
135
 
116
- CREATE POLICY "frm_document_files_select" ON public.frm_document_files
136
+ DROP POLICY IF EXISTS "plg_forms_document_files_select" ON public.plg_forms_document_files;
137
+ CREATE POLICY "plg_forms_document_files_select" ON public.plg_forms_document_files
117
138
  FOR SELECT TO authenticated
118
139
  USING (tenant_id IN (SELECT public.user_tenant_ids()));
119
- CREATE POLICY "frm_document_files_insert" ON public.frm_document_files
140
+ DROP POLICY IF EXISTS "plg_forms_document_files_insert" ON public.plg_forms_document_files;
141
+ CREATE POLICY "plg_forms_document_files_insert" ON public.plg_forms_document_files
120
142
  FOR INSERT TO authenticated
121
143
  WITH CHECK (tenant_id IN (SELECT public.user_tenant_ids()));
122
- CREATE POLICY "frm_document_files_update" ON public.frm_document_files
144
+ DROP POLICY IF EXISTS "plg_forms_document_files_update" ON public.plg_forms_document_files;
145
+ CREATE POLICY "plg_forms_document_files_update" ON public.plg_forms_document_files
123
146
  FOR UPDATE TO authenticated
124
147
  USING (tenant_id IN (SELECT public.user_tenant_ids()));
125
- CREATE POLICY "frm_document_files_delete" ON public.frm_document_files
148
+ DROP POLICY IF EXISTS "plg_forms_document_files_delete" ON public.plg_forms_document_files;
149
+ CREATE POLICY "plg_forms_document_files_delete" ON public.plg_forms_document_files
126
150
  FOR DELETE TO authenticated
127
151
  USING (tenant_id IN (SELECT public.user_tenant_ids()));
128
152
 
129
- -- View: frm_documents with template name joined
130
- CREATE OR REPLACE VIEW public.v_frm_documents AS
131
- SELECT
132
- d.*,
133
- t.name AS template_name,
134
- t.category AS template_category,
135
- p.name AS person_name
136
- FROM public.frm_documents d
137
- LEFT JOIN public.frm_templates t ON t.id = d.template_id
138
- LEFT JOIN saas_core.persons p ON p.id = d.person_id;
153
+ -- View: pre-archetype read model (needs person_id; archetype pools get
154
+ -- v_documents from 002 instead, which also drops this view when migrating).
155
+ DO $$
156
+ BEGIN
157
+ IF EXISTS (
158
+ SELECT 1 FROM information_schema.columns
159
+ WHERE table_schema = 'public' AND table_name = 'plg_forms_documents'
160
+ AND column_name = 'person_id'
161
+ ) THEN
162
+ EXECUTE $v$
163
+ CREATE OR REPLACE VIEW public.v_frm_documents AS
164
+ SELECT
165
+ d.*,
166
+ t.name AS template_name,
167
+ t.category AS template_category,
168
+ p.name AS person_name
169
+ FROM public.plg_forms_documents d
170
+ LEFT JOIN public.plg_forms_templates t ON t.id = d.template_id
171
+ LEFT JOIN public.people p ON p.id = d.person_id
172
+ $v$;
173
+ END IF;
174
+ END $$;
@@ -1,16 +1,26 @@
1
1
  -- ============================================================
2
- -- Document Archetype — saas_core.documents
2
+ -- Document Archetype — public.documents
3
3
  -- A document is any record associated with a person (or standalone):
4
4
  -- forms, images, attachments, prescriptions, contracts, etc.
5
5
  -- The `kind` column discriminates the type.
6
+ --
7
+ -- DATA SAFETY (converted pools): plg_forms_documents may already hold REAL
8
+ -- rows in its PRE-archetype shape (its own `id` PK, no `document_id` column —
9
+ -- see 001_frm_base.sql). This migration MIGRATES those rows into the new
10
+ -- document archetype instead of dropping them:
11
+ -- * pre-archetype + rows -> copy each legacy row into public.documents
12
+ -- (reusing its id) + into the new extension
13
+ -- table, migrate its files, then drop legacy.
14
+ -- * pre-archetype + empty -> plain drop + create (fresh shape).
15
+ -- * already new shape -> no-op.
6
16
  -- ============================================================
7
17
 
8
- CREATE TABLE IF NOT EXISTS saas_core.documents (
18
+ CREATE TABLE IF NOT EXISTS public.documents (
9
19
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
10
- tenant_id uuid NOT NULL REFERENCES saas_core.tenants(id) ON DELETE CASCADE,
20
+ tenant_id uuid NOT NULL REFERENCES public.tenants(id) ON DELETE CASCADE,
11
21
  kind text NOT NULL DEFAULT 'attachment',
12
22
  -- kind: 'form', 'image', 'attachment', 'prescription', 'contract', etc.
13
- person_id uuid REFERENCES saas_core.persons(id) ON DELETE SET NULL,
23
+ person_id uuid REFERENCES public.people(id) ON DELETE SET NULL,
14
24
  title text,
15
25
  description text,
16
26
  status text NOT NULL DEFAULT 'draft',
@@ -29,39 +39,63 @@ CREATE TABLE IF NOT EXISTS saas_core.documents (
29
39
  updated_at timestamptz NOT NULL DEFAULT now()
30
40
  );
31
41
 
32
- ALTER TABLE saas_core.documents ENABLE ROW LEVEL SECURITY;
42
+ ALTER TABLE public.documents ENABLE ROW LEVEL SECURITY;
33
43
 
34
- CREATE INDEX IF NOT EXISTS idx_documents_tenant ON saas_core.documents(tenant_id, kind);
35
- CREATE INDEX IF NOT EXISTS idx_documents_person ON saas_core.documents(person_id);
36
- CREATE INDEX IF NOT EXISTS idx_documents_status ON saas_core.documents(tenant_id, status);
44
+ CREATE INDEX IF NOT EXISTS idx_documents_tenant ON public.documents(tenant_id, kind);
45
+ CREATE INDEX IF NOT EXISTS idx_documents_person ON public.documents(person_id);
46
+ CREATE INDEX IF NOT EXISTS idx_documents_status ON public.documents(tenant_id, status);
37
47
 
38
- CREATE POLICY "documents_select" ON saas_core.documents
48
+ DROP POLICY IF EXISTS "documents_select" ON public.documents;
49
+ CREATE POLICY "documents_select" ON public.documents
39
50
  FOR SELECT TO authenticated
40
51
  USING (tenant_id IN (SELECT public.user_tenant_ids()));
41
- CREATE POLICY "documents_insert" ON saas_core.documents
52
+ DROP POLICY IF EXISTS "documents_insert" ON public.documents;
53
+ CREATE POLICY "documents_insert" ON public.documents
42
54
  FOR INSERT TO authenticated
43
55
  WITH CHECK (tenant_id IN (SELECT public.user_tenant_ids()));
44
- CREATE POLICY "documents_update" ON saas_core.documents
56
+ DROP POLICY IF EXISTS "documents_update" ON public.documents;
57
+ CREATE POLICY "documents_update" ON public.documents
45
58
  FOR UPDATE TO authenticated
46
59
  USING (tenant_id IN (SELECT public.user_tenant_ids()));
47
- CREATE POLICY "documents_delete" ON saas_core.documents
60
+ DROP POLICY IF EXISTS "documents_delete" ON public.documents;
61
+ CREATE POLICY "documents_delete" ON public.documents
48
62
  FOR DELETE TO authenticated
49
63
  USING (tenant_id IN (SELECT public.user_tenant_ids()));
50
64
 
51
65
  -- ============================================================
52
- -- Alter frm_documents to become extension table for 'form' kind
66
+ -- Reconcile plg_forms_documents / plg_forms_document_files with the archetype.
53
67
  -- ============================================================
54
68
 
55
- -- Drop old frm_documents and recreate as extension
56
- DROP VIEW IF EXISTS public.v_frm_documents;
57
- DROP TABLE IF EXISTS public.frm_document_files;
58
- DROP TABLE IF EXISTS public.frm_documents;
69
+ -- Step 1: if a PRE-archetype plg_forms_documents is present (no document_id
70
+ -- column), rename it (and its files table + dependent view) out of the way so
71
+ -- the new archetype-shaped tables can be created and — when it holds rows —
72
+ -- its data copied across. Fully guarded, so already-migrated pools skip this.
73
+ DO $$
74
+ BEGIN
75
+ IF to_regclass('public.plg_forms_documents') IS NOT NULL
76
+ AND NOT EXISTS (
77
+ SELECT 1 FROM information_schema.columns
78
+ WHERE table_schema = 'public'
79
+ AND table_name = 'plg_forms_documents'
80
+ AND column_name = 'document_id'
81
+ ) THEN
82
+ DROP VIEW IF EXISTS public.v_frm_documents;
83
+ ALTER TABLE public.plg_forms_documents RENAME TO plg_forms_documents_legacy;
84
+ IF to_regclass('public.plg_forms_document_files') IS NOT NULL THEN
85
+ ALTER TABLE public.plg_forms_document_files RENAME TO plg_forms_document_files_legacy;
86
+ END IF;
87
+ END IF;
88
+ END $$;
59
89
 
60
- -- frm_documents: extension table for form-type documents
61
- CREATE TABLE public.frm_documents (
62
- document_id uuid PRIMARY KEY REFERENCES saas_core.documents(id) ON DELETE CASCADE,
63
- tenant_id uuid NOT NULL REFERENCES saas_core.tenants(id) ON DELETE CASCADE,
64
- template_id uuid NOT NULL REFERENCES public.frm_templates(id),
90
+ -- Step 2: create the new archetype-shaped extension tables. IF NOT EXISTS makes
91
+ -- this a no-op on pools already migrated to the new shape; after Step 1 the
92
+ -- names are free on pre-archetype pools.
93
+
94
+ -- plg_forms_documents: extension table for form-type documents
95
+ CREATE TABLE IF NOT EXISTS public.plg_forms_documents (
96
+ document_id uuid PRIMARY KEY REFERENCES public.documents(id) ON DELETE CASCADE,
97
+ tenant_id uuid NOT NULL REFERENCES public.tenants(id) ON DELETE CASCADE,
98
+ template_id uuid NOT NULL REFERENCES public.plg_forms_templates(id),
65
99
  data jsonb NOT NULL DEFAULT '{}',
66
100
  signed_at timestamptz,
67
101
  signed_by uuid,
@@ -69,28 +103,32 @@ CREATE TABLE public.frm_documents (
69
103
  updated_at timestamptz NOT NULL DEFAULT now()
70
104
  );
71
105
 
72
- ALTER TABLE public.frm_documents ENABLE ROW LEVEL SECURITY;
106
+ ALTER TABLE public.plg_forms_documents ENABLE ROW LEVEL SECURITY;
73
107
 
74
- CREATE INDEX IF NOT EXISTS idx_frm_documents_template ON public.frm_documents(template_id);
108
+ CREATE INDEX IF NOT EXISTS idx_plg_forms_documents_template ON public.plg_forms_documents(template_id);
75
109
 
76
- CREATE POLICY "frm_documents_select" ON public.frm_documents
110
+ DROP POLICY IF EXISTS "plg_forms_documents_select" ON public.plg_forms_documents;
111
+ CREATE POLICY "plg_forms_documents_select" ON public.plg_forms_documents
77
112
  FOR SELECT TO authenticated
78
113
  USING (tenant_id IN (SELECT public.user_tenant_ids()));
79
- CREATE POLICY "frm_documents_insert" ON public.frm_documents
114
+ DROP POLICY IF EXISTS "plg_forms_documents_insert" ON public.plg_forms_documents;
115
+ CREATE POLICY "plg_forms_documents_insert" ON public.plg_forms_documents
80
116
  FOR INSERT TO authenticated
81
117
  WITH CHECK (tenant_id IN (SELECT public.user_tenant_ids()));
82
- CREATE POLICY "frm_documents_update" ON public.frm_documents
118
+ DROP POLICY IF EXISTS "plg_forms_documents_update" ON public.plg_forms_documents;
119
+ CREATE POLICY "plg_forms_documents_update" ON public.plg_forms_documents
83
120
  FOR UPDATE TO authenticated
84
121
  USING (tenant_id IN (SELECT public.user_tenant_ids()));
85
- CREATE POLICY "frm_documents_delete" ON public.frm_documents
122
+ DROP POLICY IF EXISTS "plg_forms_documents_delete" ON public.plg_forms_documents;
123
+ CREATE POLICY "plg_forms_documents_delete" ON public.plg_forms_documents
86
124
  FOR DELETE TO authenticated
87
125
  USING (tenant_id IN (SELECT public.user_tenant_ids()));
88
126
 
89
- -- frm_document_files: file attachments for form fields
90
- CREATE TABLE public.frm_document_files (
127
+ -- plg_forms_document_files: file attachments for form fields
128
+ CREATE TABLE IF NOT EXISTS public.plg_forms_document_files (
91
129
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
92
- tenant_id uuid NOT NULL REFERENCES saas_core.tenants(id) ON DELETE CASCADE,
93
- document_id uuid NOT NULL REFERENCES saas_core.documents(id) ON DELETE CASCADE,
130
+ tenant_id uuid NOT NULL REFERENCES public.tenants(id) ON DELETE CASCADE,
131
+ document_id uuid NOT NULL REFERENCES public.documents(id) ON DELETE CASCADE,
94
132
  field_key text NOT NULL,
95
133
  file_url text NOT NULL,
96
134
  file_name text,
@@ -101,23 +139,73 @@ CREATE TABLE public.frm_document_files (
101
139
  created_at timestamptz NOT NULL DEFAULT now()
102
140
  );
103
141
 
104
- ALTER TABLE public.frm_document_files ENABLE ROW LEVEL SECURITY;
142
+ ALTER TABLE public.plg_forms_document_files ENABLE ROW LEVEL SECURITY;
105
143
 
106
- CREATE INDEX IF NOT EXISTS idx_frm_document_files_document ON public.frm_document_files(document_id);
144
+ CREATE INDEX IF NOT EXISTS idx_plg_forms_document_files_document ON public.plg_forms_document_files(document_id);
107
145
 
108
- CREATE POLICY "frm_document_files_select" ON public.frm_document_files
146
+ DROP POLICY IF EXISTS "plg_forms_document_files_select" ON public.plg_forms_document_files;
147
+ CREATE POLICY "plg_forms_document_files_select" ON public.plg_forms_document_files
109
148
  FOR SELECT TO authenticated
110
149
  USING (tenant_id IN (SELECT public.user_tenant_ids()));
111
- CREATE POLICY "frm_document_files_insert" ON public.frm_document_files
150
+ DROP POLICY IF EXISTS "plg_forms_document_files_insert" ON public.plg_forms_document_files;
151
+ CREATE POLICY "plg_forms_document_files_insert" ON public.plg_forms_document_files
112
152
  FOR INSERT TO authenticated
113
153
  WITH CHECK (tenant_id IN (SELECT public.user_tenant_ids()));
114
- CREATE POLICY "frm_document_files_update" ON public.frm_document_files
154
+ DROP POLICY IF EXISTS "plg_forms_document_files_update" ON public.plg_forms_document_files;
155
+ CREATE POLICY "plg_forms_document_files_update" ON public.plg_forms_document_files
115
156
  FOR UPDATE TO authenticated
116
157
  USING (tenant_id IN (SELECT public.user_tenant_ids()));
117
- CREATE POLICY "frm_document_files_delete" ON public.frm_document_files
158
+ DROP POLICY IF EXISTS "plg_forms_document_files_delete" ON public.plg_forms_document_files;
159
+ CREATE POLICY "plg_forms_document_files_delete" ON public.plg_forms_document_files
118
160
  FOR DELETE TO authenticated
119
161
  USING (tenant_id IN (SELECT public.user_tenant_ids()));
120
162
 
163
+ -- Step 3: if legacy rows exist, migrate them, then drop the quarantined legacy
164
+ -- tables. Reusing each legacy id as the documents.id keeps every legacy file's
165
+ -- document_id FK valid after the copy. ON CONFLICT DO NOTHING makes a partial
166
+ -- re-run safe.
167
+ DO $$
168
+ BEGIN
169
+ IF to_regclass('public.plg_forms_documents_legacy') IS NOT NULL THEN
170
+ -- 3a. base document (one per legacy form document; is_active mirrors NOT is_deleted)
171
+ INSERT INTO public.documents (
172
+ id, tenant_id, kind, person_id, title, status, notes, metadata,
173
+ is_active, created_by, updated_by, created_at, updated_at
174
+ )
175
+ SELECT
176
+ id, tenant_id, 'form', person_id, title, status, notes, metadata,
177
+ NOT COALESCE(is_deleted, false), created_by, updated_by, created_at, updated_at
178
+ FROM public.plg_forms_documents_legacy
179
+ ON CONFLICT (id) DO NOTHING;
180
+
181
+ -- 3b. form extension row (keyed by the reused document id)
182
+ INSERT INTO public.plg_forms_documents (
183
+ document_id, tenant_id, template_id, data, signed_at, signed_by, created_at, updated_at
184
+ )
185
+ SELECT
186
+ id, tenant_id, template_id, data, signed_at, signed_by, created_at, updated_at
187
+ FROM public.plg_forms_documents_legacy
188
+ ON CONFLICT (document_id) DO NOTHING;
189
+
190
+ -- 3c. file attachments (document_id already points at the reused id)
191
+ IF to_regclass('public.plg_forms_document_files_legacy') IS NOT NULL THEN
192
+ INSERT INTO public.plg_forms_document_files (
193
+ id, tenant_id, document_id, field_key, file_url, file_name,
194
+ file_size, mime_type, sort_order, metadata, created_at
195
+ )
196
+ SELECT
197
+ id, tenant_id, document_id, field_key, file_url, file_name,
198
+ file_size, mime_type, sort_order, metadata, created_at
199
+ FROM public.plg_forms_document_files_legacy
200
+ ON CONFLICT (id) DO NOTHING;
201
+ END IF;
202
+ END IF;
203
+
204
+ -- Drop legacy remnants now that any data has been copied across.
205
+ DROP TABLE IF EXISTS public.plg_forms_document_files_legacy;
206
+ DROP TABLE IF EXISTS public.plg_forms_documents_legacy;
207
+ END $$;
208
+
121
209
  -- View: all documents for a person with form data joined when applicable
122
210
  CREATE OR REPLACE VIEW public.v_documents AS
123
211
  SELECT
@@ -129,7 +217,7 @@ SELECT
129
217
  t.name AS template_name,
130
218
  t.category AS template_category,
131
219
  p.name AS person_name
132
- FROM saas_core.documents d
133
- LEFT JOIN public.frm_documents f ON f.document_id = d.id
134
- LEFT JOIN public.frm_templates t ON t.id = f.template_id
135
- LEFT JOIN saas_core.persons p ON p.id = d.person_id;
220
+ FROM public.documents d
221
+ LEFT JOIN public.plg_forms_documents f ON f.document_id = d.id
222
+ LEFT JOIN public.plg_forms_templates t ON t.id = f.template_id
223
+ LEFT JOIN public.people p ON p.id = d.person_id;