2017-06-14 19:46:10 +02:00
|
|
|
package spanner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2019-05-20 08:28:07 -07:00
|
|
|
)
|
2017-06-14 19:46:10 +02:00
|
|
|
|
2019-05-20 08:28:07 -07:00
|
|
|
import (
|
|
|
|
"github.com/golang-migrate/migrate/v4"
|
2018-10-10 15:11:48 -07:00
|
|
|
dt "github.com/golang-migrate/migrate/v4/database/testing"
|
2019-02-27 00:56:57 +01:00
|
|
|
_ "github.com/golang-migrate/migrate/v4/source/file"
|
2017-06-14 19:46:10 +02:00
|
|
|
)
|
|
|
|
|
2019-05-20 08:28:07 -07:00
|
|
|
import (
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2017-06-14 19:46:10 +02:00
|
|
|
func Test(t *testing.T) {
|
|
|
|
if testing.Short() {
|
2017-06-20 17:53:41 +01:00
|
|
|
t.Skip("skipping test in short mode.")
|
|
|
|
}
|
|
|
|
|
|
|
|
db, ok := os.LookupEnv("SPANNER_DATABASE")
|
|
|
|
if !ok {
|
|
|
|
t.Skip("SPANNER_DATABASE not set, skipping test.")
|
2017-06-14 19:46:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
s := &Spanner{}
|
2019-02-27 00:56:57 +01:00
|
|
|
addr := fmt.Sprintf("spanner://%s", db)
|
2017-06-14 19:46:10 +02:00
|
|
|
d, err := s.Open(addr)
|
|
|
|
if err != nil {
|
2019-04-27 01:47:16 +03:00
|
|
|
t.Fatal(err)
|
2017-06-14 19:46:10 +02:00
|
|
|
}
|
|
|
|
dt.Test(t, d, []byte("SELECT 1"))
|
|
|
|
}
|
2019-02-27 00:56:57 +01:00
|
|
|
|
|
|
|
func TestMigrate(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping test in short mode.")
|
|
|
|
}
|
|
|
|
|
|
|
|
db, ok := os.LookupEnv("SPANNER_DATABASE")
|
|
|
|
if !ok {
|
|
|
|
t.Skip("SPANNER_DATABASE not set, skipping test.")
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &Spanner{}
|
|
|
|
addr := fmt.Sprintf("spanner://%s", db)
|
|
|
|
d, err := s.Open(addr)
|
|
|
|
if err != nil {
|
2019-04-27 01:47:16 +03:00
|
|
|
t.Fatal(err)
|
2019-02-27 00:56:57 +01:00
|
|
|
}
|
|
|
|
m, err := migrate.NewWithDatabaseInstance("file://./examples/migrations", db, d)
|
|
|
|
if err != nil {
|
2019-04-27 01:47:16 +03:00
|
|
|
t.Fatal(err)
|
2019-02-27 00:56:57 +01:00
|
|
|
}
|
|
|
|
dt.TestMigrate(t, m, []byte("SELECT 1"))
|
|
|
|
}
|
2019-05-20 08:28:07 -07:00
|
|
|
|
|
|
|
func TestMultistatementSplit(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
multiStatement string
|
|
|
|
expected []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "single statement, single line, no semicolon",
|
|
|
|
multiStatement: "CREATE TABLE table_name (id STRING(255) NOT NULL) PRIMARY KEY (id)",
|
|
|
|
expected: []string{"CREATE TABLE table_name (id STRING(255) NOT NULL) PRIMARY KEY (id)"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "single statement, multi line, no semicolon",
|
|
|
|
multiStatement: `CREATE TABLE table_name (
|
|
|
|
id STRING(255) NOT NULL,
|
|
|
|
) PRIMARY KEY (id)`,
|
|
|
|
expected: []string{`CREATE TABLE table_name (
|
|
|
|
id STRING(255) NOT NULL,
|
|
|
|
) PRIMARY KEY (id)`},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "single statement, single line, with semicolon",
|
|
|
|
multiStatement: "CREATE TABLE table_name (id STRING(255) NOT NULL) PRIMARY KEY (id);",
|
|
|
|
expected: []string{"CREATE TABLE table_name (id STRING(255) NOT NULL) PRIMARY KEY (id)"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "single statement, multi line, with semicolon",
|
|
|
|
multiStatement: `CREATE TABLE table_name (
|
|
|
|
id STRING(255) NOT NULL,
|
|
|
|
) PRIMARY KEY (id);`,
|
|
|
|
expected: []string{`CREATE TABLE table_name (
|
|
|
|
id STRING(255) NOT NULL,
|
|
|
|
) PRIMARY KEY (id)`},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multi statement, with trailing semicolon",
|
|
|
|
// From https://github.com/mattes/migrate/pull/281
|
|
|
|
multiStatement: `CREATE TABLE table_name (
|
|
|
|
id STRING(255) NOT NULL,
|
|
|
|
) PRIMARY KEY(id);
|
|
|
|
|
|
|
|
CREATE INDEX table_name_id_idx ON table_name (id);`,
|
|
|
|
expected: []string{`CREATE TABLE table_name (
|
|
|
|
id STRING(255) NOT NULL,
|
|
|
|
) PRIMARY KEY(id)`, "\n\nCREATE INDEX table_name_id_idx ON table_name (id)"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multi statement, no trailing semicolon",
|
|
|
|
// From https://github.com/mattes/migrate/pull/281
|
|
|
|
multiStatement: `CREATE TABLE table_name (
|
|
|
|
id STRING(255) NOT NULL,
|
|
|
|
) PRIMARY KEY(id);
|
|
|
|
|
|
|
|
CREATE INDEX table_name_id_idx ON table_name (id)`,
|
|
|
|
expected: []string{`CREATE TABLE table_name (
|
|
|
|
id STRING(255) NOT NULL,
|
|
|
|
) PRIMARY KEY(id)`, "\n\nCREATE INDEX table_name_id_idx ON table_name (id)"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
if stmts := migrationStatements([]byte(tc.multiStatement)); !assert.Equal(t, stmts, tc.expected) {
|
|
|
|
t.Error()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|