Skip to content

Commit

Permalink
feat: add sqlite3, sqlc and queries
Browse files Browse the repository at this point in the history
  • Loading branch information
flohansen committed Jun 12, 2024
1 parent 032e3a1 commit 5affec5
Show file tree
Hide file tree
Showing 16 changed files with 190 additions and 25 deletions.
11 changes: 10 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package main

import (
"database/sql"
"log"
"net/http"

"github.com/flohansen/dasher-server/internal/datastore"
"github.com/flohansen/dasher-server/internal/routes"
"github.com/pkg/errors"

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

func run() error {
log.Println("listening on port 3000")
featureStore := datastore.NewInMem()
db, err := sql.Open("sqlite3", "./dasher.db")
if err != nil {
return errors.Wrap(err, "sql open")
}

featureStore := datastore.NewSQLite(db)
return http.ListenAndServe(":3000", routes.New(featureStore))
}

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/mattn/go-sqlite3 v1.14.22
github.com/pkg/errors v0.9.1
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
Expand Down
16 changes: 9 additions & 7 deletions internal/datastore/repository.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package datastore

import (
"errors"
"context"

"github.com/flohansen/dasher-server/internal/model"
"github.com/flohansen/dasher-server/internal/sqlc"
)

type InMemDatastore struct {
type SQLiteDatastore struct {
q *sqlc.Queries
}

func NewInMem() *InMemDatastore {
return &InMemDatastore{}
func NewSQLite(db sqlc.DBTX) *SQLiteDatastore {
q := sqlc.New(db)
return &SQLiteDatastore{q}
}

func (repo *InMemDatastore) GetAll() ([]model.FeatureData, error) {
return nil, errors.New("not implemented")
func (repo *SQLiteDatastore) GetAll(ctx context.Context) ([]sqlc.Feature, error) {
return repo.q.GetAllFeatures(ctx)
}
4 changes: 0 additions & 4 deletions internal/model/feature.go

This file was deleted.

3 changes: 2 additions & 1 deletion internal/routes/features.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package routes

import (
"context"
"encoding/json"
"log"
"net/http"
)

func (routes *Routes) getFeatures(w http.ResponseWriter, r *http.Request) {
features, err := routes.featureStore.GetAll()
features, err := routes.featureStore.GetAll(context.Background())
if err != nil {
log.Printf("error while fetching all features: %v", err)
w.WriteHeader(http.StatusInternalServerError)
Expand Down
13 changes: 7 additions & 6 deletions internal/routes/mocks/routes.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions internal/routes/routes.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package routes

import (
"context"
"net/http"

"github.com/flohansen/dasher-server/internal/model"
"github.com/flohansen/dasher-server/internal/sqlc"
)

type FeatureStore interface {
GetAll() ([]model.FeatureData, error)
GetAll(ctx context.Context) ([]sqlc.Feature, error)
}

type Routes struct {
Expand Down
8 changes: 4 additions & 4 deletions internal/routes/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"net/http/httptest"
"testing"

"github.com/flohansen/dasher-server/internal/model"
"github.com/flohansen/dasher-server/internal/routes/mocks"
"github.com/flohansen/dasher-server/internal/sqlc"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
)
Expand All @@ -22,7 +22,7 @@ func TestRoutes(t *testing.T) {
t.Run("should return 500 INTERNAL SERVER ERROR", func(t *testing.T) {
// given
featureStore.EXPECT().
GetAll().
GetAll(gomock.Any()).
Return(nil, errors.New("some error"))

// when
Expand All @@ -37,8 +37,8 @@ func TestRoutes(t *testing.T) {
t.Run("should return 200 OK", func(t *testing.T) {
// given
featureStore.EXPECT().
GetAll().
Return([]model.FeatureData{}, nil)
GetAll(gomock.Any()).
Return([]sqlc.Feature{}, nil)

// when
w := httptest.NewRecorder()
Expand Down
31 changes: 31 additions & 0 deletions internal/sqlc/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions internal/sqlc/features.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions internal/sqlc/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions migrations/000001_create_features.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
begin;

drop table if exists features;

commit;
9 changes: 9 additions & 0 deletions migrations/000001_create_features.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
begin;

create table if not exists features (
feature_id text not null primary key,
description text,
enabled tinyint
);

commit;
13 changes: 13 additions & 0 deletions queries/features.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- name: GetAllFeatures :many
select * from features;

-- name: UpsertFeature :exec
insert into features (feature_id, description, enabled)
values (?, ?, ?)
on conflict (feature_id) do update set
description = excluded.description,
enabled = excluded.enabled
returning *;

-- name: DeleteFeature :exec
delete from features where feature_id = ?;
9 changes: 9 additions & 0 deletions sqlc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: "2"
sql:
- schema: "migrations"
queries: "queries"
engine: sqlite
gen:
go:
package: "sqlc"
out: "internal/sqlc"

0 comments on commit 5affec5

Please sign in to comment.