Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.*.swp
*.test
.idea

/sql-migrate/test.db
/test.db
33 changes: 29 additions & 4 deletions migrate.go
Original file line number Diff line number Diff line change
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,11 +739,20 @@ 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 dialect == "oci8" && strings.HasPrefix(err.Error(), "ORA-00955:") {
return dbMap, nil
}
return nil, err
}

Expand Down