diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 53bb644..a58f801 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -10,7 +10,8 @@ permissions: contents: read jobs: - build: + dependencies: + name: Verify dependencies runs-on: ubuntu-latest steps: @@ -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 diff --git a/Makefile b/Makefile index c30afd7..aafbf4f 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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 diff --git a/errors/errors.go b/errors/errors.go index ede3cb2..81df6ae 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -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") diff --git a/go.mod b/go.mod index 75ef733..b625749 100644 --- a/go.mod +++ b/go.mod @@ -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 ) @@ -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 diff --git a/go.sum b/go.sum index 5eab45f..0521b06 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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= diff --git a/keys/keys.go b/keys/keys.go index cd20582..a48909c 100644 --- a/keys/keys.go +++ b/keys/keys.go @@ -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") ) diff --git a/sqlite/db.go b/sqlite/db.go index 0610eee..cb8b118 100644 --- a/sqlite/db.go +++ b/sqlite/db.go @@ -5,7 +5,6 @@ import ( "database/sql" nativeerrors "errors" "os" - "path/filepath" "strings" coreapi "github.com/e2engine/core/api" @@ -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(), ) } @@ -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 diff --git a/sqlite/db_test.go b/sqlite/db_test.go index 5cfc276..bd7d70c 100644 --- a/sqlite/db_test.go +++ b/sqlite/db_test.go @@ -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" ) @@ -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 { @@ -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 diff --git a/tests/main_test.go b/tests/main_test.go index d1dd4d0..f3ebdf3 100644 --- a/tests/main_test.go +++ b/tests/main_test.go @@ -31,6 +31,11 @@ func getRepositories(t *testing.T) coreapi.Repositories { if err != nil { t.Fatalf("failed to open database: %v", err) } + t.Cleanup(func() { + if err := dbConn.Close(); err != nil { + t.Errorf("failed to close database: %v", err) + } + }) return sqlite.NewRepositories(dbConn) } diff --git a/tools/go.mod b/tools/go.mod index 1ec1207..cab3e37 100644 --- a/tools/go.mod +++ b/tools/go.mod @@ -2,10 +2,10 @@ module tools go 1.27 -require ( - github.com/golangci/golangci-lint/v2 v2.15.0 - github.com/sqlc-dev/sqlc v1.31.1 - golang.org/x/vuln v1.10.0 +tool ( + github.com/golangci/golangci-lint/v2/cmd/golangci-lint + github.com/sqlc-dev/sqlc/cmd/sqlc + golang.org/x/vuln/cmd/govulncheck ) require ( @@ -99,6 +99,7 @@ require ( github.com/golangci/dupl v0.0.0-20260401084720-c99c5cf5c202 // indirect github.com/golangci/go-printf-func-name v0.1.1 // indirect github.com/golangci/gofmt v0.0.0-20260820135601-e84e05053792 // indirect + github.com/golangci/golangci-lint/v2 v2.15.0 // indirect github.com/golangci/golines v0.16.0 // indirect github.com/golangci/misspell v0.8.0 // indirect github.com/golangci/plugin-module-register v0.1.2 // indirect @@ -201,6 +202,7 @@ require ( github.com/spf13/pflag v1.0.10 // indirect github.com/spf13/viper v1.12.0 // indirect github.com/sqlc-dev/doubleclick v1.0.0 // indirect + github.com/sqlc-dev/sqlc v1.31.1 // indirect github.com/ssgreg/nlreturn/v2 v2.2.1 // indirect github.com/stbenjam/no-sprintf-host-port v0.3.1 // indirect github.com/stretchr/objx v0.5.3 // indirect @@ -241,6 +243,7 @@ require ( golang.org/x/telemetry v0.0.0-20261116191925-e77a43154acb // indirect golang.org/x/text v0.44.0 // indirect golang.org/x/tools v0.53.0 // indirect + golang.org/x/vuln v1.10.0 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20260817212433-ac3dfec99bb1 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20260819154853-08b0e4226688 // indirect google.golang.org/grpc v1.85.0-dev.0.20260825072537-93e31b48545e // indirect diff --git a/tools/tools.go b/tools/tools.go deleted file mode 100644 index 7654229..0000000 --- a/tools/tools.go +++ /dev/null @@ -1,12 +0,0 @@ -//go:build tools - -// This file is for managing Go programs version with `go.mod`, which allows -// them to be kept up-to-date through tools like Dependabot. - -package tools - -import ( - _ "github.com/golangci/golangci-lint/v2/cmd/golangci-lint" - _ "github.com/sqlc-dev/sqlc/cmd/sqlc" - _ "golang.org/x/vuln/cmd/govulncheck" -)