Skip to content

Commit 0603492

Browse files
authored
Merge pull request #2630 from dolthub/taylor/dg-conflicts
Add conflict table function docs to doltgres
2 parents 3066238 + f60b5fb commit 0603492

File tree

1 file changed

+192
-1
lines changed

1 file changed

+192
-1
lines changed

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

Lines changed: 192 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ title: Dolt SQL Functions
55
# Table of Contents
66

77
- [Version Control Functions](#version-control-functions)
8+
89
- [dolt_add()](#dolt_add)
910
- [dolt_backup()](#dolt_backup)
1011
- [dolt_branch()](#dolt_branch)
@@ -45,11 +46,12 @@ title: Dolt SQL Functions
4546
- [dolt_diff_summary()](#dolt_diff_summary)
4647
- [dolt_log()](#dolt_log)
4748
- [dolt_patch()](#dolt_patch)
49+
- [dolt_preview_merge_conflicts_summary()](#dolt_preview_merge_conflicts_summary)
50+
- [dolt_preview_merge_conflicts()](#dolt_preview_merge_conflicts)
4851
- [dolt_reflog()](#dolt_reflog)
4952
- [dolt_schema_diff()](#dolt_schema_diff)
5053
- [dolt_query_diff()](#dolt_query_diff)
5154

52-
5355
# Version Control Functions
5456

5557
Doltgres provides functions for version control features that update the state of the database, such
@@ -2379,6 +2381,195 @@ With result of single row:
23792381
+-----------------+------------------+----------------------------------+---------------+-----------+---------------------+
23802382
```
23812383

2384+
## `DOLT_PREVIEW_MERGE_CONFLICTS_SUMMARY()`
2385+
2386+
The `DOLT_PREVIEW_MERGE_CONFLICTS_SUMMARY()` table function provides a summary of merge conflicts that would occur when merging a branch. This function is useful for understanding potential conflicts before performing an actual merge operation, allowing you to identify which tables would have conflicts and how many data and schema conflicts would occur.
2387+
2388+
This function performs a "dry run" merge operation and returns information about conflicts without actually modifying the database or creating a merge commit.
2389+
2390+
### Privileges
2391+
2392+
`DOLT_PREVIEW_MERGE_CONFLICTS_SUMMARY()` table function requires `SELECT` privilege for all tables.
2393+
2394+
### Options
2395+
2396+
```sql
2397+
DOLT_PREVIEW_MERGE_CONFLICTS_SUMMARY(<base_branch>, <merge_branch>)
2398+
```
2399+
2400+
The `DOLT_PREVIEW_MERGE_CONFLICTS_SUMMARY()` table function takes two required arguments:
2401+
2402+
- `base_branch` — the base branch to merge into (e.g. "main").
2403+
- `merge_branch` — the branch to merge into the base branch (e.g. "feature_branch").
2404+
2405+
### Schema
2406+
2407+
```text
2408+
+---------------------+--------+
2409+
| field | type |
2410+
+---------------------+--------+
2411+
| table | TEXT |
2412+
| num_data_conflicts | BIGINT |
2413+
| num_schema_conflicts| BIGINT |
2414+
+---------------------+--------+
2415+
```
2416+
2417+
### Example
2418+
2419+
Consider a scenario where you have a `main` branch and a `feature_branch` that have diverged and made conflicting changes to the same data. You can preview the conflicts that would occur when merging `feature_branch` into `main`:
2420+
2421+
```sql
2422+
SELECT * FROM DOLT_PREVIEW_MERGE_CONFLICTS_SUMMARY('main', 'feature_branch');
2423+
```
2424+
2425+
This might return results like:
2426+
2427+
```text
2428+
+----------+--------------------+---------------------+
2429+
| table | num_data_conflicts | num_schema_conflicts|
2430+
+----------+--------------------+---------------------+
2431+
| users | 3 | 0 |
2432+
| orders | 1 | 0 |
2433+
| products | NULL | 2 |
2434+
+----------+--------------------+---------------------+
2435+
```
2436+
2437+
Note that if there are schema conflicts the data conflicts are not able to be calculated and that column will be null.
2438+
2439+
This output indicates that merging `feature_branch` into `main` would create conflicts in three tables:
2440+
2441+
- The `users` table would have 3 data conflicts and no schema conflicts
2442+
- The `orders` table would have 1 data conflict and no schema conflicts
2443+
- The `products` table would have 2 schema conflicts
2444+
2445+
If there would be no conflicts, the function returns an empty result set.
2446+
2447+
This information helps you understand the scope of conflicts before attempting a merge, allowing you to plan conflict resolution strategies or coordinate with other developers who may have made conflicting changes.
2448+
2449+
## `DOLT_PREVIEW_MERGE_CONFLICTS()`
2450+
2451+
The `DOLT_PREVIEW_MERGE_CONFLICTS()` table function provides detailed information about merge conflicts that would occur when merging a branch. Unlike `DOLT_PREVIEW_MERGE_CONFLICTS_SUMMARY()` which only provides a count of conflicts per table, this function returns the actual conflicting rows with their base, ours, and theirs values.
2452+
2453+
This function performs a "dry run" merge operation and returns detailed conflict information without actually modifying the database or creating a merge commit. The results are similar to what you would see in the `dolt_conflicts_$TABLENAME` system tables after performing an actual merge, but without making any changes to the database.
2454+
2455+
### Privileges
2456+
2457+
`DOLT_PREVIEW_MERGE_CONFLICTS()` table function requires `SELECT` privilege for all tables.
2458+
2459+
### Options
2460+
2461+
```sql
2462+
DOLT_PREVIEW_MERGE_CONFLICTS(<base_branch>, <merge_branch>, <table_name>)
2463+
```
2464+
2465+
The `DOLT_PREVIEW_MERGE_CONFLICTS()` table function takes three required arguments:
2466+
2467+
- `base_branch` — the base branch to merge into (e.g. "main").
2468+
- `merge_branch` — the branch to merge into the base branch (e.g. "feature_branch").
2469+
- `table_name` — the name of the table to preview conflicts for.
2470+
2471+
### Schema
2472+
2473+
The schema of the `DOLT_PREVIEW_MERGE_CONFLICTS()` function depends on the schema of the specified table. For each column `X` in the table, the result set contains three columns:
2474+
2475+
- `base_X` — the value of column X at the common ancestor commit
2476+
- `our_X` — the value of column X in the base branch
2477+
- `their_X` — the value of column X in the merge branch
2478+
2479+
Additionally, the result set includes these metadata columns:
2480+
2481+
```text
2482+
+------------------+--------+
2483+
| field | type |
2484+
+------------------+--------+
2485+
| from_root_ish | TEXT |
2486+
| our_diff_type | TEXT |
2487+
| their_diff_type | TEXT |
2488+
| dolt_conflict_id | TEXT |
2489+
+------------------+--------+
2490+
```
2491+
2492+
Where:
2493+
2494+
- `from_root_ish` — the commit hash of the merge branch (the "from" branch of the merge). This hash can be used to identify which merge produced a conflict, since conflicts can accumulate across merges. User code generally ignores this column.
2495+
- `our_diff_type` and `their_diff_type` indicate whether the row was "added", "modified", or "removed" in the corresponding branch
2496+
- `dolt_conflict_id` is a unique identifier for each conflict
2497+
2498+
### Example
2499+
2500+
Consider a table `users` with columns `id`, `name`, and `email` that has conflicts between `main` and `feature_branch`. You can preview the specific conflicts:
2501+
2502+
```sql
2503+
SELECT * FROM DOLT_PREVIEW_MERGE_CONFLICTS('main', 'feature_branch', 'users');
2504+
```
2505+
2506+
This might return results like:
2507+
2508+
```text
2509+
+----------------------------------+---------+-----------+----------------+---------+-----------+------------------+---------------+-----------+-----------+-------------------+-----------------+------------------------+
2510+
| from_root_ish | base_id | base_name | base_email | our_id | our_name | our_email | our_diff_type | their_id | their_name| their_email | their_diff_type | dolt_conflict_id |
2511+
+----------------------------------+---------+-----------+----------------+---------+-----------+------------------+---------------+-----------+-----------+-------------------+-----------------+------------------------+
2512+
| abc123def456789012345678901234567 | 1 | John | [email protected] | 1 | John Doe | [email protected] | modified | 1 | John | [email protected] | modified | abc123def456 |
2513+
| abc123def456789012345678901234567 | NULL | NULL | NULL | 2 | Jane | [email protected] | added | 2 | Jane Doe | [email protected] | added | def789ghi012 |
2514+
+----------------------------------+---------+-----------+----------------+---------+-----------+------------------+---------------+-----------+-----------+-------------------+-----------------+------------------------+
2515+
```
2516+
2517+
This output shows:
2518+
2519+
- Row 1: Both branches modified the same user but with different changes (name vs email)
2520+
- Row 2: Both branches added a new user with the same ID but different data
2521+
2522+
To view only specific columns for easier reading:
2523+
2524+
```sql
2525+
SELECT dolt_conflict_id, base_name, our_name, our_diff_type, their_name, their_diff_type
2526+
FROM DOLT_PREVIEW_MERGE_CONFLICTS('main', 'feature_branch', 'users');
2527+
```
2528+
2529+
### Keyless Tables
2530+
2531+
For keyless tables (tables without primary keys), the behavior is slightly different. Dolt uses content-based addressing to identify rows, so conflicts in keyless tables are detected when the same content would be added or modified differently on each branch.
2532+
2533+
Keyless tables include additional columns not present in tables with primary keys:
2534+
2535+
```text
2536+
+-------------------+--------+
2537+
| field | type |
2538+
+-------------------+--------+
2539+
| base_cardinality | BIGINT |
2540+
| our_cardinality | BIGINT |
2541+
| their_cardinality | BIGINT |
2542+
+-------------------+--------+
2543+
```
2544+
2545+
- `base_cardinality` — the number of occurrences of the conflicting row in the merge ancestor commit
2546+
- `our_cardinality` — the number of occurrences of the conflicting row in the base branch
2547+
- `their_cardinality` — the number of occurrences of the conflicting row in the merge branch
2548+
2549+
Consider a keyless table `logs` with columns `timestamp`, `level`, and `message`:
2550+
2551+
```sql
2552+
SELECT * FROM DOLT_PREVIEW_MERGE_CONFLICTS('main', 'feature_branch', 'logs');
2553+
```
2554+
2555+
This might return results like:
2556+
2557+
```text
2558+
+----------------------------------+---------------------+-------------+------------------+---------------------+-------------+------------------+---------------+---------------------+-------------+------------------+-----------------+------------------------+------------------+-------------------+---------------------+
2559+
| from_root_ish | base_timestamp | base_level | base_message | our_timestamp | our_level | our_message | our_diff_type | their_timestamp | their_level | their_message | their_diff_type | dolt_conflict_id | base_cardinality | our_cardinality | their_cardinality |
2560+
+----------------------------------+---------------------+-------------+------------------+---------------------+-------------+------------------+---------------+---------------------+-------------+------------------+-----------------+------------------------+------------------+-------------------+---------------------+
2561+
| abc123def456789012345678901234567 | 2023-01-01 10:00:00 | ERROR | Database timeout | 2023-01-01 10:00:00 | ERROR | Database timeout | modified | 2023-01-01 10:00:00 | ERROR | Database timeout | modified | xyz789abc123 | 1 | 3 | 2 |
2562+
+----------------------------------+---------------------+-------------+------------------+---------------------+-------------+------------------+---------------+---------------------+-------------+------------------+-----------------+------------------------+------------------+-------------------+---------------------+
2563+
```
2564+
2565+
In this example, the same log entry exists once in the base branch, but appears 3 times in our branch and 2 times in their branch, creating a conflict about cardinality (how many times the row should appear).
2566+
2567+
### Notes
2568+
2569+
If there are no conflicts in the specified table, the function returns an empty result set.
2570+
2571+
This detailed view allows you to examine the exact differences that would cause conflicts and plan appropriate resolution strategies before performing the actual merge. The results are similar to what you would see in the `dolt_conflicts_$TABLENAME` system tables after an actual merge, but without making any changes to your database.
2572+
23822573
## `DOLT_REFLOG()`
23832574

23842575
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.

0 commit comments

Comments
 (0)