Ensure migrate tests cleanup after themselves (#1334)

This commit is contained in:
Egon Elbre 2019-02-21 11:28:10 +02:00 committed by GitHub
parent f552e7bc08
commit c7072620a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 3 deletions

View File

@ -11,7 +11,9 @@ import (
"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"
@ -52,12 +54,17 @@ func TestCreate_Postgres(t *testing.T) {
t.Skipf("postgres flag missing, example:\n-postgres-test-db=%s", defaultPostgresConn)
}
db, err := sql.Open("postgres", *testPostgres)
schema := "create-" + pgutil.RandomString(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)

View File

@ -16,6 +16,7 @@ import (
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/storj/internal/dbutil/pgutil"
"storj.io/storj/internal/migrate"
"storj.io/storj/internal/testcontext"
)
@ -33,10 +34,17 @@ func TestBasicMigrationPostgres(t *testing.T) {
t.Skipf("postgres flag missing, example:\n-postgres-test-db=%s", defaultPostgresConn)
}
db, err := sql.Open("postgres", *testPostgres)
require.NoError(t, err)
schema := "create-" + pgutil.RandomString(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)) }()
basicMigration(t, db, &postgresDB{DB: db})
}