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 (
|
|
|
|
"database/sql"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
2019-06-25 10:46:29 +01:00
|
|
|
_ "github.com/lib/pq"
|
|
|
|
_ "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"
|
2018-11-09 22:15:35 +00:00
|
|
|
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/dbutil/pgutil"
|
|
|
|
"storj.io/storj/private/dbutil/pgutil/pgtest"
|
|
|
|
"storj.io/storj/private/migrate"
|
2018-11-09 22:15:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreate_Sqlite(t *testing.T) {
|
|
|
|
db, err := sql.Open("sqlite3", ":memory:")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() { assert.NoError(t, db.Close()) }()
|
|
|
|
|
|
|
|
// should create table
|
2019-02-12 16:57:26 +00:00
|
|
|
err = migrate.Create("example", &sqliteDB{db, "CREATE TABLE example_table (id text)"})
|
2018-11-09 22:15:35 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// shouldn't create a new table
|
2019-02-12 16:57:26 +00:00
|
|
|
err = migrate.Create("example", &sqliteDB{db, "CREATE TABLE example_table (id text)"})
|
2018-11-09 22:15:35 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// should fail, because schema changed
|
2019-02-12 16:57:26 +00:00
|
|
|
err = migrate.Create("example", &sqliteDB{db, "CREATE TABLE example_table (id text, version int)"})
|
2018-11-09 22:15:35 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
// should fail, because of trying to CREATE TABLE with same name
|
2019-02-12 16:57:26 +00:00
|
|
|
err = migrate.Create("conflict", &sqliteDB{db, "CREATE TABLE example_table (id text, version int)"})
|
2018-11-09 22:15:35 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreate_Postgres(t *testing.T) {
|
2019-04-26 14:39:11 +01:00
|
|
|
if *pgtest.ConnStr == "" {
|
|
|
|
t.Skipf("postgres flag missing, example:\n-postgres-test-db=%s", pgtest.DefaultConnStr)
|
2018-11-09 22:15:35 +00:00
|
|
|
}
|
|
|
|
|
2019-04-02 17:52:25 +01:00
|
|
|
schema := "create-" + pgutil.CreateRandomTestingSchemaName(8)
|
2019-02-21 09:28:10 +00:00
|
|
|
|
2019-04-26 14:39:11 +01:00
|
|
|
db, err := sql.Open("postgres", pgutil.ConnstrWithSchema(*pgtest.ConnStr, schema))
|
2018-11-09 22:15:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() { assert.NoError(t, db.Close()) }()
|
|
|
|
|
2019-02-21 09:28:10 +00:00
|
|
|
require.NoError(t, pgutil.CreateSchema(db, schema))
|
|
|
|
defer func() { assert.NoError(t, pgutil.DropSchema(db, schema)) }()
|
|
|
|
|
2018-11-09 22:15:35 +00:00
|
|
|
// should create table
|
2019-02-12 16:57:26 +00:00
|
|
|
err = migrate.Create("example", &postgresDB{db, "CREATE TABLE example_table (id text)"})
|
2018-11-09 22:15:35 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// shouldn't create a new table
|
2019-02-12 16:57:26 +00:00
|
|
|
err = migrate.Create("example", &postgresDB{db, "CREATE TABLE example_table (id text)"})
|
2018-11-09 22:15:35 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// should fail, because schema changed
|
2019-02-12 16:57:26 +00:00
|
|
|
err = migrate.Create("example", &postgresDB{db, "CREATE TABLE example_table (id text, version integer)"})
|
2018-11-09 22:15:35 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
// should fail, because of trying to CREATE TABLE with same name
|
2019-02-12 16:57:26 +00:00
|
|
|
err = migrate.Create("conflict", &postgresDB{db, "CREATE TABLE example_table (id text, version integer)"})
|
2018-11-09 22:15:35 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type sqliteDB struct {
|
|
|
|
*sql.DB
|
|
|
|
schema string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *sqliteDB) Rebind(s string) string { return s }
|
|
|
|
func (db *sqliteDB) Schema() string { return db.schema }
|
|
|
|
|
|
|
|
type postgresDB struct {
|
|
|
|
*sql.DB
|
|
|
|
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 }
|