Skip to content

Commit

Permalink
sqlite: only use file name without extension as schema name
Browse files Browse the repository at this point in the history
  • Loading branch information
KarnerTh committed Nov 21, 2023
1 parent df817b8 commit 10ac644
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions database/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package database
import (
"database/sql"
"fmt"
"path/filepath"
"strings"

_ "modernc.org/sqlite"
Expand All @@ -14,12 +15,17 @@ func (c *sqliteConnector) GetDbType() DbType {
return c.dbType
}

func getFilenameFromConnectionString(connectionString string) string {
func getFilePathFromConnectionString(connectionString string) string {
return strings.Replace(connectionString, "sqlite3://", "", 1)
}

func getFileNameFromPath(path string) string {
fileName := filepath.Base(path)
return strings.Replace(fileName, filepath.Ext(fileName), "", 1)
}

func (c *sqliteConnector) Connect() error {
db, err := sql.Open(c.dbType.String(), getFilenameFromConnectionString(c.connectionString))
db, err := sql.Open(c.dbType.String(), getFilePathFromConnectionString(c.connectionString))
if err != nil {
return err
}
Expand All @@ -40,8 +46,8 @@ func (c *sqliteConnector) Close() {
}

func (c *sqliteConnector) GetSchemas() ([]string, error) {
fileName := getFilenameFromConnectionString(c.connectionString)
schema := strings.Replace(fileName, ".db", "", 1)
filePath := getFilePathFromConnectionString(c.connectionString)
schema := getFileNameFromPath(filePath)
return []string{schema}, nil
}

Expand Down

0 comments on commit 10ac644

Please sign in to comment.