Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan4th committed Aug 22, 2024
1 parent 11527ab commit 64f4d38
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
15 changes: 6 additions & 9 deletions sql/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,13 @@ func prepareDB(logger *zap.Logger, db *sqliteDatabase, config *conf, freshDB boo
return nil, fmt.Errorf("PRAGMA journal_mode=OFF: %w", err)

Check warning on line 318 in sql/database.go

View check run for this annotation

Codecov / codecov/patch

sql/database.go#L318

Added line #L318 was not covered by tests
}
if _, err := db.Exec("PRAGMA synchronous=OFF", nil, nil); err != nil {
return nil, fmt.Errorf("PRAGMA journal_mode=OFF: %w", err)
return nil, fmt.Errorf("PRAGMA synchronous=OFF: %w", err)

Check warning on line 321 in sql/database.go

View check run for this annotation

Codecov / codecov/patch

sql/database.go#L321

Added line #L321 was not covered by tests
}
}

if config.exclusive {
if err := db.startExclusive(); err != nil {
return nil, fmt.Errorf("error switching to exclusive mode: %w", err)
return nil, fmt.Errorf("start exclusive: %w", err)
}
}

Expand Down Expand Up @@ -520,7 +520,7 @@ func dbMigrationPaths(uri string) (dbPath, migratedPath string, err error) {
func handleIncompleteCopyMigration(config *conf) error {
dbPath, migratedPath, err := dbMigrationPaths(config.uri)
if err != nil {
return err
return fmt.Errorf("getting DB migration paths: %w", err)

Check warning on line 523 in sql/database.go

View check run for this annotation

Codecov / codecov/patch

sql/database.go#L523

Added line #L523 was not covered by tests
}
if migratedPath == "" {
return nil
Expand Down Expand Up @@ -634,6 +634,7 @@ func (db *sqliteDatabase) startExclusive() error {
if conn == nil {
return ErrNoConnection

Check warning on line 635 in sql/database.go

View check run for this annotation

Codecov / codecov/patch

sql/database.go#L635

Added line #L635 was not covered by tests
}
defer db.pool.Put(conn)
// We don't need to wait for long if the database is busy
conn.SetBusyTimeout(1 * time.Millisecond)
// From SQLite docs:
Expand All @@ -642,7 +643,6 @@ func (db *sqliteDatabase) startExclusive() error {
// EXCLUSIVE mode, a shared lock is obtained and held. The first time the
// database is written, an exclusive lock is obtained and held.
if _, err := exec(conn, "PRAGMA locking_mode=EXCLUSIVE", nil, nil); err != nil {
db.pool.Put(conn)
return fmt.Errorf("PRAGMA locking_mode=EXCLUSIVE: %w", err)

Check warning on line 646 in sql/database.go

View check run for this annotation

Codecov / codecov/patch

sql/database.go#L646

Added line #L646 was not covered by tests
}
// We need to perform a transaction to have the database actually locked.
Expand All @@ -654,14 +654,11 @@ func (db *sqliteDatabase) startExclusive() error {
// underway.
_, err := exec(conn, "BEGIN EXCLUSIVE", nil, nil)
if err != nil {
db.pool.Put(conn)
return fmt.Errorf("error starting the EXCLUSIVE transaction: %w", err)
}
if _, err := exec(conn, "COMMIT", nil, nil); err != nil {
db.pool.Put(conn)
return fmt.Errorf("error committing the EXCLUSIVE transaction: %w", err)

Check warning on line 660 in sql/database.go

View check run for this annotation

Codecov / codecov/patch

sql/database.go#L660

Added line #L660 was not covered by tests
}
db.pool.Put(conn)
return nil
}

Expand Down Expand Up @@ -793,7 +790,7 @@ func (db *sqliteDatabase) vacuumInto(toPath string) error {
func (db *sqliteDatabase) copyMigrateDB(config *conf) (finalDB *sqliteDatabase, err error) {
dbPath, migratedPath, err := dbMigrationPaths(config.uri)
if err != nil {
return nil, err
return nil, fmt.Errorf("getting DB migration paths: %w", err)

Check warning on line 793 in sql/database.go

View check run for this annotation

Codecov / codecov/patch

sql/database.go#L793

Added line #L793 was not covered by tests
}
if migratedPath == "" {
return nil, fmt.Errorf("cannot migrate database, only file DBs are supported: %s", config.uri)

Check warning on line 796 in sql/database.go

View check run for this annotation

Codecov / codecov/patch

sql/database.go#L796

Added line #L796 was not covered by tests
Expand Down Expand Up @@ -1060,7 +1057,7 @@ func (tx *sqliteTx) Release() error {
// Exec query.
func (tx *sqliteTx) Exec(query string, encoder Encoder, decoder Decoder) (int, error) {
if err := tx.db.runInterceptors(query); err != nil {
return 0, err
return 0, fmt.Errorf("running query interceptors: %w", err)

Check warning on line 1060 in sql/database.go

View check run for this annotation

Codecov / codecov/patch

sql/database.go#L1060

Added line #L1060 was not covered by tests
}

tx.db.queryCount.Add(1)
Expand Down
8 changes: 4 additions & 4 deletions sql/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,18 +201,18 @@ func (s *Schema) MigrateTempDB(logger *zap.Logger, db Database, before int) erro

logger.Info("syncing temporary database")

// Enable synchronous mode and WAL journal to ensure the database is synced
// Enable WAL journal and synchronous mode to ensure the database is synced
if _, err := db.Exec("PRAGMA journal_mode=WAL", nil, nil); err != nil {
return fmt.Errorf("PRAGMA journal_mode=WAL: %w", err)
return fmt.Errorf("setting WAL journal mode: %w", err)

Check warning on line 206 in sql/schema.go

View check run for this annotation

Codecov / codecov/patch

sql/schema.go#L206

Added line #L206 was not covered by tests
}

if _, err := db.Exec("PRAGMA synchronous=FULL", nil, nil); err != nil {
return fmt.Errorf("PRAGMA journal_mode=OFF: %w", err)
return fmt.Errorf("setting synchronous mode: %w", err)

Check warning on line 210 in sql/schema.go

View check run for this annotation

Codecov / codecov/patch

sql/schema.go#L210

Added line #L210 was not covered by tests
}

// This should trigger file sync
if err := s.setVersion(db, v); err != nil {
return err
return fmt.Errorf("setting DB schema version: %w", err)

Check warning on line 215 in sql/schema.go

View check run for this annotation

Codecov / codecov/patch

sql/schema.go#L215

Added line #L215 was not covered by tests
}

return nil
Expand Down

0 comments on commit 64f4d38

Please sign in to comment.