forked from mirror/gorm
Table suffix to create tables with InnoDB engine with mysql. Alter table is not affected yet, only create table and auto migration
This commit is contained in:
parent
5663048f13
commit
8848fc476d
|
@ -113,6 +113,7 @@ import (
|
|||
db, err := gorm.Open("postgres", "user=gorm dbname=gorm sslmode=disable")
|
||||
// db, err := gorm.Open("foundation", "dbname=gorm") // FoundationDB.
|
||||
// db, err := gorm.Open("mysql", "user:password@/dbname?charset=utf8&parseTime=True&loc=Local")
|
||||
// db, err := gorm.OpenWithTableSuffix("mysql", "ENGINE=InnoDB", "user:password@/dbname?charset=utf8&parseTime=True&loc=Local")
|
||||
// db, err := gorm.Open("sqlite3", "/tmp/gorm.db")
|
||||
|
||||
// You can also use an existing database connection handle
|
||||
|
|
10
main.go
10
main.go
|
@ -32,11 +32,16 @@ type DB struct {
|
|||
dialect Dialect
|
||||
singularTable bool
|
||||
source string
|
||||
tableSuffix string
|
||||
values map[string]interface{}
|
||||
joinTableHandlers map[string]JoinTableHandler
|
||||
}
|
||||
|
||||
func Open(dialect string, args ...interface{}) (DB, error) {
|
||||
return OpenWithTableSuffix(dialect, "", args)
|
||||
}
|
||||
|
||||
func OpenWithTableSuffix(dialect, tableSuffix string, args ...interface{}) (DB, error) {
|
||||
var db DB
|
||||
var err error
|
||||
|
||||
|
@ -69,6 +74,7 @@ func Open(dialect string, args ...interface{}) (DB, error) {
|
|||
logger: defaultLogger,
|
||||
callback: DefaultCallback,
|
||||
source: source,
|
||||
tableSuffix:tableSuffix,
|
||||
values: map[string]interface{}{},
|
||||
db: dbSql,
|
||||
}
|
||||
|
@ -370,7 +376,7 @@ func (s *DB) RecordNotFound() bool {
|
|||
|
||||
// Migrations
|
||||
func (s *DB) CreateTable(value interface{}) *DB {
|
||||
return s.clone().NewScope(value).createTable().db
|
||||
return s.clone().NewScope(value).Set("gorm:table_suffix", s.tableSuffix).createTable().db
|
||||
}
|
||||
|
||||
func (s *DB) DropTable(value interface{}) *DB {
|
||||
|
@ -390,7 +396,7 @@ func (s *DB) HasTable(value interface{}) bool {
|
|||
func (s *DB) AutoMigrate(values ...interface{}) *DB {
|
||||
db := s.clone()
|
||||
for _, value := range values {
|
||||
db = db.NewScope(value).NeedPtr().autoMigrate().db
|
||||
db = db.NewScope(value).NeedPtr().Set("gorm:table_suffix", s.tableSuffix).autoMigrate().db
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
|
|
@ -442,6 +442,17 @@ func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope {
|
|||
return scope
|
||||
}
|
||||
|
||||
/**
|
||||
Return the table suffix string or an empty string if the table suffix does not exist
|
||||
*/
|
||||
func (scope *Scope) getTableSuffix() string{
|
||||
tableSuffix, ok := scope.Get("gorm:table_suffix")
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
return tableSuffix.(string)
|
||||
}
|
||||
|
||||
func (scope *Scope) createJoinTable(field *StructField) {
|
||||
if relationship := field.Relationship; relationship != nil && relationship.JoinTableHandler != nil {
|
||||
joinTableHandler := relationship.JoinTableHandler
|
||||
|
@ -458,8 +469,7 @@ func (scope *Scope) createJoinTable(field *StructField) {
|
|||
sqlTypes = append(sqlTypes, scope.Quote(dbName)+" "+primaryKeySqlType)
|
||||
}
|
||||
}
|
||||
|
||||
scope.Err(scope.NewDB().Exec(fmt.Sprintf("CREATE TABLE %v (%v)", scope.Quote(joinTable), strings.Join(sqlTypes, ","))).Error)
|
||||
scope.Err(scope.NewDB().Exec(fmt.Sprintf("CREATE TABLE %v (%v) %s", scope.Quote(joinTable), strings.Join(sqlTypes, ","), scope.getTableSuffix())).Error)
|
||||
}
|
||||
scope.NewDB().Table(joinTable).AutoMigrate(joinTableHandler)
|
||||
}
|
||||
|
@ -484,7 +494,7 @@ func (scope *Scope) createTable() *Scope {
|
|||
if len(primaryKeys) > 0 {
|
||||
primaryKeyStr = fmt.Sprintf(", PRIMARY KEY (%v)", strings.Join(primaryKeys, ","))
|
||||
}
|
||||
scope.Raw(fmt.Sprintf("CREATE TABLE %v (%v %v)", scope.QuotedTableName(), strings.Join(tags, ","), primaryKeyStr)).Exec()
|
||||
scope.Raw(fmt.Sprintf("CREATE TABLE %v (%v %v) %s", scope.QuotedTableName(), strings.Join(tags, ","), primaryKeyStr, scope.getTableSuffix())).Exec()
|
||||
return scope
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue