2020-03-02 22:33:55 +00:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package preflight_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/storj/storagenode"
|
|
|
|
"storj.io/storj/storagenode/storagenodedb"
|
|
|
|
"storj.io/storj/storagenode/storagenodedb/storagenodedbtest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPreflightSchema(t *testing.T) {
|
|
|
|
// no change should not cause a preflight error
|
|
|
|
storagenodedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db storagenode.DB) {
|
|
|
|
err := db.Preflight(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
// adding something to the schema should cause a preflight error
|
|
|
|
storagenodedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db storagenode.DB) {
|
|
|
|
err := db.Preflight(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// add index to used serials db
|
|
|
|
rawDBs := db.(*storagenodedb.DB).RawDatabases()
|
2020-05-27 22:07:24 +01:00
|
|
|
satellitesDB := rawDBs[storagenodedb.SatellitesDBName]
|
|
|
|
_, err = satellitesDB.GetDB().Exec(ctx, "CREATE INDEX a_new_index ON satellites(status)")
|
2020-03-02 22:33:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// expect error from preflight check for addition
|
|
|
|
err = db.Preflight(ctx)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, storagenodedb.ErrPreflight.Has(err))
|
|
|
|
})
|
|
|
|
|
|
|
|
// removing something from the schema should cause a preflight error
|
|
|
|
storagenodedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db storagenode.DB) {
|
|
|
|
err := db.Preflight(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// remove index from used serials db
|
|
|
|
rawDBs := db.(*storagenodedb.DB).RawDatabases()
|
2020-05-27 22:07:24 +01:00
|
|
|
bandwidthDB := rawDBs[storagenodedb.BandwidthDBName]
|
|
|
|
_, err = bandwidthDB.GetDB().Exec(ctx, "DROP INDEX idx_bandwidth_usage_created;")
|
2020-03-02 22:33:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// expect error from preflight check for removal
|
|
|
|
err = db.Preflight(ctx)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, storagenodedb.ErrPreflight.Has(err))
|
|
|
|
})
|
|
|
|
|
|
|
|
// having a test table should not cause the preflight check to fail
|
|
|
|
storagenodedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db storagenode.DB) {
|
|
|
|
err := db.Preflight(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// add test_table to used serials db
|
|
|
|
rawDBs := db.(*storagenodedb.DB).RawDatabases()
|
2020-05-27 22:07:24 +01:00
|
|
|
bandwidthDB := rawDBs[storagenodedb.BandwidthDBName]
|
|
|
|
_, err = bandwidthDB.GetDB().Exec(ctx, "CREATE TABLE test_table(id int NOT NULL, name varchar(30), PRIMARY KEY (id));")
|
2020-03-02 22:33:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// expect no error from preflight check with added test_table
|
|
|
|
err = db.Preflight(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|