@company-six/qbosync 0.7.0 → 0.10.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.
@@ -1,260 +1,260 @@
1
1
  -- An app's daily report: each morning, the invoices, credit memos, bills and vendor credits the app created the day
2
2
  -- before, set beside what QBO Sync did with them. Optional; run this once in the app's own Postgres database (the Supabase
3
- -- SQL editor, for instance) if the app keeps one. One transaction; re-running it is a no-op. It does not need 001_connection.sql.
4
- --
5
- -- qbosync_report_runs one row per run of the report: the day it covers, whether it is running, finished or failed,
6
- -- and a finished day's totals by result, for the page that lists the days.
7
- -- qbosync_report_rows one row per document per run, holding what was true THAT morning, so a past report can be
8
- -- opened again and still shows what it showed then.
9
- --
10
- -- The SDK's guide ("A daily report") says how a run fills them. In short:
11
- --
12
- -- select * from qbosync_report_start('2026-11-08', 'America/Chicago'); -- the run, with that day's window in UTC
13
- -- -- list the documents your app created in [window_start, window_end), ask QBO Sync, compare, then
14
- -- insert into qbosync_report_rows (run_id, ...) values (...); -- one per document
15
- -- select qbosync_report_finish(run_id, '{"documents": 12, "matches": 11, "failed": 1}');
16
- -- -- or, when the day cannot be built:
17
- -- select qbosync_report_fail(run_id, 'QBO Sync could not be reached');
18
- --
19
- -- Rules the database keeps:
20
- -- - ONE run is running at a time: two jobs, or a manual run during the scheduled one, never overlap;
21
- -- - ONE finished report per day: finishing a second report for a day replaces the first in the same transaction,
22
- -- so the day always has one, never none;
23
- -- - a run still running after 30 minutes (its server died) is marked failed by the next start;
24
- -- - rows go only into a running run, and a failed run keeps none.
25
- --
26
- -- The tables have row level security on and no policy, and every function is granted to service_role only: your server
27
- -- reads and writes them, and shows each person only their own companies' rows. Outside Supabase, grant the tables and
28
- -- functions to the role your server connects with.
29
- --
30
- -- Undo (nothing else depends on these):
31
- -- drop function public.qbosync_report_start(date, text);
32
- -- drop function public.qbosync_report_finish(uuid, jsonb);
33
- -- drop function public.qbosync_report_fail(uuid, text);
34
- -- drop table public.qbosync_report_rows;
35
- -- drop table public.qbosync_report_runs;
36
- -- drop function public.qbosync_report_rows_only_running();
37
-
38
- begin;
39
-
40
- -- ── 1. Runs ─────────────────────────────────────────────────────────────────────────
41
-
42
- create table if not exists public.qbosync_report_runs (
43
- id uuid primary key default gen_random_uuid(),
44
- -- The day reported, in the app's own time zone.
45
- report_date date not null,
46
- -- That zone (an IANA name, such as America/Chicago) and the day's midnight to midnight in it, in UTC. Worked out by
47
- -- qbosync_report_start, so daylight saving is the database's job, not the app's.
48
- time_zone text not null,
49
- window_start timestamptz not null,
50
- window_end timestamptz not null,
51
- status text not null default 'running',
52
- started_at timestamptz not null default now(),
53
- finished_at timestamptz,
54
- -- Why a failed run failed, for the page: a day that failed stays listed with this until a later run makes it up.
55
- error text,
56
- -- A finished run's totals: {"documents": 12, "matches": 11, "failed": 1, ...}.
57
- counts jsonb,
58
- constraint qbosync_report_runs_status_check check (status in ('running', 'done', 'failed')),
59
- constraint qbosync_report_runs_window_check check (window_start < window_end),
60
- constraint qbosync_report_runs_time_zone_check check (length(btrim(time_zone)) > 0),
61
- constraint qbosync_report_runs_shape_check check (
62
- (status = 'running' and finished_at is null and error is null and counts is null)
63
- or (status = 'done' and finished_at is not null and error is null
64
- and counts is not null and jsonb_typeof(counts) = 'object')
65
- or (status = 'failed' and finished_at is not null and counts is null
66
- and error is not null and length(btrim(error)) > 0))
67
- );
68
-
69
- -- One run at a time: every running run has the same key.
70
- create unique index if not exists qbosync_report_runs_one_running
71
- on public.qbosync_report_runs ((true)) where status = 'running';
72
- -- One finished report per day.
73
- create unique index if not exists qbosync_report_runs_one_done_per_day
74
- on public.qbosync_report_runs (report_date) where status = 'done';
75
- create index if not exists qbosync_report_runs_day_idx
76
- on public.qbosync_report_runs (report_date desc, started_at desc);
77
-
78
- -- ── 2. Rows ─────────────────────────────────────────────────────────────────────────
79
-
80
- create table if not exists public.qbosync_report_rows (
81
- id uuid primary key default gen_random_uuid(),
82
- run_id uuid not null references public.qbosync_report_runs (id) on delete cascade,
83
- document_type text not null,
84
- -- The ref your app sent the document under: what QBO Sync was asked about.
85
- source_ref text not null,
86
- -- The company, as your app names it (the document's company_key): for showing each company only its own rows.
87
- company_key text not null,
88
- -- Optional: your own id for the document (any id, as text), and what people know it by (an order number, say).
89
- document_id text,
90
- label text,
91
- -- The one result the document got. See the guide for when each applies:
92
- -- matches posted, and every amount is what your app holds
93
- -- differs posted, but an amount differs, or QuickBooks no longer holds what was posted
94
- -- changed_since_posted your document changed after the version QBO Sync posted
95
- -- not_posted QBO Sync has it and nothing is posted (waiting, syncing off, excluded, rolled back)
96
- -- failed QBO Sync tried and failed; its error is kept
97
- -- not_received QBO Sync never received it
98
- result text not null,
99
- -- As QBO Sync answered that morning.
100
- qbo_number text,
101
- sync_status text,
102
- synced_at timestamptz,
103
- -- The nightly QuickBooks check's result for what was posted; null while nothing is posted.
104
- qbo_check text,
105
- -- For "differs" only: each thing that did not match, as your app chooses to describe it (a JSON array, never empty).
106
- differences jsonb,
107
- -- QBO Sync's error for "failed"; for "not_received", your own record of why the send did not get through, if any.
108
- error text,
109
- -- Optional: your own record of sending it (shown beside "not_received").
110
- send_status text,
111
- created_at timestamptz not null default now(),
112
- constraint qbosync_report_rows_type_check check (document_type in ('invoice', 'credit_memo', 'bill', 'vendor_credit')),
113
- constraint qbosync_report_rows_ref_check check (length(btrim(source_ref)) > 0),
114
- constraint qbosync_report_rows_company_check check (length(btrim(company_key)) > 0),
115
- constraint qbosync_report_rows_result_check check (result in
116
- ('matches', 'differs', 'changed_since_posted', 'not_posted', 'failed', 'not_received')),
117
- constraint qbosync_report_rows_qbo_check_check check (
118
- qbo_check is null or qbo_check in ('matches', 'differs', 'missing', 'not_checked')),
119
- constraint qbosync_report_rows_differences_check check (
120
- (result = 'differs') = (differences is not null and jsonb_typeof(differences) = 'array' and jsonb_array_length(differences) > 0))
121
- );
122
-
123
- -- A document once per run.
124
- create unique index if not exists qbosync_report_rows_one_per_document
125
- on public.qbosync_report_rows (run_id, document_type, source_ref);
126
- create index if not exists qbosync_report_rows_company_idx
127
- on public.qbosync_report_rows (run_id, company_key);
128
-
129
- -- Rows go only into a running run: a finished report never changes, and a failed one stays empty.
130
- create or replace function public.qbosync_report_rows_only_running()
131
- returns trigger
132
- language plpgsql
133
- set search_path = public
134
- as $$
135
- begin
136
- if not exists (select 1 from public.qbosync_report_runs where id = new.run_id and status = 'running') then
137
- raise exception 'qbosync_report_rows: run % is not running', new.run_id;
138
- end if;
139
- return new;
140
- end;
141
- $$;
142
-
143
- drop trigger if exists qbosync_report_rows_only_running on public.qbosync_report_rows;
144
- create trigger qbosync_report_rows_only_running
145
- before insert or update on public.qbosync_report_rows
146
- for each row execute function public.qbosync_report_rows_only_running();
147
-
148
- alter table public.qbosync_report_runs enable row level security;
149
- alter table public.qbosync_report_rows enable row level security;
150
-
151
- -- ── 3. Starting, finishing, failing ─────────────────────────────────────────────────
152
-
153
- -- Starts a run for one day in the given time zone and answers it, with the day's window in UTC. A run still running after
154
- -- 30 minutes is marked failed first (its server died); one running for less makes this raise, so two runs never overlap.
155
- create or replace function public.qbosync_report_start(p_report_date date, p_time_zone text)
156
- returns public.qbosync_report_runs
157
- language plpgsql
158
- set search_path = public
159
- as $$
160
- declare
161
- run public.qbosync_report_runs;
162
- begin
163
- if p_report_date is null then
164
- raise exception 'qbosync_report_start: a day is required';
165
- end if;
166
- if p_time_zone is null or not exists (select 1 from pg_timezone_names where name = p_time_zone) then
167
- raise exception 'qbosync_report_start: % is not a time zone name (such as America/Chicago)', coalesce(p_time_zone, 'null');
168
- end if;
169
- update public.qbosync_report_runs
170
- set status = 'failed', finished_at = now(), error = 'abandoned: still running after 30 minutes'
171
- where status = 'running' and started_at < now() - interval '30 minutes';
172
- -- An abandoned run keeps no rows, like any failed run.
173
- delete from public.qbosync_report_rows r
174
- using public.qbosync_report_runs u
175
- where r.run_id = u.id and u.status = 'failed';
176
-
177
- insert into public.qbosync_report_runs (report_date, time_zone, window_start, window_end)
178
- values (p_report_date,
179
- p_time_zone,
180
- (p_report_date::timestamp) at time zone p_time_zone,
181
- ((p_report_date + 1)::timestamp) at time zone p_time_zone)
182
- returning * into run;
183
- return run;
184
- exception when unique_violation then
185
- raise exception 'qbosync_report_start: another report is running';
186
- end;
187
- $$;
188
-
189
- -- Finishes a running run with its totals. An earlier finished report for the same day is deleted in the same transaction
190
- -- (its rows with it), so the day always has exactly one, never none while a rerun is being finished.
191
- create or replace function public.qbosync_report_finish(p_run_id uuid, p_counts jsonb)
192
- returns void
193
- language plpgsql
194
- set search_path = public
195
- as $$
196
- declare
197
- day date;
198
- begin
199
- if p_counts is null or jsonb_typeof(p_counts) <> 'object' then
200
- raise exception 'qbosync_report_finish: counts must be a JSON object';
201
- end if;
202
- select report_date into day from public.qbosync_report_runs where id = p_run_id and status = 'running' for update;
203
- if not found then
204
- raise exception 'qbosync_report_finish: run % is not running', p_run_id;
205
- end if;
206
- delete from public.qbosync_report_runs where report_date = day and status = 'done';
207
- update public.qbosync_report_runs
208
- set status = 'done', finished_at = now(), counts = p_counts
209
- where id = p_run_id;
210
- end;
211
- $$;
212
-
213
- -- Marks a running run failed, keeping why, and drops the rows it had written. An earlier finished report for the day stays.
214
- create or replace function public.qbosync_report_fail(p_run_id uuid, p_error text)
215
- returns void
216
- language plpgsql
217
- set search_path = public
218
- as $$
219
- begin
220
- delete from public.qbosync_report_rows where run_id = p_run_id;
221
- update public.qbosync_report_runs
222
- set status = 'failed', finished_at = now(),
223
- error = coalesce(nullif(btrim(p_error), ''), 'the report could not be built')
224
- where id = p_run_id and status = 'running';
225
- end;
226
- $$;
227
-
228
- do $$
229
- declare
230
- fn text;
231
- tbl text;
232
- r text;
233
- begin
234
- foreach tbl in array array['public.qbosync_report_runs', 'public.qbosync_report_rows'] loop
235
- execute format('revoke all on table %s from public', tbl);
236
- foreach r in array array['anon', 'authenticated'] loop
237
- if exists (select 1 from pg_roles where rolname = r) then
238
- execute format('revoke all on table %s from %I', tbl, r);
239
- end if;
240
- end loop;
241
- end loop;
242
- foreach fn in array array[
243
- 'public.qbosync_report_start(date, text)',
244
- 'public.qbosync_report_finish(uuid, jsonb)',
245
- 'public.qbosync_report_fail(uuid, text)',
246
- 'public.qbosync_report_rows_only_running()'
247
- ] loop
248
- execute format('revoke all on function %s from public', fn);
249
- foreach r in array array['anon', 'authenticated'] loop
250
- if exists (select 1 from pg_roles where rolname = r) then
251
- execute format('revoke all on function %s from %I', fn, r);
252
- end if;
253
- end loop;
254
- if exists (select 1 from pg_roles where rolname = 'service_role') then
255
- execute format('grant execute on function %s to service_role', fn);
256
- end if;
257
- end loop;
258
- end $$;
259
-
260
- commit;
3
+ -- SQL editor, for instance) if the app keeps one. One transaction; re-running it is a no-op. It does not need 001_connection.sql.
4
+ --
5
+ -- qbosync_report_runs one row per run of the report: the day it covers, whether it is running, finished or failed,
6
+ -- and a finished day's totals by result, for the page that lists the days.
7
+ -- qbosync_report_rows one row per document per run, holding what was true THAT morning, so a past report can be
8
+ -- opened again and still shows what it showed then.
9
+ --
10
+ -- The SDK's guide ("A daily report") says how a run fills them. In short:
11
+ --
12
+ -- select * from qbosync_report_start('2026-11-08', 'America/Chicago'); -- the run, with that day's window in UTC
13
+ -- -- list the documents your app created in [window_start, window_end), ask QBO Sync, compare, then
14
+ -- insert into qbosync_report_rows (run_id, ...) values (...); -- one per document
15
+ -- select qbosync_report_finish(run_id, '{"documents": 12, "matches": 11, "failed": 1}');
16
+ -- -- or, when the day cannot be built:
17
+ -- select qbosync_report_fail(run_id, 'QBO Sync could not be reached');
18
+ --
19
+ -- Rules the database keeps:
20
+ -- - ONE run is running at a time: two jobs, or a manual run during the scheduled one, never overlap;
21
+ -- - ONE finished report per day: finishing a second report for a day replaces the first in the same transaction,
22
+ -- so the day always has one, never none;
23
+ -- - a run still running after 30 minutes (its server died) is marked failed by the next start;
24
+ -- - rows go only into a running run, and a failed run keeps none.
25
+ --
26
+ -- The tables have row level security on and no policy, and every function is granted to service_role only: your server
27
+ -- reads and writes them, and shows each person only their own companies' rows. Outside Supabase, grant the tables and
28
+ -- functions to the role your server connects with.
29
+ --
30
+ -- Undo (nothing else depends on these):
31
+ -- drop function public.qbosync_report_start(date, text);
32
+ -- drop function public.qbosync_report_finish(uuid, jsonb);
33
+ -- drop function public.qbosync_report_fail(uuid, text);
34
+ -- drop table public.qbosync_report_rows;
35
+ -- drop table public.qbosync_report_runs;
36
+ -- drop function public.qbosync_report_rows_only_running();
37
+
38
+ begin;
39
+
40
+ -- ── 1. Runs ─────────────────────────────────────────────────────────────────────────
41
+
42
+ create table if not exists public.qbosync_report_runs (
43
+ id uuid primary key default gen_random_uuid(),
44
+ -- The day reported, in the app's own time zone.
45
+ report_date date not null,
46
+ -- That zone (an IANA name, such as America/Chicago) and the day's midnight to midnight in it, in UTC. Worked out by
47
+ -- qbosync_report_start, so daylight saving is the database's job, not the app's.
48
+ time_zone text not null,
49
+ window_start timestamptz not null,
50
+ window_end timestamptz not null,
51
+ status text not null default 'running',
52
+ started_at timestamptz not null default now(),
53
+ finished_at timestamptz,
54
+ -- Why a failed run failed, for the page: a day that failed stays listed with this until a later run makes it up.
55
+ error text,
56
+ -- A finished run's totals: {"documents": 12, "matches": 11, "failed": 1, ...}.
57
+ counts jsonb,
58
+ constraint qbosync_report_runs_status_check check (status in ('running', 'done', 'failed')),
59
+ constraint qbosync_report_runs_window_check check (window_start < window_end),
60
+ constraint qbosync_report_runs_time_zone_check check (length(btrim(time_zone)) > 0),
61
+ constraint qbosync_report_runs_shape_check check (
62
+ (status = 'running' and finished_at is null and error is null and counts is null)
63
+ or (status = 'done' and finished_at is not null and error is null
64
+ and counts is not null and jsonb_typeof(counts) = 'object')
65
+ or (status = 'failed' and finished_at is not null and counts is null
66
+ and error is not null and length(btrim(error)) > 0))
67
+ );
68
+
69
+ -- One run at a time: every running run has the same key.
70
+ create unique index if not exists qbosync_report_runs_one_running
71
+ on public.qbosync_report_runs ((true)) where status = 'running';
72
+ -- One finished report per day.
73
+ create unique index if not exists qbosync_report_runs_one_done_per_day
74
+ on public.qbosync_report_runs (report_date) where status = 'done';
75
+ create index if not exists qbosync_report_runs_day_idx
76
+ on public.qbosync_report_runs (report_date desc, started_at desc);
77
+
78
+ -- ── 2. Rows ─────────────────────────────────────────────────────────────────────────
79
+
80
+ create table if not exists public.qbosync_report_rows (
81
+ id uuid primary key default gen_random_uuid(),
82
+ run_id uuid not null references public.qbosync_report_runs (id) on delete cascade,
83
+ document_type text not null,
84
+ -- The ref your app sent the document under: what QBO Sync was asked about.
85
+ source_ref text not null,
86
+ -- The company, as your app names it (the document's company_key): for showing each company only its own rows.
87
+ company_key text not null,
88
+ -- Optional: your own id for the document (any id, as text), and what people know it by (an order number, say).
89
+ document_id text,
90
+ label text,
91
+ -- The one result the document got. See the guide for when each applies:
92
+ -- matches posted, and every amount is what your app holds
93
+ -- differs posted, but an amount differs, or QuickBooks no longer holds what was posted
94
+ -- changed_since_posted your document changed after the version QBO Sync posted
95
+ -- not_posted QBO Sync has it and nothing is posted (waiting, syncing off, excluded, rolled back)
96
+ -- failed QBO Sync tried and failed; its error is kept
97
+ -- not_received QBO Sync never received it
98
+ result text not null,
99
+ -- As QBO Sync answered that morning.
100
+ qbo_number text,
101
+ sync_status text,
102
+ synced_at timestamptz,
103
+ -- The nightly QuickBooks check's result for what was posted; null while nothing is posted.
104
+ qbo_check text,
105
+ -- For "differs" only: each thing that did not match, as your app chooses to describe it (a JSON array, never empty).
106
+ differences jsonb,
107
+ -- QBO Sync's error for "failed"; for "not_received", your own record of why the send did not get through, if any.
108
+ error text,
109
+ -- Optional: your own record of sending it (shown beside "not_received").
110
+ send_status text,
111
+ created_at timestamptz not null default now(),
112
+ constraint qbosync_report_rows_type_check check (document_type in ('invoice', 'credit_memo', 'bill', 'vendor_credit')),
113
+ constraint qbosync_report_rows_ref_check check (length(btrim(source_ref)) > 0),
114
+ constraint qbosync_report_rows_company_check check (length(btrim(company_key)) > 0),
115
+ constraint qbosync_report_rows_result_check check (result in
116
+ ('matches', 'differs', 'changed_since_posted', 'not_posted', 'failed', 'not_received')),
117
+ constraint qbosync_report_rows_qbo_check_check check (
118
+ qbo_check is null or qbo_check in ('matches', 'differs', 'missing', 'not_checked')),
119
+ constraint qbosync_report_rows_differences_check check (
120
+ (result = 'differs') = (differences is not null and jsonb_typeof(differences) = 'array' and jsonb_array_length(differences) > 0))
121
+ );
122
+
123
+ -- A document once per run.
124
+ create unique index if not exists qbosync_report_rows_one_per_document
125
+ on public.qbosync_report_rows (run_id, document_type, source_ref);
126
+ create index if not exists qbosync_report_rows_company_idx
127
+ on public.qbosync_report_rows (run_id, company_key);
128
+
129
+ -- Rows go only into a running run: a finished report never changes, and a failed one stays empty.
130
+ create or replace function public.qbosync_report_rows_only_running()
131
+ returns trigger
132
+ language plpgsql
133
+ set search_path = public
134
+ as $$
135
+ begin
136
+ if not exists (select 1 from public.qbosync_report_runs where id = new.run_id and status = 'running') then
137
+ raise exception 'qbosync_report_rows: run % is not running', new.run_id;
138
+ end if;
139
+ return new;
140
+ end;
141
+ $$;
142
+
143
+ drop trigger if exists qbosync_report_rows_only_running on public.qbosync_report_rows;
144
+ create trigger qbosync_report_rows_only_running
145
+ before insert or update on public.qbosync_report_rows
146
+ for each row execute function public.qbosync_report_rows_only_running();
147
+
148
+ alter table public.qbosync_report_runs enable row level security;
149
+ alter table public.qbosync_report_rows enable row level security;
150
+
151
+ -- ── 3. Starting, finishing, failing ─────────────────────────────────────────────────
152
+
153
+ -- Starts a run for one day in the given time zone and answers it, with the day's window in UTC. A run still running after
154
+ -- 30 minutes is marked failed first (its server died); one running for less makes this raise, so two runs never overlap.
155
+ create or replace function public.qbosync_report_start(p_report_date date, p_time_zone text)
156
+ returns public.qbosync_report_runs
157
+ language plpgsql
158
+ set search_path = public
159
+ as $$
160
+ declare
161
+ run public.qbosync_report_runs;
162
+ begin
163
+ if p_report_date is null then
164
+ raise exception 'qbosync_report_start: a day is required';
165
+ end if;
166
+ if p_time_zone is null or not exists (select 1 from pg_timezone_names where name = p_time_zone) then
167
+ raise exception 'qbosync_report_start: % is not a time zone name (such as America/Chicago)', coalesce(p_time_zone, 'null');
168
+ end if;
169
+ update public.qbosync_report_runs
170
+ set status = 'failed', finished_at = now(), error = 'abandoned: still running after 30 minutes'
171
+ where status = 'running' and started_at < now() - interval '30 minutes';
172
+ -- An abandoned run keeps no rows, like any failed run.
173
+ delete from public.qbosync_report_rows r
174
+ using public.qbosync_report_runs u
175
+ where r.run_id = u.id and u.status = 'failed';
176
+
177
+ insert into public.qbosync_report_runs (report_date, time_zone, window_start, window_end)
178
+ values (p_report_date,
179
+ p_time_zone,
180
+ (p_report_date::timestamp) at time zone p_time_zone,
181
+ ((p_report_date + 1)::timestamp) at time zone p_time_zone)
182
+ returning * into run;
183
+ return run;
184
+ exception when unique_violation then
185
+ raise exception 'qbosync_report_start: another report is running';
186
+ end;
187
+ $$;
188
+
189
+ -- Finishes a running run with its totals. An earlier finished report for the same day is deleted in the same transaction
190
+ -- (its rows with it), so the day always has exactly one, never none while a rerun is being finished.
191
+ create or replace function public.qbosync_report_finish(p_run_id uuid, p_counts jsonb)
192
+ returns void
193
+ language plpgsql
194
+ set search_path = public
195
+ as $$
196
+ declare
197
+ day date;
198
+ begin
199
+ if p_counts is null or jsonb_typeof(p_counts) <> 'object' then
200
+ raise exception 'qbosync_report_finish: counts must be a JSON object';
201
+ end if;
202
+ select report_date into day from public.qbosync_report_runs where id = p_run_id and status = 'running' for update;
203
+ if not found then
204
+ raise exception 'qbosync_report_finish: run % is not running', p_run_id;
205
+ end if;
206
+ delete from public.qbosync_report_runs where report_date = day and status = 'done';
207
+ update public.qbosync_report_runs
208
+ set status = 'done', finished_at = now(), counts = p_counts
209
+ where id = p_run_id;
210
+ end;
211
+ $$;
212
+
213
+ -- Marks a running run failed, keeping why, and drops the rows it had written. An earlier finished report for the day stays.
214
+ create or replace function public.qbosync_report_fail(p_run_id uuid, p_error text)
215
+ returns void
216
+ language plpgsql
217
+ set search_path = public
218
+ as $$
219
+ begin
220
+ delete from public.qbosync_report_rows where run_id = p_run_id;
221
+ update public.qbosync_report_runs
222
+ set status = 'failed', finished_at = now(),
223
+ error = coalesce(nullif(btrim(p_error), ''), 'the report could not be built')
224
+ where id = p_run_id and status = 'running';
225
+ end;
226
+ $$;
227
+
228
+ do $$
229
+ declare
230
+ fn text;
231
+ tbl text;
232
+ r text;
233
+ begin
234
+ foreach tbl in array array['public.qbosync_report_runs', 'public.qbosync_report_rows'] loop
235
+ execute format('revoke all on table %s from public', tbl);
236
+ foreach r in array array['anon', 'authenticated'] loop
237
+ if exists (select 1 from pg_roles where rolname = r) then
238
+ execute format('revoke all on table %s from %I', tbl, r);
239
+ end if;
240
+ end loop;
241
+ end loop;
242
+ foreach fn in array array[
243
+ 'public.qbosync_report_start(date, text)',
244
+ 'public.qbosync_report_finish(uuid, jsonb)',
245
+ 'public.qbosync_report_fail(uuid, text)',
246
+ 'public.qbosync_report_rows_only_running()'
247
+ ] loop
248
+ execute format('revoke all on function %s from public', fn);
249
+ foreach r in array array['anon', 'authenticated'] loop
250
+ if exists (select 1 from pg_roles where rolname = r) then
251
+ execute format('revoke all on function %s from %I', fn, r);
252
+ end if;
253
+ end loop;
254
+ if exists (select 1 from pg_roles where rolname = 'service_role') then
255
+ execute format('grant execute on function %s to service_role', fn);
256
+ end if;
257
+ end loop;
258
+ end $$;
259
+
260
+ commit;
@@ -1,25 +1,25 @@
1
- -- The daily report keeps rows for bills and vendor credits too (SDK 0.5.1).
2
- --
3
- -- Run this once if your app ran sql/002_daily_report.sql before 0.5.1 and sends bills or vendor credits. Until then,
4
- -- 002's check allowed only 'invoice' and 'credit_memo', so the first morning with a bill failed whole ("new row for
5
- -- relation qbosync_report_rows violates check constraint qbosync_report_rows_type_check") and every later morning with
6
- -- one did too. 002 itself now allows all four types, so an app that runs 002 from 0.5.1 on does not need this file;
7
- -- running it anyway changes nothing. One transaction; re-running it is a no-op.
8
- --
9
- -- Found by Aztec's end-to-end run on 2026-11-16, the first app to send bills.
10
- --
11
- -- Undo (only while no bill or vendor credit row exists):
12
- -- alter table public.qbosync_report_rows drop constraint qbosync_report_rows_type_check;
13
- -- alter table public.qbosync_report_rows add constraint qbosync_report_rows_type_check
14
- -- check (document_type in ('invoice', 'credit_memo'));
15
-
16
- begin;
17
-
18
- alter table public.qbosync_report_rows
19
- drop constraint if exists qbosync_report_rows_type_check;
20
-
21
- alter table public.qbosync_report_rows
22
- add constraint qbosync_report_rows_type_check
23
- check (document_type in ('invoice', 'credit_memo', 'bill', 'vendor_credit'));
24
-
25
- commit;
1
+ -- The daily report keeps rows for bills and vendor credits too (SDK 0.5.1).
2
+ --
3
+ -- Run this once if your app ran sql/002_daily_report.sql before 0.5.1 and sends bills or vendor credits. Until then,
4
+ -- 002's check allowed only 'invoice' and 'credit_memo', so the first morning with a bill failed whole ("new row for
5
+ -- relation qbosync_report_rows violates check constraint qbosync_report_rows_type_check") and every later morning with
6
+ -- one did too. 002 itself now allows all four types, so an app that runs 002 from 0.5.1 on does not need this file;
7
+ -- running it anyway changes nothing. One transaction; re-running it is a no-op.
8
+ --
9
+ -- Found by Aztec's end-to-end run on 2026-11-16, the first app to send bills.
10
+ --
11
+ -- Undo (only while no bill or vendor credit row exists):
12
+ -- alter table public.qbosync_report_rows drop constraint qbosync_report_rows_type_check;
13
+ -- alter table public.qbosync_report_rows add constraint qbosync_report_rows_type_check
14
+ -- check (document_type in ('invoice', 'credit_memo'));
15
+
16
+ begin;
17
+
18
+ alter table public.qbosync_report_rows
19
+ drop constraint if exists qbosync_report_rows_type_check;
20
+
21
+ alter table public.qbosync_report_rows
22
+ add constraint qbosync_report_rows_type_check
23
+ check (document_type in ('invoice', 'credit_memo', 'bill', 'vendor_credit'));
24
+
25
+ commit;