Skip to content

Commit

Permalink
Remove DSN related code
Browse files Browse the repository at this point in the history
We're only using sqlite on the client.
  • Loading branch information
Eric Larson authored and Eric Larson committed Dec 10, 2023
1 parent 38a6d1f commit 7943289
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 108 deletions.
57 changes: 0 additions & 57 deletions models/db.go
Expand Up @@ -2,9 +2,6 @@ package models

import (
"fmt"
"net/url"
"strconv"
"strings"
"time"

"github.com/glebarez/sqlite"
Expand Down Expand Up @@ -42,15 +39,6 @@ func (lc LocalConfig) String() {
fmt.Printf("LocalConfig:%+v\n", lc)
}

type DSN struct {
Host string
User string
Password string
DBName string
Port int
SSLMode string
}

func Connect(fn string) (*gorm.DB, error) {
db, err := gorm.Open(sqlite.Open(fn), &gorm.Config{})
if err != nil {
Expand All @@ -60,48 +48,3 @@ func Connect(fn string) (*gorm.DB, error) {

return db, err
}

func (dsn *DSN) String() string {
return fmt.Sprintf(
"host=%s user=%s password=%s dbname=%s port=%d sslmode=%s",
dsn.Host, dsn.User, dsn.Password, dsn.DBName, dsn.Port, dsn.SSLMode,
)
}

func NewDSN(dburi string) (*DSN, error) {
url, err := url.Parse(dburi)
if err != nil {
return nil, err
}

pw, _ := url.User.Password()
port := 5432
if url.Port() != "" {
port, err = strconv.Atoi(url.Port())
if err != nil {
return nil, err
}
}

path := strings.Split(strings.TrimLeft(url.Path, "/"), "/")

if len(path) == 0 {
return nil, fmt.Errorf("missing db name: %s", url.Path)
}

dbname := path[0]

sslmode := url.Query().Get("sslmode")
if sslmode == "" {
sslmode = "disable"
}

return &DSN{
Host: url.Hostname(),
User: url.User.Username(),
Password: pw,
DBName: dbname,
Port: port,
SSLMode: sslmode,
}, nil
}
51 changes: 0 additions & 51 deletions models/db_test.go

This file was deleted.

0 comments on commit 7943289

Please sign in to comment.