github.com/matcornic/migrate@v3.3.2-0.20180717234201-feea45c20506+incompatible/database/ql/ql_test.go (about)

     1  package ql
     2  
     3  import (
     4  	"database/sql"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	_ "github.com/cznic/ql/driver"
    12  	"github.com/golang-migrate/migrate"
    13  	dt "github.com/golang-migrate/migrate/database/testing"
    14  	_ "github.com/golang-migrate/migrate/source/file"
    15  )
    16  
    17  func Test(t *testing.T) {
    18  	dir, err := ioutil.TempDir("", "ql-driver-test")
    19  	if err != nil {
    20  		return
    21  	}
    22  	defer func() {
    23  		os.RemoveAll(dir)
    24  	}()
    25  	fmt.Printf("DB path : %s\n", filepath.Join(dir, "ql.db"))
    26  	p := &Ql{}
    27  	addr := fmt.Sprintf("ql://%s", filepath.Join(dir, "ql.db"))
    28  	d, err := p.Open(addr)
    29  	if err != nil {
    30  		t.Fatalf("%v", err)
    31  	}
    32  
    33  	db, err := sql.Open("ql", filepath.Join(dir, "ql.db"))
    34  	if err != nil {
    35  		return
    36  	}
    37  	defer func() {
    38  		if err := db.Close(); err != nil {
    39  			return
    40  		}
    41  	}()
    42  	dt.Test(t, d, []byte("CREATE TABLE t (Qty int, Name string);"))
    43  	driver, err := WithInstance(db, &Config{})
    44  	if err != nil {
    45  		t.Fatalf("%v", err)
    46  	}
    47  	if err := d.Drop(); err != nil {
    48  		t.Fatal(err)
    49  	}
    50  
    51  	m, err := migrate.NewWithDatabaseInstance(
    52  		"file://./migration",
    53  		"ql", driver)
    54  	if err != nil {
    55  		t.Fatalf("%v", err)
    56  	}
    57  	fmt.Println("UP")
    58  	err = m.Up()
    59  	if err != nil {
    60  		t.Fatalf("%v", err)
    61  	}
    62  }