Skip to content

Commit

Permalink
all: add SQLite3 support (#21)
Browse files Browse the repository at this point in the history
* db: add SQLite3 support

* cmd/sips: add ability to pick database drivers

* cmd/sipsctl: add the ability to pick the database driver

* cmd/sipsctl: don't specify that a database that might not be Postgres is
  • Loading branch information
DeedleFake authored Oct 27, 2021
1 parent 7410c0e commit 7d14bdd
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 15 deletions.
11 changes: 10 additions & 1 deletion cmd/sips/sips.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,19 @@ func run(ctx context.Context) error {
addr := flag.String("addr", ":8080", "address to serve HTTP on")
api := flag.String("api", "http://127.0.0.1:5001", "IPFS API to contact")
apitimeout := flag.Duration("apitimeout", 30*time.Second, "timeout for requests to the IPFS API")
dbdriver := flag.String("dbdriver", "postgres", "database driver to use (\"list\" to show available)")
rawdbpath := flag.String("db", "host=/var/run/postgresql dbname=sips", "path to database ($CONFIG will be replaced with user config dir path)")
domigration := flag.Bool("migrate", true, "perform a database migration upon starting")
flag.Parse()

if *dbdriver == "list" {
fmt.Println("Available database drivers:")
for _, t := range db.Drivers() {
fmt.Printf(" %s\n", t)
}
return nil
}

dbpath, configDirUsed, err := cli.ExpandConfig(*rawdbpath)
if err != nil {
return err
Expand All @@ -46,7 +55,7 @@ func run(ctx context.Context) error {
return fmt.Errorf("create config directory: %w", err)
}
}
entc, err := db.Open("postgres", dbpath)
entc, err := db.Open(*dbdriver, dbpath)
if err != nil {
return fmt.Errorf("open database: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/sipsctl/cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ func init() {
defer bolt.Close()

log.Infof("opening ent database")
entc, err := db.OpenAndMigrate(ctx, "postgres", rootFlags.DBPath)
entc, err := db.OpenAndMigrate(ctx, rootFlags.DBDriver, rootFlags.DBPath)
if err != nil {
return fmt.Errorf("open PostgreSQL database: %w", err)
return fmt.Errorf("open ent database: %w", err)
}
defer entc.Close()

Expand Down
8 changes: 4 additions & 4 deletions cmd/sipsctl/cmd/pins.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func init() {
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

entc, err := db.OpenAndMigrate(ctx, "postgres", rootFlags.DBPath)
entc, err := db.OpenAndMigrate(ctx, rootFlags.DBDriver, rootFlags.DBPath)
if err != nil {
return fmt.Errorf("open database: %w", err)
}
Expand Down Expand Up @@ -81,7 +81,7 @@ func init() {
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

entc, err := db.OpenAndMigrate(ctx, "postgres", rootFlags.DBPath)
entc, err := db.OpenAndMigrate(ctx, rootFlags.DBDriver, rootFlags.DBPath)
if err != nil {
return fmt.Errorf("open database: %w", err)
}
Expand Down Expand Up @@ -116,7 +116,7 @@ func init() {
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

entc, err := db.OpenAndMigrate(ctx, "postgres", rootFlags.DBPath)
entc, err := db.OpenAndMigrate(ctx, rootFlags.DBDriver, rootFlags.DBPath)
if err != nil {
return fmt.Errorf("open database: %w", err)
}
Expand Down Expand Up @@ -157,7 +157,7 @@ func init() {
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

entc, err := db.OpenAndMigrate(ctx, "postgres", rootFlags.DBPath)
entc, err := db.OpenAndMigrate(ctx, rootFlags.DBDriver, rootFlags.DBPath)
if err != nil {
return fmt.Errorf("open database: %w", err)
}
Expand Down
29 changes: 27 additions & 2 deletions cmd/sipsctl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ package cmd

import (
"context"
"errors"
"fmt"

"github.com/DeedleFake/sips/db"
"github.com/DeedleFake/sips/internal/cli"
"github.com/spf13/cobra"
)

// errEarlyExit is a signal value to indicate that a command exited
// early cleanly, despite returning an error. Cobra is weird.
var errEarlyExit = errors.New("early exit")

var rootCmd = &cobra.Command{
Use: "sipsctl",
Short: "sipsctl is an admin utility for SIPS",
Expand All @@ -18,6 +24,14 @@ var rootCmd = &cobra.Command{
return cmd.Help()
},
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if rootFlags.DBDriver == "list" {
fmt.Println("Available database drivers:")
for _, t := range db.Drivers() {
fmt.Printf(" %s\n", t)
}
return errEarlyExit
}

dbpath, _, err := cli.ExpandConfig(rootFlags.DBPath)
if err != nil {
return fmt.Errorf("expand database path: %w", err)
Expand All @@ -28,10 +42,17 @@ var rootCmd = &cobra.Command{
}

var rootFlags struct {
DBPath string
DBDriver string
DBPath string
}

func init() {
rootCmd.PersistentFlags().StringVar(
&rootFlags.DBDriver,
"dbdriver",
"postgres",
"database driver to use (\"list\" to show available)",
)
rootCmd.PersistentFlags().StringVar(
&rootFlags.DBPath,
"db",
Expand All @@ -48,5 +69,9 @@ func init() {
}

func ExecuteContext(ctx context.Context) error {
return rootCmd.ExecuteContext(ctx)
err := rootCmd.ExecuteContext(ctx)
if errors.Is(err, errEarlyExit) {
return nil
}
return err
}
6 changes: 3 additions & 3 deletions cmd/sipsctl/cmd/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func init() {
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

entc, err := db.OpenAndMigrate(ctx, "postgres", rootFlags.DBPath)
entc, err := db.OpenAndMigrate(ctx, rootFlags.DBDriver, rootFlags.DBPath)
if err != nil {
return fmt.Errorf("open database: %w", err)
}
Expand Down Expand Up @@ -84,7 +84,7 @@ func init() {
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

entc, err := db.OpenAndMigrate(ctx, "postgres", rootFlags.DBPath)
entc, err := db.OpenAndMigrate(ctx, rootFlags.DBDriver, rootFlags.DBPath)
if err != nil {
return fmt.Errorf("open database: %w", err)
}
Expand Down Expand Up @@ -129,7 +129,7 @@ func init() {
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

entc, err := db.OpenAndMigrate(ctx, "postgres", rootFlags.DBPath)
entc, err := db.OpenAndMigrate(ctx, rootFlags.DBDriver, rootFlags.DBPath)
if err != nil {
return fmt.Errorf("open database: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/sipsctl/cmd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func init() {
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

entc, err := db.OpenAndMigrate(ctx, "postgres", rootFlags.DBPath)
entc, err := db.OpenAndMigrate(ctx, rootFlags.DBDriver, rootFlags.DBPath)
if err != nil {
return fmt.Errorf("open database: %w", err)
}
Expand Down Expand Up @@ -72,7 +72,7 @@ func init() {
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

entc, err := db.OpenAndMigrate(ctx, "postgres", rootFlags.DBPath)
entc, err := db.OpenAndMigrate(ctx, rootFlags.DBDriver, rootFlags.DBPath)
if err != nil {
return fmt.Errorf("open database: %w", err)
}
Expand Down Expand Up @@ -104,7 +104,7 @@ func init() {
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

entc, err := db.OpenAndMigrate(ctx, "postgres", rootFlags.DBPath)
entc, err := db.OpenAndMigrate(ctx, rootFlags.DBDriver, rootFlags.DBPath)
if err != nil {
return fmt.Errorf("open database: %w", err)
}
Expand Down
8 changes: 8 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,11 @@ func OpenAndMigrate(ctx context.Context, driver, source string, opts ...ent.Opti
func Open(driver, source string, opts ...ent.Option) (*ent.Client, error) {
return ent.Open(driver, source, opts...)
}

var drivers = []string{"postgres"}

// Drivers returns the list of supported drivers. This list is a
// subset of the list available from database/sql.Drivers().
func Drivers() []string {
return append([]string(nil), drivers...)
}
12 changes: 12 additions & 0 deletions db/db_sqlite3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//go:build sqlite3
// +build sqlite3

package db

import (
_ "github.com/mattn/go-sqlite3"
)

func init() {
drivers = append(drivers, "sqlite3")
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/asdine/storm v2.1.2+incompatible
github.com/gorilla/mux v1.8.0
github.com/lib/pq v1.10.3
github.com/mattn/go-sqlite3 v1.14.9
github.com/spf13/cobra v1.2.1
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-sqlite3 v1.14.8/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v1.14.9 h1:10HX2Td0ocZpYEjhilsuo6WWtUqttj2Kb0KtD86/KYA=
github.com/mattn/go-sqlite3 v1.14.9/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
Expand Down

0 comments on commit 7d14bdd

Please sign in to comment.