MCPcopy
hub / github.com/rubenv/sql-migrate / getMigrationDbMap

Method getMigrationDbMap

migrate.go:841–889  ·  view source on GitHub ↗
(db *sql.DB, dialect string)

Source from the content-addressed store, hash-verified

839}
840
841func (ms MigrationSet) getMigrationDbMap(db *sql.DB, dialect string) (*gorp.DbMap, error) {
842 d, ok := MigrationDialects[dialect]
843 if !ok {
844 return nil, fmt.Errorf("Unknown dialect: %s", dialect)
845 }
846
847 // When using the mysql driver, make sure that the parseTime option is
848 // configured, otherwise it won't map time columns to time.Time. See
849 // https://github.com/rubenv/sql-migrate/issues/2
850 if dialect == "mysql" {
851 var out *time.Time
852 err := db.QueryRow("SELECT NOW()").Scan(&out)
853 if err != nil {
854 if err.Error() == "sql: Scan error on column index 0: unsupported driver -> Scan pair: []uint8 -> *time.Time" ||
855 err.Error() == "sql: Scan error on column index 0: unsupported Scan, storing driver.Value type []uint8 into type *time.Time" ||
856 err.Error() == "sql: Scan error on column index 0, name \"NOW()\": unsupported Scan, storing driver.Value type []uint8 into type *time.Time" {
857 return nil, errors.New(`Cannot parse dates.
858
859Make sure that the parseTime option is supplied to your database connection.
860Check https://github.com/go-sql-driver/mysql#parsetime for more info.`)
861 }
862 return nil, err
863 }
864 }
865
866 // Create migration database map
867 dbMap := &gorp.DbMap{Db: db, Dialect: d}
868 table := dbMap.AddTableWithNameAndSchema(MigrationRecord{}, ms.SchemaName, ms.getTableName()).SetKeys(false, "Id")
869
870 if dialect == "oci8" || dialect == "godror" {
871 table.ColMap("Id").SetMaxSize(4000)
872 }
873
874 if ms.DisableCreateTable {
875 return dbMap, nil
876 }
877
878 err := dbMap.CreateTablesIfNotExists()
879 if err != nil {
880 // Oracle database does not support `if not exists`, so use `ORA-00955:` error code
881 // to check if the table exists.
882 if (dialect == "oci8" || dialect == "godror") && strings.Contains(err.Error(), "ORA-00955:") {
883 return dbMap, nil
884 }
885 return nil, err
886 }
887
888 return dbMap, nil
889}
890
891// TODO: Run migration + record insert in transaction.

Calls 2

getTableNameMethod · 0.95
ErrorMethod · 0.45