You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
By setting `depth := 1`, only the most recent change is returned.
394
+
395
+
### Restoring previous versions
396
+
397
+
`git_resolve` gives you a json representation of a prior version of a row, which can be used for backup and restore. The first argument is a `git` json value, the second value is a valid git ref string.
398
+
399
+
Combine it with `git_log` to get a previous version - the below query uses `->1->'oid'` to get the oid from the second item in the log array:
400
+
401
+
```sql
402
+
select git_resolve(git, git_log(git)->1->>'oid')
403
+
from test_table
404
+
where id =2
405
+
```
406
+
407
+
```json
408
+
[
409
+
{
410
+
"git_resolve": {
411
+
"id": 2,
412
+
"text": "original value set by alice"
413
+
}
414
+
}
415
+
]
416
+
```
417
+
418
+
This can be used in an update query to revert a change:
419
+
420
+
```sql
421
+
update test_table set (id, text) =
422
+
(
423
+
select id, text
424
+
from json_populate_record(
425
+
null::test_table, (
426
+
select git_resolve(git, git_log(git)->1->>'oid')
427
+
from test_table
428
+
where id =2
429
+
)
430
+
)
431
+
)
432
+
where id =2
433
+
returning id, text
434
+
```
435
+
436
+
```json
437
+
[
438
+
{
439
+
"id": 2,
440
+
"text": "original value set by alice"
441
+
}
442
+
]
443
+
```
444
+
445
+
Or a similar technique can restore a deleted item:
where tablename ='test_table'and identifier->>'id'='1'
455
+
)
456
+
)
457
+
returning id, text
458
+
```
459
+
460
+
```json
461
+
[
462
+
{
463
+
"id": 1,
464
+
"text": "updated content"
465
+
}
466
+
]
467
+
```
370
468
<!-- codegen:end -->
371
469
372
470
## Caveat
@@ -375,6 +473,7 @@ By setting `depth := 1`, only the most recent change is returned.
375
473
- It hasn't been performance-tested yet. It works well for rows with small, easily-json-stringifiable data. Large, frequently updated rows may hit issues.
376
474
- It currently uses the `JSON` data type to store a serialised copy of the `.git` repo folder. This can likely be optimised to use `BYTEA` or another data type.
377
475
- It uses several tools that were _not_ built with each other in mind (although each is well-designed and flexible enough for them to play nice without too many problems). See the [implementation section](#implementation)
476
+
- It's still in v0, so breaking changes may occur.
0 commit comments