Skip to content

Commit

Permalink
Added oracle support
Browse files Browse the repository at this point in the history
  • Loading branch information
snakeice committed Aug 9, 2019
1 parent a8e16d5 commit 9c2aed8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@
[[constraint]]
branch = "master"
name = "github.com/lib/pq"

[[constraint]]
name = "gopkg.in/goracle.v2"
version = "2.18.2"
8 changes: 6 additions & 2 deletions cmd/goose/main.go → cmd/goose/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -53,6 +53,8 @@ func main() {
driver = "postgres"
case "tidb":
driver = "mysql"
case "oracle":
driver = "goracle"
}

switch dbstring {
Expand Down Expand Up @@ -90,6 +92,7 @@ Drivers:
mysql
sqlite3
redshift
oracle
Examples:
goose sqlite3 ./foo.db status
Expand All @@ -102,6 +105,7 @@ Examples:
goose mysql "user:password@/dbname?parseTime=true" status
goose redshift "postgres://user:[email protected]:5439/db" status
goose tidb "user:password@/dbname?parseTime=true" status
goose oracle "ora://user/password@localhost" status
Options:
`
Expand Down
32 changes: 32 additions & 0 deletions dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}

0 comments on commit 9c2aed8

Please sign in to comment.