d3885b7b78
* Added retry logic and tests for handling if crypto/rand fails. * Added retry logic and tests for handling if crypto/rand fails. * Fixing linting error. * Removing use of `crypto/rand` for use of `math/rand`. * Changing code comment about why we ignore this error.
117 lines
3.2 KiB
Go
117 lines
3.2 KiB
Go
// Copyright (C) 2019 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package migrate_test
|
|
|
|
import (
|
|
"database/sql"
|
|
"flag"
|
|
"os"
|
|
"strconv"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"storj.io/storj/internal/dbutil/pgutil"
|
|
"storj.io/storj/internal/migrate"
|
|
|
|
_ "github.com/lib/pq"
|
|
_ "github.com/mattn/go-sqlite3"
|
|
)
|
|
|
|
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
|
|
err = migrate.Create("example", &sqliteDB{db, "CREATE TABLE example_table (id text)"})
|
|
assert.NoError(t, err)
|
|
|
|
// shouldn't create a new table
|
|
err = migrate.Create("example", &sqliteDB{db, "CREATE TABLE example_table (id text)"})
|
|
assert.NoError(t, err)
|
|
|
|
// should fail, because schema changed
|
|
err = migrate.Create("example", &sqliteDB{db, "CREATE TABLE example_table (id text, version int)"})
|
|
assert.Error(t, err)
|
|
|
|
// should fail, because of trying to CREATE TABLE with same name
|
|
err = migrate.Create("conflict", &sqliteDB{db, "CREATE TABLE example_table (id text, version int)"})
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
// this connstring is expected to work under the storj-test docker-compose instance
|
|
const defaultPostgresConn = "postgres://storj:storj-pass@test-postgres/teststorj?sslmode=disable"
|
|
|
|
var testPostgres = flag.String("postgres-test-db", os.Getenv("STORJ_POSTGRES_TEST"), "PostgreSQL test database connection string")
|
|
|
|
func TestCreate_Postgres(t *testing.T) {
|
|
if *testPostgres == "" {
|
|
t.Skipf("postgres flag missing, example:\n-postgres-test-db=%s", defaultPostgresConn)
|
|
}
|
|
|
|
schema := "create-" + pgutil.CreateRandomTestingSchemaName(8)
|
|
|
|
db, err := sql.Open("postgres", pgutil.ConnstrWithSchema(*testPostgres, schema))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer func() { assert.NoError(t, db.Close()) }()
|
|
|
|
require.NoError(t, pgutil.CreateSchema(db, schema))
|
|
defer func() { assert.NoError(t, pgutil.DropSchema(db, schema)) }()
|
|
|
|
// should create table
|
|
err = migrate.Create("example", &postgresDB{db, "CREATE TABLE example_table (id text)"})
|
|
assert.NoError(t, err)
|
|
|
|
// shouldn't create a new table
|
|
err = migrate.Create("example", &postgresDB{db, "CREATE TABLE example_table (id text)"})
|
|
assert.NoError(t, err)
|
|
|
|
// should fail, because schema changed
|
|
err = migrate.Create("example", &postgresDB{db, "CREATE TABLE example_table (id text, version integer)"})
|
|
assert.Error(t, err)
|
|
|
|
// should fail, because of trying to CREATE TABLE with same name
|
|
err = migrate.Create("conflict", &postgresDB{db, "CREATE TABLE example_table (id text, version integer)"})
|
|
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 }
|