github.com/Elate-DevOps/migrate/v4@v4.0.12/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 "log" 10 "strings" 11 "testing" 12 13 "github.com/Elate-DevOps/migrate/v4" 14 "github.com/dhui/dktest" 15 16 _ "github.com/lib/pq" 17 18 dt "github.com/Elate-DevOps/migrate/v4/database/testing" 19 "github.com/Elate-DevOps/migrate/v4/dktesting" 20 21 _ "github.com/Elate-DevOps/migrate/v4/source/file" 22 ) 23 24 const defaultPort = 26257 25 26 var ( 27 opts = dktest.Options{Cmd: []string{"start", "--insecure"}, PortRequired: true, ReadyFunc: isReady} 28 // Released versions: https://www.cockroachlabs.com/docs/releases/ 29 specs = []dktesting.ContainerSpec{ 30 {ImageName: "cockroachdb/cockroach:v1.0.7", Options: opts}, 31 {ImageName: "cockroachdb/cockroach:v1.1.9", Options: opts}, 32 {ImageName: "cockroachdb/cockroach:v2.0.7", Options: opts}, 33 {ImageName: "cockroachdb/cockroach:v2.1.3", Options: opts}, 34 } 35 ) 36 37 func isReady(ctx context.Context, c dktest.ContainerInfo) bool { 38 ip, port, err := c.Port(defaultPort) 39 if err != nil { 40 log.Println("port error:", err) 41 return false 42 } 43 44 db, err := sql.Open("postgres", fmt.Sprintf("postgres://root@%v:%v?sslmode=disable", ip, port)) 45 if err != nil { 46 log.Println("open error:", err) 47 return false 48 } 49 if err := db.PingContext(ctx); err != nil { 50 log.Println("ping error:", err) 51 return false 52 } 53 if err := db.Close(); err != nil { 54 log.Println("close error:", err) 55 } 56 return true 57 } 58 59 func createDB(t *testing.T, c dktest.ContainerInfo) { 60 ip, port, err := c.Port(defaultPort) 61 if err != nil { 62 t.Fatal(err) 63 } 64 65 db, err := sql.Open("postgres", fmt.Sprintf("postgres://root@%v:%v?sslmode=disable", ip, port)) 66 if err != nil { 67 t.Fatal(err) 68 } 69 if err = db.Ping(); err != nil { 70 t.Fatal(err) 71 } 72 defer func() { 73 if err := db.Close(); err != nil { 74 t.Error(err) 75 } 76 }() 77 78 if _, err = db.Exec("CREATE DATABASE migrate"); err != nil { 79 t.Fatal(err) 80 } 81 } 82 83 func Test(t *testing.T) { 84 dktesting.ParallelTest(t, specs, func(t *testing.T, ci dktest.ContainerInfo) { 85 createDB(t, ci) 86 87 ip, port, err := ci.Port(26257) 88 if err != nil { 89 t.Fatal(err) 90 } 91 92 addr := fmt.Sprintf("cockroach://root@%v:%v/migrate?sslmode=disable", ip, port) 93 c := &CockroachDb{} 94 d, err := c.Open(addr) 95 if err != nil { 96 t.Fatal(err) 97 } 98 dt.Test(t, d, []byte("SELECT 1")) 99 }) 100 } 101 102 func TestMigrate(t *testing.T) { 103 dktesting.ParallelTest(t, specs, func(t *testing.T, ci dktest.ContainerInfo) { 104 createDB(t, ci) 105 106 ip, port, err := ci.Port(26257) 107 if err != nil { 108 t.Fatal(err) 109 } 110 111 addr := fmt.Sprintf("cockroach://root@%v:%v/migrate?sslmode=disable", ip, port) 112 c := &CockroachDb{} 113 d, err := c.Open(addr) 114 if err != nil { 115 t.Fatal(err) 116 } 117 118 m, err := migrate.NewWithDatabaseInstance("file://./examples/migrations", "migrate", d) 119 if err != nil { 120 t.Fatal(err) 121 } 122 dt.TestMigrate(t, m) 123 }) 124 } 125 126 func TestMultiStatement(t *testing.T) { 127 dktesting.ParallelTest(t, specs, func(t *testing.T, ci dktest.ContainerInfo) { 128 createDB(t, ci) 129 130 ip, port, err := ci.Port(26257) 131 if err != nil { 132 t.Fatal(err) 133 } 134 135 addr := fmt.Sprintf("cockroach://root@%v:%v/migrate?sslmode=disable", ip, port) 136 c := &CockroachDb{} 137 d, err := c.Open(addr) 138 if err != nil { 139 t.Fatal(err) 140 } 141 if err := d.Run(strings.NewReader("CREATE TABLE foo (foo text); CREATE TABLE bar (bar text);")); err != nil { 142 t.Fatalf("expected err to be nil, got %v", err) 143 } 144 145 // make sure second table exists 146 var exists bool 147 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 { 148 t.Fatal(err) 149 } 150 if !exists { 151 t.Fatalf("expected table bar to exist") 152 } 153 }) 154 } 155 156 func TestFilterCustomQuery(t *testing.T) { 157 dktesting.ParallelTest(t, specs, func(t *testing.T, ci dktest.ContainerInfo) { 158 createDB(t, ci) 159 160 ip, port, err := ci.Port(26257) 161 if err != nil { 162 t.Fatal(err) 163 } 164 165 addr := fmt.Sprintf("cockroach://root@%v:%v/migrate?sslmode=disable&x-custom=foobar", ip, port) 166 c := &CockroachDb{} 167 _, err = c.Open(addr) 168 if err != nil { 169 t.Fatal(err) 170 } 171 }) 172 }