forked from aquametalabs/aquameta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
001-functions.sql
791 lines (666 loc) · 28 KB
/
001-functions.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
/*******************************************************************************
* Bundle
* Data Version Control System
*
* Copyriright (c) 2019 - Aquameta - http://aquameta.org/
******************************************************************************/
/*
* User Functions
* 1. commit
* 2. stage
* 3. checkout
*/
------------------------------------------------------------------------------
-- COMMIT FUNCTIONS
------------------------------------------------------------------------------
create or replace function commit (bundle_name text, message text) returns void as $$
declare
_bundle_id uuid;
new_rowset_id uuid;
new_commit_id uuid;
begin
raise notice 'bundle: Committing to %', bundle_name;
select id
into _bundle_id
from bundle.bundle
where name = bundle_name;
-- make a rowset that will hold the contents of this commit
insert into bundle.rowset default values
returning id into new_rowset_id;
-- STAGE
raise notice 'bundle: Committing rowset_rows...';
-- ROWS: copy everything in stage_row to the new rowset
insert into bundle.rowset_row (rowset_id, row_id)
select new_rowset_id, row_id from bundle.stage_row where bundle_id=_bundle_id;
raise notice 'bundle: Committing blobs...';
-- FIELDS: copy all the fields in stage_row_field to the new rowset's fields
insert into bundle.blob (value)
select f.value
from bundle.rowset_row rr
join bundle.rowset r on r.id=new_rowset_id and rr.rowset_id=r.id
join bundle.stage_row_field f on (f.field_id).row_id = rr.row_id; -- TODO: should we be checking here to see if the staged value is different than the w.c. value??
raise notice 'bundle: Committing stage_row_fields...';
-- FIELDS: copy all the fields in stage_row_field to the new rowset's fields
insert into bundle.rowset_row_field (rowset_row_id, field_id, value_hash)
select rr.id, f.field_id, public.digest(value, 'sha256')
from bundle.rowset_row rr
join bundle.rowset r on r.id=new_rowset_id and rr.rowset_id=r.id
join bundle.stage_row_field f on (f.field_id).row_id = rr.row_id;
raise notice 'bundle: Creating the commit...';
-- create the commit
insert into bundle.commit (bundle_id, parent_id, rowset_id, message)
values (_bundle_id, (select head_commit_id from bundle.bundle b where b.id=_bundle_id), new_rowset_id, message)
returning id into new_commit_id;
raise notice 'bundle: Updating bundle.head_commit_id...';
-- point HEAD at new commit
update bundle.bundle bundle set head_commit_id=new_commit_id where bundle.id=_bundle_id;
raise notice 'bundle: Cleaning up after commit...';
-- clear the stage
delete from bundle.stage_row_added where bundle_id=_bundle_id;
delete from bundle.stage_row_deleted where bundle_id=_bundle_id;
delete from bundle.stage_field_changed where bundle_id=_bundle_id;
end
$$ language plpgsql;
create or replace function head_rows (
in bundle_name text,
out commit_id uuid,
out schema_name text,
out relation_name text,
out pk_column_name text,
out pk_value text)
returns setof record
as $$
select c.id,
(row_id::meta.schema_id).name,
(row_id::meta.relation_id).name,
((row_id).pk_column_id).name,
(row_id).pk_value
from bundle.bundle bundle
join bundle.commit c on bundle.head_commit_id=c.id
join bundle.rowset r on c.rowset_id = r.id
join bundle.rowset_row rr on rr.rowset_id = r.id
where bundle.name = bundle_name
$$ language sql;
create or replace function commit_log (in bundle_name text, out commit_id uuid, out message text, out count bigint)
returns setof record
as $$
select c.id as commit_id, message, count(*)
from bundle b
join bundle.commit c on c.bundle_id = b.id
join bundle.rowset r on c.rowset_id=r.id
join bundle.rowset_row rr on rr.rowset_id = r.id
where b.name = bundle_name
group by b.id, c.id, message
$$ language sql;
------------------------------------------------------------------------------
-- TRACKED ROW FUNCTIONS
------------------------------------------------------------------------------
-- track a row
create or replace function tracked_row_add (
bundle_name text,
schema_name text,
relation_name text,
pk_column_name text,
pk_value text
) returns text
as $$
-- TODO: check to see if this row is not tracked by some other bundle?
insert into bundle.tracked_row_added (bundle_id, row_id) values (
(select id from bundle.bundle where name=bundle_name),
meta.row_id(schema_name, relation_name, pk_column_name, pk_value)
);
select bundle_name || ' - ' || schema_name || '.' || relation_name || '.' || pk_value;
$$ language sql;
create or replace function tracked_row_add (
bundle_name text,
row_id meta.row_id
) returns text
as $$
select bundle.tracked_row_add(bundle_name, (
row_id::meta.schema_id).name,
(row_id::meta.relation_id).name,
((row_id).pk_column_id).name,
(row_id).pk_value
);
$$ language sql;
-- untrack a row
create or replace function untrack_row (
bundle_name text,
schema_name text,
relation_name text,
pk_column_name text,
pk_value text
) returns text
as $$
-- TODO: check to see if this row is not tracked by some other bundle?
delete from bundle.tracked_row_added
where bundle_id = (select id from bundle.bundle where name=bundle_name)
and row_id = meta.row_id(schema_name, relation_name, pk_column_name, pk_value);
select 'untracked: ' || bundle_name || ' - ' || schema_name || '.' || relation_name || '.' || pk_value;
$$ language sql;
create or replace function untrack_row (
bundle_name text,
row_id meta.row_id
) returns text
as $$
select bundle.untrack_row(bundle_name, (
row_id::meta.schema_id).name,
(row_id::meta.relation_id).name,
((row_id).pk_column_id).name,
(row_id).pk_value
);
$$ language sql;
-------------------------------------------------------------------------------
-- STAGE FUNCTIONS
------------------------------------------------------------------------------
-- stage an add
create or replace function stage_row_add (
bundle_name text,
schema_name text,
relation_name text,
pk_column_name text,
pk_value text
) returns text
as $$
begin
insert into bundle.stage_row_added (bundle_id, row_id)
select b.id, meta.row_id(schema_name, relation_name, pk_column_name, pk_value)
from bundle.bundle b
join bundle.tracked_row_added tra on tra.bundle_id=b.id
where b.name=bundle_name and tra.row_id=meta.row_id(schema_name, relation_name, pk_column_name, pk_value);
if not FOUND then
raise exception 'No such bundle, or this row is not yet tracked by this bundle.';
end if;
delete from bundle.tracked_row_added tra
where tra.row_id=meta.row_id(schema_name, relation_name, pk_column_name, pk_value);
if not FOUND then
raise exception 'Row could not be deleted from bundle.tracked_row_added';
end if;
return (bundle_name || ' - ' || schema_name || '.' || relation_name || '.' || pk_value)::text;
end;
$$
language plpgsql;
create or replace function stage_row_add (
bundle_name text,
row_id meta.row_id
) returns text
as $$
select bundle.stage_row_add(bundle_name, (
row_id::meta.schema_id).name,
(row_id::meta.relation_id).name,
((row_id).pk_column_id).name,
(row_id).pk_value
);
$$ language sql;
-- unstage an add
create or replace function unstage_row_add (
bundle_name text,
schema_name text,
relation_name text,
pk_column_name text,
pk_value text
) returns text
as $$
begin
delete from bundle.stage_row_added
where bundle_id = (select id from bundle.bundle where name=bundle_name)
and row_id=meta.row_id(schema_name, relation_name, pk_column_name, pk_value);
if not FOUND then
raise exception 'No such bundle or row.';
end if;
insert into bundle.tracked_row_added (bundle_id, row_id)
values (
(select id from bundle.bundle where name=bundle_name),
meta.row_id(schema_name, relation_name, pk_column_name, pk_value)
);
if not FOUND then
raise exception 'No such bundle or row.';
end if;
return 'hi'; --- bundle_name || ' - ' || schema_name || '.' || relation_name || '.' || pk_value;
end
;
$$
language plpgsql;
create or replace function unstage_row_add (
bundle_name text,
row_id meta.row_id
) returns text
as $$
select bundle.unstage_row_add(bundle_name, (
row_id::meta.schema_id).name,
(row_id::meta.relation_id).name,
((row_id).pk_column_id).name,
(row_id).pk_value
);
$$ language sql;
create or replace function stage_row_delete (
bundle_name text,
schema_name text,
relation_name text,
pk_column_name text,
pk_value text
) returns text
as $$
insert into bundle.stage_row_deleted (bundle_id, rowset_row_id)
select
bundle.id as bundle_id,
rr.id as rowset_row_id
from bundle.bundle bundle
join bundle.commit c on bundle.head_commit_id = c.id
join bundle.rowset r on c.rowset_id = r.id
join bundle.rowset_row rr on rr.rowset_id = r.id
where bundle.name = bundle_name
and rr.row_id = meta.row_id(schema_name, relation_name, pk_column_name, pk_value);
select bundle_name || ' - ' || schema_name || '.' || relation_name || '.' || pk_value;
$$ language sql;
create or replace function stage_row_delete (
bundle_name text,
row_id meta.row_id
) returns text
as $$
select bundle.stage_row_delete(bundle_name, (
row_id::meta.schema_id).name,
(row_id::meta.relation_id).name,
((row_id).pk_column_id).name,
(row_id).pk_value
);
$$ language sql;
create or replace function unstage_row_delete (
bundle_name text,
schema_name text,
relation_name text,
pk_column_name text,
pk_value text
) returns text
as $$
delete from bundle.stage_row_deleted srd
using bundle.rowset_row rr
where rr.id = srd.rowset_row_id
and srd.bundle_id=(select id from bundle.bundle where name=bundle_name)
and rr.row_id=meta.row_id(schema_name, relation_name, pk_column_name, pk_value);
select bundle_name || ' - ' || schema_name || '.' || relation_name || '.' || pk_value;
$$ language sql;
create or replace function unstage_row_delete (
bundle_name text,
row_id meta.row_id
) returns text
as $$
select bundle.unstage_row_delete(bundle_name, (
row_id::meta.schema_id).name,
(row_id::meta.relation_id).name,
((row_id).pk_column_id).name,
(row_id).pk_value
);
$$ language sql;
/* all text interface */
create or replace function stage_field_change (
bundle_name text,
schema_name text,
relation_name text,
pk_column_name text,
pk_value text,
column_name text -- FIXME: somehow the webserver thinks it's a relation if column_name is present??
) returns text
as $$
insert into bundle.stage_field_changed (bundle_id, field_id, new_value)
values (
(select id from bundle.bundle where name=bundle_name),
meta.field_id (schema_name, relation_name, pk_column_name, pk_value, column_name),
meta.field_id_literal_value(
meta.field_id (schema_name, relation_name, pk_column_name, pk_value, column_name)
)
);
select bundle_name || ' - ' || schema_name || '.' || relation_name || '.' || pk_value || ' - ' || column_name;
$$ language sql;
/* all id interface */
create or replace function stage_field_change (
bundle_id uuid,
changed_field_id meta.field_id
) returns void
as $$
insert into bundle.stage_field_changed (bundle_id, field_id, new_value)
values (bundle_id, changed_field_id, meta.field_id_literal_value(changed_field_id)
);
$$ language sql;
/* all text interface */
create or replace function unstage_field_change (
bundle_name text,
schema_name text,
relation_name text,
pk_column_name text,
pk_value text,
column_name text -- FIXME: somehow the webserver thinks it's a relation if column_name is present??
) returns text
as $$
delete from bundle.stage_field_changed
where field_id=
meta.field_id (schema_name, relation_name, pk_column_name, pk_value, column_name);
select bundle_name || ' - ' || schema_name || '.' || relation_name || '.' || pk_value || ' - ' || column_name;
$$ language sql;
/* all id interface */
create or replace function unstage_field_change (
bundle_id uuid,
changed_field_id meta.field_id
) returns void
as $$
delete from bundle.stage_field_changed where field_id=changed_field_id;
$$ language sql;
------------------------------------------------------------------------------
-- CHECKOUT FUNCTIONS
-- user stories:
--
-- 1. user downloads a new bundle, checking out where everything is fresh and
-- new. we don't run into any collissions and just plop it all into place.
--
-- 2. user tries to check out a bundle when his working copy is different from
-- previous commit. this would be indicated by rows in offstage_row_deleted and
-- offstage_field_change, or stage_row_*.
--
------------------------------------------------------------------------------
create type checkout_field as (name text, value text, type_name text);
-- create or replace function checkout_row (in row_id meta.row_id, in fields text[], in vals text[], in force_overwrite boolean) returns void as $$
create or replace function checkout_row (in row_id meta.row_id, in fields checkout_field[], in force_overwrite boolean) returns void as $$
declare
query_str text;
begin
-- raise log '------------ checkout_row % ----------',
-- (row_id::meta.schema_id).name || '.' || (row_id::meta.relation_id).name ;
set search_path=bundle,meta,public;
if meta.row_exists(row_id) then
-- raise log '---------------------- row % already exists.... overwriting.',
-- (row_id::meta.schema_id).name || '.' || (row_id::meta.relation_id).name ;
-- check to see if this row which is being merged is going to overwrite a row that is
-- different from the head commit
-- overwrite existing values with new values.
/*
execute 'update ' || quote_ident((row_id::meta.schema_id).name) || '.' || quote_ident((row_id::meta.relation_id).name)
|| ' set (' || array_to_string(fields.name,', ','NULL') || ')'
|| ' = (' || array_to_string(fields.value || '::'||fields.type_name, ', ','NULL') || ')'
|| ' where ' || (row_id.pk_column_id).name
|| ' = ' || row_id.pk_value;
*/
query_str := 'update '
|| quote_ident((row_id::meta.schema_id).name)
|| '.'
|| quote_ident((row_id::meta.relation_id).name)
|| ' set (';
for i in 1 .. array_upper(fields, 1)
loop
query_str := query_str || quote_ident(fields[i].name);
if i < array_upper(fields, 1) then
query_str := query_str || ', ';
end if;
end loop;
query_str := query_str
|| ') = (';
for i in 1 .. array_upper(fields, 1)
loop
query_str := query_str
|| coalesce(
quote_literal(fields[i].value)
|| '::text::'
|| fields[i].type_name,
'NULL'
);
if i < array_upper(fields, 1) then
query_str := query_str || ', ';
end if;
end loop;
query_str := query_str
|| ')'
|| ' where ' || quote_ident((row_id.pk_column_id).name)
|| '::text = ' || quote_literal(row_id.pk_value) || '::text'; -- cast them both to text instead of look up the column's type... maybe lazy?
-- raise log 'query_str: %', query_str;
execute query_str;
else
-- raise log '---------------------- row doesn''t exists.... INSERT:';
query_str := 'insert into '
|| quote_ident((row_id::meta.schema_id).name)
|| '.'
|| quote_ident((row_id::meta.relation_id).name)
|| ' (';
for i in 1 .. array_upper(fields, 1)
loop
query_str := query_str || quote_ident(fields[i].name);
if i < array_upper(fields, 1) then
query_str := query_str || ', ';
end if;
end loop;
query_str := query_str
|| ') values (';
for i in 1 .. array_upper(fields, 1)
loop
query_str := query_str
|| coalesce(
quote_literal(fields[i].value)
|| '::text::'
|| fields[i].type_name,
'NULL'
);
if i < array_upper(fields, 1) then
query_str := query_str || ', ';
end if;
end loop;
query_str := query_str || ')';
-- raise log 'query_str: %', query_str;
execute query_str;
/*
(select string_agg (quote_ident((f::bundle.checkout_field).name), ',') from unnest(fields) as f) || ')'
|| ' values '
|| ' (' || (select string_agg (quote_literal(f.value) || '::' || (f::bundle.checkout_field).type_name, ',') from unnest(fields) as f) || ')';
*/
end if;
end;
$$ language plpgsql;
-- checkout can only be run by superusers because it disables triggers, as described here: http://blog.endpoint.com/2012/10/postgres-system-triggers-error.html
create or replace function checkout (in commit_id uuid) returns void as $$
declare
commit_row record;
bundle_name text;
commit_message text;
_commit_id uuid;
commit_role text;
commit_time timestamp;
begin
set local search_path=bundle,meta,public;
select b.name, c.id, c.message, c.time, (c.role_id).name
into bundle_name, _commit_id, commit_message, commit_time, commit_role
from bundle.bundle b
join bundle.commit c on c.bundle_id = b.id
where c.id = commit_id;
if _commit_id is null then
raise exception 'bundle.checkout() commit with id % does not exist', commit_id;
end if;
raise notice 'bundle.checkout(): % / % @ % by %: "%"', bundle_name, commit_id, commit_time, commit_role, commit_message;
-- raise notice 'bundle: Checking out bundle %', commit_id;
-- insert the meta-rows in this commit to the database
for commit_row in
select
rr.row_id,
array_agg(
row(
((f.field_id).column_id).name,
b.value,
col.type_name
)::bundle.checkout_field
) as fields_agg
from bundle.commit c
join bundle.rowset r on c.rowset_id=r.id
join bundle.rowset_row rr on rr.rowset_id=r.id
join bundle.rowset_row_field f on f.rowset_row_id=rr.id
join bundle.blob b on f.value_hash=b.hash
join meta.relation_column col on (f.field_id).column_id = col.id
where c.id=commit_id
and (rr.row_id::meta.schema_id).name = 'meta'
group by rr.id
-- add meta rows first, in sensible order
order by
case
when row_id::meta.relation_id = meta.relation_id('meta','schema') then 0
when row_id::meta.relation_id = meta.relation_id('meta','type_definition') then 1
when row_id::meta.relation_id = meta.relation_id('meta','table') then 2
when row_id::meta.relation_id = meta.relation_id('meta','column') then 3
when row_id::meta.relation_id = meta.relation_id('meta','sequence') then 4
when row_id::meta.relation_id = meta.relation_id('meta','constraint_check') then 4
when row_id::meta.relation_id = meta.relation_id('meta','constraint_unique') then 4
when row_id::meta.relation_id = meta.relation_id('meta','function_definition') then 5
else 100
end asc /*,
case
when row_id::meta.relation_id = meta.relation_id('meta','column') then array_agg(quote_literal(f.value))->position::integer
else 0
end
*/
loop
-- raise log '-- CHECKOUT meta row: % %',
-- (commit_row.row_id).pk_column_id.relation_id.name,
-- (commit_row.row_id).pk_column_id.relation_id.schema_id.name;-- , commit_row.fields_agg;
perform bundle.checkout_row(commit_row.row_id, commit_row.fields_agg, true);
end loop;
-- raise notice '################################################## DISABLING TRIGGERS % ###############################', commit_id;
-- turn off constraints
for commit_row in
select distinct
(rr.row_id).pk_column_id.relation_id.name as relation_name,
(rr.row_id).pk_column_id.relation_id.schema_id.name as schema_name
from bundle.commit c
join bundle.rowset r on c.rowset_id=r.id
join bundle.rowset_row rr on rr.rowset_id=r.id
where c.id = commit_id
and (rr.row_id::meta.schema_id).name != 'meta'
loop
-- raise log '-------------------------------- DISABLING TRIGGER on table %',
-- quote_ident(commit_row.schema_name) || '.' || quote_ident(commit_row.relation_name);
execute 'alter table '
|| quote_ident(commit_row.schema_name) || '.' || quote_ident(commit_row.relation_name)
|| ' disable trigger all';
end loop;
-- raise notice 'CHECKOUT DATA %', commit_id;
-- insert the non-meta rows
for commit_row in
select
rr.row_id,
array_agg(
row(
((f.field_id).column_id).name,
b.value,
col.type_name
)::bundle.checkout_field
) as fields_agg
from bundle.commit c
join bundle.rowset r on c.rowset_id=r.id
join bundle.rowset_row rr on rr.rowset_id=r.id
join bundle.rowset_row_field f on f.rowset_row_id=rr.id
join bundle.blob b on f.value_hash=b.hash
join meta.relation_column col on (f.field_id).column_id = col.id
where c.id=commit_id
and (rr.row_id::meta.schema_id).name != 'meta'
group by rr.id
loop
-- raise log '------------------------------------------------------------------------CHECKOUT row: % %',
-- (commit_row.row_id).pk_column_id.relation_id.name,
-- (commit_row.row_id).pk_column_id.relation_id.schema_id.name;-- , commit_row.fields_agg;
perform bundle.checkout_row(commit_row.row_id, commit_row.fields_agg, true);
end loop;
-- turn constraints back on
-- raise notice '################################################## ENABLING TRIGGERS % ###############################', commit_id;
for commit_row in
select distinct
(rr.row_id).pk_column_id.relation_id.name as relation_name,
(rr.row_id).pk_column_id.relation_id.schema_id.name as schema_name
from bundle.commit c
join bundle.rowset r on c.rowset_id=r.id
join bundle.rowset_row rr on rr.rowset_id=r.id
where c.id = commit_id
and (rr.row_id::meta.schema_id).name != 'meta'
loop
execute 'alter table '
|| quote_ident(commit_row.schema_name) || '.' || quote_ident(commit_row.relation_name)
|| ' enable trigger all';
end loop;
-- point head_commit_id to this commit
update bundle.bundle set head_commit_id = commit_id where id in (select bundle_id from bundle.commit c where c.id = commit_id);
return;
end;
$$ language plpgsql;
/*
* row_id here is text because composite types custom input functions, they all
* use record_in, so we can't pass it a text string without explicitly casting
* it in the call. So it just takes text and casts it internally.
*/
create function checkout_row(_row_id text, commit_id uuid) returns void as $$
declare
commit_row record;
begin
for commit_row in
select
rr.row_id,
array_agg(
row(
((f.field_id).column_id).name,
b.value,
col.type_name
)::bundle.checkout_field
) as fields_agg
from bundle.commit c
join bundle.rowset r on c.rowset_id=r.id
join bundle.rowset_row rr on rr.rowset_id=r.id
join bundle.rowset_row_field f on f.rowset_row_id=rr.id
join bundle.blob b on f.value_hash=b.hash
join meta.relation_column col on (f.field_id).column_id = col.id
where c.id=commit_id
and rr.row_id = _row_id::meta.row_id
group by rr.id
loop
perform bundle.checkout_row(commit_row.row_id, commit_row.fields_agg, true);
end loop;
return;
end;
$$ language plpgsql;
------------------------------------------------------------------------------
-- BUNDLE COPY
------------------------------------------------------------------------------
/*
create function bundle.bundle_copy(_bundle_id uuid, new_name text) returns uuid as $$
insert into bundle.bundle select * from bundle.bundle where id=_bundle_id;
insert into bundle.rowset select * from bundle.rowset r join commit c on c.rowset_id=r.id where c.bundle_id = _bundle_id;
insert into bundle.commit select * from bundle.commit where bundle_id = _bundle_id;
$$ language sql;
*/
------------------------------------------------------------------------------
-- BUNDLE CREATE / DELETE
------------------------------------------------------------------------------
create or replace function bundle.bundle_create (name text) returns uuid as $$
declare
bundle_id uuid;
begin
insert into bundle.bundle (name) values (name) returning id into bundle_id;
return bundle_id;
end;
$$ language plpgsql;
create or replace function bundle.bundle_delete (in _bundle_id uuid) returns void as $$
-- TODO: delete blobs
delete from bundle.rowset r where r.id in (select c.rowset_id from bundle.commit c join bundle.bundle b on c.bundle_id = b.id where b.id = _bundle_id);
delete from bundle.bundle where id = _bundle_id;
$$ language sql;
------------------------------------------------------------------------------
-- COMMIT DELETE
------------------------------------------------------------------------------
create or replace function bundle.commit_delete(in _commit_id uuid) returns void as $$
-- TODO: delete blobs
-- TODO: delete commits in order?
delete from bundle.rowset r where r.id in (select c.rowset_id from bundle.commit c where c.id = _commit_id);
delete from bundle.commit c where c.id = _commit_id;
$$ language sql;
------------------------------------------------------------------------------
-- STATUS FUNCTIONS
------------------------------------------------------------------------------
create or replace function bundle.status()
returns setof text as $$
select b.name || ' - '
|| hds.change_type || ' - '
|| hds.row_id::text
from bundle.bundle b
join bundle.head_db_stage_changed hds on hds.bundle_id = b.id
order by b.name, hds.row_id::text;
$$ language sql;