Files
dolt/integration-tests/bats/sql-pull.bats
Maximilian Hoffman 3ec0e48b0d dolt_checkout and read replication working set fixes (#3808)
* dolt_checkout and read replication working set fixes

Sql path 'dolt_checkout' used to create a new branch from HEAD
if the checkout name did not have a remote tracking ref. The CLI,
on the other hand, will initialize a local branch if a remote tracking
ref with the same name in the default remote exists. The SQl path
now mirrors the CLI checkout: if we checkout a new branch, like
'new_feature', and we have previously fetched that branch from our
default remote, `dolt_checkout('feature')` will initialize a branch
using the head commit from the remote `feature`.

Second, read replication was creating a local tracking branch for
remote branches (sourced from the master remote), but not creating
corresponding working sets. This let users attach to new branches with
`use db/feature` syntax, but prevented users from
`dolt_checkout('feature')` both for the bug in dolt checkout, but also
because we created an intermediary state with a local branch, but
without a corresponding working set. SQL replication will now fetch,
create the new branch, and create the appropriate working set.

* jennifer's feedback

* unskip
2022-07-12 14:17:28 -07:00

501 lines
13 KiB
Bash

#!/usr/bin/env bats
load $BATS_TEST_DIRNAME/helper/common.bash
setup() {
setup_common
TMPDIRS=$(pwd)/tmpdirs
mkdir -p $TMPDIRS/{rem1,repo1}
# repo1 -> rem1 -> repo2
cd $TMPDIRS/repo1
dolt init
dolt branch feature
dolt remote add origin file://../rem1
dolt remote add test-remote file://../rem1
dolt push origin main
cd $TMPDIRS
dolt clone file://rem1 repo2
cd $TMPDIRS/repo2
dolt log
dolt branch feature
dolt remote add test-remote file://../rem1
# table and commits only present on repo1, rem1 at start
cd $TMPDIRS/repo1
dolt sql -q "create table t1 (a int primary key, b int)"
dolt commit -am "First commit"
dolt sql -q "insert into t1 values (0,0)"
dolt commit -am "Second commit"
dolt push origin main
cd $TMPDIRS
}
teardown() {
teardown_common
rm -rf $TMPDIRS
cd $BATS_TMPDIR
}
@test "sql-pull: dolt_pull main" {
cd repo2
dolt sql -q "select dolt_pull('origin')"
run dolt sql -q "show tables" -r csv
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 2 ]
[[ "$output" =~ "Table" ]] || false
[[ "$output" =~ "t1" ]] || false
}
@test "sql-pull: CALL dolt_pull main" {
cd repo2
dolt sql -q "CALL dolt_pull('origin')"
run dolt sql -q "show tables" -r csv
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 2 ]
[[ "$output" =~ "Table" ]] || false
[[ "$output" =~ "t1" ]] || false
}
@test "sql-pull: CALL dpull main" {
cd repo2
dolt sql -q "CALL dpull('origin')"
run dolt sql -q "show tables" -r csv
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 2 ]
[[ "$output" =~ "Table" ]] || false
[[ "$output" =~ "t1" ]] || false
}
@test "sql-pull: dolt_pull custom remote" {
cd repo2
dolt sql -q "select dolt_pull('test-remote')"
run dolt sql -q "show tables" -r csv
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 2 ]
[[ "$output" =~ "Table" ]] || false
[[ "$output" =~ "t1" ]] || false
}
@test "sql-pull: CALL dolt_pull custom remote" {
cd repo2
dolt sql -q "CALL dolt_pull('test-remote')"
run dolt sql -q "show tables" -r csv
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 2 ]
[[ "$output" =~ "Table" ]] || false
[[ "$output" =~ "t1" ]] || false
}
@test "sql-pull: dolt_pull default origin" {
cd repo2
dolt remote remove test-remote
dolt sql -q "select dolt_pull()"
run dolt sql -q "show tables" -r csv
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 2 ]
[[ "$output" =~ "Table" ]] || false
[[ "$output" =~ "t1" ]] || false
}
@test "sql-pull: CALL dolt_pull default origin" {
cd repo2
dolt remote remove test-remote
dolt sql -q "CALL dolt_pull()"
run dolt sql -q "show tables" -r csv
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 2 ]
[[ "$output" =~ "Table" ]] || false
[[ "$output" =~ "t1" ]] || false
}
@test "sql-pull: dolt_pull default custom remote" {
cd repo2
dolt remote remove origin
dolt sql -q "select dolt_pull()"
run dolt sql -q "show tables" -r csv
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 2 ]
[[ "$output" =~ "Table" ]] || false
[[ "$output" =~ "t1" ]] || false
}
@test "sql-pull: CALL dolt_pull default custom remote" {
cd repo2
dolt remote remove origin
dolt sql -q "CALL dolt_pull()"
run dolt sql -q "show tables" -r csv
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 2 ]
[[ "$output" =~ "Table" ]] || false
[[ "$output" =~ "t1" ]] || false
}
@test "sql-pull: dolt_pull up to date does not error" {
cd repo2
dolt sql -q "select dolt_pull('origin')"
dolt sql -q "select dolt_pull('origin')"
run dolt sql -q "show tables" -r csv
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 2 ]
[[ "$output" =~ "Table" ]] || false
[[ "$output" =~ "t1" ]] || false
}
@test "sql-pull: CALL dolt_pull up to date does not error" {
cd repo2
dolt sql -q "CALL dolt_pull('origin')"
dolt sql -q "CALL dolt_pull('origin')"
run dolt sql -q "show tables" -r csv
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 2 ]
[[ "$output" =~ "Table" ]] || false
[[ "$output" =~ "t1" ]] || false
}
@test "sql-pull: dolt_pull unknown remote fails" {
cd repo2
run dolt sql -q "select dolt_pull('unknown')"
[ "$status" -eq 1 ]
[[ "$output" =~ "unknown remote" ]] || false
[[ ! "$output" =~ "panic" ]] || false
}
@test "sql-pull: CALL dolt_pull unknown remote fails" {
cd repo2
run dolt sql -q "CALL dolt_pull('unknown')"
[ "$status" -eq 1 ]
[[ "$output" =~ "unknown remote" ]] || false
[[ ! "$output" =~ "panic" ]] || false
}
@test "sql-pull: dolt_pull unknown feature branch fails" {
cd repo2
dolt checkout feature
run dolt sql -q "select dolt_pull('origin')"
[ "$status" -eq 1 ]
[[ "$output" =~ "You asked to pull from the remote 'origin', but did not specify a branch" ]] || false
[[ ! "$output" =~ "panic" ]] || false
}
@test "sql-pull: CALL dolt_pull unknown feature branch fails" {
cd repo2
dolt checkout feature
run dolt sql -q "CALL dolt_pull('origin')"
[ "$status" -eq 1 ]
[[ "$output" =~ "You asked to pull from the remote 'origin', but did not specify a branch" ]] || false
[[ ! "$output" =~ "panic" ]] || false
}
@test "sql-pull: dolt_pull feature branch" {
cd repo1
dolt checkout feature
dolt push --set-upstream origin feature
cd ../repo2
dolt checkout feature
dolt push --set-upstream origin feature
cd ../repo1
dolt merge main
dolt push
cd ../repo2
dolt sql -q "select dolt_pull('origin')"
run dolt sql -q "show tables" -r csv
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 2 ]
[[ "$output" =~ "Table" ]] || false
[[ "$output" =~ "t1" ]] || false
}
@test "sql-pull: CALL dolt_pull feature branch" {
cd repo1
dolt checkout feature
dolt push --set-upstream origin feature
cd ../repo2
dolt checkout feature
dolt push --set-upstream origin feature
cd ../repo1
dolt merge main
dolt push
cd ../repo2
dolt sql -q "CALL dolt_pull('origin')"
run dolt sql -q "show tables" -r csv
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 2 ]
[[ "$output" =~ "Table" ]] || false
[[ "$output" =~ "t1" ]] || false
}
@test "sql-pull: CALL dolt_checkout after dolt_fetch a new feature branch" {
cd repo1
dolt checkout -b feature2
dolt sql -q "create table t2 (i int primary key);"
dolt sql -q "call dolt_commit('-am', 'create t2')"
dolt push --set-upstream origin feature2
cd ../repo2
dolt sql -q "CALL dolt_fetch('origin', 'feature2')"
run dolt sql -q "call dolt_checkout('feature2'); show tables" -r csv
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 5 ]
[[ "$output" =~ "Table" ]] || false
[[ "$output" =~ "t1" ]] || false
[[ "$output" =~ "t2" ]] || false
}
@test "sql-pull: dolt_pull force" {
skip "todo: support dolt pull --force (cli too)"
cd repo2
dolt sql -q "create table t2 (a int)"
dolt commit -am "2.0 commit"
dolt push origin main
cd ../repo1
dolt sql -q "create table t2 (a int primary key)"
dolt sql -q "create table t3 (a int primary key)"
dolt commit -am "2.1 commit"
dolt push -f origin main
cd ../repo2
run dolt sql -q "select dolt_pull('origin')"
[ "$status" -eq 1 ]
[[ ! "$output" =~ "panic" ]] || false
[[ "$output" =~ "fetch failed; dataset head is not ancestor of commit" ]] || false
dolt sql -q "select dolt_pull('-f', 'origin')"
run dolt log -n 1
[ "$status" -eq 0 ]
[[ "$output" =~ "2.1 commit" ]] || false
run dolt sql -q "show tables" -r csv
[ "${#lines[@]}" -eq 4 ]
[[ "$output" =~ "t3" ]] || false
}
@test "sql-pull: CALL dolt_pull force" {
skip "todo: support dolt pull --force (cli too)"
cd repo2
dolt sql -q "create table t2 (a int)"
dolt commit -am "2.0 commit"
dolt push origin main
cd ../repo1
dolt sql -q "create table t2 (a int primary key)"
dolt sql -q "create table t3 (a int primary key)"
dolt commit -am "2.1 commit"
dolt push -f origin main
cd ../repo2
run dolt sql -q "CALL dolt_pull('origin')"
[ "$status" -eq 1 ]
[[ ! "$output" =~ "panic" ]] || false
[[ "$output" =~ "fetch failed; dataset head is not ancestor of commit" ]] || false
dolt sql -q "CALL dolt_pull('-f', 'origin')"
run dolt log -n 1
[ "$status" -eq 0 ]
[[ "$output" =~ "2.1 commit" ]] || false
run dolt sql -q "show tables" -r csv
[ "${#lines[@]}" -eq 4 ]
[[ "$output" =~ "t3" ]] || false
}
@test "sql-pull: dolt_pull squash" {
skip "todo: support dolt pull --squash (cli too)"
cd repo2
dolt sql -q "select dolt_pull('--squash', 'origin')"
run dolt sql -q "show tables" -r csv
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 2 ]
[[ "$output" =~ "Table" ]] || false
[[ "$output" =~ "t1" ]] || false
}
@test "sql-pull: CALL dolt_pull squash" {
skip "todo: support dolt pull --squash (cli too)"
cd repo2
dolt sql -q "CALL dolt_pull('--squash', 'origin')"
run dolt sql -q "show tables" -r csv
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 2 ]
[[ "$output" =~ "Table" ]] || false
[[ "$output" =~ "t1" ]] || false
}
@test "sql-pull: dolt_pull --noff flag" {
cd repo2
dolt sql -q "select dolt_pull('--no-ff', 'origin')"
dolt status
run dolt log -n 1
[ "$status" -eq 0 ]
# TODO change the default message name
[[ "$output" =~ "automatic SQL merge" ]] || false
run dolt sql -q "show tables" -r csv
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 2 ]
[[ "$output" =~ "Table" ]] || false
[[ "$output" =~ "t1" ]] || false
}
@test "sql-pull: CALL dolt_pull --noff flag" {
cd repo2
dolt sql -q "CALL dolt_pull('--no-ff', 'origin')"
dolt status
run dolt log -n 1
[ "$status" -eq 0 ]
# TODO change the default message name
[[ "$output" =~ "automatic SQL merge" ]] || false
run dolt sql -q "show tables" -r csv
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 2 ]
[[ "$output" =~ "Table" ]] || false
[[ "$output" =~ "t1" ]] || false
}
@test "sql-pull: empty remote name does not panic" {
cd repo2
dolt sql -q "select dolt_pull('')"
}
@test "sql-pull: empty remote name does not panic on CALL" {
cd repo2
dolt sql -q "CALL dolt_pull('')"
}
@test "sql-pull: dolt_pull dirty working set fails" {
cd repo2
dolt sql -q "create table t2 (a int)"
run dolt sql -q "select dolt_pull('origin')"
[ "$status" -eq 1 ]
[[ "$output" =~ "cannot merge with uncommitted changes" ]] || false
}
@test "sql-pull: CALL dolt_pull dirty working set fails" {
cd repo2
dolt sql -q "create table t2 (a int)"
run dolt sql -q "CALL dolt_pull('origin')"
[ "$status" -eq 1 ]
[[ "$output" =~ "cannot merge with uncommitted changes" ]] || false
}
@test "sql-pull: dolt_pull tag" {
cd repo1
dolt tag v1
dolt push origin v1
dolt tag
cd ../repo2
dolt sql -q "select dolt_pull('origin')"
run dolt tag
[ "$status" -eq 0 ]
[[ "$output" =~ "v1" ]] || false
}
@test "sql-pull: CALL dolt_pull tag" {
cd repo1
dolt tag v1
dolt push origin v1
dolt tag
cd ../repo2
dolt sql -q "CALL dolt_pull('origin')"
run dolt tag
[ "$status" -eq 0 ]
[[ "$output" =~ "v1" ]] || false
}
@test "sql-pull: dolt_pull tags only for resolved commits" {
cd repo1
dolt tag v1 head
dolt tag v2 head^
dolt push origin v1
dolt push origin v2
dolt checkout feature
dolt sql -q "create table t2 (a int)"
dolt commit -am "feature commit"
dolt tag v3
dolt push origin v3
cd ../repo2
dolt sql -q "select dolt_pull('origin')"
run dolt tag
[ "$status" -eq 0 ]
[[ "$output" =~ "v1" ]] || false
[[ "$output" =~ "v2" ]] || false
[[ ! "$output" =~ "v3" ]] || false
}
@test "sql-pull: CALL dolt_pull tags only for resolved commits" {
cd repo1
dolt tag v1 head
dolt tag v2 head^
dolt push origin v1
dolt push origin v2
dolt checkout feature
dolt sql -q "create table t2 (a int)"
dolt commit -am "feature commit"
dolt tag v3
dolt push origin v3
cd ../repo2
dolt sql -q "CALL dolt_pull('origin')"
run dolt tag
[ "$status" -eq 0 ]
[[ "$output" =~ "v1" ]] || false
[[ "$output" =~ "v2" ]] || false
[[ ! "$output" =~ "v3" ]] || false
}
@test "sql-pull: dolt_pull also fetches, but does not merge other branches" {
cd repo1
dolt checkout -b other
dolt push --set-upstream origin other
dolt checkout feature
dolt push origin feature
cd ../repo2
dolt fetch
# this checkout will set upstream because 'other' branch is a new branch that matches one of remote tracking branch
dolt checkout other
# this checkout will not set upstream because this 'feature' branch existed before matching remote tracking branch was created
dolt checkout feature
dolt push --set-upstream origin feature
cd ../repo1
dolt merge main
dolt push origin feature
dolt checkout other
dolt commit --allow-empty -m "new commit on other"
dolt push
cd ../repo2
dolt sql -q "select dolt_pull()"
run dolt sql -q "show tables" -r csv
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 2 ]
[[ "$output" =~ "Table" ]] || false
[[ "$output" =~ "t1" ]] || false
dolt checkout other
run dolt log --oneline -n 1
[ "$status" -eq 0 ]
[[ ! "$output" =~ "new commit on other" ]] || false
run dolt status
[ "$status" -eq 0 ]
[[ "$output" =~ "behind 'origin/other' by 1 commit" ]] || false
}