-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstash.sql
More file actions
1380 lines (1225 loc) · 48.5 KB
/
stash.sql
File metadata and controls
1380 lines (1225 loc) · 48.5 KB
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
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
------------------------------------------------------------------------------
-- BUNDLE STASH
-- Git-stash-like functionality for saving uncommitted changes
------------------------------------------------------------------------------
-- Table to store stashes
create table bundle.stash (
id uuid primary key default public.uuid_generate_v4(),
repository_id uuid not null references bundle.repository(id) on delete cascade,
message text,
created_at timestamptz not null default now(),
-- Staged state (from repository.stage_*)
stage_rows_to_add meta.row_id[] not null default '{}',
stage_rows_to_remove meta.row_id[] not null default '{}',
stage_fields_to_change meta.field_id[] not null default '{}',
-- Offstage state
offstage_tracked_rows_added meta.row_id[] not null default '{}',
offstage_deleted_rows meta.row_id[] not null default '{}',
offstage_updated_fields bundle.stash_field_value[] not null default '{}'
);
create index stash_repository_id_idx on bundle.stash(repository_id);
create index stash_created_at_idx on bundle.stash(created_at);
-- Extract row_id from field_id
create or replace function bundle._field_to_row(_fid meta.field_id)
returns meta.row_id as $$
select meta.make_row_id(
_fid->>'schema_name',
_fid->>'relation_name',
array(select jsonb_array_elements_text(_fid->'pk_column_names')),
array(select jsonb_array_elements_text(_fid->'pk_values'))
);
$$ language sql immutable;
-- Stash all uncommitted changes (staged + offstage)
-- Saves current state, then reverts to committed state
create or replace function bundle._stash(
_repository_id uuid,
_message text default null,
_keep_changes boolean default false
) returns uuid as $$
declare
_stash_id uuid;
_repo record;
_stage_rows_add meta.row_id[] := '{}';
_stage_rows_remove meta.row_id[] := '{}';
_stage_fields meta.field_id[] := '{}';
_offstage_tracked meta.row_id[] := '{}';
_offstage_deleted meta.row_id[] := '{}';
_offstage_fields bundle.stash_field_value[] := '{}';
_field record;
_row record;
_field_value text;
_fid meta.field_id;
_rid meta.row_id;
begin
-- Get repository
select * into _repo from bundle.repository where id = _repository_id;
if _repo is null then
raise exception 'Repository not found: %', _repository_id;
end if;
-- Convert staged jsonb arrays to typed arrays
select coalesce(array_agg(r::meta.row_id), '{}')
into _stage_rows_add
from jsonb_array_elements(_repo.stage_rows_to_add) r;
select coalesce(array_agg(r::meta.row_id), '{}')
into _stage_rows_remove
from jsonb_array_elements(_repo.stage_rows_to_remove) r;
select coalesce(array_agg(f::meta.field_id), '{}')
into _stage_fields
from jsonb_array_elements(_repo.stage_fields_to_change) f;
-- Collect offstage updated fields with their current values
for _field in
select field_id from bundle._get_offstage_updated_fields(_repository_id)
loop
_fid := _field.field_id;
-- Get current value from database
execute format(
'select %I::text from %I.%I where %I = %L',
_fid->>'column_name',
_fid->>'schema_name',
_fid->>'relation_name',
(_fid->'pk_column_names'->>0),
(_fid->'pk_values'->>0)
) into _field_value;
_offstage_fields := array_append(
_offstage_fields,
row(_fid, _field_value)::bundle.stash_field_value
);
end loop;
-- Collect offstage tracked rows added
for _row in
select row_id from bundle._get_tracked_rows_added(_repository_id)
loop
_offstage_tracked := array_append(_offstage_tracked, _row.row_id::meta.row_id);
end loop;
-- Collect offstage deleted rows
for _rid in
select bundle._get_offstage_deleted_rows(_repository_id)
loop
_offstage_deleted := array_append(_offstage_deleted, _rid);
end loop;
-- Create stash record
insert into bundle.stash (
repository_id,
message,
stage_rows_to_add,
stage_rows_to_remove,
stage_fields_to_change,
offstage_tracked_rows_added,
offstage_deleted_rows,
offstage_updated_fields
) values (
_repository_id,
_message,
_stage_rows_add,
_stage_rows_remove,
_stage_fields,
_offstage_tracked,
_offstage_deleted,
_offstage_fields
) returning id into _stash_id;
-- Only revert if not keeping changes
if not _keep_changes then
-- Revert to committed state (upsert=true to handle existing rows)
perform bundle.checkout(_repo.name, true);
-- Clear staging area
update bundle.repository set
stage_rows_to_add = '[]',
stage_rows_to_remove = '[]',
stage_fields_to_change = '[]'
where id = _repository_id;
end if;
return _stash_id;
end;
$$ language plpgsql;
-- Convenience wrapper that takes repository name
create or replace function bundle.stash(
_repository_name text,
_message text default null,
_keep_changes boolean default false
) returns uuid as $$
select bundle._stash(
(select id from bundle.repository where name = _repository_name),
_message,
_keep_changes
);
$$ language sql;
-- Stash only changes for specific rows
-- Does NOT auto-revert (selective checkout would require more work)
create or replace function bundle._stash_rows(
_repository_id uuid,
_row_ids meta.row_id[],
_message text default null,
_keep_changes boolean default false
) returns uuid as $$
declare
_stash_id uuid;
_repo record;
_stage_rows_add meta.row_id[] := '{}';
_stage_rows_remove meta.row_id[] := '{}';
_stage_fields meta.field_id[] := '{}';
_offstage_tracked meta.row_id[] := '{}';
_offstage_deleted meta.row_id[] := '{}';
_offstage_fields bundle.stash_field_value[] := '{}';
_field record;
_row record;
_field_value text;
_fid meta.field_id;
_rid meta.row_id;
_all_stage_rows_add meta.row_id[];
_all_stage_rows_remove meta.row_id[];
_all_stage_fields meta.field_id[];
begin
-- Get repository
select * into _repo from bundle.repository where id = _repository_id;
if _repo is null then
raise exception 'Repository not found: %', _repository_id;
end if;
-- Convert staged jsonb arrays to typed arrays
select coalesce(array_agg(r::meta.row_id), '{}')
into _all_stage_rows_add
from jsonb_array_elements(_repo.stage_rows_to_add) r;
select coalesce(array_agg(r::meta.row_id), '{}')
into _all_stage_rows_remove
from jsonb_array_elements(_repo.stage_rows_to_remove) r;
select coalesce(array_agg(f::meta.field_id), '{}')
into _all_stage_fields
from jsonb_array_elements(_repo.stage_fields_to_change) f;
-- Filter staged rows to add
select coalesce(array_agg(r), '{}')
into _stage_rows_add
from unnest(_all_stage_rows_add) r
where r = any(_row_ids);
-- Filter staged rows to remove
select coalesce(array_agg(r), '{}')
into _stage_rows_remove
from unnest(_all_stage_rows_remove) r
where r = any(_row_ids);
-- Filter staged fields (by row)
select coalesce(array_agg(f), '{}')
into _stage_fields
from unnest(_all_stage_fields) f
where bundle._field_to_row(f) = any(_row_ids);
-- Collect offstage updated fields (filtered) with their current values
for _field in
select field_id from bundle._get_offstage_updated_fields(_repository_id)
where bundle._field_to_row(field_id) = any(_row_ids)
loop
_fid := _field.field_id;
execute format(
'select %I::text from %I.%I where %I = %L',
_fid->>'column_name',
_fid->>'schema_name',
_fid->>'relation_name',
(_fid->'pk_column_names'->>0),
(_fid->'pk_values'->>0)
) into _field_value;
_offstage_fields := array_append(
_offstage_fields,
row(_fid, _field_value)::bundle.stash_field_value
);
end loop;
-- Collect offstage tracked rows added (filtered)
for _row in
select row_id from bundle._get_tracked_rows_added(_repository_id)
where row_id::meta.row_id = any(_row_ids)
loop
_offstage_tracked := array_append(_offstage_tracked, _row.row_id::meta.row_id);
end loop;
-- Collect offstage deleted rows (filtered)
for _rid in
select bundle._get_offstage_deleted_rows(_repository_id)
loop
if _rid = any(_row_ids) then
_offstage_deleted := array_append(_offstage_deleted, _rid);
end if;
end loop;
-- Check if there's anything to stash
if cardinality(_stage_rows_add) = 0 and
cardinality(_stage_rows_remove) = 0 and
cardinality(_stage_fields) = 0 and
cardinality(_offstage_tracked) = 0 and
cardinality(_offstage_deleted) = 0 and
cardinality(_offstage_fields) = 0 then
raise exception 'No changes found for specified rows';
end if;
-- Create stash record
insert into bundle.stash (
repository_id,
message,
stage_rows_to_add,
stage_rows_to_remove,
stage_fields_to_change,
offstage_tracked_rows_added,
offstage_deleted_rows,
offstage_updated_fields
) values (
_repository_id,
_message,
_stage_rows_add,
_stage_rows_remove,
_stage_fields,
_offstage_tracked,
_offstage_deleted,
_offstage_fields
) returning id into _stash_id;
-- Only remove from staging if not keeping changes
if not _keep_changes then
-- Remove stashed items from staging area (keep non-stashed items)
update bundle.repository set
stage_rows_to_add = (
select coalesce(jsonb_agg(r), '[]')
from jsonb_array_elements(stage_rows_to_add) r
where not (r::meta.row_id = any(_stage_rows_add))
),
stage_rows_to_remove = (
select coalesce(jsonb_agg(r), '[]')
from jsonb_array_elements(stage_rows_to_remove) r
where not (r::meta.row_id = any(_stage_rows_remove))
),
stage_fields_to_change = (
select coalesce(jsonb_agg(f), '[]')
from jsonb_array_elements(stage_fields_to_change) f
where not (f::meta.field_id = any(_stage_fields))
)
where id = _repository_id;
-- Note: Does NOT auto-revert. User must manually revert rows if desired.
-- Selective checkout would need to restore individual rows from commit.
end if;
return _stash_id;
end;
$$ language plpgsql;
-- Convenience wrapper for selective stash (accepts jsonb array for endpoint compatibility)
create or replace function bundle.stash_rows(
_repository_name text,
_row_ids jsonb,
_message text default null,
_keep_changes boolean default false
) returns uuid as $$
declare
_row_id_array meta.row_id[];
_elem jsonb;
begin
-- Convert jsonb array to meta.row_id[]
for _elem in select * from jsonb_array_elements(_row_ids)
loop
_row_id_array := array_append(_row_id_array, _elem::meta.row_id);
end loop;
return bundle._stash_rows(
(select id from bundle.repository where name = _repository_name),
_row_id_array,
_message,
_keep_changes
);
end;
$$ language plpgsql;
-- Pop the most recent stash (apply and remove)
create or replace function bundle._stash_pop(
_repository_id uuid,
_force boolean default false
) returns uuid as $$
declare
_stash record;
_sfv bundle.stash_field_value;
_rid meta.row_id;
_field_row_id meta.row_id;
_conflicts text[];
_current_value text;
begin
-- Get most recent stash
select * into _stash
from bundle.stash
where repository_id = _repository_id
order by created_at desc
limit 1;
if _stash is null then
raise exception 'No stash found for repository';
end if;
-- Check for conflicts: stash fields with different values from current uncommitted changes
if not _force then
_conflicts := '{}';
foreach _sfv in array _stash.offstage_updated_fields
loop
-- Check if this field is also uncommitted
if exists (
select 1 from bundle._get_offstage_updated_fields(_repository_id)
where field_id::jsonb = (((_sfv).field_id)::text::jsonb)
) then
-- Get current value from database
execute format(
'select %I::text from %I.%I where %I = %L',
(_sfv).field_id->>'column_name',
(_sfv).field_id->>'schema_name',
(_sfv).field_id->>'relation_name',
((_sfv).field_id->'pk_column_names'->>0),
((_sfv).field_id->'pk_values'->>0)
) into _current_value;
-- Only conflict if values differ
if _current_value is distinct from (_sfv).value then
_conflicts := array_append(_conflicts,
((_sfv).field_id->>'schema_name') || '.' ||
((_sfv).field_id->>'relation_name') || '.' ||
((_sfv).field_id->>'column_name')
);
end if;
end if;
end loop;
if array_length(_conflicts, 1) > 0 then
raise exception 'CONFLICT: You have uncommitted changes to: %. Applying this stash would overwrite them.', array_to_string(_conflicts, ', ');
end if;
end if;
-- Restore offstage updated field values (skip if row no longer exists)
foreach _sfv in array _stash.offstage_updated_fields
loop
-- Build row_id from field_id
_field_row_id := jsonb_build_object(
'schema_name', ((_sfv).field_id->>'schema_name'),
'relation_name', ((_sfv).field_id->>'relation_name'),
'pk_column_names', ((_sfv).field_id->'pk_column_names'),
'pk_values', ((_sfv).field_id->'pk_values')
)::meta.row_id;
-- Only update if row still exists
if meta.row_exists(_field_row_id) then
execute format(
'update %I.%I set %I = %L where %I = %L',
((_sfv).field_id->>'schema_name'),
((_sfv).field_id->>'relation_name'),
((_sfv).field_id->>'column_name'),
(_sfv).value,
(((_sfv).field_id->'pk_column_names'->>0)),
(((_sfv).field_id->'pk_values'->>0))
);
end if;
end loop;
-- Restore offstage tracked rows (re-track them if not already tracked and row exists)
foreach _rid in array _stash.offstage_tracked_rows_added
loop
-- Only track if row exists and not already tracked
if meta.row_exists(_rid) and not bundle._is_newly_tracked(_repository_id, _rid) then
perform bundle._track_untracked_row(_repository_id, _rid);
end if;
end loop;
-- Remove stash
delete from bundle.stash where id = _stash.id;
return _stash.id;
end;
$$ language plpgsql;
-- Convenience wrapper
create or replace function bundle.stash_pop(
_repository_name text,
_force boolean default false
) returns uuid as $$
select bundle._stash_pop(
(select id from bundle.repository where name = _repository_name),
_force
);
$$ language sql;
-- Apply stash without removing (like git stash apply)
-- _force: overwrite all conflicts
-- _skip_conflicts: skip conflicting fields, apply safe ones
create or replace function bundle._stash_apply(
_repository_id uuid,
_stash_id uuid default null,
_force boolean default false,
_skip_conflicts boolean default false
) returns uuid as $$
declare
_stash record;
_sfv bundle.stash_field_value;
_rid meta.row_id;
_field_row_id meta.row_id;
_conflicts text[];
_conflict_fields jsonb[];
_current_value text;
_is_conflict boolean;
begin
-- Get specified stash or most recent
if _stash_id is not null then
select * into _stash
from bundle.stash
where id = _stash_id and repository_id = _repository_id;
else
select * into _stash
from bundle.stash
where repository_id = _repository_id
order by created_at desc
limit 1;
end if;
if _stash is null then
raise exception 'Stash not found';
end if;
-- Build list of conflicting fields
_conflicts := '{}';
_conflict_fields := '{}';
foreach _sfv in array _stash.offstage_updated_fields
loop
-- Check if this field is also uncommitted
if exists (
select 1 from bundle._get_offstage_updated_fields(_repository_id)
where field_id::jsonb = (((_sfv).field_id)::text::jsonb)
) then
-- Get current value from database
execute format(
'select %I::text from %I.%I where %I = %L',
(_sfv).field_id->>'column_name',
(_sfv).field_id->>'schema_name',
(_sfv).field_id->>'relation_name',
((_sfv).field_id->'pk_column_names'->>0),
((_sfv).field_id->'pk_values'->>0)
) into _current_value;
-- Only conflict if values differ
if _current_value is distinct from (_sfv).value then
_conflicts := array_append(_conflicts,
((_sfv).field_id->>'schema_name') || '.' ||
((_sfv).field_id->>'relation_name') || '.' ||
((_sfv).field_id->>'column_name')
);
_conflict_fields := array_append(_conflict_fields, ((_sfv).field_id)::jsonb);
end if;
end if;
end loop;
-- Handle conflicts based on mode
if array_length(_conflicts, 1) > 0 and not _force and not _skip_conflicts then
raise exception 'CONFLICT: You have uncommitted changes to: %. Applying this stash would overwrite them.', array_to_string(_conflicts, ', ');
end if;
-- Restore offstage updated field values (skip if row no longer exists)
foreach _sfv in array _stash.offstage_updated_fields
loop
-- Check if this field is a conflict
_is_conflict := ((_sfv).field_id)::jsonb = any(_conflict_fields);
-- Skip conflicts if _skip_conflicts is true
if _is_conflict and _skip_conflicts then
continue;
end if;
-- Build row_id from field_id
_field_row_id := jsonb_build_object(
'schema_name', ((_sfv).field_id->>'schema_name'),
'relation_name', ((_sfv).field_id->>'relation_name'),
'pk_column_names', ((_sfv).field_id->'pk_column_names'),
'pk_values', ((_sfv).field_id->'pk_values')
)::meta.row_id;
-- Only update if row still exists
if meta.row_exists(_field_row_id) then
execute format(
'update %I.%I set %I = %L where %I = %L',
((_sfv).field_id->>'schema_name'),
((_sfv).field_id->>'relation_name'),
((_sfv).field_id->>'column_name'),
(_sfv).value,
(((_sfv).field_id->'pk_column_names'->>0)),
(((_sfv).field_id->'pk_values'->>0))
);
end if;
end loop;
-- Restore offstage tracked rows (re-track them if not already tracked and row exists)
foreach _rid in array _stash.offstage_tracked_rows_added
loop
-- Only track if row exists and not already tracked
if meta.row_exists(_rid) and not bundle._is_newly_tracked(_repository_id, _rid) then
perform bundle._track_untracked_row(_repository_id, _rid);
end if;
end loop;
return _stash.id;
end;
$$ language plpgsql;
-- Convenience wrapper
create or replace function bundle.stash_apply(
_repository_name text,
_stash_id uuid default null,
_force boolean default false,
_skip_conflicts boolean default false
) returns uuid as $$
select bundle._stash_apply(
(select id from bundle.repository where name = _repository_name),
_stash_id,
_force,
_skip_conflicts
);
$$ language sql;
-- List stashes for a repository
create or replace function bundle.stash_list(
_repository_name text
) returns table (
id uuid,
message text,
created_at timestamptz,
offstage_fields int,
offstage_rows int,
staged_fields int,
staged_rows int
) as $$
select
s.id,
s.message,
s.created_at,
cardinality(s.offstage_updated_fields),
cardinality(s.offstage_tracked_rows_added) + cardinality(s.offstage_deleted_rows),
cardinality(s.stage_fields_to_change),
cardinality(s.stage_rows_to_add) + cardinality(s.stage_rows_to_remove)
from bundle.stash s
join bundle.repository r on r.id = s.repository_id
where r.name = _repository_name
order by s.created_at desc;
$$ language sql;
-- Show details of a specific stash
create or replace function bundle.stash_show(
_stash_id uuid
) returns table (
category text,
item_type text,
identifier text,
value_preview text
) as $$
begin
-- Offstage updated fields
return query
select
'offstage'::text,
'field'::text,
((sfv).field_id->>'schema_name') || '.' ||
((sfv).field_id->>'relation_name') || '.' ||
((sfv).field_id->>'column_name'),
left((sfv).value, 80) || case when length((sfv).value) > 80 then '...' else '' end
from bundle.stash s,
unnest(s.offstage_updated_fields) sfv
where s.id = _stash_id;
-- Offstage tracked rows added
return query
select
'offstage'::text,
'row_add'::text,
(rid->>'schema_name') || '.' || (rid->>'relation_name') || ':' || (rid->'pk_values'->>0),
null::text
from bundle.stash s,
unnest(s.offstage_tracked_rows_added) rid
where s.id = _stash_id;
-- Offstage deleted rows
return query
select
'offstage'::text,
'row_delete'::text,
(rid->>'schema_name') || '.' || (rid->>'relation_name') || ':' || (rid->'pk_values'->>0),
null::text
from bundle.stash s,
unnest(s.offstage_deleted_rows) rid
where s.id = _stash_id;
-- Staged fields
return query
select
'staged'::text,
'field'::text,
(fid->>'schema_name') || '.' || (fid->>'relation_name') || '.' || (fid->>'column_name'),
null::text
from bundle.stash s,
unnest(s.stage_fields_to_change) fid
where s.id = _stash_id;
-- Staged rows to add
return query
select
'staged'::text,
'row_add'::text,
(rid->>'schema_name') || '.' || (rid->>'relation_name') || ':' || (rid->'pk_values'->>0),
null::text
from bundle.stash s,
unnest(s.stage_rows_to_add) rid
where s.id = _stash_id;
-- Staged rows to remove
return query
select
'staged'::text,
'row_remove'::text,
(rid->>'schema_name') || '.' || (rid->>'relation_name') || ':' || (rid->'pk_values'->>0),
null::text
from bundle.stash s,
unnest(s.stage_rows_to_remove) rid
where s.id = _stash_id;
end;
$$ language plpgsql;
-- Add rows to an existing stash (captures current state of those rows)
create or replace function bundle.stash_add_rows(
_stash_id uuid,
_row_ids meta.row_id[]
) returns void as $$
declare
_stash record;
_repo record;
_new_offstage_fields bundle.stash_field_value[] := '{}';
_new_offstage_tracked meta.row_id[] := '{}';
_new_offstage_deleted meta.row_id[] := '{}';
_new_stage_rows_add meta.row_id[] := '{}';
_new_stage_rows_remove meta.row_id[] := '{}';
_new_stage_fields meta.field_id[] := '{}';
_all_stage_rows_add meta.row_id[];
_all_stage_rows_remove meta.row_id[];
_all_stage_fields meta.field_id[];
_field record;
_row record;
_fid meta.field_id;
_rid meta.row_id;
_field_value text;
begin
-- Get stash
select * into _stash from bundle.stash where id = _stash_id;
if _stash is null then
raise exception 'Stash not found: %', _stash_id;
end if;
-- Get repository
select * into _repo from bundle.repository where id = _stash.repository_id;
-- Convert staged jsonb arrays to typed arrays
select coalesce(array_agg(r::meta.row_id), '{}')
into _all_stage_rows_add
from jsonb_array_elements(_repo.stage_rows_to_add) r;
select coalesce(array_agg(r::meta.row_id), '{}')
into _all_stage_rows_remove
from jsonb_array_elements(_repo.stage_rows_to_remove) r;
select coalesce(array_agg(f::meta.field_id), '{}')
into _all_stage_fields
from jsonb_array_elements(_repo.stage_fields_to_change) f;
-- Filter staged rows to add (not already in stash)
select coalesce(array_agg(r), '{}')
into _new_stage_rows_add
from unnest(_all_stage_rows_add) r
where r = any(_row_ids)
and not (r = any(_stash.stage_rows_to_add));
-- Filter staged rows to remove (not already in stash)
select coalesce(array_agg(r), '{}')
into _new_stage_rows_remove
from unnest(_all_stage_rows_remove) r
where r = any(_row_ids)
and not (r = any(_stash.stage_rows_to_remove));
-- Filter staged fields (not already in stash)
select coalesce(array_agg(f), '{}')
into _new_stage_fields
from unnest(_all_stage_fields) f
where bundle._field_to_row(f) = any(_row_ids)
and not (f = any(_stash.stage_fields_to_change));
-- Collect offstage updated fields (filtered, not already in stash)
for _field in
select field_id from bundle._get_offstage_updated_fields(_stash.repository_id)
where bundle._field_to_row(field_id) = any(_row_ids)
loop
_fid := _field.field_id;
-- Skip if already in stash
if exists (
select 1 from unnest(_stash.offstage_updated_fields) f
where (f).field_id = _fid
) then
continue;
end if;
execute format(
'select %I::text from %I.%I where %I = %L',
_fid->>'column_name',
_fid->>'schema_name',
_fid->>'relation_name',
(_fid->'pk_column_names'->>0),
(_fid->'pk_values'->>0)
) into _field_value;
_new_offstage_fields := array_append(
_new_offstage_fields,
row(_fid, _field_value)::bundle.stash_field_value
);
end loop;
-- Collect offstage tracked rows added (filtered, not already in stash)
for _row in
select row_id from bundle._get_tracked_rows_added(_stash.repository_id)
where row_id::meta.row_id = any(_row_ids)
and not (row_id::meta.row_id = any(_stash.offstage_tracked_rows_added))
loop
_new_offstage_tracked := array_append(_new_offstage_tracked, _row.row_id::meta.row_id);
end loop;
-- Collect offstage deleted rows (filtered, not already in stash)
for _rid in
select bundle._get_offstage_deleted_rows(_stash.repository_id)
loop
if _rid = any(_row_ids) and not (_rid = any(_stash.offstage_deleted_rows)) then
_new_offstage_deleted := array_append(_new_offstage_deleted, _rid);
end if;
end loop;
-- Check if there's anything to add
if cardinality(_new_stage_rows_add) = 0 and
cardinality(_new_stage_rows_remove) = 0 and
cardinality(_new_stage_fields) = 0 and
cardinality(_new_offstage_tracked) = 0 and
cardinality(_new_offstage_deleted) = 0 and
cardinality(_new_offstage_fields) = 0 then
raise exception 'No new changes found for specified rows';
end if;
-- Update stash with new items
update bundle.stash set
stage_rows_to_add = stage_rows_to_add || _new_stage_rows_add,
stage_rows_to_remove = stage_rows_to_remove || _new_stage_rows_remove,
stage_fields_to_change = stage_fields_to_change || _new_stage_fields,
offstage_tracked_rows_added = offstage_tracked_rows_added || _new_offstage_tracked,
offstage_deleted_rows = offstage_deleted_rows || _new_offstage_deleted,
offstage_updated_fields = offstage_updated_fields || _new_offstage_fields
where id = _stash_id;
-- Remove newly stashed items from staging area
update bundle.repository set
stage_rows_to_add = (
select coalesce(jsonb_agg(r), '[]')
from jsonb_array_elements(stage_rows_to_add) r
where not (r::meta.row_id = any(_new_stage_rows_add))
),
stage_rows_to_remove = (
select coalesce(jsonb_agg(r), '[]')
from jsonb_array_elements(stage_rows_to_remove) r
where not (r::meta.row_id = any(_new_stage_rows_remove))
),
stage_fields_to_change = (
select coalesce(jsonb_agg(f), '[]')
from jsonb_array_elements(stage_fields_to_change) f
where not (f::meta.field_id = any(_new_stage_fields))
)
where id = _stash.repository_id;
end;
$$ language plpgsql;
-- Remove rows from an existing stash
create or replace function bundle.stash_remove_rows(
_stash_id uuid,
_row_ids meta.row_id[]
) returns void as $$
declare
_stash record;
_removed_count int := 0;
_new_stage_rows_add meta.row_id[];
_new_stage_rows_remove meta.row_id[];
_new_stage_fields meta.field_id[];
_new_offstage_tracked meta.row_id[];
_new_offstage_deleted meta.row_id[];
_new_offstage_fields bundle.stash_field_value[];
begin
-- Get stash
select * into _stash from bundle.stash where id = _stash_id;
if _stash is null then
raise exception 'Stash not found: %', _stash_id;
end if;
-- Filter out specified rows from each array
select coalesce(array_agg(r), '{}')
into _new_stage_rows_add
from unnest(_stash.stage_rows_to_add) r
where not (r = any(_row_ids));
select coalesce(array_agg(r), '{}')
into _new_stage_rows_remove
from unnest(_stash.stage_rows_to_remove) r
where not (r = any(_row_ids));
select coalesce(array_agg(f), '{}')
into _new_stage_fields
from unnest(_stash.stage_fields_to_change) f
where not (bundle._field_to_row(f) = any(_row_ids));
select coalesce(array_agg(r), '{}')
into _new_offstage_tracked
from unnest(_stash.offstage_tracked_rows_added) r
where not (r = any(_row_ids));
select coalesce(array_agg(r), '{}')
into _new_offstage_deleted
from unnest(_stash.offstage_deleted_rows) r
where not (r = any(_row_ids));
select coalesce(array_agg(f), '{}')
into _new_offstage_fields
from unnest(_stash.offstage_updated_fields) f
where not (bundle._field_to_row((f).field_id) = any(_row_ids));
-- Count removed items
_removed_count := (cardinality(_stash.stage_rows_to_add) - cardinality(_new_stage_rows_add))
+ (cardinality(_stash.stage_rows_to_remove) - cardinality(_new_stage_rows_remove))
+ (cardinality(_stash.stage_fields_to_change) - cardinality(_new_stage_fields))
+ (cardinality(_stash.offstage_tracked_rows_added) - cardinality(_new_offstage_tracked))
+ (cardinality(_stash.offstage_deleted_rows) - cardinality(_new_offstage_deleted))
+ (cardinality(_stash.offstage_updated_fields) - cardinality(_new_offstage_fields));
if _removed_count = 0 then
raise exception 'No matching rows found in stash';
end if;
-- Update stash
update bundle.stash set
stage_rows_to_add = _new_stage_rows_add,
stage_rows_to_remove = _new_stage_rows_remove,
stage_fields_to_change = _new_stage_fields,
offstage_tracked_rows_added = _new_offstage_tracked,
offstage_deleted_rows = _new_offstage_deleted,
offstage_updated_fields = _new_offstage_fields
where id = _stash_id;
-- If stash is now empty, delete it
if cardinality(_new_stage_rows_add) = 0 and
cardinality(_new_stage_rows_remove) = 0 and
cardinality(_new_stage_fields) = 0 and
cardinality(_new_offstage_tracked) = 0 and
cardinality(_new_offstage_deleted) = 0 and
cardinality(_new_offstage_fields) = 0 then
delete from bundle.stash where id = _stash_id;
raise notice 'Stash is now empty and has been deleted';
end if;
end;
$$ language plpgsql;
-- Drop a stash without applying
create or replace function bundle.stash_drop(
_stash_id uuid
) returns void as $$
delete from bundle.stash where id = _stash_id;
$$ language sql;
-- Clear all stashes for a repository
create or replace function bundle.stash_clear(
_repository_name text
) returns int as $$
declare
_count int;
begin
delete from bundle.stash
where repository_id = (select id from bundle.repository where name = _repository_name);
get diagnostics _count = row_count;
return _count;
end;
$$ language plpgsql;
-- Get all uncommitted changes for a repository (for stash row selector UI)
create or replace function bundle.uncommitted_changes(
_repository_name text
) returns table (
category text,
item_type text,
row_id meta.row_id,
identifier text,
value_preview text
) as $$
declare
_repository_id uuid;
_repo record;
begin
select id into _repository_id from bundle.repository where name = _repository_name;
select * into _repo from bundle.repository where id = _repository_id;