Skip to content

Commit 5a99b03

Browse files
committed
feat: setup database and run migrations
- Add sqlite3 driver and goose for migrations - Create collections table - Generate SQL queries for collections - Add .gitignore file
0 parents  commit 5a99b03

10 files changed

Lines changed: 266 additions & 0 deletions

File tree

.gitignore

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# If you prefer the allow list template instead of the deny list, see community template:
2+
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
3+
#
4+
# Binaries for programs and plugins
5+
*.exe
6+
*.exe~
7+
*.dll
8+
*.so
9+
*.dylib
10+
11+
# Test binary, built with `go test -c`
12+
*.test
13+
14+
# Code coverage profiles and other test artifacts
15+
*.out
16+
coverage.*
17+
*.coverprofile
18+
profile.cov
19+
20+
# Go workspace file
21+
go.work
22+
go.work.sum
23+
24+
# env file
25+
.env
26+
27+
# Editor/IDE
28+
.vscode/
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- +goose Up
2+
CREATE TABLE collections (
3+
id INTEGER PRIMARY KEY AUTOINCREMENT,
4+
name TEXT NOT NULL,
5+
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
6+
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
7+
);
8+
9+
-- +goose Down
10+
DROP TABLE collections;

db/queries/collections.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- name: CreateCollection :one
2+
INSERT INTO collections (name) VALUES (?) RETURNING *;

go.mod

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module github.com/maniac-en/req
2+
3+
go 1.24.4
4+
5+
require (
6+
github.com/mattn/go-sqlite3 v1.14.29
7+
github.com/pressly/goose/v3 v3.24.3
8+
)
9+
10+
require (
11+
github.com/mfridman/interpolate v0.0.2 // indirect
12+
github.com/sethvargo/go-retry v0.3.0 // indirect
13+
go.uber.org/multierr v1.11.0 // indirect
14+
golang.org/x/sync v0.14.0 // indirect
15+
)

go.sum

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
4+
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
5+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
6+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
7+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
8+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
9+
github.com/mattn/go-sqlite3 v1.14.29 h1:1O6nRLJKvsi1H2Sj0Hzdfojwt8GiGKm+LOfLaBFaouQ=
10+
github.com/mattn/go-sqlite3 v1.14.29/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
11+
github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6BbAxPY=
12+
github.com/mfridman/interpolate v0.0.2/go.mod h1:p+7uk6oE07mpE/Ik1b8EckO0O4ZXiGAfshKBWLUM9Xg=
13+
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
14+
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
15+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
16+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
17+
github.com/pressly/goose/v3 v3.24.3 h1:DSWWNwwggVUsYZ0X2VitiAa9sKuqtBfe+Jr9zFGwWlM=
18+
github.com/pressly/goose/v3 v3.24.3/go.mod h1:v9zYL4xdViLHCUUJh/mhjnm6JrK7Eul8AS93IxiZM4E=
19+
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
20+
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
21+
github.com/sethvargo/go-retry v0.3.0 h1:EEt31A35QhrcRZtrYFDTBg91cqZVnFL2navjDrah2SE=
22+
github.com/sethvargo/go-retry v0.3.0/go.mod h1:mNX17F0C/HguQMyMyJxcnU471gOZGxCLyYaFyAZraas=
23+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
24+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
25+
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
26+
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
27+
golang.org/x/exp v0.0.0-20250506013437-ce4c2cf36ca6 h1:y5zboxd6LQAqYIhHnB48p0ByQ/GnQx2BE33L8BOHQkI=
28+
golang.org/x/exp v0.0.0-20250506013437-ce4c2cf36ca6/go.mod h1:U6Lno4MTRCDY+Ba7aCcauB9T60gsv5s4ralQzP72ZoQ=
29+
golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ=
30+
golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
31+
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
32+
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
33+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
34+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
35+
modernc.org/libc v1.65.0 h1:e183gLDnAp9VJh6gWKdTy0CThL9Pt7MfcR/0bgb7Y1Y=
36+
modernc.org/libc v1.65.0/go.mod h1:7m9VzGq7APssBTydds2zBcxGREwvIGpuUBaKTXdm2Qs=
37+
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
38+
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
39+
modernc.org/memory v1.10.0 h1:fzumd51yQ1DxcOxSO+S6X7+QTuVU+n8/Aj7swYjFfC4=
40+
modernc.org/memory v1.10.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
41+
modernc.org/sqlite v1.37.0 h1:s1TMe7T3Q3ovQiK2Ouz4Jwh7dw4ZDqbebSDTlSJdfjI=
42+
modernc.org/sqlite v1.37.0/go.mod h1:5YiWv+YviqGMuGw4V+PNplcyaJ5v+vQd7TQOgkACoJM=

internal/database/collections.sql.go

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/database/db.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/database/models.go

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

main.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"database/sql"
6+
"embed"
7+
"fmt"
8+
"io/fs"
9+
"log"
10+
"os"
11+
"path/filepath"
12+
13+
"github.com/maniac-en/req/internal/database"
14+
_ "github.com/mattn/go-sqlite3"
15+
"github.com/pressly/goose/v3"
16+
)
17+
18+
// Embed migration files into the binary
19+
//
20+
//go:embed db/migrations/*.sql
21+
var migrationsFS embed.FS
22+
23+
var (
24+
USERHOMEDIR string
25+
DBDIR string
26+
DBPATH string
27+
DB *sql.DB
28+
)
29+
30+
type Config struct {
31+
DB *database.Queries
32+
}
33+
34+
func init() {
35+
// setup DB path based on user's home path
36+
userHomeDir, err := os.UserHomeDir()
37+
if err != nil {
38+
log.Fatal("error reading user's home path")
39+
}
40+
USERHOMEDIR = userHomeDir
41+
DBDIR = filepath.Join(USERHOMEDIR, ".cache", "req")
42+
if err := os.MkdirAll(DBDIR, 0o755); err != nil {
43+
log.Fatal("error creating a database directory")
44+
}
45+
DBPATH = filepath.Join(DBDIR, "app.db")
46+
47+
// Run migrations
48+
if err := runMigrations(); err != nil {
49+
log.Fatalf("error running migrations: %v", err)
50+
}
51+
}
52+
53+
func runMigrations() error {
54+
// Connect to database
55+
db, err := sql.Open("sqlite3", DBPATH)
56+
if err != nil {
57+
return fmt.Errorf("error opening database: %w", err)
58+
}
59+
60+
// Create a sub-filesystem for just the migrations directory
61+
migrationSubFS, err := fs.Sub(migrationsFS, filepath.Join("db", "migrations"))
62+
if err != nil {
63+
return fmt.Errorf("error creating sub-filesystem: %w", err)
64+
}
65+
66+
// Create goose provider with the embedded filesystem
67+
gooseProvider, err := goose.NewProvider("sqlite3", db, migrationSubFS)
68+
if err != nil {
69+
return fmt.Errorf("error creating goose provider: %w", err)
70+
}
71+
72+
// Run migrations
73+
_, err = gooseProvider.Up(context.Background())
74+
if err != nil {
75+
return fmt.Errorf("error running migrations: %w", err)
76+
}
77+
DB = db
78+
return nil
79+
}
80+
81+
func main() {
82+
db := database.New(DB)
83+
cfg := Config{
84+
DB: db,
85+
}
86+
_, err := cfg.DB.CreateCollection(context.Background(), "testing")
87+
if err != nil {
88+
log.Fatal(err)
89+
}
90+
}

sqlc.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: "2"
2+
sql:
3+
- schema: "db/migrations"
4+
queries: "db/queries"
5+
engine: "sqlite"
6+
gen:
7+
go:
8+
out: "internal/database"
9+
emit_json_tags: true
10+
emit_db_tags: true

0 commit comments

Comments
 (0)