2017-02-08 06:01:29 +00:00
package postgres
// error codes https://github.com/lib/pq/blob/master/error.go
import (
"bytes"
"database/sql"
2018-01-20 07:57:43 +00:00
sqldriver "database/sql/driver"
2017-02-08 06:01:29 +00:00
"fmt"
"io"
"testing"
2017-10-27 02:56:10 +00:00
"context"
2018-01-19 18:56:55 +00:00
dt "github.com/golang-migrate/migrate/database/testing"
mt "github.com/golang-migrate/migrate/testing"
2018-01-20 07:57:43 +00:00
// "github.com/lib/pq"
2017-02-08 06:01:29 +00:00
)
2017-02-28 23:10:56 +00:00
var versions = [ ] mt . Version {
{ Image : "postgres:9.6" } ,
{ Image : "postgres:9.5" } ,
{ Image : "postgres:9.4" } ,
{ Image : "postgres:9.3" } ,
{ Image : "postgres:9.2" } ,
2017-02-08 06:01:29 +00:00
}
func isReady ( i mt . Instance ) bool {
db , err := sql . Open ( "postgres" , fmt . Sprintf ( "postgres://postgres@%v:%v/postgres?sslmode=disable" , i . Host ( ) , i . Port ( ) ) )
if err != nil {
return false
}
defer db . Close ( )
2018-01-20 07:57:43 +00:00
if err = db . Ping ( ) ; err != nil {
switch err {
case sqldriver . ErrBadConn , io . EOF :
2017-02-08 06:01:29 +00:00
return false
2018-01-20 07:57:43 +00:00
default :
fmt . Println ( err )
2017-02-08 06:01:29 +00:00
}
2018-01-20 07:57:43 +00:00
return false
2017-02-08 06:01:29 +00:00
}
return true
}
func Test ( t * testing . T ) {
mt . ParallelTest ( t , versions , isReady ,
func ( t * testing . T , i mt . Instance ) {
p := & Postgres { }
2017-02-08 09:49:57 +00:00
addr := fmt . Sprintf ( "postgres://postgres@%v:%v/postgres?sslmode=disable" , i . Host ( ) , i . Port ( ) )
d , err := p . Open ( addr )
2017-02-08 06:01:29 +00:00
if err != nil {
2017-02-08 09:49:57 +00:00
t . Fatalf ( "%v" , err )
2017-02-08 06:01:29 +00:00
}
2018-01-20 07:57:43 +00:00
defer d . Close ( )
2017-02-08 06:01:29 +00:00
dt . Test ( t , d , [ ] byte ( "SELECT 1" ) )
} )
}
2017-02-17 06:10:01 +00:00
func TestMultiStatement ( t * testing . T ) {
mt . ParallelTest ( t , versions , isReady ,
func ( t * testing . T , i mt . Instance ) {
p := & Postgres { }
addr := fmt . Sprintf ( "postgres://postgres@%v:%v/postgres?sslmode=disable" , i . Host ( ) , i . Port ( ) )
d , err := p . Open ( addr )
if err != nil {
t . Fatalf ( "%v" , err )
}
2018-01-20 07:57:43 +00:00
defer d . Close ( )
2017-02-19 23:15:00 +00:00
if err := d . Run ( bytes . NewReader ( [ ] byte ( "CREATE TABLE foo (foo text); CREATE TABLE bar (bar text);" ) ) ) ; err != nil {
2017-02-17 06:10:01 +00:00
t . Fatalf ( "expected err to be nil, got %v" , err )
}
// make sure second table exists
var exists bool
2018-02-20 23:40:20 +00:00
if err := d . ( * Postgres ) . conn . QueryRowContext ( context . Background ( ) , "SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'bar' AND table_schema = (SELECT current_schema()))" ) . Scan ( & exists ) ; err != nil {
2017-02-17 06:10:01 +00:00
t . Fatal ( err )
}
if ! exists {
t . Fatalf ( "expected table bar to exist" )
}
} )
}
2017-02-18 00:59:47 +00:00
func TestFilterCustomQuery ( t * testing . T ) {
mt . ParallelTest ( t , versions , isReady ,
func ( t * testing . T , i mt . Instance ) {
p := & Postgres { }
addr := fmt . Sprintf ( "postgres://postgres@%v:%v/postgres?sslmode=disable&x-custom=foobar" , i . Host ( ) , i . Port ( ) )
2018-01-20 07:57:43 +00:00
d , err := p . Open ( addr )
2017-02-18 00:59:47 +00:00
if err != nil {
t . Fatalf ( "%v" , err )
}
2018-01-20 07:57:43 +00:00
defer d . Close ( )
2017-02-18 00:59:47 +00:00
} )
}
2017-02-08 06:01:29 +00:00
func TestWithSchema ( t * testing . T ) {
mt . ParallelTest ( t , versions , isReady ,
func ( t * testing . T , i mt . Instance ) {
p := & Postgres { }
2017-02-08 09:49:57 +00:00
addr := fmt . Sprintf ( "postgres://postgres@%v:%v/postgres?sslmode=disable" , i . Host ( ) , i . Port ( ) )
d , err := p . Open ( addr )
2017-02-08 06:01:29 +00:00
if err != nil {
2017-02-08 09:49:57 +00:00
t . Fatalf ( "%v" , err )
2017-02-08 06:01:29 +00:00
}
2018-01-20 07:57:43 +00:00
defer d . Close ( )
2017-02-08 06:01:29 +00:00
// create foobar schema
2017-02-19 23:15:00 +00:00
if err := d . Run ( bytes . NewReader ( [ ] byte ( "CREATE SCHEMA foobar AUTHORIZATION postgres" ) ) ) ; err != nil {
t . Fatal ( err )
}
if err := d . SetVersion ( 1 , false ) ; err != nil {
2017-02-08 06:01:29 +00:00
t . Fatal ( err )
}
// re-connect using that schema
d2 , err := p . Open ( fmt . Sprintf ( "postgres://postgres@%v:%v/postgres?sslmode=disable&search_path=foobar" , i . Host ( ) , i . Port ( ) ) )
if err != nil {
2017-02-08 09:49:57 +00:00
t . Fatalf ( "%v" , err )
2017-02-08 06:01:29 +00:00
}
2018-01-20 07:57:43 +00:00
defer d2 . Close ( )
2017-02-08 06:01:29 +00:00
2017-02-19 23:15:00 +00:00
version , _ , err := d2 . Version ( )
2017-02-08 06:01:29 +00:00
if err != nil {
t . Fatal ( err )
}
if version != - 1 {
t . Fatal ( "expected NilVersion" )
}
// now update version and compare
2017-02-19 23:15:00 +00:00
if err := d2 . SetVersion ( 2 , false ) ; err != nil {
2017-02-08 06:01:29 +00:00
t . Fatal ( err )
}
2017-02-19 23:15:00 +00:00
version , _ , err = d2 . Version ( )
2017-02-08 06:01:29 +00:00
if err != nil {
t . Fatal ( err )
}
if version != 2 {
t . Fatal ( "expected version 2" )
}
// meanwhile, the public schema still has the other version
2017-02-19 23:15:00 +00:00
version , _ , err = d . Version ( )
2017-02-08 06:01:29 +00:00
if err != nil {
t . Fatal ( err )
}
2017-02-19 23:15:00 +00:00
if version != 1 {
2017-02-08 06:01:29 +00:00
t . Fatal ( "expected version 2" )
}
} )
}
func TestWithInstance ( t * testing . T ) {
}
2017-10-27 02:56:10 +00:00
func TestPostgres_Lock ( t * testing . T ) {
mt . ParallelTest ( t , versions , isReady ,
func ( t * testing . T , i mt . Instance ) {
p := & Postgres { }
addr := fmt . Sprintf ( "postgres://postgres@%v:%v/postgres?sslmode=disable" , i . Host ( ) , i . Port ( ) )
d , err := p . Open ( addr )
if err != nil {
t . Fatalf ( "%v" , err )
}
dt . Test ( t , d , [ ] byte ( "SELECT 1" ) )
ps := d . ( * Postgres )
err = ps . Lock ( )
if err != nil {
t . Fatal ( err )
}
err = ps . Unlock ( )
if err != nil {
t . Fatal ( err )
}
err = ps . Lock ( )
if err != nil {
t . Fatal ( err )
}
err = ps . Unlock ( )
if err != nil {
t . Fatal ( err )
}
} )
2018-02-20 23:32:34 +00:00
}