Skip to content

Commit 7d14bdd

Browse files
authored
all: add SQLite3 support (#21)
* 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
1 parent 7410c0e commit 7d14bdd

File tree

10 files changed

+72
-15
lines changed

10 files changed

+72
-15
lines changed

cmd/sips/sips.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,19 @@ func run(ctx context.Context) error {
2424
addr := flag.String("addr", ":8080", "address to serve HTTP on")
2525
api := flag.String("api", "http://127.0.0.1:5001", "IPFS API to contact")
2626
apitimeout := flag.Duration("apitimeout", 30*time.Second, "timeout for requests to the IPFS API")
27+
dbdriver := flag.String("dbdriver", "postgres", "database driver to use (\"list\" to show available)")
2728
rawdbpath := flag.String("db", "host=/var/run/postgresql dbname=sips", "path to database ($CONFIG will be replaced with user config dir path)")
2829
domigration := flag.Bool("migrate", true, "perform a database migration upon starting")
2930
flag.Parse()
3031

32+
if *dbdriver == "list" {
33+
fmt.Println("Available database drivers:")
34+
for _, t := range db.Drivers() {
35+
fmt.Printf(" %s\n", t)
36+
}
37+
return nil
38+
}
39+
3140
dbpath, configDirUsed, err := cli.ExpandConfig(*rawdbpath)
3241
if err != nil {
3342
return err
@@ -46,7 +55,7 @@ func run(ctx context.Context) error {
4655
return fmt.Errorf("create config directory: %w", err)
4756
}
4857
}
49-
entc, err := db.Open("postgres", dbpath)
58+
entc, err := db.Open(*dbdriver, dbpath)
5059
if err != nil {
5160
return fmt.Errorf("open database: %w", err)
5261
}

cmd/sipsctl/cmd/migrate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ func init() {
3636
defer bolt.Close()
3737

3838
log.Infof("opening ent database")
39-
entc, err := db.OpenAndMigrate(ctx, "postgres", rootFlags.DBPath)
39+
entc, err := db.OpenAndMigrate(ctx, rootFlags.DBDriver, rootFlags.DBPath)
4040
if err != nil {
41-
return fmt.Errorf("open PostgreSQL database: %w", err)
41+
return fmt.Errorf("open ent database: %w", err)
4242
}
4343
defer entc.Close()
4444

cmd/sipsctl/cmd/pins.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func init() {
3232
RunE: func(cmd *cobra.Command, args []string) error {
3333
ctx := cmd.Context()
3434

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

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

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

160-
entc, err := db.OpenAndMigrate(ctx, "postgres", rootFlags.DBPath)
160+
entc, err := db.OpenAndMigrate(ctx, rootFlags.DBDriver, rootFlags.DBPath)
161161
if err != nil {
162162
return fmt.Errorf("open database: %w", err)
163163
}

cmd/sipsctl/cmd/root.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@ package cmd
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67

8+
"github.com/DeedleFake/sips/db"
79
"github.com/DeedleFake/sips/internal/cli"
810
"github.com/spf13/cobra"
911
)
1012

13+
// errEarlyExit is a signal value to indicate that a command exited
14+
// early cleanly, despite returning an error. Cobra is weird.
15+
var errEarlyExit = errors.New("early exit")
16+
1117
var rootCmd = &cobra.Command{
1218
Use: "sipsctl",
1319
Short: "sipsctl is an admin utility for SIPS",
@@ -18,6 +24,14 @@ var rootCmd = &cobra.Command{
1824
return cmd.Help()
1925
},
2026
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
27+
if rootFlags.DBDriver == "list" {
28+
fmt.Println("Available database drivers:")
29+
for _, t := range db.Drivers() {
30+
fmt.Printf(" %s\n", t)
31+
}
32+
return errEarlyExit
33+
}
34+
2135
dbpath, _, err := cli.ExpandConfig(rootFlags.DBPath)
2236
if err != nil {
2337
return fmt.Errorf("expand database path: %w", err)
@@ -28,10 +42,17 @@ var rootCmd = &cobra.Command{
2842
}
2943

3044
var rootFlags struct {
31-
DBPath string
45+
DBDriver string
46+
DBPath string
3247
}
3348

3449
func init() {
50+
rootCmd.PersistentFlags().StringVar(
51+
&rootFlags.DBDriver,
52+
"dbdriver",
53+
"postgres",
54+
"database driver to use (\"list\" to show available)",
55+
)
3556
rootCmd.PersistentFlags().StringVar(
3657
&rootFlags.DBPath,
3758
"db",
@@ -48,5 +69,9 @@ func init() {
4869
}
4970

5071
func ExecuteContext(ctx context.Context) error {
51-
return rootCmd.ExecuteContext(ctx)
72+
err := rootCmd.ExecuteContext(ctx)
73+
if errors.Is(err, errEarlyExit) {
74+
return nil
75+
}
76+
return err
5277
}

cmd/sipsctl/cmd/tokens.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func init() {
3232
RunE: func(cmd *cobra.Command, args []string) error {
3333
ctx := cmd.Context()
3434

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

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

132-
entc, err := db.OpenAndMigrate(ctx, "postgres", rootFlags.DBPath)
132+
entc, err := db.OpenAndMigrate(ctx, rootFlags.DBDriver, rootFlags.DBPath)
133133
if err != nil {
134134
return fmt.Errorf("open database: %w", err)
135135
}

cmd/sipsctl/cmd/users.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func init() {
3030
RunE: func(cmd *cobra.Command, args []string) error {
3131
ctx := cmd.Context()
3232

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

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

107-
entc, err := db.OpenAndMigrate(ctx, "postgres", rootFlags.DBPath)
107+
entc, err := db.OpenAndMigrate(ctx, rootFlags.DBDriver, rootFlags.DBPath)
108108
if err != nil {
109109
return fmt.Errorf("open database: %w", err)
110110
}

db/db.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,11 @@ func OpenAndMigrate(ctx context.Context, driver, source string, opts ...ent.Opti
3030
func Open(driver, source string, opts ...ent.Option) (*ent.Client, error) {
3131
return ent.Open(driver, source, opts...)
3232
}
33+
34+
var drivers = []string{"postgres"}
35+
36+
// Drivers returns the list of supported drivers. This list is a
37+
// subset of the list available from database/sql.Drivers().
38+
func Drivers() []string {
39+
return append([]string(nil), drivers...)
40+
}

db/db_sqlite3.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//go:build sqlite3
2+
// +build sqlite3
3+
4+
package db
5+
6+
import (
7+
_ "github.com/mattn/go-sqlite3"
8+
)
9+
10+
func init() {
11+
drivers = append(drivers, "sqlite3")
12+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/asdine/storm v2.1.2+incompatible
88
github.com/gorilla/mux v1.8.0
99
github.com/lib/pq v1.10.3
10+
github.com/mattn/go-sqlite3 v1.14.9
1011
github.com/spf13/cobra v1.2.1
1112
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359
1213
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m
239239
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
240240
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
241241
github.com/mattn/go-sqlite3 v1.14.8/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
242+
github.com/mattn/go-sqlite3 v1.14.9 h1:10HX2Td0ocZpYEjhilsuo6WWtUqttj2Kb0KtD86/KYA=
243+
github.com/mattn/go-sqlite3 v1.14.9/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
242244
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
243245
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
244246
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=

0 commit comments

Comments
 (0)