github.com/eatigo/migrate@v3.0.2-0.20210729130915-7610befb1b6b+incompatible/database/postgres/postgres_test.go (about)

     1  package postgres
     2  
     3  // error codes https://github.com/lib/pq/blob/master/error.go
     4  
     5  import (
     6  	"bytes"
     7  	"database/sql"
     8  	"fmt"
     9  	"io"
    10  	"testing"
    11  
    12  	"github.com/lib/pq"
    13  	dt "github.com/eatigo/migrate/database/testing"
    14  	mt "github.com/eatigo/migrate/testing"
    15  )
    16  
    17  var versions = []mt.Version{
    18  	{Image: "postgres:9.6"},
    19  	{Image: "postgres:9.5"},
    20  	{Image: "postgres:9.4"},
    21  	{Image: "postgres:9.3"},
    22  	{Image: "postgres:9.2"},
    23  }
    24  
    25  func isReady(i mt.Instance) bool {
    26  	db, err := sql.Open("postgres", fmt.Sprintf("postgres://postgres@%v:%v/postgres?sslmode=disable", i.Host(), i.Port()))
    27  	if err != nil {
    28  		return false
    29  	}
    30  	defer db.Close()
    31  	err = db.Ping()
    32  	if err == io.EOF {
    33  		return false
    34  
    35  	} else if e, ok := err.(*pq.Error); ok {
    36  		if e.Code.Name() == "cannot_connect_now" {
    37  			return false
    38  		}
    39  	}
    40  
    41  	return true
    42  }
    43  
    44  func Test(t *testing.T) {
    45  	mt.ParallelTest(t, versions, isReady,
    46  		func(t *testing.T, i mt.Instance) {
    47  			p := &Postgres{}
    48  			addr := fmt.Sprintf("postgres://postgres@%v:%v/postgres?sslmode=disable", i.Host(), i.Port())
    49  			d, err := p.Open(addr)
    50  			if err != nil {
    51  				t.Fatalf("%v", err)
    52  			}
    53  			dt.Test(t, d, []byte("SELECT 1"))
    54  		})
    55  }
    56  
    57  func TestMultiStatement(t *testing.T) {
    58  	mt.ParallelTest(t, versions, isReady,
    59  		func(t *testing.T, i mt.Instance) {
    60  			p := &Postgres{}
    61  			addr := fmt.Sprintf("postgres://postgres@%v:%v/postgres?sslmode=disable", i.Host(), i.Port())
    62  			d, err := p.Open(addr)
    63  			if err != nil {
    64  				t.Fatalf("%v", err)
    65  			}
    66  			if err := d.Run(bytes.NewReader([]byte("CREATE TABLE foo (foo text); CREATE TABLE bar (bar text);"))); err != nil {
    67  				t.Fatalf("expected err to be nil, got %v", err)
    68  			}
    69  
    70  			// make sure second table exists
    71  			var exists bool
    72  			if err := d.(*Postgres).db.QueryRow("SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'bar' AND table_schema = (SELECT current_schema()))").Scan(&exists); err != nil {
    73  				t.Fatal(err)
    74  			}
    75  			if !exists {
    76  				t.Fatalf("expected table bar to exist")
    77  			}
    78  		})
    79  }
    80  
    81  func TestFilterCustomQuery(t *testing.T) {
    82  	mt.ParallelTest(t, versions, isReady,
    83  		func(t *testing.T, i mt.Instance) {
    84  			p := &Postgres{}
    85  			addr := fmt.Sprintf("postgres://postgres@%v:%v/postgres?sslmode=disable&x-custom=foobar", i.Host(), i.Port())
    86  			_, err := p.Open(addr)
    87  			if err != nil {
    88  				t.Fatalf("%v", err)
    89  			}
    90  		})
    91  }
    92  
    93  func TestWithSchema(t *testing.T) {
    94  	mt.ParallelTest(t, versions, isReady,
    95  		func(t *testing.T, i mt.Instance) {
    96  			p := &Postgres{}
    97  			addr := fmt.Sprintf("postgres://postgres@%v:%v/postgres?sslmode=disable", i.Host(), i.Port())
    98  			d, err := p.Open(addr)
    99  			if err != nil {
   100  				t.Fatalf("%v", err)
   101  			}
   102  
   103  			// create foobar schema
   104  			if err := d.Run(bytes.NewReader([]byte("CREATE SCHEMA foobar AUTHORIZATION postgres"))); err != nil {
   105  				t.Fatal(err)
   106  			}
   107  			if err := d.SetVersion(1, false); err != nil {
   108  				t.Fatal(err)
   109  			}
   110  
   111  			// re-connect using that schema
   112  			d2, err := p.Open(fmt.Sprintf("postgres://postgres@%v:%v/postgres?sslmode=disable&search_path=foobar", i.Host(), i.Port()))
   113  			if err != nil {
   114  				t.Fatalf("%v", err)
   115  			}
   116  
   117  			version, _, err := d2.Version()
   118  			if err != nil {
   119  				t.Fatal(err)
   120  			}
   121  			if version != -1 {
   122  				t.Fatal("expected NilVersion")
   123  			}
   124  
   125  			// now update version and compare
   126  			if err := d2.SetVersion(2, false); err != nil {
   127  				t.Fatal(err)
   128  			}
   129  			version, _, err = d2.Version()
   130  			if err != nil {
   131  				t.Fatal(err)
   132  			}
   133  			if version != 2 {
   134  				t.Fatal("expected version 2")
   135  			}
   136  
   137  			// meanwhile, the public schema still has the other version
   138  			version, _, err = d.Version()
   139  			if err != nil {
   140  				t.Fatal(err)
   141  			}
   142  			if version != 1 {
   143  				t.Fatal("expected version 2")
   144  			}
   145  		})
   146  }
   147  
   148  func TestWithInstance(t *testing.T) {
   149  
   150  }