From f6e5886e38f68dc26b616dae3cc55403ec9c7f48 Mon Sep 17 00:00:00 2001 From: fanpei91 Date: Sat, 8 Feb 2020 15:27:45 +0800 Subject: [PATCH 1/2] fix: oracle database does support 'if not exists' and 'text' data type --- .gitignore | 1 + migrate.go | 33 +++++++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index a2cac92c..f543d10c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .*.swp *.test +.idea /sql-migrate/test.db /test.db diff --git a/migrate.go b/migrate.go index df1c76c0..0a21d7c6 100644 --- a/migrate.go +++ b/migrate.go @@ -2,12 +2,12 @@ package migrate import ( "bytes" - "os" "database/sql" "errors" "fmt" "io" "net/http" + "os" "path" "regexp" "sort" @@ -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 { @@ -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 { @@ -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 } From 5ae6ff7b2bf72e948c31096b78bbfbaa1b340f62 Mon Sep 17 00:00:00 2001 From: fanpei91 Date: Sat, 8 Feb 2020 15:49:54 +0800 Subject: [PATCH 2/2] fixed 'CreateTablesIfNotExists' error logic --- migrate.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/migrate.go b/migrate.go index 0a21d7c6..fae3b4c5 100644 --- a/migrate.go +++ b/migrate.go @@ -747,11 +747,15 @@ Check https://github.com/go-sql-driver/mysql#parsetime for more info.`) } err := dbMap.CreateTablesIfNotExists() - // 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:") { + 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 } + return dbMap, nil }