Skip to content

Commit dfa5dd1

Browse files
Luis Silvaluisfvieirasilva
authored andcommitted
fix(shell/history): use host name instead of user to set history name
1 parent 78461ae commit dfa5dd1

File tree

3 files changed

+46
-16
lines changed

3 files changed

+46
-16
lines changed

internal/shell/history.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ func GetHistoryFileBasedOnMode(dbPath string, mode enums.HistoryMode, historyNam
1818
case enums.LocalHistory:
1919
return sharedHistoryFileName
2020
case enums.PerDatabaseHistory:
21-
if parsedName, err := parseNameFromDbPath(dbPath); err == nil && parsedName != "" {
22-
return getHistoryFileFullPath(historyName, getHistoryFileName(parsedName))
21+
if host, err := getHostFromDbPath(dbPath); err == nil && host != "" {
22+
return getHistoryFileFullPath(historyName, getHistoryFileName(host))
2323
}
2424
}
2525

@@ -40,16 +40,16 @@ func getHistoryFolderPath(historyName string) string {
4040
return path
4141
}
4242

43-
func parseNameFromDbPath(dbPath string) (string, error) {
43+
func getHostFromDbPath(dbPath string) (string, error) {
4444
if db.IsUrl(dbPath) {
4545
url, err := url.Parse(dbPath)
4646
if err != nil {
4747
return "", err
4848
}
49-
if url.User == nil {
50-
return "", &shellerrors.UrlDoesNotContainUserError{}
49+
if url.Host == "" {
50+
return "", &shellerrors.UrlDoesNotContainHostError{}
5151
}
52-
return url.User.String(), nil
52+
return url.Host, nil
5353
}
5454

5555
return getFileNameWithoutExtension(dbPath), nil

internal/shell/history_test.go

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,51 @@ func TestGetHistoryFileBasedOnMode_GivenPerDatabaseHistory_WhenPathIsEmpty_Expec
7878
c.Assert(result, qt.Equals, expectedPath)
7979
}
8080

81-
func TestGetHistoryFileBasedOnMode_GivenPerDatabaseHistory_WhenPathIsHttpUrl_ExpectSpecificGlobalHistory(t *testing.T) {
81+
func TestGetHistoryFileBasedOnMode_GivenPerDatabaseHistory_WhenPathIsHttpUrlWithUser_ExpectSpecificGlobalHistory(t *testing.T) {
8282
c := qt.New(t)
8383

84-
dbPath := "https://username:password@company.turso.io"
85-
expectedPath := getExpectedHistoryFullPath("username:password")
84+
dbPath := "https://username:password@database-username.turso.io"
85+
expectedPath := getExpectedHistoryFullPath("database-username.turso.io")
8686
result := shell.GetHistoryFileBasedOnMode(dbPath, enums.PerDatabaseHistory, historyName)
8787

8888
c.Assert(result, qt.Equals, expectedPath)
8989
}
9090

91-
func TestGetHistoryFileBasedOnMode_GivenPerDatabaseHistory_WhenPathIsHttpUrlWithoutUser_ExpectSharedGlobalHistory(t *testing.T) {
91+
func TestGetHistoryFileBasedOnMode_GivenPerDatabaseHistory_WhenPathIsHttpUrlWithoutUser_ExpectSpecificGlobalHistory(t *testing.T) {
9292
c := qt.New(t)
9393

94-
dbPath := "https://company.turso.io"
95-
expectedPath := getExpectedHistoryFullPath(historyName)
94+
dbPath := "https://database-username.turso.io"
95+
expectedPath := getExpectedHistoryFullPath("database-username.turso.io")
96+
result := shell.GetHistoryFileBasedOnMode(dbPath, enums.PerDatabaseHistory, historyName)
97+
98+
c.Assert(result, qt.Equals, expectedPath)
99+
}
100+
101+
func TestGetHistoryFileBasedOnMode_GivenPerDatabaseHistory_WhenPathIsLibsqlUrl_ExpectSpecificGlobalHistory(t *testing.T) {
102+
c := qt.New(t)
103+
104+
dbPath := "libsql://database-username.turso.io/?jwt=some_token"
105+
expectedPath := getExpectedHistoryFullPath("database-username.turso.io")
106+
result := shell.GetHistoryFileBasedOnMode(dbPath, enums.PerDatabaseHistory, historyName)
107+
108+
c.Assert(result, qt.Equals, expectedPath)
109+
}
110+
111+
func TestGetHistoryFileBasedOnMode_GivenPerDatabaseHistory_WhenPathIsWssUrl_ExpectSpecificGlobalHistory(t *testing.T) {
112+
c := qt.New(t)
113+
114+
dbPath := "wss://database-username.turso.io/?jwt=some_token"
115+
expectedPath := getExpectedHistoryFullPath("database-username.turso.io")
116+
result := shell.GetHistoryFileBasedOnMode(dbPath, enums.PerDatabaseHistory, historyName)
117+
118+
c.Assert(result, qt.Equals, expectedPath)
119+
}
120+
121+
func TestGetHistoryFileBasedOnMode_GivenPerDatabaseHistory_WhenPathIsWsUrl_ExpectSpecificGlobalHistory(t *testing.T) {
122+
c := qt.New(t)
123+
124+
dbPath := "ws://database-username.turso.io/?jwt=some_token"
125+
expectedPath := getExpectedHistoryFullPath("database-username.turso.io")
96126
result := shell.GetHistoryFileBasedOnMode(dbPath, enums.PerDatabaseHistory, historyName)
97127

98128
c.Assert(result, qt.Equals, expectedPath)

pkg/shell/shellerrors/errors.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ func (e *TransactionNotSupportedError) userError() string {
1919
return "transactions are only supported in the shell using semicolons to separate each statement.\nFor example: \"BEGIN; [your SQL statements]; END\""
2020
}
2121

22-
type UrlDoesNotContainUserError struct{}
22+
type UrlDoesNotContainHostError struct{}
2323

24-
func (e *UrlDoesNotContainUserError) Error() string {
24+
func (e *UrlDoesNotContainHostError) Error() string {
2525
return e.userError()
2626
}
27-
func (e *UrlDoesNotContainUserError) userError() string {
28-
return "url does not contain user"
27+
func (e *UrlDoesNotContainHostError) userError() string {
28+
return "url does not contain host"
2929
}
3030

3131
type InvalidTursoProtocolError struct{}

0 commit comments

Comments
 (0)