Skip to content

Commit

Permalink
✨ feat: add DSN to URL conversion for database migrations
Browse files Browse the repository at this point in the history
♻️ refactor: improve database connection handling with separate DSN parsing

📝 docs: add GitHub star link to index page
  • Loading branch information
watzon committed Nov 13, 2024
1 parent 84f0f17 commit 800c84d
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
74 changes: 68 additions & 6 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package database

import (
"fmt"
"strings"

"github.com/glebarez/sqlite"
"github.com/watzon/0x45/internal/config"
Expand Down Expand Up @@ -49,20 +50,81 @@ func (d *Database) Migrate(config *config.Config) error {
return fmt.Errorf("failed to get database instance: %w", err)
}

dsn := ""
switch config.Database.Driver {
case "postgres":
pgURL := fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s",
config.Database.User,
config.Database.Password,
dsn = fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=%s sslmode=%s",
config.Database.Host,
config.Database.Port,
config.Database.User,
config.Database.Password,
config.Database.Name,
config.Database.SSLMode,
)
return migrations.RunMigrations(pgURL)
case "sqlite":
return migrations.RunMigrations("sqlite://" + config.Database.Name)
dsn = config.Database.Name
}

return fmt.Errorf("unsupported database driver")
url, err := dsnToUrl(config.Database.Driver, dsn)
if err != nil {
return fmt.Errorf("failed to convert DSN to URL: %w", err)
}

return migrations.RunMigrations(url)
}

// DsnToUrl converts a database DSN to a URL format suitable for migrations
func dsnToUrl(driver string, dsn string) (string, error) {
switch driver {
case "postgres":
// Parse the DSN into components
params := make(map[string]string)
for _, pair := range strings.Split(dsn, " ") {
parts := strings.SplitN(pair, "=", 2)
if len(parts) == 2 {
params[parts[0]] = parts[1]
}
}

// Build auth part of URL (optional)
auth := ""
if user, ok := params["user"]; ok {
auth = user
if pass, ok := params["password"]; ok {
auth += ":" + pass
}
auth += "@"
}

// Construct the URL
host := params["host"]
if host == "" {
host = "localhost"
}
port := params["port"]
if port == "" {
port = "5432"
}
dbname := params["dbname"]
sslmode := params["sslmode"]
if sslmode == "" {
sslmode = "disable"
}

url := fmt.Sprintf("postgres://%s%s:%s/%s?sslmode=%s",
auth,
host,
port,
dbname,
sslmode,
)
return url, nil

case "sqlite":
return "sqlite://" + dsn, nil

default:
return "", fmt.Errorf("unsupported database driver: %s", driver)
}
}
4 changes: 4 additions & 0 deletions views/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<p>
Paste69 is an fast and open source pastebin, file bin, and link shortener written in Go.
</p>

<p>
<a href="https://github.com/watzon/0x45">Star the Project on GitHub ⭐</a>
</p>
</section>

<section>
Expand Down

0 comments on commit 800c84d

Please sign in to comment.