fix: golangci-lint v2 config and lint-clean production code

- Fix .golangci.yml for v2 format (linters-settings -> linters.settings)
- All production code now passes golangci-lint with zero issues
- Line length 88, funlen 80/50, cyclop 15, dupl 100
- Extract shared helpers in db (scanChannels, scanInt64s, scanMessages)
- Split runMigrations into applyMigration/execMigration
- Fix fanOut return signature (remove unused int64)
- Add fanOutSilent helper to avoid dogsled
- Rewrite CLI code for lint compliance (nlreturn, wsl_v5, noctx, etc)
- Rename CLI api package to chatapi to avoid revive var-naming
- Fix all noinlineerr, mnd, perfsprint, funcorder issues
- Fix db tests: extract helpers, add t.Parallel, proper error checks
- Broker tests already clean
- Handler integration tests still have lint issues (next commit)
This commit is contained in:
clawbot
2026-02-10 18:50:24 -08:00
committed by user
parent d6408b2853
commit a7792168a1
16 changed files with 2404 additions and 980 deletions

View File

@@ -16,13 +16,11 @@ import (
"git.eeqj.de/sneak/chat/internal/logger"
"go.uber.org/fx"
_ "github.com/joho/godotenv/autoload" // loads .env file
_ "modernc.org/sqlite" // SQLite driver
_ "github.com/joho/godotenv/autoload" // .env
_ "modernc.org/sqlite" // driver
)
const (
minMigrationParts = 2
)
const minMigrationParts = 2
// SchemaFiles contains embedded SQL migration files.
//
@@ -37,15 +35,18 @@ type Params struct {
Config *config.Config
}
// Database manages the SQLite database connection and migrations.
// Database manages the SQLite connection and migrations.
type Database struct {
db *sql.DB
log *slog.Logger
params *Params
}
// New creates a new Database instance and registers lifecycle hooks.
func New(lc fx.Lifecycle, params Params) (*Database, error) {
// New creates a new Database and registers lifecycle hooks.
func New(
lc fx.Lifecycle,
params Params,
) (*Database, error) {
s := new(Database)
s.params = &params
s.log = params.Logger.Get()
@@ -55,13 +56,16 @@ func New(lc fx.Lifecycle, params Params) (*Database, error) {
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
s.log.Info("Database OnStart Hook")
return s.connect(ctx)
},
OnStop: func(_ context.Context) error {
s.log.Info("Database OnStop Hook")
if s.db != nil {
return s.db.Close()
}
return nil
},
})
@@ -84,20 +88,29 @@ func (s *Database) connect(ctx context.Context) error {
d, err := sql.Open("sqlite", dbURL)
if err != nil {
s.log.Error("failed to open database", "error", err)
s.log.Error(
"failed to open database", "error", err,
)
return err
}
err = d.PingContext(ctx)
if err != nil {
s.log.Error("failed to ping database", "error", err)
s.log.Error(
"failed to ping database", "error", err,
)
return err
}
s.db = d
s.log.Info("database connected")
if _, err := s.db.ExecContext(ctx, "PRAGMA foreign_keys = ON"); err != nil {
_, err = s.db.ExecContext(
ctx, "PRAGMA foreign_keys = ON",
)
if err != nil {
return fmt.Errorf("enable foreign keys: %w", err)
}
@@ -110,14 +123,17 @@ type migration struct {
sql string
}
func (s *Database) runMigrations(ctx context.Context) error {
func (s *Database) runMigrations(
ctx context.Context,
) error {
_, err := s.db.ExecContext(ctx,
`CREATE TABLE IF NOT EXISTS schema_migrations (
version INTEGER PRIMARY KEY,
applied_at DATETIME DEFAULT CURRENT_TIMESTAMP
)`)
version INTEGER PRIMARY KEY,
applied_at DATETIME DEFAULT CURRENT_TIMESTAMP)`)
if err != nil {
return fmt.Errorf("create schema_migrations table: %w", err)
return fmt.Errorf(
"create schema_migrations: %w", err,
)
}
migrations, err := s.loadMigrations()
@@ -126,74 +142,125 @@ func (s *Database) runMigrations(ctx context.Context) error {
}
for _, m := range migrations {
var exists int
err := s.db.QueryRowContext(ctx,
"SELECT COUNT(*) FROM schema_migrations WHERE version = ?",
m.version,
).Scan(&exists)
err = s.applyMigration(ctx, m)
if err != nil {
return fmt.Errorf("check migration %d: %w", m.version, err)
}
if exists > 0 {
continue
}
s.log.Info("applying migration", "version", m.version, "name", m.name)
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return fmt.Errorf("begin tx for migration %d: %w", m.version, err)
}
_, err = tx.ExecContext(ctx, m.sql)
if err != nil {
_ = tx.Rollback()
return fmt.Errorf("apply migration %d (%s): %w", m.version, m.name, err)
}
_, err = tx.ExecContext(ctx,
"INSERT INTO schema_migrations (version) VALUES (?)",
m.version,
)
if err != nil {
_ = tx.Rollback()
return fmt.Errorf("record migration %d: %w", m.version, err)
}
if err := tx.Commit(); err != nil {
return fmt.Errorf("commit migration %d: %w", m.version, err)
return err
}
}
s.log.Info("database migrations complete")
return nil
}
func (s *Database) loadMigrations() ([]migration, error) {
func (s *Database) applyMigration(
ctx context.Context,
m migration,
) error {
var exists int
err := s.db.QueryRowContext(ctx,
`SELECT COUNT(*) FROM schema_migrations
WHERE version = ?`,
m.version,
).Scan(&exists)
if err != nil {
return fmt.Errorf(
"check migration %d: %w", m.version, err,
)
}
if exists > 0 {
return nil
}
s.log.Info(
"applying migration",
"version", m.version,
"name", m.name,
)
return s.execMigration(ctx, m)
}
func (s *Database) execMigration(
ctx context.Context,
m migration,
) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return fmt.Errorf(
"begin tx for migration %d: %w",
m.version, err,
)
}
_, err = tx.ExecContext(ctx, m.sql)
if err != nil {
_ = tx.Rollback()
return fmt.Errorf(
"apply migration %d (%s): %w",
m.version, m.name, err,
)
}
_, err = tx.ExecContext(ctx,
`INSERT INTO schema_migrations (version)
VALUES (?)`,
m.version,
)
if err != nil {
_ = tx.Rollback()
return fmt.Errorf(
"record migration %d: %w",
m.version, err,
)
}
return tx.Commit()
}
func (s *Database) loadMigrations() (
[]migration,
error,
) {
entries, err := fs.ReadDir(SchemaFiles, "schema")
if err != nil {
return nil, fmt.Errorf("read schema dir: %w", err)
return nil, fmt.Errorf(
"read schema dir: %w", err,
)
}
var migrations []migration
for _, entry := range entries {
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".sql") {
if entry.IsDir() ||
!strings.HasSuffix(entry.Name(), ".sql") {
continue
}
parts := strings.SplitN(entry.Name(), "_", minMigrationParts)
parts := strings.SplitN(
entry.Name(), "_", minMigrationParts,
)
if len(parts) < minMigrationParts {
continue
}
version, err := strconv.Atoi(parts[0])
if err != nil {
version, parseErr := strconv.Atoi(parts[0])
if parseErr != nil {
continue
}
content, err := SchemaFiles.ReadFile("schema/" + entry.Name())
if err != nil {
return nil, fmt.Errorf("read migration %s: %w", entry.Name(), err)
content, readErr := SchemaFiles.ReadFile(
"schema/" + entry.Name(),
)
if readErr != nil {
return nil, fmt.Errorf(
"read migration %s: %w",
entry.Name(), readErr,
)
}
migrations = append(migrations, migration{