Skip to content

Commit 2e39228

Browse files
authored
Merge pull request #1815 from dolthub/fulghum/minor-doc-updates
Documentation for `dolt_reflog()` table function
2 parents b85951c + 85ea3d3 commit 2e39228

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

content/reference/sql/version-control/branches.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ started, and can be changed for new connections with a [system
2121
variable](./dolt-sysvars.md#dbname_default_branch). Using database revision specifiers, clients can
2222
choose a specific branch, tag, or commit to pin their queries to.
2323

24+
## Recovering a deleted branch
25+
The data on a branch is versioned, but the metadata of the branch head itself is not, so if you delete a
26+
branch or reset it to point at an older commit, you can't revert or undo that change the same way you can
27+
with your data. Instead, you can use
28+
[the `dolt_reflog()` table function](./dolt-sql-functions.md#dolt_reflog) to see the history of commits
29+
your branch has referenced and either recreate the branch from the last referenced commit with
30+
[the `dolt_branch()` stored procedure](./dolt-sql-procedures.md#dolt_branch) or reset the branch to a
31+
previous commit with [the `dolt_reset()` stored procedure](./dolt-sql-procedures.md#dolt_reset). See
32+
[the `dolt_reflog()` table function](./dolt-sql-functions.md#dolt_reflog) for an example of recreating
33+
a deleted branch and more information on how the Dolt reflog works and what limitations it has.
34+
2435
## Specify a database revision in the connection string
2536

2637
The exact connection string you need to use will vary depending on your client.

content/reference/sql/version-control/dolt-sql-functions.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ title: Dolt SQL Functions
1919
- [dolt_diff_summary()](#dolt_diff_summary)
2020
- [dolt_log()](#dolt_log)
2121
- [dolt_patch()](#dolt_patch)
22+
- [dolt_reflog()](#dolt_reflog)
2223
- [dolt_schema_diff](#dolt_schema_diff)
2324
- [dolt_query_diff](#dolt_query_diff)
2425

@@ -806,6 +807,69 @@ With result of single row:
806807
+-----------------+------------------+----------------------------------+------------+-----------+---------------------+
807808
```
808809

810+
## `DOLT_REFLOG()`
811+
812+
The `DOLT_REFLOG()` table function shows the history of named refs (e.g. branches and tags), which is useful when you want to understand how a branch or tag has changed over time to reference different commits, particularly for information that isn't surfaced through the `dolt_log` system table or `dolt_log()` table function. For example, if you use `dolt_reset()` to change the commit a branch points to, you can use `dolt_reflog()` to see what commit the branch was pointing to before it was moved to that commit. Another common use case for `dolt_reflog()` is to recreate a branch or tag that was accidentally deleted. The example section below shows how to recreate a deleted branch.
813+
814+
The data from Dolt's reflog comes from [Dolt's journaling chunk store](https://www.dolthub.com/blog/2023-03-08-dolt-chunk-journal/). This data is local to a Dolt database and never included when pushing, pulling, or cloning a Dolt database. This means when you clone a Dolt database, it will not have any reflog data until you perform operations that change what commit branches or tags reference.
815+
816+
Dolt's reflog is similar to [Git's reflog](https://git-scm.com/docs/git-reflog), but there are a few differences:
817+
- The Dolt reflog currently only supports named references, such as branches and tags, and not any of Git's special refs (e.g. `HEAD`, `FETCH-HEAD`, `MERGE-HEAD`).
818+
- The Dolt reflog can be queried for the log of references, even after a reference has been deleted. In Git, once a branch or tag is deleted, the reflog for that ref is also deleted and to find the last commit a branch or tag pointed to you have to use Git's special `HEAD` reflog to find the commit, which can sometimes be challenging. Dolt makes this much easier by allowing you to see the history for a deleted ref so you can easily see the last commit a branch or tag pointed to before it was deleted.
819+
820+
### Privileges
821+
822+
There are no special privileges required to use the `dolt_reflog()` table function.
823+
824+
### Options
825+
826+
```sql
827+
DOLT_REFLOG()
828+
DOLT_REFLOG(<ref_name>)
829+
```
830+
831+
The `dolt_reflog()` table function can be called with no arguments or with one argument. If called without any arguments, it will return the full reference log, which lists changes from newest to oldest for all tracked references. If called with one argument, that argument is the name of a ref to query. This can be the name of a branch (e.g. "myBranch") or the name of a tag (e.g. "v1.1.4") or it can be the fully qualified ref path (e.g. "refs/heads/myBranch"). The `ref_name` parameter is case-insensitive.
832+
833+
### Schema
834+
835+
```text
836+
+-----------------------+-----------+
837+
| field | type |
838+
+-----------------------+-----------+
839+
| ref | TEXT |
840+
| ref_timestamp | TIMESTAMP |
841+
| commit_hash | TEXT |
842+
| commit_message | TEXT |
843+
+-----------------------+-----------+
844+
```
845+
846+
### Example
847+
848+
The example below shows how to recreate a branch that was deleted by finding the last commit it referenced in Dolt's reflog.
849+
850+
```sql
851+
-- Someone accidentally deletes the wrong branch!
852+
call dolt_branch('-D', 'prodBranch');
853+
854+
-- After we realize the wrong branch has been deleted, we query the Dolt reflog on the same Dolt database instance
855+
-- where the branch was deleted to see what commits the prodBranch branch has referenced. Using the same Dolt
856+
-- instance is important, since reflog information is always local and not included when pushing/pulling databases.
857+
select * from dolt_reflog('prodBranch');
858+
+-----------------------+---------------------+----------------------------------+-------------------------------+
859+
| ref | ref_timestamp | commit_hash | commit_message |
860+
+-----------------------+---------------------+----------------------------------+-------------------------------+
861+
| refs/heads/prodBranch | 2023-10-25 20:54:37 | v531ptpmv2tquig8v591tsjghtj84ksg | inserting row 42 |
862+
| refs/heads/prodBranch | 2023-10-25 20:53:12 | rvt34lqrbtdr3dhnjchruu73lik4e398 | inserting row 100000 |
863+
| refs/heads/prodBranch | 2023-10-25 20:53:06 | v531ptpmv2tquig8v591tsjghtj84ksg | inserting row 42 |
864+
| refs/heads/prodBranch | 2023-10-25 20:52:43 | ihuj1l7fmqq37sjhtlrgpup5n76gfhju | inserting row 1 into table xy |
865+
+-----------------------+---------------------+----------------------------------+-------------------------------+
866+
867+
-- The last commit prodBranch pointed to was v531ptpmv2tquig8v591tsjghtj84ksg, so to restore our branch, we
868+
-- just need to create a branch with the same name, pointing to that last commit.
869+
call dolt_branch('prodBranch', 'v531ptpmv2tquig8v591tsjghtj84ksg');
870+
```
871+
872+
809873
## `DOLT_SCHEMA_DIFF()`
810874

811875
The `DOLT_SCHEMA_DIFF()` table function calculates the schema difference between any two commits in the database.

0 commit comments

Comments
 (0)