diff --git a/README.md b/README.md index e068a74..68bae7e 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/goose/dbconf.go b/lib/goose/dbconf.go index 9917b8c..85aad79 100644 --- a/lib/goose/dbconf.go +++ b/lib/goose/dbconf.go @@ -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 diff --git a/lib/goose/dialect.go b/lib/goose/dialect.go index f9416bb..758ddda 100644 --- a/lib/goose/dialect.go +++ b/lib/goose/dialect.go @@ -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 +} diff --git a/lib/goose/migrate.go b/lib/goose/migrate.go index c728864..f83667a 100644 --- a/lib/goose/migrate.go +++ b/lib/goose/migrate.go @@ -6,6 +6,7 @@ import ( "fmt" _ "github.com/lib/pq" _ "github.com/ziutek/mymysql/godrv" + _ "github.com/mattn/go-sqlite3" "log" "os" "path/filepath"