Skip to content

Commit

Permalink
Merge pull request #8910 from dolthub/nicktobey/autoincrement
Browse files Browse the repository at this point in the history
Report auto increment changes in dolt diff and `dolt_diff_summary()`
  • Loading branch information
nicktobey authored Mar 1, 2025
2 parents 9ef74e7 + 9a3082c commit 78215f2
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 10 deletions.
2 changes: 1 addition & 1 deletion go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ require (
github.com/cespare/xxhash/v2 v2.2.0
github.com/creasty/defaults v1.6.0
github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2
github.com/dolthub/go-mysql-server v0.19.1-0.20250228013728-e1e27339cbd6
github.com/dolthub/go-mysql-server v0.19.1-0.20250301010531-8637b99d8f90
github.com/dolthub/gozstd v0.0.0-20240423170813-23a2903bca63
github.com/dolthub/swiss v0.1.0
github.com/esote/minmaxheap v1.0.0
Expand Down
6 changes: 4 additions & 2 deletions go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,10 @@ github.com/dolthub/fslock v0.0.3 h1:iLMpUIvJKMKm92+N1fmHVdxJP5NdyDK5bK7z7Ba2s2U=
github.com/dolthub/fslock v0.0.3/go.mod h1:QWql+P17oAAMLnL4HGB5tiovtDuAjdDTPbuqx7bYfa0=
github.com/dolthub/go-icu-regex v0.0.0-20241215010122-db690dd53c90 h1:Sni8jrP0sy/w9ZYXoff4g/ixe+7bFCZlfCqXKJSU+zM=
github.com/dolthub/go-icu-regex v0.0.0-20241215010122-db690dd53c90/go.mod h1:ylU4XjUpsMcvl/BKeRRMXSH7e7WBrPXdSLvnRJYrxEA=
github.com/dolthub/go-mysql-server v0.19.1-0.20250228013728-e1e27339cbd6 h1:ro8L5w2iioL+IvFNB6Kj3BSf1yi6UVUYmn5zMPOiFkA=
github.com/dolthub/go-mysql-server v0.19.1-0.20250228013728-e1e27339cbd6/go.mod h1:8lcOOj+5kMrp6G+O/owfzTQOAivninxfdehL7XGhJvk=
github.com/dolthub/go-mysql-server v0.19.1-0.20250228215144-f8da474ecd9f h1:lwQH9jVmSiPg1DFMYB9rWyyJTMPMoBpGrYRsOGOD/hA=
github.com/dolthub/go-mysql-server v0.19.1-0.20250228215144-f8da474ecd9f/go.mod h1:JTlrabhq5TJqvlL+J3NKlm0EzTHQQugUAH6yAxWi4Ww=
github.com/dolthub/go-mysql-server v0.19.1-0.20250301010531-8637b99d8f90 h1:4OxKWWixh5Zhc6RqBMAAj4AHEvEAquPjbidqnc5fptg=
github.com/dolthub/go-mysql-server v0.19.1-0.20250301010531-8637b99d8f90/go.mod h1:8lcOOj+5kMrp6G+O/owfzTQOAivninxfdehL7XGhJvk=
github.com/dolthub/gozstd v0.0.0-20240423170813-23a2903bca63 h1:OAsXLAPL4du6tfbBgK0xXHZkOlos63RdKYS3Sgw/dfI=
github.com/dolthub/gozstd v0.0.0-20240423170813-23a2903bca63/go.mod h1:lV7lUeuDhH5thVGDCKXbatwKy2KW80L4rMT46n+Y2/Q=
github.com/dolthub/ishell v0.0.0-20240701202509-2b217167d718 h1:lT7hE5k+0nkBdj/1UOSFwjWpNxf+LCApbRHgnCA17XE=
Expand Down
6 changes: 2 additions & 4 deletions go/go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,8 @@ github.com/creack/pty v1.1.9 h1:uDmaGzcdjhF4i/plgjmEsriH11Y0o7RKapEf/LDaM3w=
github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dimchansky/utfbom v1.1.1/go.mod h1:SxdoEBH5qIqFocHMyGOXVAybYJdr71b1Q/j0mACtrfE=
github.com/dolthub/go-mysql-server v0.18.2-0.20240918194055-f75a63f64679 h1:O6eSicGwVPYnEzE0jgLakX2Ud54p6OGdcaqwBgNB+vU=
github.com/dolthub/go-mysql-server v0.18.2-0.20240918194055-f75a63f64679/go.mod h1:m88EMm9OthVVa6qIhbpnRDpj/eYUXuNpvY/+0YWKVwc=
github.com/dolthub/go-mysql-server v0.19.1-0.20250228215144-f8da474ecd9f h1:lwQH9jVmSiPg1DFMYB9rWyyJTMPMoBpGrYRsOGOD/hA=
github.com/dolthub/go-mysql-server v0.19.1-0.20250228215144-f8da474ecd9f/go.mod h1:JTlrabhq5TJqvlL+J3NKlm0EzTHQQugUAH6yAxWi4Ww=
github.com/dolthub/vitess v0.0.0-20241104125316-860772ba6683 h1:2/RJeUfNAXS7mbBnEr9C36htiCJHk5XldDPzhxtEsME=
github.com/dolthub/vitess v0.0.0-20241104125316-860772ba6683/go.mod h1:uBvlRluuL+SbEWTCZ68o0xvsdYZER3CEG/35INdzfJM=
github.com/dolthub/vitess v0.0.0-20241231200706-18992bb25fdc/go.mod h1:1gQZs/byeHLMSul3Lvl3MzioMtOW1je79QYGyi2fd70=
Expand All @@ -404,8 +404,6 @@ github.com/envoyproxy/protoc-gen-validate v0.10.1 h1:c0g45+xCJhdgFGw7a5QAfdS4byA
github.com/envoyproxy/protoc-gen-validate v0.10.1/go.mod h1:DRjgyB0I43LtJapqN6NiRwroiAU2PaFuvk/vjgh61ss=
github.com/envoyproxy/protoc-gen-validate v1.0.2 h1:QkIBuU5k+x7/QXPvPPnWXWlCdaBFApVqftFV6k087DA=
github.com/envoyproxy/protoc-gen-validate v1.0.2/go.mod h1:GpiZQP3dDbg4JouG/NNS7QWXpgx6x8QiMKdmN72jogE=
github.com/esote/minmaxheap v1.0.0 h1:rgA7StnXXpZG6qlM0S7pUmEv1KpWe32rYT4x8J8ntaA=
github.com/esote/minmaxheap v1.0.0/go.mod h1:Ln8+i7fS1k3PLgZI2JAo0iA1as95QnIYiGCrqSJ5FZk=
github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
github.com/form3tech-oss/jwt-go v3.2.2+incompatible h1:TcekIExNqud5crz4xD2pavyTgWiPvpYe4Xau31I0PRk=
github.com/franela/goblin v0.0.0-20200105215937-c9ffbefa60db h1:gb2Z18BhTPJPpLQWj4T+rfKHYCHxRHCtRxhKKjRidVw=
Expand Down
14 changes: 14 additions & 0 deletions go/libraries/doltcore/diff/table_deltas.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,20 @@ func (td TableDelta) HasSchemaChanged(ctx context.Context) (bool, error) {
return true, nil
}

fromAutoInc, err := td.FromTable.GetAutoIncrementValue(ctx)
if err != nil {
return false, err
}

toAutoInc, err := td.ToTable.GetAutoIncrementValue(ctx)
if err != nil {
return false, err
}

if fromAutoInc != toAutoInc {
return true, nil
}

fromSchemaHash, err := td.FromTable.GetSchemaHash(ctx)
if err != nil {
return false, err
Expand Down
19 changes: 19 additions & 0 deletions go/libraries/doltcore/sqle/enginetest/dolt_queries_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -3042,6 +3042,25 @@ var DiffSummaryTableFunctionScriptTests = []queries.ScriptTest{
},
},
},
{
Name: "raising the autoincrement value should be seen as a schema change",
SetUpScript: []string{
"CREATE table t (pk int primary key auto_increment);",
"INSERT INTO t values (1);",
"CALL DOLT_COMMIT('-Am', 'first commit');",
"INSERT INTO t values (2);",
"DELETE FROM t where pk = 2",
"CALL DOLT_COMMIT('-am', 'second commit');",
},
Assertions: []queries.ScriptTestAssertion{
{
Query: "SELECT * from dolt_diff_summary('HEAD~', 'HEAD')",
Expected: []sql.Row{
{"t", "t", "modified", false, true},
},
},
},
},
}

var PatchTableFunctionScriptTests = []queries.ScriptTest{
Expand Down
6 changes: 3 additions & 3 deletions go/libraries/doltcore/sqle/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,8 @@ func (t *DoltTable) HasIndex(ctx *sql.Context, idx sql.Index) (bool, error) {
return index.TableHasIndex(ctx, t.db.Name(), t.tableName, tbl, idx)
}

// GetAutoIncrementValue gets the last AUTO_INCREMENT value
func (t *DoltTable) GetAutoIncrementValue(ctx *sql.Context) (uint64, error) {
// PeekNextAutoIncrementValue gets the next AUTO_INCREMENT value
func (t *DoltTable) PeekNextAutoIncrementValue(ctx *sql.Context) (uint64, error) {
table, err := t.DoltTable(ctx)
if err != nil {
return 0, err
Expand Down Expand Up @@ -1154,7 +1154,7 @@ func (t *WritableDoltTable) PeekNextAutoIncrementValue(ctx *sql.Context) (uint64
return 0, sql.ErrNoAutoIncrementCol
}

return t.DoltTable.GetAutoIncrementValue(ctx)
return t.DoltTable.PeekNextAutoIncrementValue(ctx)
}

// GetNextAutoIncrementValue implements sql.AutoIncrementTable
Expand Down
28 changes: 28 additions & 0 deletions integration-tests/bats/auto_increment.bats
Original file line number Diff line number Diff line change
Expand Up @@ -850,3 +850,31 @@ SQL
[ $status -eq 0 ]
[[ "$output" =~ "4" ]] || false
}


@test "auto_increment: auto inc shows up in SHOW CREATE (AS OF)" {
dolt checkout -b other
dolt sql -q "create table t(pk int primary key auto_increment);"
dolt sql -q "insert into t values (1);"
dolt commit -Am "create table"

dolt sql -q "insert into t values (2);"
dolt commit -Am "insert into table"

run dolt sql -q 'show create table t'
[ "$status" -eq 0 ]
[[ "$output" =~ AUTO_INCREMENT ]] || false

run dolt sql -q 'show create table t as of other'
[ "$status" -eq 0 ]
[[ "$output" =~ AUTO_INCREMENT ]] || false

run dolt sql -q 'show create table t as of HEAD'
[ "$status" -eq 0 ]
[[ "$output" =~ AUTO_INCREMENT ]] || false

dolt sql -q 'show create table t as of `HEAD^`'
run dolt sql -q 'show create table t as of `HEAD^`'
[ "$status" -eq 0 ]
[[ "$output" =~ AUTO_INCREMENT ]] || false
}
58 changes: 58 additions & 0 deletions integration-tests/bats/diff.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2124,3 +2124,61 @@ EOF
# Count the line numbers to make sure there are no schema changes output
[ "${#lines[@]}" -eq 11 ]
}

@test "diff: autoincrement registers as schema diff" {
dolt sql <<SQL
drop table test;
create table test (pk int primary key auto_increment);
insert into test values (1);
SQL
dolt add .
dolt commit -am "First commit"

dolt sql <<SQL
insert into test values (2);
delete from test where pk = 2;
SQL

run dolt diff

# Somehow, bats rejects this Heredoc if it contains unmatched parentheses.
# So we add a parenthesis to the first line and remove it with tail.
EXPECTED=$(tail -n 8 <<'EOF'
(
diff --dolt a/test b/test
--- a/test
+++ b/test
CREATE TABLE `test` (
`pk` int NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`pk`)
-) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
+) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
EOF
)

[ "$status" -eq 0 ]
[[ "$output" =~ "$EXPECTED" ]] || false

run dolt diff --data --schema
[ "$status" -eq 0 ]
[[ "$output" =~ "$EXPECTED" ]] || false

dolt diff --schema
run dolt diff --schema

[[ "$output" =~ "$EXPECTED" ]] || false
# Count the line numbers to make sure there are no data changes output
[ "${#lines[@]}" -eq 8 ]

run dolt diff --data
EXPECTED=$(cat <<'EOF'
diff --dolt a/test b/test
--- a/test
+++ b/test
EOF
)

[[ "$output" =~ "$EXPECTED" ]] || false
# Count the line numbers to make sure there are no schema changes output
[ "${#lines[@]}" -eq 3 ]
}

0 comments on commit 78215f2

Please sign in to comment.