From 9c2aed8e4231749674dbd6f3586265b37d5b3269 Mon Sep 17 00:00:00 2001 From: Rodrigo Bernardi Date: Fri, 9 Aug 2019 19:25:39 -0300 Subject: [PATCH] Added oracle support --- Gopkg.toml | 4 ++++ cmd/goose/{main.go => app.go} | 8 ++++++-- dialect.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) rename cmd/goose/{main.go => app.go} (94%) diff --git a/Gopkg.toml b/Gopkg.toml index db42b8de6..e717bcc36 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -5,3 +5,7 @@ [[constraint]] branch = "master" name = "github.com/lib/pq" + +[[constraint]] + name = "gopkg.in/goracle.v2" + version = "2.18.2" diff --git a/cmd/goose/main.go b/cmd/goose/app.go similarity index 94% rename from cmd/goose/main.go rename to cmd/goose/app.go index cdb33c6f0..2cd936c81 100644 --- a/cmd/goose/main.go +++ b/cmd/goose/app.go @@ -3,15 +3,15 @@ package main import ( "database/sql" "flag" + "github.com/hashload/goose" "log" "os" - "github.com/pressly/goose" - // Init DB drivers. _ "github.com/go-sql-driver/mysql" _ "github.com/lib/pq" _ "github.com/ziutek/mymysql/godrv" + _ "gopkg.in/goracle.v2" ) var ( @@ -53,6 +53,8 @@ func main() { driver = "postgres" case "tidb": driver = "mysql" + case "oracle": + driver = "goracle" } switch dbstring { @@ -90,6 +92,7 @@ Drivers: mysql sqlite3 redshift + oracle Examples: goose sqlite3 ./foo.db status @@ -102,6 +105,7 @@ Examples: goose mysql "user:password@/dbname?parseTime=true" status goose redshift "postgres://user:password@qwerty.us-east-1.redshift.amazonaws.com:5439/db" status goose tidb "user:password@/dbname?parseTime=true" status + goose oracle "ora://user/password@localhost" status Options: ` diff --git a/dialect.go b/dialect.go index 488f5e040..682920d7d 100644 --- a/dialect.go +++ b/dialect.go @@ -33,6 +33,8 @@ func SetDialect(d string) error { dialect = &RedshiftDialect{} case "tidb": dialect = &TiDBDialect{} + case "oracle": + dialect = &OracleDialect{} default: return fmt.Errorf("%q: unknown dialect", d) } @@ -188,3 +190,33 @@ func (m TiDBDialect) dbVersionQuery(db *sql.DB) (*sql.Rows, error) { return rows, err } + +//////////////////////////// +// Oracle +//////////////////////////// + +// OracleDialect struct. +type OracleDialect struct{} + +func (OracleDialect) createVersionTableSQL() string { + return fmt.Sprintf(`CREATE TABLE "%[1]s" ( + id NUMBER GENERATED ALWAYS AS IDENTITY, + version_id NUMBER(19) NOT NULL, + is_applied char(1) NOT NULL, + tstamp TIMESTAMP(6) default SYS_EXTRACT_UTC(SYSTIMESTAMP) + ); + ALTER TABLE "%[1]s" ADD PRIMARY KEY ("ID");`, TableName()) +} + +func (OracleDialect) insertVersionSQL() string { + return fmt.Sprintf(`INSERT INTO "%s" (version_id, is_applied) VALUES (?, ?);`, TableName()) +} + +func (OracleDialect) dbVersionQuery(db *sql.DB) (*sql.Rows, error) { + rows, err := db.Query(fmt.Sprintf(`SELECT version_id, is_applied from "%s" ORDER BY id DESC`, TableName())) + if err != nil { + return nil, err + } + + return rows, err +}