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

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