Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 115 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ permissions:
contents: read

jobs:
build:
dependencies:
name: Verify dependencies
runs-on: ubuntu-latest

steps:
Expand All @@ -20,14 +21,122 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v7
with:
go-version: "1.27.x"
go-version: '1.27.x'
check-latest: true
cache-dependency-path: |
go.sum
tools/go.sum

- name: Go version
run: go version
- name: Download modules
run: go mod download

- name: Verify
run: make verify
- name: Verify modules
run: go mod verify

generated:
name: Verify generated code
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v7

- name: Setup Go
uses: actions/setup-go@v7
with:
go-version: '1.27.x'
check-latest: true
cache-dependency-path: |
go.sum
tools/go.sum

- name: Verify generated code
run: make normalizers

lint:
name: Lint
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v7

- name: Setup Go
uses: actions/setup-go@v7
with:
go-version: '1.27.x'
check-latest: true
cache-dependency-path: |
go.sum
tools/go.sum

- name: Lint
uses: golangci/golangci-lint-action@v9
with:
version: v2.15.0
args: --timeout=10m

vulncheck:
name: Check vulnerabilities
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v7

- name: Setup Go
uses: actions/setup-go@v7
with:
go-version: '1.27.x'
check-latest: true
cache-dependency-path: |
go.sum
tools/go.sum

- name: Check vulnerabilities
run: make vulncheck

test:
name: Test (${{ matrix.os }})
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]

steps:
- name: Checkout
uses: actions/checkout@v7

- name: Setup Go
uses: actions/setup-go@v7
with:
go-version: '1.27.x'
check-latest: true
cache-dependency-path: |
go.sum
tools/go.sum

- name: Test
run: go test ./... -count=1 -timeout=600s

race:
name: Race detector
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v7

- name: Setup Go
uses: actions/setup-go@v7
with:
go-version: '1.27.x'
check-latest: true
cache-dependency-path: |
go.sum
tools/go.sum

- name: Race tests
run: go test -race ./... -count=1 -timeout=600s
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ROOT_PATH := $(dir $(realpath $(lastword $(MAKEFILE_LIST))))
COVERAGE_PATH := $(ROOT_PATH).coverage/
GENERATED_PATHS := sqlite/internal/sqlc/ sqlite/testsuiteexecution_normalizers.gen.go

# Use the locally installed Go toolchain.
export GOTOOLCHAIN=local
Expand All @@ -18,6 +19,8 @@ sqlc: install-sqlc
normalizers: sqlc
@echo "Generating normalizers..."
@cd sqlite && go generate
@git diff HEAD --exit-code -- $(GENERATED_PATHS)
@test -z "$$(git ls-files --others --exclude-standard -- $(GENERATED_PATHS))"

.PHONY: lint
lint: install-golangci-lint
Expand Down
1 change: 1 addition & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package errors
import "github.com/ygrebnov/errorc"

var (
ErrUnsupportedDBDriver = errorc.New("unsupported database driver")
ErrCannotOpenDBConnection = errorc.New("cannot open db connection")
ErrCannotConfigureDB = errorc.New("cannot configure database")
ErrCannotMigrateDB = errorc.New("cannot migrate database")
Expand Down
11 changes: 6 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ module github.com/e2engine/repository
go 1.27

require (
github.com/e2engine/core v0.3.2
github.com/e2engine/core v0.3.3
github.com/golang-migrate/migrate/v4 v4.20.1
github.com/ygrebnov/errorc v0.6.1
github.com/ygrebnov/errorc v0.7.0
github.com/ygrebnov/keys v0.2.0
github.com/ygrebnov/model v0.8.1
github.com/ygrebnov/model v0.9.0
modernc.org/sqlite v1.62.0
)

Expand All @@ -18,12 +18,13 @@ require (
github.com/mattn/go-isatty v0.0.24 // indirect
github.com/ncruces/go-strftime v1.1.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/ygrebnov/log v0.5.1 // indirect
github.com/ygrebnov/fs v0.1.0 // indirect
github.com/ygrebnov/log v0.6.0 // indirect
golang.org/x/net v0.63.0 // indirect
golang.org/x/sync v0.25.0 // indirect
golang.org/x/sys v0.50.0 // indirect
golang.org/x/text v0.44.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20261118221215-c8830f628b3f // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20261123191846-1e707ab9fbc2 // indirect
google.golang.org/grpc v1.85.0 // indirect
google.golang.org/protobuf v1.36.12 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
22 changes: 12 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.1.0 h1:dbKTrvD0klcbBV/h4AWJdMuZogJACoMlvWIWZ5b2xWg=
github.com/dustin/go-humanize v1.1.0/go.mod h1:hc1CvRkJMsgxqjmjMQF3QNRAZBwY8AXBAzKYoSX9sFI=
github.com/e2engine/core v0.3.2 h1:6CUOMzVfwLlSS+O1uzBYXjVyb45THfSXy8nqyO3edfY=
github.com/e2engine/core v0.3.2/go.mod h1:pQTg3FaajaO4OKt3exuEpSasr1BoS5JcBNgW+2FK+o8=
github.com/e2engine/core v0.3.3 h1:uoh7xn6786eurWwtJajq7meoClhOyV/49k/OEZy79do=
github.com/e2engine/core v0.3.3/go.mod h1:WzxdDsUl8qH/xk7WcPymatQqdZCwR6LaXgxIEIC6tV4=
github.com/golang-migrate/migrate/v4 v4.20.1 h1:2N/ToVTKrKl58ynBpgeVJ4In7VcLCjWTZtm4eP1LxhU=
github.com/golang-migrate/migrate/v4 v4.20.1/go.mod h1:DDPgKVb4ovSWc4FwSPfV2Uz1160f4XBiTHTrAJtljmM=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
Expand All @@ -30,14 +30,16 @@ github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/ygrebnov/errorc v0.6.1 h1:ICww1JOesR0HyE3cVWMr0SBmB8RV1XSmGwB5yVP1SqE=
github.com/ygrebnov/errorc v0.6.1/go.mod h1:akitMzADmnm0i1X5fSJfBqiTqCzckka9sgukTRQN5ns=
github.com/ygrebnov/errorc v0.7.0 h1:UBk753h/SNWfX9W6Byp+zckIdybXJ/ixc+VWukpQfdQ=
github.com/ygrebnov/errorc v0.7.0/go.mod h1:akitMzADmnm0i1X5fSJfBqiTqCzckka9sgukTRQN5ns=
github.com/ygrebnov/fs v0.1.0 h1:cswbccHB+YuluFYwes02NigqSjP3ZgM5stOvW+MxjMo=
github.com/ygrebnov/fs v0.1.0/go.mod h1:c3BP57rdNpeWJBW9a45dvsfbV6Yydxer3cEbSzSW2I8=
github.com/ygrebnov/keys v0.2.0 h1:KQSQG1la9WkTW59WbB3CK1yhxZ92el2ZJfV1XtlHTp0=
github.com/ygrebnov/keys v0.2.0/go.mod h1:4IfRPgv7tFSlToKzHtA9MPMwXP0+HRJ0Kk8XSrvhDvQ=
github.com/ygrebnov/log v0.5.1 h1:+Kkr6sjOHcYRvzLEDvEsRdDcQPRy0YUtmgBCNdio7Fk=
github.com/ygrebnov/log v0.5.1/go.mod h1:Y21zKtQ3rEy1FDhSli2NvgaqVyig3hhUfxegjLPuknw=
github.com/ygrebnov/model v0.8.1 h1:KvnS97b/q3Z1GKSHSDYlfHoL3bNDR+ZT9Kf4yc1WnIE=
github.com/ygrebnov/model v0.8.1/go.mod h1:WEMMqdGYF45Kfce0pAe0poiF9hu+PONRSHd2L9li3tw=
github.com/ygrebnov/log v0.6.0 h1:BwIDPh3uE/kCTRV0rsSp7i8NDv0ayfYOJRrypXhLdOI=
github.com/ygrebnov/log v0.6.0/go.mod h1:ks5SJPRtemWcKFpXwi9pMxJ92cMWjqnmNQa2QXiXd+4=
github.com/ygrebnov/model v0.9.0 h1:G6QaYVcm9Nqyed/lPzkWfQ543ol9D0C8Yjz9gVUDy9Q=
github.com/ygrebnov/model v0.9.0/go.mod h1:KA8zospuMg3/LbNc+Lgh+8nEz1uR9nv8SN4Vxxt2ruM=
go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y=
go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU=
golang.org/x/mod v0.42.0 h1:ICeL3t2L30ljSPHHxDnR4fqVcBt/zR2zJAb9juavmQg=
Expand All @@ -54,8 +56,8 @@ golang.org/x/tools v0.52.0 h1:ryxg4U63oaUYk7uOLTF37n43f4qgtGvqV4Ioit1BaEs=
golang.org/x/tools v0.52.0/go.mod h1:ItcP5UNaVGG+Yc4eIYmaFLNJ4soEopkx5Y2d4lcEurs=
gonum.org/v1/gonum v0.17.0 h1:VbpOemQlsSMrYmn7T2OUvQ4dqxQXU+ouZFQsZOx50z4=
gonum.org/v1/gonum v0.17.0/go.mod h1:El3tOrEuMpv2UdMrbNlKEh9vd86bmQ6vqIcDwxEOc1E=
google.golang.org/genproto/googleapis/rpc v0.0.0-20261118221215-c8830f628b3f h1:aDW6Vb2T1K0xbAgW+ED8iz39WlgjjX/OKiNh6tXv/+g=
google.golang.org/genproto/googleapis/rpc v0.0.0-20261118221215-c8830f628b3f/go.mod h1:OaIUM3+LpYcK2GXM4FTmhWoIq371Owdr+Cc7/BsYHHc=
google.golang.org/genproto/googleapis/rpc v0.0.0-20261123191846-1e707ab9fbc2 h1:FCWn4wv5of/ooWpbWDIeOXqI3+h2MGnV7SvvcJ8f+bQ=
google.golang.org/genproto/googleapis/rpc v0.0.0-20261123191846-1e707ab9fbc2/go.mod h1:OaIUM3+LpYcK2GXM4FTmhWoIq371Owdr+Cc7/BsYHHc=
google.golang.org/grpc v1.85.0 h1:BPBCfuVRQ1qvBhKDOq9uvQllMvO59rKsYhXpp7Dd8vw=
google.golang.org/grpc v1.85.0/go.mod h1:0rQrOD58yjal/0EaVbGBANindixsCZNQPDV3VKtaKuQ=
google.golang.org/protobuf v1.36.12 h1:pJOKDDOyeXErUroCihFAd5LQuwXBSpVnKGrj5o/fwxc=
Expand Down
1 change: 1 addition & 0 deletions keys/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keys
import "github.com/ygrebnov/keys"

var (
DBDriver = keys.New("db.driver")
JournalMode = keys.New("journal.mode")
Filename = keys.New("filename")
)
25 changes: 14 additions & 11 deletions sqlite/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"database/sql"
nativeerrors "errors"
"os"
"path/filepath"
"strings"

coreapi "github.com/e2engine/core/api"
Expand All @@ -19,27 +18,26 @@ import (
"github.com/e2engine/repository/sqlite/migrate"
)

// Open opens a SQLite database connection, configures the connection pool, and runs migrations.
// Open opens a SQLite database connection, configures the connection pool,
// and runs migrations. The caller must prepare the database directory first.
func Open(ctx context.Context, path string, cfg *config.Config) (*sql.DB, error) {
dbConn, err := openDB(path, cfg)
if err != nil {
return nil, nativeerrors.Join(errors.ErrCannotOpenDBConnection, err)
return nil, errorc.WithCause(errors.ErrCannotOpenDBConnection, err)
}

configureDB(dbConn, cfg)

if errConfigure := configureSQLite(ctx, dbConn); errConfigure != nil {
return nil, nativeerrors.Join(
errors.ErrCannotConfigureDB,
errConfigure,
errorc.WithCause(errors.ErrCannotConfigureDB, errConfigure),
dbConn.Close(),
)
}

if err := migrate.RunDBMigration(ctx, dbConn); err != nil {
if errRun := migrate.RunDBMigration(ctx, dbConn); errRun != nil {
return nil, nativeerrors.Join(
errors.ErrCannotMigrateDB,
err,
errorc.WithCause(errors.ErrCannotMigrateDB, errRun),
dbConn.Close(),
)
}
Expand All @@ -54,10 +52,15 @@ func openDB(path string, cfg *config.Config) (*sql.DB, error) {
if strings.ContainsRune(path, '?') || strings.HasPrefix(path, "file:") {
return nil, errors.ErrUnsafeDBPath
}

if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
return nil, err
if cfg.Driver != "sqlite" {
return nil, errorc.With(
errors.ErrUnsupportedDBDriver,
errorc.String(keys.DBDriver, cfg.Driver),
)
}

// Reject an existing non-regular path before opening the connection pool.
// This check does not prevent changes to the path after it returns.
if info, err := os.Lstat(path); err == nil {
if !info.Mode().IsRegular() {
return nil, errors.ErrUnsafeDBPath
Expand Down
66 changes: 60 additions & 6 deletions sqlite/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"strings"
"testing"

corekeys "github.com/e2engine/core/pkg/keys"
"github.com/ygrebnov/errorc"

repositoryerrors "github.com/e2engine/repository/errors"
"github.com/e2engine/repository/sqlite/config"
)
Expand Down Expand Up @@ -77,12 +80,6 @@ func TestOpenDB(t *testing.T) {
return filepath.Join(dir, "test.sqlite")
},
},
{
name: "database in nested directory",
path: func(dir string) string {
return filepath.Join(dir, "nested", "data", "test.sqlite")
},
},
}

for _, testCase := range testCases {
Expand Down Expand Up @@ -148,6 +145,63 @@ func TestOpenDB(t *testing.T) {
}
}

func TestOpenRequiresExistingDirectory(t *testing.T) {
dir := filepath.Join(t.TempDir(), "missing")
path := filepath.Join(dir, "test.sqlite")
db, err := Open(t.Context(), path, &config.Config{
Driver: "sqlite",
MaxOpenConns: 1,
MaxIdleConns: 1,
})
if db != nil {
_ = db.Close()
t.Fatal("opened database without a prepared directory")
}
if err == nil {
t.Fatal("expected an error for a missing database directory")
}
if _, statErr := os.Stat(dir); !errors.Is(statErr, os.ErrNotExist) {
t.Fatalf("database directory was created: %v", statErr)
}
}

func TestOpenPreservesDriverCause(t *testing.T) {
_, err := Open(
t.Context(),
filepath.Join(t.TempDir(), "test.sqlite"),
&config.Config{Driver: "unsupported"},
)
if !errors.Is(err, repositoryerrors.ErrCannotOpenDBConnection) ||
!errors.Is(err, repositoryerrors.ErrUnsupportedDBDriver) {
t.Fatalf("Open() error = %v, want both database and driver errors", err)
}

cause, ok := errorc.Lookup[error](err, corekeys.Cause)
if !ok || !errors.Is(cause, repositoryerrors.ErrUnsupportedDBDriver) {
t.Fatalf("Open() cause = %v, want unsupported driver error", cause)
}
}

func TestOpenPreservesConfigurationCause(t *testing.T) {
ctx, cancel := context.WithCancel(t.Context())
cancel()

_, err := Open(
ctx,
filepath.Join(t.TempDir(), "test.sqlite"),
&config.Config{Driver: "sqlite", MaxOpenConns: 1, MaxIdleConns: 1},
)
if !errors.Is(err, repositoryerrors.ErrCannotConfigureDB) ||
!errors.Is(err, context.Canceled) {
t.Fatalf("Open() error = %v, want both configuration and cancellation errors", err)
}

cause, ok := errorc.Lookup[error](err, corekeys.Cause)
if !ok || !errors.Is(cause, context.Canceled) {
t.Fatalf("Open() cause = %v, want context cancellation", cause)
}
}

func TestConfigureDB(t *testing.T) {
testCases := []struct {
name string
Expand Down
Loading
Loading