Skip to content

Commit

Permalink
fix: oracle database does support 'if not exists' and 'text' data type
Browse files Browse the repository at this point in the history
  • Loading branch information
fanpei91 committed Feb 8, 2020
1 parent 8794cec commit f6e5886
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,5 +1,6 @@
.*.swp
*.test
.idea

/sql-migrate/test.db
/test.db
33 changes: 27 additions & 6 deletions migrate.go
Expand Up @@ -2,12 +2,12 @@ package migrate

import (
"bytes"
"os"
"database/sql"
"errors"
"fmt"
"io"
"net/http"
"os"
"path"
"regexp"
"sort"
Expand Down Expand Up @@ -171,12 +171,28 @@ type MigrationRecord struct {
AppliedAt time.Time `db:"applied_at"`
}

type OracleDialect struct {
gorp.OracleDialect
}

func (d OracleDialect) IfTableNotExists(command, schema, table string) string {
return command
}

func (d OracleDialect) IfSchemaNotExists(command, schema string) string {
return command
}

func (d OracleDialect) IfTableExists(command, schema, table string) string {
return command
}

var MigrationDialects = map[string]gorp.Dialect{
"sqlite3": gorp.SqliteDialect{},
"postgres": gorp.PostgresDialect{},
"mysql": gorp.MySQLDialect{Engine: "InnoDB", Encoding: "UTF8"},
"mssql": gorp.SqlServerDialect{},
"oci8": gorp.OracleDialect{},
"oci8": OracleDialect{},
}

type MigrationSource interface {
Expand Down Expand Up @@ -262,7 +278,7 @@ func migrationFromFile(dir http.FileSystem, info os.FileInfo) (*Migration, error
if err != nil {
return nil, fmt.Errorf("Error while opening %s: %s", info.Name(), err)
}
defer func () { _ = file.Close() }()
defer func() { _ = file.Close() }()

migration, err := ParseMigration(info.Name(), file)
if err != nil {
Expand Down Expand Up @@ -723,14 +739,19 @@ Check https://github.com/go-sql-driver/mysql#parsetime for more info.`)

// Create migration database map
dbMap := &gorp.DbMap{Db: db, Dialect: d}
dbMap.AddTableWithNameAndSchema(MigrationRecord{}, ms.SchemaName, ms.getTableName()).SetKeys(false, "Id")
table := dbMap.AddTableWithNameAndSchema(MigrationRecord{}, ms.SchemaName, ms.getTableName()).SetKeys(false, "Id")
//dbMap.TraceOn("", log.New(os.Stdout, "migrate: ", log.Lmicroseconds))

if dialect == "oci8" {
table.ColMap("Id").SetMaxSize(4000)
}

err := dbMap.CreateTablesIfNotExists()
if err != nil {
// Oracle database does not support `if not exists`, so use `ORA-00955:` error code
// to check if the table exists.
if err != nil && dialect == "oci8" && !strings.HasPrefix(err.Error(), "ORA-00955:") {
return nil, err
}

return dbMap, nil
}

Expand Down

0 comments on commit f6e5886

Please sign in to comment.