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