2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-09 22:15:35 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-02-12 16:57:26 +00:00
|
|
|
package migrate_test
|
2018-11-09 22:15:35 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
2020-06-28 04:56:29 +01:00
|
|
|
_ "github.com/jackc/pgx/v4/stdlib"
|
2019-06-25 10:46:29 +01:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2018-11-09 22:15:35 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-02-21 09:28:10 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-01-17 18:22:44 +00:00
|
|
|
|
2020-01-13 13:18:48 +00:00
|
|
|
"storj.io/common/testcontext"
|
2021-04-23 10:52:40 +01:00
|
|
|
"storj.io/private/dbutil/pgtest"
|
|
|
|
"storj.io/private/dbutil/tempdb"
|
|
|
|
"storj.io/private/tagsql"
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/migrate"
|
2018-11-09 22:15:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreate_Sqlite(t *testing.T) {
|
2020-01-13 13:18:48 +00:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2020-10-29 08:11:02 +00:00
|
|
|
db, err := tagsql.Open(ctx, "sqlite3", ":memory:")
|
2018-11-09 22:15:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-01-15 07:25:26 +00:00
|
|
|
defer func() { assert.NoError(t, db.Close()) }()
|
2018-11-09 22:15:35 +00:00
|
|
|
|
|
|
|
// should create table
|
2020-01-19 14:41:23 +00:00
|
|
|
err = migrate.Create(ctx, "example", &sqliteDB{db, "CREATE TABLE example_table (id text)"})
|
2019-12-04 03:36:21 +00:00
|
|
|
require.NoError(t, err)
|
2018-11-09 22:15:35 +00:00
|
|
|
|
|
|
|
// shouldn't create a new table
|
2020-01-19 14:41:23 +00:00
|
|
|
err = migrate.Create(ctx, "example", &sqliteDB{db, "CREATE TABLE example_table (id text)"})
|
2019-12-04 03:36:21 +00:00
|
|
|
require.NoError(t, err)
|
2018-11-09 22:15:35 +00:00
|
|
|
|
|
|
|
// should fail, because schema changed
|
2020-01-19 14:41:23 +00:00
|
|
|
err = migrate.Create(ctx, "example", &sqliteDB{db, "CREATE TABLE example_table (id text, version int)"})
|
2019-12-04 03:36:21 +00:00
|
|
|
require.Error(t, err)
|
2018-11-09 22:15:35 +00:00
|
|
|
|
|
|
|
// should fail, because of trying to CREATE TABLE with same name
|
2020-01-19 14:41:23 +00:00
|
|
|
err = migrate.Create(ctx, "conflict", &sqliteDB{db, "CREATE TABLE example_table (id text, version int)"})
|
2019-12-04 03:36:21 +00:00
|
|
|
require.Error(t, err)
|
2018-11-09 22:15:35 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 20:34:42 +01:00
|
|
|
func TestCreate(t *testing.T) {
|
|
|
|
pgtest.Run(t, func(ctx *testcontext.Context, t *testing.T, connstr string) {
|
|
|
|
db, err := tempdb.OpenUnique(ctx, connstr, "create-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() { assert.NoError(t, db.Close()) }()
|
2018-11-09 22:15:35 +00:00
|
|
|
|
2020-04-27 20:34:42 +01:00
|
|
|
// should create table
|
|
|
|
err = migrate.Create(ctx, "example", &postgresDB{db.DB, "CREATE TABLE example_table (id text)"})
|
|
|
|
require.NoError(t, err)
|
2018-11-09 22:15:35 +00:00
|
|
|
|
2020-04-27 20:34:42 +01:00
|
|
|
// shouldn't create a new table
|
|
|
|
err = migrate.Create(ctx, "example", &postgresDB{db.DB, "CREATE TABLE example_table (id text)"})
|
|
|
|
require.NoError(t, err)
|
2018-11-09 22:15:35 +00:00
|
|
|
|
2020-04-27 20:34:42 +01:00
|
|
|
// should fail, because schema changed
|
|
|
|
err = migrate.Create(ctx, "example", &postgresDB{db.DB, "CREATE TABLE example_table (id text, version integer)"})
|
|
|
|
require.Error(t, err)
|
2018-11-09 22:15:35 +00:00
|
|
|
|
2020-04-27 20:34:42 +01:00
|
|
|
// should fail, because of trying to CREATE TABLE with same name
|
|
|
|
err = migrate.Create(ctx, "conflict", &postgresDB{db.DB, "CREATE TABLE example_table (id text, version integer)"})
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
2018-11-09 22:15:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type sqliteDB struct {
|
2020-01-17 18:22:44 +00:00
|
|
|
tagsql.DB
|
2018-11-09 22:15:35 +00:00
|
|
|
schema string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *sqliteDB) Rebind(s string) string { return s }
|
|
|
|
func (db *sqliteDB) Schema() string { return db.schema }
|
|
|
|
|
|
|
|
type postgresDB struct {
|
2020-01-17 18:22:44 +00:00
|
|
|
tagsql.DB
|
2018-11-09 22:15:35 +00:00
|
|
|
schema string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *postgresDB) Rebind(sql string) string {
|
|
|
|
out := make([]byte, 0, len(sql)+10)
|
|
|
|
|
|
|
|
j := 1
|
|
|
|
for i := 0; i < len(sql); i++ {
|
|
|
|
ch := sql[i]
|
|
|
|
if ch != '?' {
|
|
|
|
out = append(out, ch)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
out = append(out, '$')
|
|
|
|
out = append(out, strconv.Itoa(j)...)
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(out)
|
|
|
|
}
|
|
|
|
func (db *postgresDB) Schema() string { return db.schema }
|