github.com/scraniel/migrate@v0.0.0-20230320185700-339088f36cee/database/sqlite3/sqlite3_test.go (about) 1 package sqlite3 2 3 import ( 4 "database/sql" 5 "fmt" 6 "path/filepath" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 11 "github.com/golang-migrate/migrate/v4" 12 dt "github.com/golang-migrate/migrate/v4/database/testing" 13 _ "github.com/golang-migrate/migrate/v4/source/file" 14 _ "github.com/mattn/go-sqlite3" 15 ) 16 17 func Test(t *testing.T) { 18 dir := t.TempDir() 19 t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db")) 20 p := &Sqlite{} 21 addr := fmt.Sprintf("sqlite3://%s", filepath.Join(dir, "sqlite3.db")) 22 d, err := p.Open(addr) 23 if err != nil { 24 t.Fatal(err) 25 } 26 dt.Test(t, d, []byte("CREATE TABLE t (Qty int, Name string);")) 27 } 28 29 func TestMigrate(t *testing.T) { 30 dir := t.TempDir() 31 t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db")) 32 33 db, err := sql.Open("sqlite3", filepath.Join(dir, "sqlite3.db")) 34 if err != nil { 35 return 36 } 37 defer func() { 38 if err := db.Close(); err != nil { 39 return 40 } 41 }() 42 driver, err := WithInstance(db, &Config{}) 43 if err != nil { 44 t.Fatal(err) 45 } 46 47 m, err := migrate.NewWithDatabaseInstance( 48 "file://./examples/migrations", 49 "ql", driver) 50 if err != nil { 51 t.Fatal(err) 52 } 53 dt.TestMigrate(t, m) 54 } 55 56 func TestMigrationTable(t *testing.T) { 57 dir := t.TempDir() 58 59 t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db")) 60 61 db, err := sql.Open("sqlite3", filepath.Join(dir, "sqlite3.db")) 62 if err != nil { 63 return 64 } 65 defer func() { 66 if err := db.Close(); err != nil { 67 return 68 } 69 }() 70 71 config := &Config{ 72 MigrationsTable: "my_migration_table", 73 } 74 driver, err := WithInstance(db, config) 75 if err != nil { 76 t.Fatal(err) 77 } 78 m, err := migrate.NewWithDatabaseInstance( 79 "file://./examples/migrations", 80 "ql", driver) 81 if err != nil { 82 t.Fatal(err) 83 } 84 t.Log("UP") 85 err = m.Up() 86 if err != nil { 87 t.Fatal(err) 88 } 89 90 _, err = db.Query(fmt.Sprintf("SELECT * FROM %s", config.MigrationsTable)) 91 if err != nil { 92 t.Fatal(err) 93 } 94 } 95 96 func TestNoTxWrap(t *testing.T) { 97 dir := t.TempDir() 98 t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db")) 99 p := &Sqlite{} 100 addr := fmt.Sprintf("sqlite3://%s?x-no-tx-wrap=true", filepath.Join(dir, "sqlite3.db")) 101 d, err := p.Open(addr) 102 if err != nil { 103 t.Fatal(err) 104 } 105 // An explicit BEGIN statement would ordinarily fail without x-no-tx-wrap. 106 // (Transactions in sqlite may not be nested.) 107 dt.Test(t, d, []byte("BEGIN; CREATE TABLE t (Qty int, Name string); COMMIT;")) 108 } 109 110 func TestNoTxWrapInvalidValue(t *testing.T) { 111 dir := t.TempDir() 112 t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db")) 113 p := &Sqlite{} 114 addr := fmt.Sprintf("sqlite3://%s?x-no-tx-wrap=yeppers", filepath.Join(dir, "sqlite3.db")) 115 _, err := p.Open(addr) 116 if assert.Error(t, err) { 117 assert.Contains(t, err.Error(), "x-no-tx-wrap") 118 assert.Contains(t, err.Error(), "invalid syntax") 119 } 120 } 121 122 func TestMigrateWithDirectoryNameContainsWhitespaces(t *testing.T) { 123 dir := t.TempDir() 124 dbPath := filepath.Join(dir, "sqlite3.db") 125 t.Logf("DB path : %s\n", dbPath) 126 p := &Sqlite{} 127 addr := fmt.Sprintf("sqlite3://file:%s", dbPath) 128 d, err := p.Open(addr) 129 if err != nil { 130 t.Fatal(err) 131 } 132 dt.Test(t, d, []byte("CREATE TABLE t (Qty int, Name string);")) 133 }