Merge pull request #5733 from dolthub/zachmu/autoinc-bug

Fixed a bug in auto increment tracking
This commit is contained in:
AndyA
2023-04-13 15:35:47 -07:00
committed by GitHub
5 changed files with 92 additions and 22 deletions

View File

@@ -31,16 +31,23 @@ type AutoIncrementTracker struct {
mu *sync.Mutex
}
// NewAutoIncrementTracker returns a new autoincrement tracker for the working sets given. All working sets must be
// NewAutoIncrementTracker returns a new autoincrement tracker for the roots given. All roots sets must be
// considered because the auto increment value for a table is tracked globally, across all branches.
func NewAutoIncrementTracker(ctx context.Context, wses ...*doltdb.WorkingSet) (AutoIncrementTracker, error) {
// Roots provided should be the working sets when available, or the branches when they are not (e.g. for remote
// branches that don't have a local working set)
func NewAutoIncrementTracker(ctx context.Context, roots ...doltdb.Rootish) (AutoIncrementTracker, error) {
ait := AutoIncrementTracker{
sequences: make(map[string]uint64),
mu: &sync.Mutex{},
}
for _, ws := range wses {
err := ws.WorkingRoot().IterTables(ctx, func(tableName string, table *doltdb.Table, sch schema.Schema) (bool, error) {
for _, ws := range roots {
root, err := ws.ResolveRootValue(ctx)
if err != nil {
return AutoIncrementTracker{}, err
}
err = root.IterTables(ctx, func(tableName string, table *doltdb.Table, sch schema.Schema) (bool, error) {
ok := schema.HasAutoIncrement(sch)
if !ok {
return false, nil

View File

@@ -34,25 +34,47 @@ func NewGlobalStateStoreForDb(ctx context.Context, db *doltdb.DoltDB) (GlobalSta
return GlobalState{}, err
}
var wses []*doltdb.WorkingSet
for _, b := range branches {
wsRef, err := ref.WorkingSetRefForHead(b)
if err != nil {
return GlobalState{}, err
}
ws, err := db.ResolveWorkingSet(ctx, wsRef)
if err == doltdb.ErrWorkingSetNotFound {
// skip, continue working on other branches
continue
} else if err != nil {
return GlobalState{}, err
}
wses = append(wses, ws)
remotes, err := db.GetRemoteRefs(ctx)
if err != nil {
return GlobalState{}, err
}
tracker, err := NewAutoIncrementTracker(ctx, wses...)
rootRefs := make([]ref.DoltRef, 0, len(branches)+len(remotes))
rootRefs = append(rootRefs, branches...)
rootRefs = append(rootRefs, remotes...)
var roots []doltdb.Rootish
for _, b := range rootRefs {
switch b.GetType() {
case ref.BranchRefType:
wsRef, err := ref.WorkingSetRefForHead(b)
if err != nil {
return GlobalState{}, err
}
ws, err := db.ResolveWorkingSet(ctx, wsRef)
if err == doltdb.ErrWorkingSetNotFound {
// use the branch head if there isn't a working set for it
cm, err := db.ResolveCommitRef(ctx, b)
if err != nil {
return GlobalState{}, err
}
roots = append(roots, cm)
} else if err != nil {
return GlobalState{}, err
} else {
roots = append(roots, ws)
}
case ref.RemoteRefType:
cm, err := db.ResolveCommitRef(ctx, b)
if err != nil {
return GlobalState{}, err
}
roots = append(roots, cm)
}
}
tracker, err := NewAutoIncrementTracker(ctx, roots...)
if err != nil {
return GlobalState{}, err
}

View File

@@ -776,3 +776,43 @@ SQL
[[ "$output" =~ "5,5" ]] || false
[[ "$output" =~ "6,6" ]] || false
}
@test "auto_increment: newly cloned database" {
dolt sql <<SQL
call dolt_add('.');
call dolt_commit('-am', 'empty table');
call dolt_branch('branch1');
call dolt_branch('branch2');
insert into test (c0) values (1), (2);
call dolt_commit('-am', 'main values');
call dolt_checkout('branch1');
insert into test (c0) values (3), (4);
call dolt_commit('-am', 'branch1 values');
call dolt_checkout('branch2');
insert into test (c0) values (5), (6);
call dolt_commit('-am', 'branch2 values');
SQL
dolt remote add remote1 file://./remote1
dolt push remote1 main
dolt push remote1 branch1
dolt push remote1 branch2
dolt clone file://./remote1 clone
cd clone
# The clone should find the values on the remote branches that haven't been
# checked out locally
dolt sql <<SQL
insert into test (c0) values (7), (8);
SQL
run dolt sql -q 'select * from test' -r csv
[ $status -eq 0 ]
[[ "$output" =~ "7,7" ]] || false
[[ "$output" =~ "8,8" ]] || false
}

View File

@@ -125,7 +125,7 @@ SQL
run dolt --feature-version $OLD pull
[ "$status" -ne 0 ]
[[ "$output" =~ "visit https://github.com/dolthub/dolt/releases/latest/" ]] || false
dolt --feature-version $OLD sql -q "SELECT * FROM test"
dolt --feature-version $OLD ls -v
dolt --feature-version $OLD checkout other
popd
}

View File

@@ -33,6 +33,7 @@ teardown() {
dolt sql-server --remotesapi-port 50051 &
srv_pid=$!
sleep 2
cd ../
dolt clone http://localhost:50051/remote repo1