Skip to content

Commit

Permalink
fix(shell/history): use host name instead of user to set history name
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Silva authored and luisfvieirasilva committed Apr 27, 2023
1 parent 78461ae commit dfa5dd1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
12 changes: 6 additions & 6 deletions internal/shell/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func GetHistoryFileBasedOnMode(dbPath string, mode enums.HistoryMode, historyNam
case enums.LocalHistory:
return sharedHistoryFileName
case enums.PerDatabaseHistory:
if parsedName, err := parseNameFromDbPath(dbPath); err == nil && parsedName != "" {
return getHistoryFileFullPath(historyName, getHistoryFileName(parsedName))
if host, err := getHostFromDbPath(dbPath); err == nil && host != "" {
return getHistoryFileFullPath(historyName, getHistoryFileName(host))
}
}

Expand All @@ -40,16 +40,16 @@ func getHistoryFolderPath(historyName string) string {
return path
}

func parseNameFromDbPath(dbPath string) (string, error) {
func getHostFromDbPath(dbPath string) (string, error) {
if db.IsUrl(dbPath) {
url, err := url.Parse(dbPath)
if err != nil {
return "", err
}
if url.User == nil {
return "", &shellerrors.UrlDoesNotContainUserError{}
if url.Host == "" {
return "", &shellerrors.UrlDoesNotContainHostError{}
}
return url.User.String(), nil
return url.Host, nil
}

return getFileNameWithoutExtension(dbPath), nil
Expand Down
42 changes: 36 additions & 6 deletions internal/shell/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,51 @@ func TestGetHistoryFileBasedOnMode_GivenPerDatabaseHistory_WhenPathIsEmpty_Expec
c.Assert(result, qt.Equals, expectedPath)
}

func TestGetHistoryFileBasedOnMode_GivenPerDatabaseHistory_WhenPathIsHttpUrl_ExpectSpecificGlobalHistory(t *testing.T) {
func TestGetHistoryFileBasedOnMode_GivenPerDatabaseHistory_WhenPathIsHttpUrlWithUser_ExpectSpecificGlobalHistory(t *testing.T) {
c := qt.New(t)

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

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

func TestGetHistoryFileBasedOnMode_GivenPerDatabaseHistory_WhenPathIsHttpUrlWithoutUser_ExpectSharedGlobalHistory(t *testing.T) {
func TestGetHistoryFileBasedOnMode_GivenPerDatabaseHistory_WhenPathIsHttpUrlWithoutUser_ExpectSpecificGlobalHistory(t *testing.T) {
c := qt.New(t)

dbPath := "https://company.turso.io"
expectedPath := getExpectedHistoryFullPath(historyName)
dbPath := "https://database-username.turso.io"
expectedPath := getExpectedHistoryFullPath("database-username.turso.io")
result := shell.GetHistoryFileBasedOnMode(dbPath, enums.PerDatabaseHistory, historyName)

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

func TestGetHistoryFileBasedOnMode_GivenPerDatabaseHistory_WhenPathIsLibsqlUrl_ExpectSpecificGlobalHistory(t *testing.T) {
c := qt.New(t)

dbPath := "libsql://database-username.turso.io/?jwt=some_token"
expectedPath := getExpectedHistoryFullPath("database-username.turso.io")
result := shell.GetHistoryFileBasedOnMode(dbPath, enums.PerDatabaseHistory, historyName)

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

func TestGetHistoryFileBasedOnMode_GivenPerDatabaseHistory_WhenPathIsWssUrl_ExpectSpecificGlobalHistory(t *testing.T) {
c := qt.New(t)

dbPath := "wss://database-username.turso.io/?jwt=some_token"
expectedPath := getExpectedHistoryFullPath("database-username.turso.io")
result := shell.GetHistoryFileBasedOnMode(dbPath, enums.PerDatabaseHistory, historyName)

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

func TestGetHistoryFileBasedOnMode_GivenPerDatabaseHistory_WhenPathIsWsUrl_ExpectSpecificGlobalHistory(t *testing.T) {
c := qt.New(t)

dbPath := "ws://database-username.turso.io/?jwt=some_token"
expectedPath := getExpectedHistoryFullPath("database-username.turso.io")
result := shell.GetHistoryFileBasedOnMode(dbPath, enums.PerDatabaseHistory, historyName)

c.Assert(result, qt.Equals, expectedPath)
Expand Down
8 changes: 4 additions & 4 deletions pkg/shell/shellerrors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ func (e *TransactionNotSupportedError) userError() string {
return "transactions are only supported in the shell using semicolons to separate each statement.\nFor example: \"BEGIN; [your SQL statements]; END\""
}

type UrlDoesNotContainUserError struct{}
type UrlDoesNotContainHostError struct{}

func (e *UrlDoesNotContainUserError) Error() string {
func (e *UrlDoesNotContainHostError) Error() string {
return e.userError()
}
func (e *UrlDoesNotContainUserError) userError() string {
return "url does not contain user"
func (e *UrlDoesNotContainHostError) userError() string {
return "url does not contain host"
}

type InvalidTursoProtocolError struct{}
Expand Down

0 comments on commit dfa5dd1

Please sign in to comment.