2019-09-17 22:42:40 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package sqliteutil_test
|
|
|
|
|
|
|
|
import (
|
2020-01-14 11:29:25 +00:00
|
|
|
"context"
|
2019-09-17 22:42:40 +01:00
|
|
|
"database/sql"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/testcontext"
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/dbutil/sqliteutil"
|
2019-09-17 22:42:40 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMigrateTablesToDatabase(t *testing.T) {
|
2019-09-19 22:21:03 +01:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2019-09-17 22:42:40 +01:00
|
|
|
srcDB := newMemDB(t)
|
2019-09-19 22:21:03 +01:00
|
|
|
defer ctx.Check(srcDB.Close)
|
2019-09-17 22:42:40 +01:00
|
|
|
destDB := newMemDB(t)
|
2019-09-19 22:21:03 +01:00
|
|
|
defer ctx.Check(srcDB.Close)
|
2019-09-17 22:42:40 +01:00
|
|
|
|
|
|
|
query := `
|
|
|
|
CREATE TABLE bobby_jones(I Int);
|
|
|
|
INSERT INTO bobby_jones VALUES (1);
|
|
|
|
`
|
|
|
|
|
2020-01-14 11:29:25 +00:00
|
|
|
execSQL(ctx, t, srcDB, query)
|
2019-09-17 22:42:40 +01:00
|
|
|
// This table should be removed after migration
|
2020-01-14 11:29:25 +00:00
|
|
|
execSQL(ctx, t, srcDB, "CREATE TABLE what(I Int);")
|
2019-09-17 22:42:40 +01:00
|
|
|
|
|
|
|
err := sqliteutil.MigrateTablesToDatabase(ctx, srcDB, destDB, "bobby_jones")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-01-13 13:03:30 +00:00
|
|
|
destSchema, err := sqliteutil.QuerySchema(ctx, destDB)
|
2019-09-17 22:42:40 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-01-13 13:03:30 +00:00
|
|
|
destData, err := sqliteutil.QueryData(ctx, destDB, destSchema)
|
2019-09-17 22:42:40 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-01-13 13:03:30 +00:00
|
|
|
snapshot, err := sqliteutil.LoadSnapshotFromSQL(ctx, query)
|
2019-09-17 22:42:40 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, snapshot.Schema, destSchema)
|
|
|
|
require.Equal(t, snapshot.Data, destData)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKeepTables(t *testing.T) {
|
2019-09-19 22:21:03 +01:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2019-09-17 22:42:40 +01:00
|
|
|
db := newMemDB(t)
|
2019-09-19 22:21:03 +01:00
|
|
|
defer ctx.Check(db.Close)
|
2019-09-17 22:42:40 +01:00
|
|
|
|
|
|
|
table1SQL := `
|
|
|
|
CREATE TABLE table_one(I int);
|
|
|
|
INSERT INTO table_one VALUES(1);
|
|
|
|
`
|
|
|
|
|
|
|
|
table2SQL := `
|
|
|
|
CREATE TABLE table_two(I int);
|
|
|
|
INSERT INTO table_two VALUES(2);
|
|
|
|
`
|
|
|
|
|
2020-01-14 11:29:25 +00:00
|
|
|
execSQL(ctx, t, db, table1SQL)
|
|
|
|
execSQL(ctx, t, db, table2SQL)
|
2019-09-17 22:42:40 +01:00
|
|
|
|
|
|
|
err := sqliteutil.KeepTables(ctx, db, "table_one")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-01-13 13:03:30 +00:00
|
|
|
schema, err := sqliteutil.QuerySchema(ctx, db)
|
2019-09-17 22:42:40 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-01-13 13:03:30 +00:00
|
|
|
data, err := sqliteutil.QueryData(ctx, db, schema)
|
2019-09-17 22:42:40 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-01-13 13:03:30 +00:00
|
|
|
snapshot, err := sqliteutil.LoadSnapshotFromSQL(ctx, table1SQL)
|
2019-09-17 22:42:40 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, snapshot.Schema, schema)
|
|
|
|
require.Equal(t, snapshot.Data, data)
|
|
|
|
}
|
|
|
|
|
2020-01-14 11:29:25 +00:00
|
|
|
func execSQL(ctx context.Context, t *testing.T, db *sql.DB, query string, args ...interface{}) {
|
|
|
|
_, err := db.ExecContext(ctx, query, args...)
|
2019-09-17 22:42:40 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMemDB(t *testing.T) *sql.DB {
|
|
|
|
db, err := sql.Open("sqlite3", ":memory:")
|
|
|
|
require.NoError(t, err)
|
|
|
|
return db
|
|
|
|
}
|