2019-09-11 21:31:46 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package testdata
|
|
|
|
|
|
|
|
import (
|
2020-01-13 13:03:30 +00:00
|
|
|
"context"
|
2019-09-11 21:31:46 +01:00
|
|
|
"fmt"
|
|
|
|
|
2021-04-23 10:52:40 +01:00
|
|
|
"storj.io/private/dbutil/dbschema"
|
|
|
|
"storj.io/private/dbutil/sqliteutil"
|
2019-09-11 21:31:46 +01:00
|
|
|
)
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// States is the global variable that stores all the states for testing.
|
2019-09-11 21:31:46 +01:00
|
|
|
var States = MultiDBStates{
|
|
|
|
List: []*MultiDBState{
|
|
|
|
&v0,
|
|
|
|
&v1,
|
|
|
|
&v2,
|
|
|
|
&v3,
|
|
|
|
&v4,
|
|
|
|
&v5,
|
|
|
|
&v6,
|
|
|
|
&v7,
|
|
|
|
&v8,
|
|
|
|
&v9,
|
|
|
|
&v10,
|
|
|
|
&v11,
|
|
|
|
&v12,
|
|
|
|
&v13,
|
|
|
|
&v14,
|
|
|
|
&v15,
|
|
|
|
&v16,
|
|
|
|
&v17,
|
|
|
|
&v18,
|
|
|
|
&v19,
|
|
|
|
&v20,
|
2019-09-11 23:57:53 +01:00
|
|
|
&v21,
|
2019-09-23 20:36:46 +01:00
|
|
|
&v22,
|
|
|
|
&v23,
|
2019-09-27 14:55:51 +01:00
|
|
|
&v24,
|
2019-10-01 15:34:03 +01:00
|
|
|
&v25,
|
2019-11-20 16:28:49 +00:00
|
|
|
&v26,
|
2019-12-15 22:41:22 +00:00
|
|
|
&v27,
|
2019-12-16 17:59:01 +00:00
|
|
|
&v28,
|
2020-01-07 23:34:51 +00:00
|
|
|
&v29,
|
2020-01-30 15:53:03 +00:00
|
|
|
&v30,
|
2020-01-30 18:01:50 +00:00
|
|
|
&v31,
|
2020-03-12 14:42:01 +00:00
|
|
|
&v32,
|
2020-03-15 13:06:01 +00:00
|
|
|
&v33,
|
2020-03-27 18:50:57 +00:00
|
|
|
&v34,
|
2020-04-10 15:03:14 +01:00
|
|
|
&v35,
|
2020-04-16 16:40:28 +01:00
|
|
|
&v36,
|
2020-04-02 20:15:42 +01:00
|
|
|
&v37,
|
2020-04-30 21:36:04 +01:00
|
|
|
&v38,
|
2020-05-03 17:30:54 +01:00
|
|
|
&v39,
|
2020-05-18 11:01:34 +01:00
|
|
|
&v40,
|
2020-05-20 13:01:19 +01:00
|
|
|
&v41,
|
2020-05-27 22:07:24 +01:00
|
|
|
&v42,
|
2020-06-24 18:05:43 +01:00
|
|
|
&v43,
|
2020-09-01 12:20:12 +01:00
|
|
|
&v44,
|
2020-09-02 16:37:54 +01:00
|
|
|
&v45,
|
2020-09-21 12:06:43 +01:00
|
|
|
&v46,
|
2020-12-30 19:47:14 +00:00
|
|
|
&v47,
|
2020-12-18 19:27:28 +00:00
|
|
|
&v48,
|
2021-02-02 10:51:02 +00:00
|
|
|
&v49,
|
2021-02-09 16:15:17 +00:00
|
|
|
&v50,
|
2021-02-17 18:07:06 +00:00
|
|
|
&v51,
|
2021-07-02 14:13:13 +01:00
|
|
|
&v52,
|
2021-07-08 10:14:15 +01:00
|
|
|
&v53,
|
2022-07-21 12:15:03 +01:00
|
|
|
&v54,
|
2019-09-11 21:31:46 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// MultiDBStates provides a convenient list of MultiDBState.
|
2019-09-11 21:31:46 +01:00
|
|
|
type MultiDBStates struct {
|
|
|
|
List []*MultiDBState
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindVersion finds a MultiDBState with the specified version.
|
|
|
|
func (mdbs *MultiDBStates) FindVersion(version int) (*MultiDBState, bool) {
|
|
|
|
for _, state := range mdbs.List {
|
|
|
|
if state.Version == version {
|
|
|
|
return state, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// MultiDBState represents an expected state across multiple DBs, defined in SQL
|
|
|
|
// commands.
|
|
|
|
type MultiDBState struct {
|
|
|
|
Version int
|
|
|
|
DBStates DBStates
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// DBStates is a convenience type.
|
2019-09-11 21:31:46 +01:00
|
|
|
type DBStates map[string]*DBState
|
|
|
|
|
|
|
|
// DBState allows you to define the desired state of the DB using SQl commands.
|
|
|
|
// Both the SQl and NewData fields contains SQL that will be executed to create
|
|
|
|
// the expected DB. The NewData SQL additionally will be executed on the testDB
|
2021-02-17 18:07:06 +00:00
|
|
|
// to ensure data is consistent. If OldData is not empty, it is executed on the
|
|
|
|
// testDB before the migration is run, and NewData is not run on the testDB. This
|
|
|
|
// is used to assert that a migration that modifies data runs as expected.
|
2019-09-11 21:31:46 +01:00
|
|
|
type DBState struct {
|
|
|
|
SQL string
|
2021-02-17 18:07:06 +00:00
|
|
|
OldData string
|
2019-09-11 21:31:46 +01:00
|
|
|
NewData string
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// MultiDBSnapshot represents an expected state among multiple databases.
|
2019-09-11 21:31:46 +01:00
|
|
|
type MultiDBSnapshot struct {
|
|
|
|
Version int
|
|
|
|
DBSnapshots DBSnapshots
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// NewMultiDBSnapshot returns a new MultiDBSnapshot.
|
2019-09-11 21:31:46 +01:00
|
|
|
func NewMultiDBSnapshot() *MultiDBSnapshot {
|
|
|
|
return &MultiDBSnapshot{
|
|
|
|
DBSnapshots: DBSnapshots{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// DBSnapshots is a convenience type.
|
2019-09-11 21:31:46 +01:00
|
|
|
type DBSnapshots map[string]*DBSnapshot
|
|
|
|
|
|
|
|
// DBSnapshot is a snapshot of a single DB.
|
|
|
|
type DBSnapshot struct {
|
|
|
|
Schema *dbschema.Schema
|
|
|
|
Data *dbschema.Data
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadMultiDBSnapshot converts a MultiDBState into a MultiDBSnapshot. It
|
2021-02-17 18:07:06 +00:00
|
|
|
// executes the SQL and stores the schema and data.
|
2020-01-13 13:03:30 +00:00
|
|
|
func LoadMultiDBSnapshot(ctx context.Context, multiDBState *MultiDBState) (*MultiDBSnapshot, error) {
|
2019-09-11 21:31:46 +01:00
|
|
|
multiDBSnapshot := NewMultiDBSnapshot()
|
|
|
|
for dbName, dbState := range multiDBState.DBStates {
|
2020-01-13 13:03:30 +00:00
|
|
|
snapshot, err := sqliteutil.LoadSnapshotFromSQL(ctx, fmt.Sprintf("%s\n%s", dbState.SQL, dbState.NewData))
|
2019-09-11 21:31:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
multiDBSnapshot.DBSnapshots[dbName] = &DBSnapshot{
|
|
|
|
Schema: snapshot.Schema,
|
|
|
|
Data: snapshot.Data,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return multiDBSnapshot, nil
|
|
|
|
}
|