@davoodmehraban89 / Factor-easy-2 Mirror · read-only / commits / d570548
Fork 0

سامانه صدور فاکتور و حسابداری

Mirrored from https://github.com/davoodmehraban89/Factor-easy.git

Synced Dec 3, 18:15 UTC

feat: add non-destructive D2 legacy request cutover

davoodmehraban89 committed Oct 1, 2026 at 22:39 UTC d57054888ce5efd4a88dc8ba4d264d6d7246b5a5
1 file changed +121
supabase/migrations/20261001223500_request_workflow_d2_legacy_cutover.sql new
+121
@@ -0,0 +1,121 @@
1 +-- Slice D2: non-destructive legacy request cutover into the D1 server model.
2 +-- Legacy records remain in public.records for rollback/audit; imported rows are traceable and idempotent.
3 +
4 +alter table public.request_type_definitions add column if not exists legacy_record_id text;
5 +alter table public.request_instances add column if not exists legacy_record_id text;
6 +alter table public.request_instances add column if not exists legacy_number text;
7 +
8 +create unique index if not exists request_type_definitions_org_legacy_record_uidx
9 + on public.request_type_definitions(organization_id,legacy_record_id) where legacy_record_id is not null;
10 +create unique index if not exists request_instances_org_legacy_record_uidx
11 + on public.request_instances(organization_id,legacy_record_id) where legacy_record_id is not null;
12 +
13 +insert into public.request_type_definitions(
14 + id,organization_id,code,title,category,revision,active,created_by,created_at,updated_at,legacy_record_id
15 +)
16 +select
17 + md5('legacy-request-type:'||r.organization_id::text||':'||r.id)::uuid,
18 + r.organization_id,
19 + upper(trim(r.data->>'code')),
20 + trim(r.data->>'title'),
21 + nullif(trim(r.data->>'category'),''),
22 + 1,
23 + coalesce((r.data->>'active')::boolean,true),
24 + r.owner_id,
25 + coalesce(nullif(r.data->>'createdAt','')::timestamptz,now()),
26 + coalesce(nullif(r.data->>'createdAt','')::timestamptz,now()),
27 + r.id
28 +from public.records r
29 +where r.collection='requestTypes'
30 + and nullif(trim(r.data->>'code'),'') is not null
31 + and nullif(trim(r.data->>'title'),'') is not null
32 +on conflict do nothing;
33 +
34 +insert into public.request_type_versions(
35 + id,organization_id,request_type_id,version_no,title,category,form_schema,published_by,published_at
36 +)
37 +select
38 + md5('legacy-request-version:'||r.organization_id::text||':'||r.id)::uuid,
39 + r.organization_id,
40 + d.id,
41 + 1,
42 + d.title,
43 + d.category,
44 + jsonb_build_object('schemaVersion',1,'fields',case when jsonb_typeof(r.data->'fields')='array' then r.data->'fields' else '[]'::jsonb end,'legacyImported',true),
45 + r.owner_id,
46 + coalesce(nullif(r.data->>'createdAt','')::timestamptz,now())
47 +from public.records r
48 +join public.request_type_definitions d
49 + on d.organization_id=r.organization_id
50 + and (d.legacy_record_id=r.id or d.code=upper(trim(r.data->>'code')))
51 +where r.collection='requestTypes'
52 +on conflict do nothing;
53 +
54 +insert into public.request_workflow_versions(
55 + id,organization_id,request_type_version_id,workflow_schema,published_by,published_at
56 +)
57 +select
58 + md5('legacy-request-workflow:'||r.organization_id::text||':'||r.id)::uuid,
59 + r.organization_id,
60 + v.id,
61 + case when jsonb_typeof(r.data->'workflow')='object' then (r.data->'workflow')||jsonb_build_object('schemaVersion',1,'legacyImported',true) else jsonb_build_object('schemaVersion',1,'mode','single_owner','steps','[]'::jsonb,'legacyImported',true) end,
62 + r.owner_id,
63 + coalesce(nullif(r.data->>'createdAt','')::timestamptz,now())
64 +from public.records r
65 +join public.request_type_definitions d
66 + on d.organization_id=r.organization_id
67 + and (d.legacy_record_id=r.id or d.code=upper(trim(r.data->>'code')))
68 +join public.request_type_versions v on v.request_type_id=d.id and v.version_no=1
69 +where r.collection='requestTypes'
70 +on conflict do nothing;
71 +
72 +insert into public.request_instances(
73 + id,organization_id,request_type_version_id,workflow_version_id,requester_user_id,values_json,status,revision,idempotency_key,created_at,updated_at,legacy_record_id,legacy_number
74 +)
75 +select
76 + md5('legacy-request-instance:'||r.organization_id::text||':'||r.id)::uuid,
77 + r.organization_id,
78 + v.id,
79 + w.id,
80 + r.owner_id,
81 + case when jsonb_typeof(r.data->'values')='object' then r.data->'values' else '{}'::jsonb end,
82 + case when lower(coalesce(r.data->>'status','submitted')) in ('draft','submitted','approved','rejected','returned','cancelled') then lower(coalesce(r.data->>'status','submitted')) else 'submitted' end,
83 + 1,
84 + 'legacy:'||md5(r.organization_id::text||':'||r.id),
85 + coalesce(nullif(r.data->>'createdAt','')::timestamptz,now()),
86 + coalesce(nullif(r.data->>'submittedAt','')::timestamptz,nullif(r.data->>'createdAt','')::timestamptz,now()),
87 + r.id,
88 + nullif(trim(r.data->>'number'),'')
89 +from public.records r
90 +join public.records rt
91 + on rt.organization_id=r.organization_id
92 + and rt.collection='requestTypes'
93 + and rt.id=r.data->>'requestTypeId'
94 +join public.request_type_definitions d
95 + on d.organization_id=rt.organization_id
96 + and (d.legacy_record_id=rt.id or d.code=upper(trim(rt.data->>'code')))
97 +join public.request_type_versions v on v.request_type_id=d.id and v.version_no=1
98 +join public.request_workflow_versions w on w.request_type_version_id=v.id
99 +where r.collection='requests'
100 +on conflict do nothing;
101 +
102 +insert into public.request_instance_events(
103 + id,organization_id,request_instance_id,actor_user_id,action,from_status,to_status,revision,note,idempotency_key,created_at
104 +)
105 +select
106 + md5('legacy-request-event:'||r.organization_id::text||':'||r.id)::uuid,
107 + r.organization_id,
108 + i.id,
109 + r.owner_id,
110 + 'legacy_import',
111 + null,
112 + i.status,
113 + 1,
114 + 'Imported non-destructively from legacy records collection',
115 + 'legacy-event:'||md5(r.organization_id::text||':'||r.id),
116 + i.created_at
117 +from public.records r
118 +join public.request_instances i
119 + on i.organization_id=r.organization_id and i.legacy_record_id=r.id
120 +where r.collection='requests'
121 +on conflict do nothing;