-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
21d1d82
commit df60088
Showing
12 changed files
with
606 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//go:build !testing | ||
|
||
package sqlite | ||
|
||
const ( | ||
busyTimeout = 5000 // 5 seconds | ||
retryAttempts = 15 // 15 attempts | ||
factor = 2.0 // factor ^ retryAttempts = backoff time in milliseconds | ||
|
||
// the number of records to limit long-running sector queries to | ||
sqlSectorBatchSize = 256 // 1 GiB | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//go:build testing | ||
|
||
package sqlite | ||
|
||
const ( | ||
busyTimeout = 5 // 5ms | ||
retryAttempts = 10 // 10 attempts | ||
factor = 2.0 // factor ^ retryAttempts = backoff time in milliseconds | ||
|
||
// the number of records to limit long-running sector queries to | ||
sqlSectorBatchSize = 5 // 20 MiB | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package sqlite | ||
|
||
import ( | ||
"database/sql" | ||
_ "embed" // for init.sql | ||
"errors" | ||
"time" | ||
|
||
"fmt" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
// init queries are run when the database is first created. | ||
// | ||
//go:embed init.sql | ||
var initDatabase string | ||
|
||
func (s *Store) initNewDatabase(target int64) error { | ||
return s.transaction(func(tx txn) error { | ||
if _, err := tx.Exec(initDatabase); err != nil { | ||
return fmt.Errorf("failed to initialize database: %w", err) | ||
} else if err := setDBVersion(tx, target); err != nil { | ||
return fmt.Errorf("failed to set initial database version: %w", err) | ||
} | ||
return nil | ||
}) | ||
} | ||
|
||
func (s *Store) upgradeDatabase(current, target int64) error { | ||
log := s.log.Named("migrations") | ||
log.Info("migrating database", zap.Int64("current", current), zap.Int64("target", target)) | ||
|
||
// disable foreign key constraints during migration | ||
if _, err := s.db.Exec("PRAGMA foreign_keys = OFF"); err != nil { | ||
return fmt.Errorf("failed to disable foreign key constraints: %w", err) | ||
} | ||
defer func() { | ||
// re-enable foreign key constraints | ||
if _, err := s.db.Exec("PRAGMA foreign_keys = ON"); err != nil { | ||
log.Panic("failed to enable foreign key constraints", zap.Error(err)) | ||
} | ||
}() | ||
|
||
return s.transaction(func(tx txn) error { | ||
for _, fn := range migrations[current-1:] { | ||
current++ | ||
start := time.Now() | ||
if err := fn(tx, log.With(zap.Int64("version", current))); err != nil { | ||
return fmt.Errorf("failed to migrate database to version %v: %w", current, err) | ||
} | ||
// check that no foreign key constraints were violated | ||
if err := tx.QueryRow("PRAGMA foreign_key_check").Scan(); !errors.Is(err, sql.ErrNoRows) { | ||
return fmt.Errorf("foreign key constraints are not satisfied") | ||
} | ||
log.Debug("migration complete", zap.Int64("current", current), zap.Int64("target", target), zap.Duration("elapsed", time.Since(start))) | ||
} | ||
|
||
// set the final database version | ||
return setDBVersion(tx, target) | ||
}) | ||
} | ||
|
||
func (s *Store) init() error { | ||
// calculate the expected final database version | ||
target := int64(len(migrations) + 1) | ||
// disable foreign key constraints during migration | ||
if _, err := s.db.Exec("PRAGMA foreign_keys = OFF"); err != nil { | ||
return fmt.Errorf("failed to disable foreign key constraints: %w", err) | ||
} | ||
|
||
version := getDBVersion(s.db) | ||
switch { | ||
case version == 0: | ||
return s.initNewDatabase(target) | ||
case version < target: | ||
return s.upgradeDatabase(version, target) | ||
case version > target: | ||
return fmt.Errorf("database version %v is newer than expected %v. database downgrades are not supported", version, target) | ||
} | ||
// nothing to do | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CREATE TABLE global_settings ( | ||
id INTEGER PRIMARY KEY NOT NULL DEFAULT 0 CHECK (id = 0), -- enforce a single row | ||
db_version INTEGER NOT NULL -- used for migrations | ||
); | ||
|
||
-- initialize the global settings table | ||
INSERT INTO global_settings (id, db_version) VALUES (0, 0); -- should not be changed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package sqlite | ||
|
||
import "go.uber.org/zap" | ||
|
||
var migrations []func(tx txn, log *zap.Logger) error |
Oops, something went wrong.