diff options
author | Julien Dessaux | 2024-11-28 00:11:11 +0100 |
---|---|---|
committer | Julien Dessaux | 2024-12-17 23:19:22 +0100 |
commit | 6cf7872b22755e97408d4f67a645ef3ccc6c0b4d (patch) | |
tree | c9bffabde6ca73607ed87622685d948bb78b9bc6 | |
parent | fix(tfstated): rollack transaction in case of panic (diff) | |
download | tfstated-6cf7872b22755e97408d4f67a645ef3ccc6c0b4d.tar.gz tfstated-6cf7872b22755e97408d4f67a645ef3ccc6c0b4d.tar.bz2 tfstated-6cf7872b22755e97408d4f67a645ef3ccc6c0b4d.zip |
chore(tfstated): improve error messages and refactored PRAGMA code in the database module
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | go.mod | 7 | ||||
-rw-r--r-- | pkg/database/db.go | 33 |
3 files changed, 22 insertions, 20 deletions
@@ -1,4 +1,4 @@ -tfstated +cover.out tfstate.db tfstate.db-shm tfstate.db-wal @@ -2,9 +2,8 @@ module git.adyxax.org/adyxax/tfstated go 1.23.3 -require github.com/mattn/go-sqlite3 v1.14.24 - require ( - go.n16f.net/uuid v0.0.0-20240707135755-e4fd26b968ad // indirect - golang.org/x/crypto v0.29.0 // indirect + github.com/mattn/go-sqlite3 v1.14.24 + go.n16f.net/uuid v0.0.0-20240707135755-e4fd26b968ad + golang.org/x/crypto v0.29.0 ) diff --git a/pkg/database/db.go b/pkg/database/db.go index 0777556..b7f0e89 100644 --- a/pkg/database/db.go +++ b/pkg/database/db.go @@ -12,7 +12,7 @@ import ( func initDB(ctx context.Context, url string) (*sql.DB, error) { db, err := sql.Open("sqlite3", url) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to open database: %w", err) } defer func() { if err != nil { @@ -20,7 +20,7 @@ func initDB(ctx context.Context, url string) (*sql.DB, error) { } }() if _, err = db.ExecContext(ctx, "PRAGMA busy_timeout = 5000"); err != nil { - return nil, err + return nil, fmt.Errorf("failed to set pragma: %w", err) } return db, nil @@ -37,7 +37,7 @@ type DB struct { func NewDB(ctx context.Context, url string) (*DB, error) { readDB, err := initDB(ctx, url) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to init read database connection: %w", err) } defer func() { if err != nil { @@ -48,7 +48,7 @@ func NewDB(ctx context.Context, url string) (*DB, error) { writeDB, err := initDB(ctx, url) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to init write database connection: %w", err) } defer func() { if err != nil { @@ -63,20 +63,22 @@ func NewDB(ctx context.Context, url string) (*DB, error) { versionsHistoryLimit: 64, writeDB: writeDB, } - if _, err = db.Exec("PRAGMA foreign_keys = ON"); err != nil { - return nil, err + pragmas := []struct { + key string + value string + }{ + {"foreign_keys", "ON"}, + {"cache_size", "10000000"}, + {"journal_mode", "WAL"}, + {"synchronous", "NORMAL"}, } - if _, err = db.Exec("PRAGMA cache_size = 10000000"); err != nil { - return nil, err - } - if _, err = db.Exec("PRAGMA journal_mode = WAL"); err != nil { - return nil, err - } - if _, err = db.Exec("PRAGMA synchronous = NORMAL"); err != nil { - return nil, err + for _, pragma := range pragmas { + if _, err = db.Exec(fmt.Sprintf("PRAGMA %s = %s", pragma.key, pragma.value)); err != nil { + return nil, fmt.Errorf("failed to set pragma: %w", err) + } } if err = db.migrate(); err != nil { - return nil, err + return nil, fmt.Errorf("failed to migrate: %w", err) } return &db, nil @@ -85,6 +87,7 @@ func NewDB(ctx context.Context, url string) (*DB, error) { func (db *DB) Close() error { if err := db.readDB.Close(); err != nil { _ = db.writeDB.Close() + return fmt.Errorf("failed to close read database connection: %w", err) } return db.writeDB.Close() } |