storj/storagenode/storagenodedb/storagenodedbtest/run.go
Andrew Harding 4fdea51d5c storagenode/storagenodedb: faster test db init
Running all of the migrations necessary to initialize a storage node
database takes a significant amount of time during runs.

The package current supports initializing a database from manually coalesced
migration data (i.e. snapshot) which improves the situation somewhat.

This change takes things a bit further by changing the snapshot code to
instead hydrate the database directory from a pre-generated snapshot zip
file.

name                                old time/op  new time/op  delta
Run_StorageNodeCount_4/Postgres-16   2.50s ± 0%   0.16s ± 0%   ~     (p=1.000 n=1+1)

Change-Id: I213bbba5f9199497fbe8ce889b627e853f8b29a0
2022-12-01 20:45:36 +00:00

60 lines
1.3 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package storagenodedbtest
// This package should be referenced only in test files!
import (
"database/sql"
"path/filepath"
"testing"
"github.com/mattn/go-sqlite3"
"go.uber.org/zap/zaptest"
"storj.io/common/testcontext"
"storj.io/private/dbutil/utccheck"
"storj.io/storj/storagenode"
"storj.io/storj/storagenode/storagenodedb"
)
func init() {
sql.Register("sqlite3+utccheck", utccheck.WrapDriver(&sqlite3.SQLiteDriver{}))
}
// Run method will iterate over all supported databases. Will establish
// connection and will create tables for each DB.
func Run(t *testing.T, test func(ctx *testcontext.Context, t *testing.T, db storagenode.DB)) {
t.Run("Sqlite", func(t *testing.T) {
t.Parallel()
ctx := testcontext.New(t)
defer ctx.Cleanup()
log := zaptest.NewLogger(t)
storageDir := ctx.Dir("storage")
cfg := storagenodedb.Config{
Storage: storageDir,
Info: filepath.Join(storageDir, "piecestore.db"),
Info2: filepath.Join(storageDir, "info.db"),
Driver: "sqlite3+utccheck",
Pieces: storageDir,
}
db, err := OpenNew(ctx, log, cfg)
if err != nil {
t.Fatal(err)
}
defer ctx.Check(db.Close)
err = db.MigrateToLatest(ctx)
if err != nil {
t.Fatal(err)
}
test(ctx, t, db)
})
}