github.com/mrqzzz/migrate@v5.1.7+incompatible/database/cockroachdb/cockroachdb_test.go (about) 1 package cockroachdb 2 3 // error codes https://github.com/lib/pq/blob/master/error.go 4 5 import ( 6 "context" 7 "database/sql" 8 "fmt" 9 "strings" 10 "testing" 11 ) 12 13 import ( 14 "github.com/dhui/dktest" 15 _ "github.com/lib/pq" 16 ) 17 18 import ( 19 dt "github.com/golang-migrate/migrate/v4/database/testing" 20 "github.com/golang-migrate/migrate/v4/dktesting" 21 ) 22 23 const defaultPort = 26257 24 25 var ( 26 opts = dktest.Options{Cmd: []string{"start", "--insecure"}, PortRequired: true, ReadyFunc: isReady} 27 // Released versions: https://www.cockroachlabs.com/docs/releases/ 28 specs = []dktesting.ContainerSpec{ 29 {ImageName: "cockroachdb/cockroach:v1.0.7", Options: opts}, 30 {ImageName: "cockroachdb/cockroach:v1.1.9", Options: opts}, 31 {ImageName: "cockroachdb/cockroach:v2.0.7", Options: opts}, 32 {ImageName: "cockroachdb/cockroach:v2.1.3", Options: opts}, 33 } 34 ) 35 36 func isReady(ctx context.Context, c dktest.ContainerInfo) bool { 37 ip, port, err := c.Port(defaultPort) 38 if err != nil { 39 fmt.Println("port error:", err) 40 return false 41 } 42 43 db, err := sql.Open("postgres", fmt.Sprintf("postgres://root@%v:%v?sslmode=disable", ip, port)) 44 if err != nil { 45 fmt.Println("open error:", err) 46 return false 47 } 48 if err := db.PingContext(ctx); err != nil { 49 fmt.Println("ping error:", err) 50 return false 51 } 52 db.Close() 53 return true 54 } 55 56 func createDB(t *testing.T, c dktest.ContainerInfo) { 57 ip, port, err := c.Port(defaultPort) 58 if err != nil { 59 t.Fatal(err) 60 } 61 62 db, err := sql.Open("postgres", fmt.Sprintf("postgres://root@%v:%v?sslmode=disable", ip, port)) 63 if err != nil { 64 t.Fatal(err) 65 } 66 if err = db.Ping(); err != nil { 67 t.Fatal(err) 68 } 69 defer db.Close() 70 71 if _, err = db.Exec("CREATE DATABASE migrate"); err != nil { 72 t.Fatal(err) 73 } 74 } 75 76 func Test(t *testing.T) { 77 dktesting.ParallelTest(t, specs, func(t *testing.T, ci dktest.ContainerInfo) { 78 createDB(t, ci) 79 80 ip, port, err := ci.Port(26257) 81 if err != nil { 82 t.Fatal(err) 83 } 84 85 addr := fmt.Sprintf("cockroach://root@%v:%v/migrate?sslmode=disable", ip, port) 86 c := &CockroachDb{} 87 d, err := c.Open(addr) 88 if err != nil { 89 t.Fatalf("%v", err) 90 } 91 dt.Test(t, d, []byte("SELECT 1")) 92 }) 93 } 94 95 func TestMultiStatement(t *testing.T) { 96 dktesting.ParallelTest(t, specs, func(t *testing.T, ci dktest.ContainerInfo) { 97 createDB(t, ci) 98 99 ip, port, err := ci.Port(26257) 100 if err != nil { 101 t.Fatal(err) 102 } 103 104 addr := fmt.Sprintf("cockroach://root@%v:%v/migrate?sslmode=disable", ip, port) 105 c := &CockroachDb{} 106 d, err := c.Open(addr) 107 if err != nil { 108 t.Fatalf("%v", err) 109 } 110 if err := d.Run(strings.NewReader("CREATE TABLE foo (foo text); CREATE TABLE bar (bar text);")); err != nil { 111 t.Fatalf("expected err to be nil, got %v", err) 112 } 113 114 // make sure second table exists 115 var exists bool 116 if err := d.(*CockroachDb).db.QueryRow("SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'bar' AND table_schema = (SELECT current_schema()))").Scan(&exists); err != nil { 117 t.Fatal(err) 118 } 119 if !exists { 120 t.Fatalf("expected table bar to exist") 121 } 122 }) 123 } 124 125 func TestFilterCustomQuery(t *testing.T) { 126 dktesting.ParallelTest(t, specs, func(t *testing.T, ci dktest.ContainerInfo) { 127 createDB(t, ci) 128 129 ip, port, err := ci.Port(26257) 130 if err != nil { 131 t.Fatal(err) 132 } 133 134 addr := fmt.Sprintf("cockroach://root@%v:%v/migrate?sslmode=disable&x-custom=foobar", ip, port) 135 c := &CockroachDb{} 136 _, err = c.Open(addr) 137 if err != nil { 138 t.Fatalf("%v", err) 139 } 140 }) 141 }