Default branch now database-specific global variable (#3422)

* Default branch now database-specific global variable

* fix bats
This commit is contained in:
Maximilian Hoffman
2022-05-16 13:21:50 -07:00
committed by GitHub
parent d2196bad9e
commit 33a679f20b
6 changed files with 71 additions and 24 deletions

View File

@@ -287,7 +287,7 @@ func getDbStates(ctx context.Context, dbs []dsqle.SqlDatabase) ([]dsess.InitialD
var init dsess.InitialDbState
var err error
_, val, ok := sql.SystemVariables.GetGlobal(dsqle.DefaultBranchKey)
_, val, ok := sql.SystemVariables.GetGlobal(dsess.DefaultBranchKey(db.Name()))
if ok && val != "" {
init, err = getInitialDBStateWithDefaultBranch(ctx, db, val.(string))
} else {
@@ -314,7 +314,7 @@ func getInitialDBStateWithDefaultBranch(ctx context.Context, db dsqle.SqlDatabas
head, err := ddb.ResolveCommitRef(ctx, r)
if err != nil {
init.Err = fmt.Errorf("failed to connect to dolt_default_branch='%s' on database '%s'; %w", branch, db.Name(), err)
init.Err = fmt.Errorf("failed to connect to database default branch: '%s/%s'; %w", db.Name(), branch, err)
} else {
init.Err = nil
}

View File

@@ -21,10 +21,11 @@ import (
)
const (
HeadKeySuffix = "_head"
HeadRefKeySuffix = "_head_ref"
WorkingKeySuffix = "_working"
StagedKeySuffix = "_staged"
HeadKeySuffix = "_head"
HeadRefKeySuffix = "_head_ref"
WorkingKeySuffix = "_working"
StagedKeySuffix = "_staged"
DefaultBranchKeySuffix = "_default_branch"
)
const (
@@ -118,6 +119,14 @@ func defineSystemVariables(name string) {
Type: sql.NewSystemStringType(StagedKey(name)),
Default: "",
},
{
Name: DefaultBranchKey(name),
Scope: sql.SystemVariableScope_Global,
Dynamic: true,
SetVarHintApplies: false,
Type: sql.NewSystemStringType(DefaultBranchKey(name)),
Default: "",
},
})
}
}
@@ -138,6 +147,10 @@ func StagedKey(dbName string) string {
return dbName + StagedKeySuffix
}
func DefaultBranchKey(dbName string) string {
return dbName + DefaultBranchKeySuffix
}
func IsHeadKey(key string) (bool, string) {
if strings.HasSuffix(key, HeadKeySuffix) {
return true, key[:len(key)-len(HeadKeySuffix)]
@@ -162,6 +175,14 @@ func IsWorkingKey(key string) (bool, string) {
return false, ""
}
func IsDefaultBranchKey(key string) (bool, string) {
if strings.HasSuffix(key, DefaultBranchKeySuffix) {
return true, key[:len(key)-len(DefaultBranchKeySuffix)]
}
return false, ""
}
func IsReadOnlyVersionKey(key string) bool {
return strings.HasSuffix(key, HeadKeySuffix) ||
strings.HasSuffix(key, StagedKeySuffix) ||

View File

@@ -19,7 +19,6 @@ import (
)
const (
DefaultBranchKey = "dolt_default_branch"
ReplicateToRemoteKey = "dolt_replicate_to_remote"
ReadReplicaRemoteKey = "dolt_read_replica_remote"
SkipReplicationErrorsKey = "dolt_skip_replication_errors"
@@ -39,14 +38,6 @@ func init() {
func AddDoltSystemVariables() {
sql.SystemVariables.AddSystemVariables([]sql.SystemVariable{
{
Name: DefaultBranchKey,
Scope: sql.SystemVariableScope_Global,
Dynamic: true,
SetVarHintApplies: false,
Type: sql.NewSystemStringType(DefaultBranchKey),
Default: "",
},
{
Name: ReplicateToRemoteKey,
Scope: sql.SystemVariableScope_Global,

View File

@@ -36,7 +36,7 @@ make_it() {
start_sql_server "dolt_repo_$$"
server_query "dolt_repo_$$" 1 "SET @@GLOBAL.dolt_default_branch = 'to_keep'"
server_query "dolt_repo_$$" 1 "SET @@GLOBAL.dolt_repo_$$_default_branch = 'to_keep'"
server_query "dolt_repo_$$" 1 'delete from dolt_branches where name = "main"' ""
@@ -63,7 +63,7 @@ make_it() {
start_sql_server "dolt_repo_$$"
server_query "dolt_repo_$$" 1 "SET @@GLOBAL.dolt_default_branch = 'this_branch_does_not_exist'"
server_query "dolt_repo_$$" 1 "SET @@GLOBAL.dolt_repo_$$_default_branch = 'this_branch_does_not_exist'"
# Against the default branch it fails
run server_query "dolt_repo_$$" 1 "SELECT * FROM test" ""
@@ -78,7 +78,7 @@ make_it() {
start_sql_server "dolt_repo_$$"
server_query "dolt_repo_$$" 1 "SET @@GLOBAL.dolt_default_branch = 'this_branch_does_not_exist'"
server_query "dolt_repo_$$" 1 "SET @@GLOBAL.dolt_repo_$$_default_branch = 'this_branch_does_not_exist'"
multi_query "dolt_repo_$$/main" 1 "
SELECT * FROM test;
@@ -101,14 +101,14 @@ SELECT DOLT_CHECKOUT('to_checkout');
SELECT * FROM test;"
}
@test "deleted-branches: can DOLT_CHECKOUT on SQL connecttion with dolt_default_branch set to existing branch when checked out branch is deleted" {
@test "deleted-branches: can DOLT_CHECKOUT on SQL connection with dolt_default_branch set to existing branch when checked out branch is deleted" {
make_it
dolt branch -c to_keep to_checkout
start_sql_server "dolt_repo_$$"
server_query "dolt_repo_$$" 1 "SET @@GLOBAL.dolt_default_branch = 'to_keep'"
server_query "dolt_repo_$$" 1 "SET @@GLOBAL.dolt_repo_$$_default_branch = 'to_keep'"
server_query "dolt_repo_$$" 1 'delete from dolt_branches where name = "main"' ""

View File

@@ -0,0 +1,35 @@
#!/usr/bin/env bats
load $BATS_TEST_DIRNAME/helper/common.bash
load $BATS_TEST_DIRNAME/helper/query-server-common.bash
setup() {
setup_common
TMPDIRS=$(pwd)/tmpdirs
init_helper $TMPDIRS
cd $TMPDIRS
}
init_helper() {
TMPDIRS=$1
mkdir -p "${TMPDIRS}/dbs1"
for i in {1..2}; do
mkdir "${TMPDIRS}/dbs1/repo${i}"
cd "${TMPDIRS}/dbs1/repo${i}"
dolt init
done
}
teardown() {
stop_sql_server
teardown_common
rm -rf $TMPDIRS
cd $BATS_TMPDIR
}
@test "multidb: database default branches" {
cd dbs1
start_multi_db_server repo1
multi_query repo1 1 "create database new; use new; call dcheckout('-b', 'feat'); create table t (x int); call dcommit('-am', 'cm'); set @@global.new_default_branch='feat'"
server_query repo1 1 "use repo1"
}

View File

@@ -755,8 +755,8 @@ SQL
INSERT INTO t VALUES (2,2),(3,3);' ""
server_query repo1 1 "SHOW tables" "" # no tables on main
server_query repo1 1 "set GLOBAL dolt_default_branch = 'refs/heads/new';" ""
server_query repo1 1 "select @@GLOBAL.dolt_default_branch;" "@@GLOBAL.dolt_default_branch\nrefs/heads/new"
server_query repo1 1 "set GLOBAL repo1_default_branch = 'refs/heads/new';" ""
server_query repo1 1 "select @@GLOBAL.repo1_default_branch;" "@@GLOBAL.repo1_default_branch\nrefs/heads/new"
server_query repo1 1 "select active_branch()" "active_branch()\nnew"
server_query repo1 1 "SHOW tables" "Tables_in_repo1\nt"
}
@@ -775,8 +775,8 @@ SQL
INSERT INTO t VALUES (2,2),(3,3);' ""
server_query repo1 1 "SHOW tables" "" # no tables on main
server_query repo1 1 "set GLOBAL dolt_default_branch = 'new';" ""
server_query repo1 1 "select @@GLOBAL.dolt_default_branch;" "@@GLOBAL.dolt_default_branch\nnew"
server_query repo1 1 "set GLOBAL repo1_default_branch = 'new';" ""
server_query repo1 1 "select @@GLOBAL.repo1_default_branch;" "@@GLOBAL.repo1_default_branch\nnew"
server_query repo1 1 "select active_branch()" "active_branch()\nnew"
server_query repo1 1 "SHOW tables" "Tables_in_repo1\nt"
}