Skip to content

Commit

Permalink
use database name that is the same as directory name on disk (#7020)
Browse files Browse the repository at this point in the history
  • Loading branch information
jennifersp authored Nov 20, 2023
1 parent 517e867 commit d09c29c
Show file tree
Hide file tree
Showing 15 changed files with 95 additions and 54 deletions.
9 changes: 6 additions & 3 deletions go/libraries/doltcore/dbfactory/dirtodbname.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ import (
// DirToDBName takes the physical directory name, |dirName|, and replaces any unsupported characters to create a
// valid logical database name. For example, spaces are replaced with underscores.
func DirToDBName(dirName string) string {
// this environment variable is used whether to replace hyphens in the database name with underscores.
var translateHyphensToUnderscores = os.Getenv(dconfig.EnvDbNameReplaceHyphens) != ""
// this environment variable is used whether to replace hyphen and space characters in the database name with underscores.
if os.Getenv(dconfig.EnvDbNameReplace) == "" {
return dirName
}

dbName := strings.TrimSpace(dirName)
dbName = strings.Map(func(r rune) rune {
if unicode.IsSpace(r) || (translateHyphensToUnderscores && r == '-') {
if unicode.IsSpace(r) || r == '-' {
return '_'
}
return r
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/dconfig/envvars.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ const (
EnvDoltAssistAgree = "DOLT_ASSIST_AGREE"
EnvDoltAuthorDate = "DOLT_AUTHOR_DATE"
EnvDoltCommitterDate = "DOLT_COMMITTER_DATE"
EnvDbNameReplaceHyphens = "DOLT_DBNAME_REPLACE_HYPHENS"
EnvDbNameReplace = "DOLT_DBNAME_REPLACE"
)
10 changes: 5 additions & 5 deletions go/libraries/doltcore/env/multi_repo_env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestDirToDBName(t *testing.T) {
" real - name ": "real_name",
}

err := os.Setenv(dconfig.EnvDbNameReplaceHyphens, "true")
err := os.Setenv(dconfig.EnvDbNameReplace, "true")
require.NoError(t, err)

for dirName, expected := range replaceHyphenTests {
Expand All @@ -49,10 +49,10 @@ func TestDirToDBName(t *testing.T) {
allowHyphenTests := map[string]string{
"irs": "irs",
"corona-virus": "corona-virus",
" fake - name ": "fake_-_name",
" fake - name ": " fake - name ",
}

err = os.Setenv(dconfig.EnvDbNameReplaceHyphens, "")
err = os.Setenv(dconfig.EnvDbNameReplace, "")
require.NoError(t, err)

for dirName, expected := range allowHyphenTests {
Expand Down Expand Up @@ -133,7 +133,7 @@ func TestMultiEnvForDirectory(t *testing.T) {

expected := []envCmp{
{
name: "test---name_123",
name: " test---name _ 123",
doltDir: dEnv.GetDoltDir(),
},
}
Expand Down Expand Up @@ -164,7 +164,7 @@ func TestMultiEnvForDirectoryWithMultipleRepos(t *testing.T) {
assert.Len(t, mrEnv.envs, 3)

expected := make(map[string]string)
expected["test---name_123"] = dEnv.GetDoltDir()
expected[" test---name _ 123"] = dEnv.GetDoltDir()
expected["abc"] = subEnv1.GetDoltDir()
expected["def"] = subEnv2.GetDoltDir()

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/bats/db-revision-specifiers.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ load $BATS_TEST_DIRNAME/helper/common.bash

setup() {
setup_common
export DOLT_DBNAME_REPLACE_HYPHENS="true"
export DOLT_DBNAME_REPLACE="true"
database_name=dolt_repo_$$

dolt sql -q "CREATE TABLE test(pk int PRIMARY KEY, color varchar(200))"
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/bats/deleted-branches.bats
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ setup() {
skiponwindows "Missing dependencies"

setup_common
export DOLT_DBNAME_REPLACE_HYPHENS="true"
export DOLT_DBNAME_REPLACE="true"
}

teardown() {
Expand Down
22 changes: 22 additions & 0 deletions integration-tests/bats/dump.bats
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,28 @@ teardown() {
[[ "$output" =~ "No tables to export." ]] || false
}

@test "dump: roundtrip on database with leading space character and hyphen" {
mkdir ' test-db'
cd ' test-db'
dolt init
create_tables
insert_data_into_tables

run dolt dump
[ "$status" -eq 0 ]
[[ "$output" =~ "Successfully exported data." ]] || false
[ -f doltdump.sql ]

mkdir roundtrip
cd roundtrip
dolt init

dolt sql < ../doltdump.sql
run dolt sql -q "show databases"
[ $status -eq 0 ]
[[ $output =~ "| test-db" ]] || false
}

@test "dump: SQL type - with multiple tables" {
dolt sql -q "CREATE TABLE new_table(pk int primary key);"
dolt sql -q "INSERT INTO new_table VALUES (1);"
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/bats/sql-checkout.bats
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ SQL
}

@test "sql-checkout: DOLT_CHECKOUT updates the head ref session var" {
export DOLT_DBNAME_REPLACE_HYPHENS="true"
export DOLT_DBNAME_REPLACE="true"
run dolt sql <<SQL
call dolt_checkout('-b', 'feature-branch');
select @@dolt_repo_$$_head_ref;
Expand All @@ -141,7 +141,7 @@ SQL
}

@test "sql-checkout: CALL DOLT_CHECKOUT updates the head ref session var" {
export DOLT_DBNAME_REPLACE_HYPHENS="true"
export DOLT_DBNAME_REPLACE="true"
run dolt sql <<SQL
CALL DOLT_CHECKOUT('-b', 'feature-branch');
select @@dolt_repo_$$_head_ref;
Expand Down
17 changes: 2 additions & 15 deletions integration-tests/bats/sql-client.bats
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ teardown() {
[[ $output =~ "not found" ]] || false
}

@test "sql-client: handle dashes for implicit database with hyphen disabled" {
@test "sql-client: handle dashes for implicit database" {
make_repo test-dashes
cd test-dashes
PORT=$( definePORT )
export DOLT_DBNAME_REPLACE_HYPHENS="true"
export DOLT_DBNAME_REPLACE="true"
dolt sql-server --user=root --port=$PORT &
SERVER_PID=$! # will get killed by teardown_common
sleep 5 # not using python wait so this works on windows
Expand All @@ -148,19 +148,6 @@ teardown() {
[[ $output =~ " test_dashes " ]] || false
}

@test "sql-client: handle dashes for implicit database with hyphen allowed" {
make_repo test-dashes
cd test-dashes
PORT=$( definePORT )
dolt sql-server --user=root --port=$PORT &
SERVER_PID=$! # will get killed by teardown_common
sleep 5 # not using python wait so this works on windows

run dolt sql-client -u root -P $PORT -q "show databases"
[ $status -eq 0 ]
[[ $output =~ " test-dashes " ]] || false
}

@test "sql-client: select statement prints accurate query timing" {
cd repo1
start_sql_server repo1
Expand Down
8 changes: 4 additions & 4 deletions integration-tests/bats/sql-commit.bats
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ SQL
}

@test "sql-commit: DOLT_COMMIT updates session variables" {
export DOLT_DBNAME_REPLACE_HYPHENS="true"
export DOLT_DBNAME_REPLACE="true"
head_variable=@@dolt_repo_$$_head
head_commit=$(get_head_commit)
run dolt sql << SQL
Expand All @@ -233,7 +233,7 @@ SQL
}

@test "sql-commit: CALL DOLT_COMMIT updates session variables" {
export DOLT_DBNAME_REPLACE_HYPHENS="true"
export DOLT_DBNAME_REPLACE="true"
head_variable=@@dolt_repo_$$_head
head_commit=$(get_head_commit)
run dolt sql << SQL
Expand All @@ -254,7 +254,7 @@ SQL
}

@test "sql-commit: DOLT_COMMIT with unstaged tables leaves them in the working set" {
export DOLT_DBNAME_REPLACE_HYPHENS="true"
export DOLT_DBNAME_REPLACE="true"
head_variable=@@dolt_repo_$$_head

run dolt sql << SQL
Expand Down Expand Up @@ -314,7 +314,7 @@ SQL
}

@test "sql-commit: CALL DOLT_COMMIT with unstaged tables leaves them in the working set" {
export DOLT_DBNAME_REPLACE_HYPHENS="true"
export DOLT_DBNAME_REPLACE="true"
head_variable=@@dolt_repo_$$_head

run dolt sql << SQL
Expand Down
29 changes: 29 additions & 0 deletions integration-tests/bats/sql-create-database.bats
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,32 @@ SQL
[[ "$output" =~ "def,metabase,utf8mb4,utf8mb4_unicode_ci,,NO" ]] || false
cd ..
}

@test "sql-create-database: creating database with hyphen and space characters replaced" {
mkdir 'test- dashes'
cd 'test- dashes'
dolt init
export DOLT_DBNAME_REPLACE="true"

# aliasing with 'a' allows check on the exact length of the database name
run dolt sql << SQL
USE test_dashes;
SELECT DATABASE() AS a;
SQL
[ $status -eq 0 ]
[[ $output =~ "| test_dashes |" ]] || false
}

@test "sql-create-database: creating database with hyphen and space characters allowed" {
mkdir ' test- db _ '
cd ' test- db _ '
dolt init

# aliasing with 'a' allows check on the exact length of the database name
run dolt sql << SQL
USE \` test- db _ \`;
SELECT DATABASE() AS a;
SQL
[ $status -eq 0 ]
[[ $output =~ "| test- db _ |" ]] || false
}
4 changes: 2 additions & 2 deletions integration-tests/bats/sql-merge.bats
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ SQL
}

@test "sql-merge: DOLT_MERGE correctly returns head and working session variables." {
export DOLT_DBNAME_REPLACE_HYPHENS="true"
export DOLT_DBNAME_REPLACE="true"
dolt sql << SQL
call dolt_commit('-a', '-m', 'Step 1');
call dolt_checkout('-b', 'feature-branch');
Expand Down Expand Up @@ -317,7 +317,7 @@ SQL
}

@test "sql-merge: DOLT_MERGE -no-ff correctly changes head and working session variables." {
export DOLT_DBNAME_REPLACE_HYPHENS="true"
export DOLT_DBNAME_REPLACE="true"
dolt sql << SQL
call dolt_commit('-a', '-m', 'Step 1');
call dolt_checkout('-b', 'feature-branch');
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/bats/sql-reset.bats
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ SQL
}

@test "sql-reset: CALL DOLT_RESET --hard properly maintains session variables." {
export DOLT_DBNAME_REPLACE_HYPHENS="true"
export DOLT_DBNAME_REPLACE="true"
head_variable=@@dolt_repo_$$_head
head_hash=$(get_head_commit)
run dolt sql << SQL
Expand Down Expand Up @@ -318,7 +318,7 @@ SQL
}

@test "sql-reset: CALL DOLT_RESET soft maintains staged session variable" {
export DOLT_DBNAME_REPLACE_HYPHENS="true"
export DOLT_DBNAME_REPLACE="true"
working_hash_var=@@dolt_repo_$$_working
run dolt sql -q "SELECT $working_hash_var"
working_hash=$output
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/bats/sql-server.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1789,7 +1789,7 @@ behavior:

@test "sql-server: dropping database with '-' in it but replaced with underscore" {
skiponwindows "Missing dependencies"
export DOLT_DBNAME_REPLACE_HYPHENS="true"
export DOLT_DBNAME_REPLACE="true"
mkdir my-db
cd my-db
dolt init
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/bats/sql.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ load $BATS_TEST_DIRNAME/helper/common.bash

setup() {
setup_common
export DOLT_DBNAME_REPLACE_HYPHENS="true"
export DOLT_DBNAME_REPLACE="true"
dolt sql <<SQL
CREATE TABLE one_pk (
pk BIGINT NOT NULL,
Expand Down
32 changes: 16 additions & 16 deletions integration-tests/bats/undrop.bats
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ teardown() {
}

@test "undrop: undrop root database with hyphen replaced in its name" {
export DOLT_DBNAME_REPLACE_HYPHENS="true"
export DOLT_DBNAME_REPLACE="true"
setup_remote_server
# Create a new Dolt database directory to use as a root database
# NOTE: We use hyphens here to test how db dirs are renamed.
Expand Down Expand Up @@ -111,32 +111,32 @@ call dolt_commit('-Am', 'creating table t1');
EOF
run dolt sql -q "show databases;"
[ $status -eq 0 ]
[[ $output =~ "test-_db-1" ]] || false
[[ $output =~ " test- db-1 " ]] || false

# Drop the root database
dolt sql -q "drop database \`test-_db-1\`;"
dolt sql -q "drop database \` test- db-1 \`;"
run dolt sql -q "show databases;"
[ $status -eq 0 ]
[[ ! $output =~ "test-_db-1" ]] || false
[[ ! $output =~ " test- db-1 " ]] || false

# Undrop the test-_db-1 database
# Undrop the ' test- db-1 ' database
# NOTE: After being undropped, the database is no longer the root database,
# but contained in a subdirectory like a non-root database.
dolt sql -q "call dolt_undrop('test-_db-1');"
dolt sql -q "call dolt_undrop(' test- db-1 ');"
run dolt sql -q "show databases;"
[ $status -eq 0 ]
[[ $output =~ "test-_db-1" ]] || false
[[ $output =~ " test- db-1 " ]] || false

# Sanity check querying some data
run dolt sql -r csv -q "select * from \`test-_db-1\`.t1;"
run dolt sql -r csv -q "select * from \` test- db-1 \`.t1;"
[ $status -eq 0 ]
[[ $output =~ "1,one" ]] || false
}

# Asserts that a non-root database can be dropped and then restored with dolt_undrop(), even when
# the case of the database name given to dolt_undrop() doesn't match match the original case.
@test "undrop: undrop non-root database with hyphen replaced in its name" {
export DOLT_DBNAME_REPLACE_HYPHENS="true"
export DOLT_DBNAME_REPLACE="true"
setup_remote_server
dolt sql << EOF
use drop_me_2;
Expand Down Expand Up @@ -170,28 +170,28 @@ EOF
@test "undrop: undrop non-root database with hyphen allowed in its name" {
setup_remote_server
dolt sql << EOF
use \`drop-_me-2\`;
use \` drop- me-2 \`;
create table t1 (pk int primary key, c1 varchar(200));
insert into t1 values (1, "one");
call dolt_commit('-Am', 'creating table t1');
EOF
run dolt sql -q "show databases;"
[ $status -eq 0 ]
[[ $output =~ "drop-_me-2" ]] || false
[[ $output =~ " drop- me-2 " ]] || false

dolt sql -q "drop database \`drop-_me-2\`;"
dolt sql -q "drop database \` drop- me-2 \`;"
run dolt sql -q "show databases;"
[ $status -eq 0 ]
[[ ! $output =~ "drop-_me-2" ]] || false
[[ ! $output =~ " drop- me-2 " ]] || false

# Call dolt_undrop() with non-matching case for the database name to
# ensure dolt_undrop() works with case-insensitive database names.
dolt sql -q "call dolt_undrop('DrOp-_mE-2');"
dolt sql -q "call dolt_undrop(' DrOp- mE-2 ');"
run dolt sql -q "show databases;"
[ $status -eq 0 ]
[[ $output =~ "drop-_me-2" ]] || false
[[ $output =~ " drop- me-2 " ]] || false

run dolt sql -r csv -q "select * from \`drop-_me-2\`.t1;"
run dolt sql -r csv -q "select * from \` drop- me-2 \`.t1;"
[ $status -eq 0 ]
[[ $output =~ "1,one" ]] || false
}
Expand Down

0 comments on commit d09c29c

Please sign in to comment.