Skip to content

Commit

Permalink
clean: moving the connect function
Browse files Browse the repository at this point in the history
  • Loading branch information
paganotoni committed Jun 22, 2024
1 parent 42cbeea commit 7b0d994
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
12 changes: 10 additions & 2 deletions cmd/database/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"database/sql"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/leapkit/core/db"

Expand Down Expand Up @@ -35,9 +37,16 @@ func main() {

switch os.Args[1] {
case "migrate":
conn, err := db.Connect(url)
driver := "sqlite3"
if strings.HasPrefix(url, "postgres") {
driver = "postgres"
}

conn, err := sql.Open(driver, url)
if err != nil {
fmt.Println(err)

return
}

err = db.RunMigrationsDir(filepath.Join("internal", "migrations"), conn)
Expand Down Expand Up @@ -67,7 +76,6 @@ func main() {
}

fmt.Println("✅ Database dropped successfully")

default:
fmt.Println("command not found")

Expand Down
12 changes: 0 additions & 12 deletions db/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package db

import (
"database/sql"
"strings"
"sync"
)

Expand Down Expand Up @@ -56,14 +55,3 @@ func WithDriver(name string) connectionOption {
driverName = name
}
}

// connect to the database based on the driver and connection string.
func Connect(url string) (*sql.DB, error) {
// Based on DSN
driver := "sqlite3"
if strings.HasPrefix(url, "postgres") {
driver = "postgres"
}

return sql.Open(driver, url)
}

0 comments on commit 7b0d994

Please sign in to comment.