All checks were successful
check / check (push) Successful in 5s
Updates golangci-lint to v2.12.2 everywhere it is pinned and installs the canonical `.golangci.yml`, then remediates every finding the new linter/config surfaces so `make check` is green. ## Version bump - `Dockerfile` lint stage: `golangci/golangci-lint:v2.11.3-alpine` -> `v2.12.2-alpine` (digest-pinned, date comment updated) - `Makefile` `deps` target: `go install` moved from the old v1 module path at `@latest` to the pinned `github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.12.2` - `.golangci.yml` replaced with the canonical config (v2 schema; settings under `linters.settings` so the thresholds actually apply; `default: all` with the standard six disables) - `script/bootstrap` installs golangci-lint via the system package manager and carries no version pin, so it is unchanged - CI (`.gitea/workflows/check.yml`) only runs `script/cibuild`, so it needed no change ## Lint remediation The canonical config surfaced ~3,300 findings across 56k lines. All are fixed, behavior-preserving; incorporates and supersedes the per-package mechanical passes already merged to `main` (refs #61). Highlights: - `err113`: dynamic errors replaced with package sentinels + `%w` wrapping; comparisons via `errors.Is` - `goprintffuncname`: printf-style helpers renamed with an `f` suffix (`ui.Writer` message methods, `cli.ReportErrorf`, `database.Fatalf`) and all call sites updated - `revive` stutter renames: `blob.Handler`, `blob.WithReader`, `blob.ChunkPosition`, `storage.URL`, `storage.Info`; missing doc comments added - `contextcheck`/`noctx`: `context.Context` threaded through `blob.Packer` and the scanner call sites; context-aware `exec`/`sql` variants - `funlen`/`cyclop`/`gocognit`/`dupl`: oversized and duplicated functions split into focused helpers (production and test code) - tests: `t.Parallel()` added where safe (global logger init kept in the serial phase for `-race`), `t.TempDir()`/`t.Helper()` adopted, several suites converted to external test packages - `gosec`: bounded integer conversions, `ReadHeaderTimeout` on the test HTTP server; remaining warnings suppressed per-site with justifications - remaining `nolint` directives are rare, targeted, and each carries a reason (e.g. `nilnil` not-found contract in the repository layer, fx module globals, on-disk snake_case struct tags) - removed the deprecated `log.LogOptions` alias (callers migrated to `log.Options`) `make check` (tests with `-race`, lint, fmt-check) passes. Co-authored-by: sneak <sneak@sneak.berlin> Reviewed-on: #62 Co-authored-by: clawbot <clawbot@noreply.example.org> Co-committed-by: clawbot <clawbot@noreply.example.org>
301 lines
6.8 KiB
Go
301 lines
6.8 KiB
Go
//nolint:testpackage // exercises unexported migration internals
|
|
package database
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestDatabase(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx := context.Background()
|
|
dbPath := filepath.Join(t.TempDir(), "test.db")
|
|
|
|
db, err := New(ctx, dbPath)
|
|
if err != nil {
|
|
t.Fatalf("failed to create database: %v", err)
|
|
}
|
|
defer func() {
|
|
err := db.Close()
|
|
if err != nil {
|
|
t.Errorf("failed to close database: %v", err)
|
|
}
|
|
}()
|
|
|
|
// Test connection
|
|
if db.Conn() == nil {
|
|
t.Fatal("database connection is nil")
|
|
}
|
|
|
|
// Test schema creation (already done in New via migrations)
|
|
// Verify tables exist
|
|
tables := []string{
|
|
"schema_migrations",
|
|
"files", "file_chunks", "chunks", "blobs",
|
|
"blob_chunks", "chunk_files", "snapshots",
|
|
}
|
|
|
|
for _, table := range tables {
|
|
var name string
|
|
|
|
err := db.conn.QueryRowContext(ctx,
|
|
"SELECT name FROM sqlite_master WHERE type='table' AND name=?", table,
|
|
).Scan(&name)
|
|
if err != nil {
|
|
t.Errorf("table %s does not exist: %v", table, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDatabaseInvalidPath(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx := context.Background()
|
|
|
|
// Test with invalid path
|
|
_, err := New(ctx, "/invalid/path/that/does/not/exist/test.db")
|
|
if err == nil {
|
|
t.Fatal("expected error for invalid path")
|
|
}
|
|
}
|
|
|
|
func TestDatabaseConcurrentAccess(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx := context.Background()
|
|
dbPath := filepath.Join(t.TempDir(), "test.db")
|
|
|
|
db, err := New(ctx, dbPath)
|
|
if err != nil {
|
|
t.Fatalf("failed to create database: %v", err)
|
|
}
|
|
defer func() {
|
|
err := db.Close()
|
|
if err != nil {
|
|
t.Errorf("failed to close database: %v", err)
|
|
}
|
|
}()
|
|
|
|
// Test concurrent writes
|
|
type result struct {
|
|
index int
|
|
err error
|
|
}
|
|
|
|
results := make(chan result, 10)
|
|
|
|
for i := range 10 {
|
|
go func(i int) {
|
|
_, err := db.ExecWithLog(ctx,
|
|
"INSERT INTO chunks (chunk_hash, size) VALUES (?, ?)",
|
|
fmt.Sprintf("hash%d", i), i*1024)
|
|
results <- result{index: i, err: err}
|
|
}(i)
|
|
}
|
|
|
|
// Wait for all goroutines and check results
|
|
for range 10 {
|
|
r := <-results
|
|
if r.err != nil {
|
|
t.Fatalf("concurrent insert %d failed: %v", r.index, r.err)
|
|
}
|
|
}
|
|
|
|
// Verify all inserts succeeded
|
|
var count int
|
|
|
|
err = db.conn.QueryRowContext(ctx, "SELECT COUNT(*) FROM chunks").Scan(&count)
|
|
if err != nil {
|
|
t.Fatalf("failed to count chunks: %v", err)
|
|
}
|
|
|
|
if count != 10 {
|
|
t.Errorf("expected 10 chunks, got %d", count)
|
|
}
|
|
}
|
|
|
|
func TestParseMigrationVersion(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
filename string
|
|
wantVer int
|
|
wantError bool
|
|
}{
|
|
{name: "valid 000.sql", filename: "000.sql", wantVer: 0, wantError: false},
|
|
{name: "valid 001.sql", filename: "001.sql", wantVer: 1, wantError: false},
|
|
{name: "valid 099.sql", filename: "099.sql", wantVer: 99, wantError: false},
|
|
{
|
|
name: "valid with description", filename: "001_initial_schema.sql",
|
|
wantVer: 1, wantError: false,
|
|
},
|
|
{
|
|
name: "valid large version", filename: "123_big_migration.sql",
|
|
wantVer: 123, wantError: false,
|
|
},
|
|
{name: "invalid alpha version", filename: "abc.sql", wantVer: 0, wantError: true},
|
|
{name: "invalid mixed chars", filename: "12a.sql", wantVer: 0, wantError: true},
|
|
{name: "invalid no extension", filename: "schema.sql", wantVer: 0, wantError: true},
|
|
{name: "empty string", filename: "", wantVer: 0, wantError: true},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
got, err := ParseMigrationVersion(tc.filename)
|
|
if tc.wantError {
|
|
if err == nil {
|
|
t.Errorf("ParseMigrationVersion(%q) = %d, nil; want error",
|
|
tc.filename, got)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
if err != nil {
|
|
t.Errorf("ParseMigrationVersion(%q) unexpected error: %v",
|
|
tc.filename, err)
|
|
|
|
return
|
|
}
|
|
|
|
if got != tc.wantVer {
|
|
t.Errorf("ParseMigrationVersion(%q) = %d; want %d",
|
|
tc.filename, got, tc.wantVer)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestApplyMigrations_Idempotent(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx := context.Background()
|
|
|
|
conn, err := sql.Open("sqlite", ":memory:?_foreign_keys=ON")
|
|
if err != nil {
|
|
t.Fatalf("failed to open database: %v", err)
|
|
}
|
|
defer func() {
|
|
err := conn.Close()
|
|
if err != nil {
|
|
t.Errorf("failed to close database: %v", err)
|
|
}
|
|
}()
|
|
|
|
conn.SetMaxOpenConns(1)
|
|
conn.SetMaxIdleConns(1)
|
|
|
|
// First run: apply all migrations.
|
|
err = applyMigrations(ctx, conn)
|
|
if err != nil {
|
|
t.Fatalf("first applyMigrations failed: %v", err)
|
|
}
|
|
|
|
// Count rows in schema_migrations after first run.
|
|
var countBefore int
|
|
|
|
err = conn.QueryRowContext(ctx,
|
|
"SELECT COUNT(*) FROM schema_migrations",
|
|
).Scan(&countBefore)
|
|
if err != nil {
|
|
t.Fatalf("failed to count schema_migrations after first run: %v", err)
|
|
}
|
|
|
|
// Second run: must be a no-op.
|
|
err = applyMigrations(ctx, conn)
|
|
if err != nil {
|
|
t.Fatalf("second applyMigrations failed: %v", err)
|
|
}
|
|
|
|
// Count rows in schema_migrations after second run — must be unchanged.
|
|
var countAfter int
|
|
|
|
err = conn.QueryRowContext(ctx,
|
|
"SELECT COUNT(*) FROM schema_migrations",
|
|
).Scan(&countAfter)
|
|
if err != nil {
|
|
t.Fatalf("failed to count schema_migrations after second run: %v", err)
|
|
}
|
|
|
|
if countBefore != countAfter {
|
|
t.Errorf("schema_migrations row count changed: before=%d, after=%d",
|
|
countBefore, countAfter)
|
|
}
|
|
}
|
|
|
|
func TestBootstrapMigrationsTable_FreshDatabase(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx := context.Background()
|
|
|
|
conn, err := sql.Open("sqlite", ":memory:?_foreign_keys=ON")
|
|
if err != nil {
|
|
t.Fatalf("failed to open database: %v", err)
|
|
}
|
|
defer func() {
|
|
err := conn.Close()
|
|
if err != nil {
|
|
t.Errorf("failed to close database: %v", err)
|
|
}
|
|
}()
|
|
|
|
conn.SetMaxOpenConns(1)
|
|
conn.SetMaxIdleConns(1)
|
|
|
|
// Verify schema_migrations does NOT exist yet.
|
|
var tableBefore int
|
|
|
|
err = conn.QueryRowContext(ctx,
|
|
"SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='schema_migrations'",
|
|
).Scan(&tableBefore)
|
|
if err != nil {
|
|
t.Fatalf("failed to check for table before bootstrap: %v", err)
|
|
}
|
|
|
|
if tableBefore != 0 {
|
|
t.Fatal("schema_migrations table should not exist before bootstrap")
|
|
}
|
|
|
|
// Run bootstrap.
|
|
err = bootstrapMigrationsTable(ctx, conn)
|
|
if err != nil {
|
|
t.Fatalf("bootstrapMigrationsTable failed: %v", err)
|
|
}
|
|
|
|
// Verify schema_migrations now exists.
|
|
var tableAfter int
|
|
|
|
err = conn.QueryRowContext(ctx,
|
|
"SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='schema_migrations'",
|
|
).Scan(&tableAfter)
|
|
if err != nil {
|
|
t.Fatalf("failed to check for table after bootstrap: %v", err)
|
|
}
|
|
|
|
if tableAfter != 1 {
|
|
t.Fatalf("schema_migrations table should exist after bootstrap, got count=%d",
|
|
tableAfter)
|
|
}
|
|
|
|
// Verify version 0 row exists.
|
|
var version int
|
|
|
|
err = conn.QueryRowContext(ctx,
|
|
"SELECT version FROM schema_migrations WHERE version = 0",
|
|
).Scan(&version)
|
|
if err != nil {
|
|
t.Fatalf("version 0 row not found in schema_migrations: %v", err)
|
|
}
|
|
|
|
if version != 0 {
|
|
t.Errorf("expected version 0, got %d", version)
|
|
}
|
|
}
|