Add core sqlite3 support using mattn/go-sqlite3

pull/2/head
John K. Luebs 2014-02-23 20:02:35 -05:00
parent 215fc52614
commit eac4a3744d
4 changed files with 36 additions and 1 deletions

View File

@ -181,7 +181,7 @@ goose will expand environment variables in the `open` element. For an example, s
## Other Drivers
goose knows about some common SQL drivers, but it can still be used to run Go-based migrations with any driver supported by database/sql. An import path and known dialect are required.
Currently, available dialects are: "postgres" or "mysql"
Currently, available dialects are: "postgres", "mysql", or "sqlite3"
To run Go-based migrations with another driver, specify its import path and dialect, as shown below.

View File

@ -95,6 +95,10 @@ func newDBDriver(name, open string) DBDriver {
case "mymysql":
d.Import = "github.com/ziutek/mymysql/godrv"
d.Dialect = &MySqlDialect{}
case "sqlite3":
d.Import = "github.com/mattn/go-sqlite3"
d.Dialect = &Sqlite3Dialect{}
}
return d

View File

@ -2,6 +2,7 @@ package goose
import (
"database/sql"
"github.com/mattn/go-sqlite3"
)
// SqlDialect abstracts the details of specific SQL dialects
@ -89,3 +90,32 @@ func (m *MySqlDialect) dbVersionQuery(db *sql.DB) (*sql.Rows, error) {
return rows, err
}
////////////////////////////
// sqlite3
////////////////////////////
type Sqlite3Dialect struct{}
func (m *Sqlite3Dialect) createVersionTableSql() string {
return `CREATE TABLE goose_db_version (
id INTEGER PRIMARY KEY AUTOINCREMENT,
version_id INTEGER NOT NULL,
is_applied INTEGER NOT NULL,
tstamp TEXT DEFAULT (datetime('now'))
);`
}
func (m *Sqlite3Dialect) insertVersionSql() string {
return "INSERT INTO goose_db_version (version_id, is_applied) VALUES (?, ?);"
}
func (m *Sqlite3Dialect) dbVersionQuery(db *sql.DB) (*sql.Rows, error) {
rows, err := db.Query("SELECT version_id, is_applied from goose_db_version ORDER BY id DESC")
switch err.(type) {
case sqlite3.Error:
return nil, ErrTableDoesNotExist
}
return rows, err
}

View File

@ -6,6 +6,7 @@ import (
"fmt"
_ "github.com/lib/pq"
_ "github.com/ziutek/mymysql/godrv"
_ "github.com/mattn/go-sqlite3"
"log"
"os"
"path/filepath"