Skip to content

Commit c8d4be6

Browse files
authored
Merge pull request #164 from fanpei91/master
fix: oracle database does not support 'if not exists' and 'text' data type
2 parents 8794cec + 5ae6ff7 commit c8d4be6

2 files changed

Lines changed: 30 additions & 4 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.*.swp
22
*.test
3+
.idea
34

45
/sql-migrate/test.db
56
/test.db

migrate.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package migrate
22

33
import (
44
"bytes"
5-
"os"
65
"database/sql"
76
"errors"
87
"fmt"
98
"io"
109
"net/http"
10+
"os"
1111
"path"
1212
"regexp"
1313
"sort"
@@ -171,12 +171,28 @@ type MigrationRecord struct {
171171
AppliedAt time.Time `db:"applied_at"`
172172
}
173173

174+
type OracleDialect struct {
175+
gorp.OracleDialect
176+
}
177+
178+
func (d OracleDialect) IfTableNotExists(command, schema, table string) string {
179+
return command
180+
}
181+
182+
func (d OracleDialect) IfSchemaNotExists(command, schema string) string {
183+
return command
184+
}
185+
186+
func (d OracleDialect) IfTableExists(command, schema, table string) string {
187+
return command
188+
}
189+
174190
var MigrationDialects = map[string]gorp.Dialect{
175191
"sqlite3": gorp.SqliteDialect{},
176192
"postgres": gorp.PostgresDialect{},
177193
"mysql": gorp.MySQLDialect{Engine: "InnoDB", Encoding: "UTF8"},
178194
"mssql": gorp.SqlServerDialect{},
179-
"oci8": gorp.OracleDialect{},
195+
"oci8": OracleDialect{},
180196
}
181197

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

267283
migration, err := ParseMigration(info.Name(), file)
268284
if err != nil {
@@ -723,11 +739,20 @@ Check https://github.com/go-sql-driver/mysql#parsetime for more info.`)
723739

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

745+
if dialect == "oci8" {
746+
table.ColMap("Id").SetMaxSize(4000)
747+
}
748+
729749
err := dbMap.CreateTablesIfNotExists()
730750
if err != nil {
751+
// Oracle database does not support `if not exists`, so use `ORA-00955:` error code
752+
// to check if the table exists.
753+
if dialect == "oci8" && strings.HasPrefix(err.Error(), "ORA-00955:") {
754+
return dbMap, nil
755+
}
731756
return nil, err
732757
}
733758

0 commit comments

Comments
 (0)