-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistory.sql
More file actions
658 lines (558 loc) · 18 KB
/
history.sql
File metadata and controls
658 lines (558 loc) · 18 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
------------------------------------------------------------------------------
-- HISTORY / TIME TRAVEL functions
--
-- Retrieve historical row/field data from any commit.
------------------------------------------------------------------------------
--
-- _get_jsonb_row_at_commit()
--
-- Returns a single row as JSONB, with column names as keys and unhashed values.
--
create or replace function _get_jsonb_row_at_commit(_commit_id uuid, _row_id meta.row_id)
returns jsonb as $$
declare
row_fields jsonb;
result jsonb := '{}';
col_name text;
col_hash text;
col_value text;
begin
-- look up the row's field hashes from the commit
select jsonb_fields->(_row_id::text)
into row_fields
from bundle.commit
where id = _commit_id;
-- row not found in commit
if row_fields is null then
return null;
end if;
-- unhash each field value
-- values are stored as to_jsonb(val)::text, so parse back to jsonb
for col_name, col_hash in select * from jsonb_each_text(row_fields) loop
col_value := bundle.unhash(col_hash);
if col_value is not null then
result := result || jsonb_build_object(col_name, col_value::jsonb);
else
result := result || jsonb_build_object(col_name, null);
end if;
end loop;
return result;
end;
$$ language plpgsql stable;
--
-- _get_jsonb_rows_at_commit()
--
-- Returns all rows from a relation at a specific commit as setof jsonb.
--
create or replace function _get_jsonb_rows_at_commit(_commit_id uuid, _relation_id meta.relation_id)
returns setof jsonb as $$
select bundle._get_jsonb_row_at_commit(_commit_id, row_id)
from bundle._get_commit_rows(_commit_id, _relation_id);
$$ language sql stable;
--
-- _get_jsonb_field_at_commit()
--
-- Returns a single field value (as text) at a specific commit.
--
create or replace function _get_jsonb_field_at_commit(_commit_id uuid, _field_id meta.field_id)
returns text as $$
declare
_row_id meta.row_id;
_column_name text;
_hash text;
_value text;
begin
-- extract row_id and column_name from field_id
_row_id := meta.field_id_to_row_id(_field_id);
_column_name := _field_id->>'column_name';
-- look up the hash
select jsonb_fields->(_row_id::text)->>_column_name
into _hash
from bundle.commit
where id = _commit_id;
if _hash is null then
return null;
end if;
-- unhash returns to_jsonb(val)::text, so parse back and extract text
_value := bundle.unhash(_hash);
if _value is null then
return null;
end if;
return _value::jsonb #>> '{}';
end;
$$ language plpgsql stable;
------------------------------------------------------------------------------
-- RECORD-RETURNING FUNCTIONS
--
-- Use jsonb_populate_record to return actual table row types.
------------------------------------------------------------------------------
--
-- _get_row_at_commit()
--
-- Returns a single row as an actual record type matching the table structure.
-- Pass null::table_name as the third argument to specify the return type.
--
-- Example:
-- select * from bundle._get_row_at_commit(commit_id, row_id, null::widget.widget);
--
create or replace function _get_row_at_commit(
_commit_id uuid,
_row_id meta.row_id,
_record anyelement
)
returns anyelement as $$
select jsonb_populate_record(
_record,
bundle._get_jsonb_row_at_commit(_commit_id, _row_id)
);
$$ language sql stable;
--
-- _get_rows_at_commit()
--
-- Returns all rows from a relation at a specific commit as actual records.
-- Pass null::table_name as the third argument to specify the return type.
--
-- Example:
-- select * from bundle._get_rows_at_commit(commit_id, relation_id, null::widget.widget);
--
create or replace function _get_rows_at_commit(
_commit_id uuid,
_relation_id meta.relation_id,
_record anyelement
)
returns setof anyelement as $$
select jsonb_populate_record(
_record,
bundle._get_jsonb_row_at_commit(_commit_id, row_id)
)
from bundle._get_commit_rows(_commit_id, _relation_id);
$$ language sql stable;
------------------------------------------------------------------------------
-- PUBLIC API
--
-- User-friendly functions that take bundle name + offset or timestamp.
------------------------------------------------------------------------------
--
-- get_row_at_commit() - by offset
--
-- Offset: 0 = HEAD, -1 = parent, -2 = grandparent, etc.
--
create or replace function get_row_at_commit(
repository_name text,
_offset int,
_row_id meta.row_id,
_record anyelement
)
returns anyelement as $$
declare
_commit_id uuid;
begin
if _offset > 0 then
raise exception 'Offset must be 0 or negative (0 = HEAD, -1 = parent, etc.)';
end if;
select commit_id into _commit_id
from bundle._get_commit_ancestry(bundle.head_commit_id(repository_name))
where position = (1 - _offset); -- position 1 = HEAD, 2 = parent, etc.
if _commit_id is null then
raise exception 'Commit not found at offset %', _offset;
end if;
return bundle._get_row_at_commit(_commit_id, _row_id, _record);
end;
$$ language plpgsql stable;
--
-- get_row_at_commit() - by timestamp
--
-- Returns row as it was at the most recent commit at or before the given time.
--
create or replace function get_row_at_commit(
repository_name text,
_time timestamptz,
_row_id meta.row_id,
_record anyelement
)
returns anyelement as $$
declare
_commit_id uuid;
begin
select c.id into _commit_id
from bundle.commit c
join bundle.repository r on c.repository_id = r.id
where r.name = repository_name
and c.commit_time <= _time
order by c.commit_time desc
limit 1;
if _commit_id is null then
raise exception 'No commit found at or before %', _time;
end if;
return bundle._get_row_at_commit(_commit_id, _row_id, _record);
end;
$$ language plpgsql stable;
--
-- get_rows_at_commit() - by offset
--
create or replace function get_rows_at_commit(
repository_name text,
_offset int,
_relation_id meta.relation_id,
_record anyelement
)
returns setof anyelement as $$
declare
_commit_id uuid;
begin
if _offset > 0 then
raise exception 'Offset must be 0 or negative (0 = HEAD, -1 = parent, etc.)';
end if;
select commit_id into _commit_id
from bundle._get_commit_ancestry(bundle.head_commit_id(repository_name))
where position = (1 - _offset);
if _commit_id is null then
raise exception 'Commit not found at offset %', _offset;
end if;
return query select * from bundle._get_rows_at_commit(_commit_id, _relation_id, _record);
end;
$$ language plpgsql stable;
--
-- get_rows_at_commit() - by timestamp
--
create or replace function get_rows_at_commit(
repository_name text,
_time timestamptz,
_relation_id meta.relation_id,
_record anyelement
)
returns setof anyelement as $$
declare
_commit_id uuid;
begin
select c.id into _commit_id
from bundle.commit c
join bundle.repository r on c.repository_id = r.id
where r.name = repository_name
and c.commit_time <= _time
order by c.commit_time desc
limit 1;
if _commit_id is null then
raise exception 'No commit found at or before %', _time;
end if;
return query select * from bundle._get_rows_at_commit(_commit_id, _relation_id, _record);
end;
$$ language plpgsql stable;
--
-- get_field_at_commit() - by offset
--
create or replace function get_field_at_commit(
repository_name text,
_offset int,
_field_id meta.field_id
)
returns text as $$
declare
_commit_id uuid;
begin
if _offset > 0 then
raise exception 'Offset must be 0 or negative (0 = HEAD, -1 = parent, etc.)';
end if;
select commit_id into _commit_id
from bundle._get_commit_ancestry(bundle.head_commit_id(repository_name))
where position = (1 - _offset);
if _commit_id is null then
raise exception 'Commit not found at offset %', _offset;
end if;
return bundle._get_jsonb_field_at_commit(_commit_id, _field_id);
end;
$$ language plpgsql stable;
--
-- get_field_at_commit() - by timestamp
--
create or replace function get_field_at_commit(
repository_name text,
_time timestamptz,
_field_id meta.field_id
)
returns text as $$
declare
_commit_id uuid;
begin
select c.id into _commit_id
from bundle.commit c
join bundle.repository r on c.repository_id = r.id
where r.name = repository_name
and c.commit_time <= _time
order by c.commit_time desc
limit 1;
if _commit_id is null then
raise exception 'No commit found at or before %', _time;
end if;
return bundle._get_jsonb_field_at_commit(_commit_id, _field_id);
end;
$$ language plpgsql stable;
------------------------------------------------------------------------------
-- ROW CHANGE ANCESTRY
--
-- Find commits where a specific row was changed.
-- Different from commit ancestry - only includes commits that modified the row.
------------------------------------------------------------------------------
--
-- _get_row_change_ancestry()
--
-- Returns commits where the given row was changed, walking back from a starting commit.
-- A row is "changed" if its field hashes differ from the parent commit
-- (including being added or deleted).
--
-- Returns change_number 1 as most recent change, with full commit metadata.
--
create or replace function _get_row_change_ancestry(
_row_id meta.row_id,
_starting_commit_id uuid
) returns table(
commit_id uuid,
change_number int,
commit_time timestamptz,
message text,
author_name text,
author_email text
) as $$
with recursive ancestry as (
-- Start with the starting commit
select
c.id as commit_id,
c.parent_id,
c.jsonb_fields->(_row_id::text) as row_fields,
c.commit_time,
c.message,
c.author_name,
c.author_email,
1 as depth
from bundle.commit c
where c.id = _starting_commit_id
union all
-- Walk back through parents
select
c.id,
c.parent_id,
c.jsonb_fields->(_row_id::text),
c.commit_time,
c.message,
c.author_name,
c.author_email,
a.depth + 1
from bundle.commit c
join ancestry a on c.id = a.parent_id
),
changes as (
-- Find commits where the row changed from its parent
-- lead() gives the parent's row_fields (next depth = older commit)
select
a.commit_id,
a.row_fields,
a.commit_time,
a.message,
a.author_name,
a.author_email,
a.depth,
lead(a.row_fields) over (order by a.depth) as parent_row_fields
from ancestry a
)
select
c.commit_id,
row_number() over (order by c.depth)::int as change_number,
c.commit_time,
c.message,
c.author_name,
c.author_email
from changes c
where c.row_fields is distinct from c.parent_row_fields
order by c.depth;
$$ language sql stable;
--
-- get_row_at_change()
--
-- Returns a row as it was at the Nth change to that row.
-- change_number 1 = most recent change, 2 = second most recent, etc.
--
create or replace function get_row_at_change(
_repository_name text,
_change_number int,
_row_id meta.row_id,
_record anyelement
) returns anyelement as $$
declare
_commit_id uuid;
begin
if _change_number < 1 then
raise exception 'change_number must be >= 1 (1 = most recent change)';
end if;
select commit_id into _commit_id
from bundle._get_row_change_ancestry(
_row_id,
bundle.head_commit_id(_repository_name)
)
where change_number = _change_number;
if _commit_id is null then
raise exception 'Change #% not found for this row', _change_number
using hint = 'The row may not have that many changes in history';
end if;
return bundle._get_row_at_commit(_commit_id, _row_id, _record);
end;
$$ language plpgsql stable;
--
-- get_field_at_change()
--
-- Returns a field value as it was at the Nth change to that row.
-- change_number 1 = most recent change, 2 = second most recent, etc.
--
create or replace function get_field_at_change(
_repository_name text,
_change_number int,
_field_id meta.field_id
) returns text as $$
declare
_row_id meta.row_id;
_commit_id uuid;
begin
if _change_number < 1 then
raise exception 'change_number must be >= 1 (1 = most recent change)';
end if;
_row_id := meta.field_id_to_row_id(_field_id);
select commit_id into _commit_id
from bundle._get_row_change_ancestry(
_row_id,
bundle.head_commit_id(_repository_name)
)
where change_number = _change_number;
if _commit_id is null then
raise exception 'Change #% not found for this row', _change_number
using hint = 'The row may not have that many changes in history';
end if;
return bundle._get_jsonb_field_at_commit(_commit_id, _field_id);
end;
$$ language plpgsql stable;
------------------------------------------------------------------------------
-- VERSION RESOLVER
--
-- Parse version specifiers and return commit UUIDs.
------------------------------------------------------------------------------
--
-- resolve_version()
--
-- Parses a version specifier and returns the corresponding commit UUID.
--
-- Supported formats:
-- 'latest' / 'head' → HEAD commit
-- 'head~3' → 3 commits back from HEAD
-- '@2024-01-15' → most recent commit at or before timestamp
-- '{uuid}' → direct commit UUID (passthrough)
-- '1.0.0' (future) → semver tag lookup
--
create or replace function resolve_version(
_repository_name text,
_version_spec text
) returns uuid as $$
declare
_commit_id uuid;
_offset int;
_timestamp timestamptz;
begin
-- Normalize to lowercase for keyword matching
_version_spec := lower(trim(_version_spec));
-- 'latest' or 'head' → HEAD commit
if _version_spec in ('latest', 'head') then
return bundle.head_commit_id(_repository_name);
end if;
-- 'head~N' → N commits back
if _version_spec ~ '^head~[0-9]+$' then
_offset := substring(_version_spec from 6)::int;
select commit_id into _commit_id
from bundle._get_commit_ancestry(bundle.head_commit_id(_repository_name))
where position = (_offset + 1); -- position 1 = HEAD, 2 = head~1, etc.
if _commit_id is null then
raise exception 'Commit not found at head~%', _offset
using hint = 'The repository may not have that many commits';
end if;
return _commit_id;
end if;
-- '@timestamp' → commit at or before timestamp
if _version_spec ~ '^@' then
begin
_timestamp := substring(_version_spec from 2)::timestamptz;
exception when others then
raise exception 'Invalid timestamp format: %', substring(_version_spec from 2)
using hint = 'Use ISO 8601 format, e.g., @2024-01-15 or @2024-01-15T14:30:00Z';
end;
select c.id into _commit_id
from bundle.commit c
join bundle.repository r on c.repository_id = r.id
where r.name = _repository_name
and c.commit_time <= _timestamp
order by c.commit_time desc
limit 1;
if _commit_id is null then
raise exception 'No commit found at or before %', _timestamp;
end if;
return _commit_id;
end if;
-- Try parsing as UUID (direct commit reference)
begin
_commit_id := _version_spec::uuid;
-- Verify it exists and belongs to this repository
if not exists (
select 1 from bundle.commit c
join bundle.repository r on c.repository_id = r.id
where c.id = _commit_id and r.name = _repository_name
) then
raise exception 'Commit % not found in repository %', _version_spec, _repository_name;
end if;
return _commit_id;
exception when invalid_text_representation then
-- Not a UUID, continue to other formats
null;
end;
-- Semver range: ^X.Y → highest version >= X.Y.0 and < (X+1).0.0
if _version_spec ~ '^\^[0-9]+\.[0-9]+' then
declare
_major int;
_minor int;
_repo_id uuid;
begin
_major := (regexp_match(_version_spec, '^\^([0-9]+)'))[1]::int;
_minor := (regexp_match(_version_spec, '^\^[0-9]+\.([0-9]+)'))[1]::int;
select id into _repo_id from bundle.repository where name = _repository_name;
-- Find highest version that satisfies ^X.Y (>= X.Y.0, < (X+1).0.0)
select c.id into _commit_id
from bundle.commit c
where c.repository_id = _repo_id
and c.version is not null
and bundle.major(c.version) = _major
and (bundle.major(c.version) > _major
or bundle.minor(c.version) >= _minor)
order by c.version desc
limit 1;
if _commit_id is not null then
return _commit_id;
end if;
end;
end if;
-- Exact semver: X.Y.Z → specific version
if _version_spec ~ '^[0-9]+\.[0-9]+\.[0-9]+' then
declare
_repo_id uuid;
begin
select id into _repo_id from bundle.repository where name = _repository_name;
select c.id into _commit_id
from bundle.commit c
where c.repository_id = _repo_id
and c.version = _version_spec::bundle.version;
if _commit_id is not null then
return _commit_id;
end if;
end;
end if;
-- 'live' → return NULL (caller uses current database)
if _version_spec = 'live' then
return null;
end if;
raise exception 'Unknown version specifier: %', _version_spec
using hint = 'Valid formats: live, latest, head, head~N, @timestamp, ^X.Y, X.Y.Z, or commit UUID';
end;
$$ language plpgsql stable;