Skip to content

Commit

Permalink
Merge pull request #166 from bcho/fix/sqlite3-migration-table
Browse files Browse the repository at this point in the history
sqlite3: fix wrong schema table usage
  • Loading branch information
dhui authored Feb 2, 2019
2 parents f6d624c + 95e701d commit 598addc
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 10 deletions.
9 changes: 5 additions & 4 deletions database/sqlite3/sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package sqlite3
import (
"database/sql"
"fmt"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database"
_ "github.com/mattn/go-sqlite3"
"io"
"io/ioutil"
nurl "net/url"
"strings"

"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database"
_ "github.com/mattn/go-sqlite3"
)

func init() {
Expand Down Expand Up @@ -62,7 +63,7 @@ func (m *Sqlite) ensureVersionTable() error {
query := fmt.Sprintf(`
CREATE TABLE IF NOT EXISTS %s (version uint64,dirty bool);
CREATE UNIQUE INDEX IF NOT EXISTS version_unique ON %s (version);
`, DefaultMigrationsTable, DefaultMigrationsTable)
`, m.config.MigrationsTable, m.config.MigrationsTable)

if _, err := m.db.Exec(query); err != nil {
return err
Expand Down
59 changes: 53 additions & 6 deletions database/sqlite3/sqlite3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package sqlite3
import (
"database/sql"
"fmt"
"github.com/golang-migrate/migrate/v4"
dt "github.com/golang-migrate/migrate/v4/database/testing"
_ "github.com/golang-migrate/migrate/v4/source/file"
_ "github.com/mattn/go-sqlite3"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/golang-migrate/migrate/v4"
dt "github.com/golang-migrate/migrate/v4/database/testing"
_ "github.com/golang-migrate/migrate/v4/source/file"
_ "github.com/mattn/go-sqlite3"
)

func Test(t *testing.T) {
Expand All @@ -21,7 +22,7 @@ func Test(t *testing.T) {
defer func() {
os.RemoveAll(dir)
}()
fmt.Printf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
p := &Sqlite{}
addr := fmt.Sprintf("sqlite3://%s", filepath.Join(dir, "sqlite3.db"))
d, err := p.Open(addr)
Expand Down Expand Up @@ -53,9 +54,55 @@ func Test(t *testing.T) {
if err != nil {
t.Fatalf("%v", err)
}
fmt.Println("UP")
t.Log("UP")
err = m.Up()
if err != nil {
t.Fatalf("%v", err)
}
}

func TestMigrationTable(t *testing.T) {
dir, err := ioutil.TempDir("", "sqlite3-driver-test-migration-table")
if err != nil {
return
}
defer func() {
os.RemoveAll(dir)
}()

t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))

db, err := sql.Open("sqlite3", filepath.Join(dir, "sqlite3.db"))
if err != nil {
return
}
defer func() {
if err := db.Close(); err != nil {
return
}
}()

config := &Config{
MigrationsTable: "my_migration_table",
}
driver, err := WithInstance(db, config)
if err != nil {
t.Fatalf("%v", err)
}
m, err := migrate.NewWithDatabaseInstance(
"file://./migration",
"ql", driver)
if err != nil {
t.Fatalf("%v", err)
}
t.Log("UP")
err = m.Up()
if err != nil {
t.Fatalf("%v", err)
}

_, err = db.Query(fmt.Sprintf("SELECT * FROM %s", config.MigrationsTable))
if err != nil {
t.Fatalf("%v", err)
}
}

0 comments on commit 598addc

Please sign in to comment.