From 8848fc476dc93dfc4db337744462a1675f993f7f Mon Sep 17 00:00:00 2001 From: Gabriel Date: Sun, 19 Jul 2015 22:37:08 +0000 Subject: [PATCH 1/3] Table suffix to create tables with InnoDB engine with mysql. Alter table is not affected yet, only create table and auto migration --- README.md | 1 + main.go | 10 ++++++++-- scope_private.go | 16 +++++++++++++--- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ccab06db..5919b188 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/main.go b/main.go index aba51fc4..b2c625f5 100644 --- a/main.go +++ b/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 } diff --git a/scope_private.go b/scope_private.go index 85f07e99..2ab340b2 100644 --- a/scope_private.go +++ b/scope_private.go @@ -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 } From 260000d00f244674ce23b5a960465db1a508e768 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Mon, 20 Jul 2015 22:46:04 +0000 Subject: [PATCH 2/3] Propagate argument in open function with table options --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index b2c625f5..8470a8c7 100644 --- a/main.go +++ b/main.go @@ -38,7 +38,7 @@ type DB struct { } func Open(dialect string, args ...interface{}) (DB, error) { - return OpenWithTableSuffix(dialect, "", args) + return OpenWithTableSuffix(dialect, "", args...) } func OpenWithTableSuffix(dialect, tableSuffix string, args ...interface{}) (DB, error) { From eef40a06ff56eca675bf36d176812156f46d8d3a Mon Sep 17 00:00:00 2001 From: Gabriel Date: Sat, 1 Aug 2015 22:46:38 +0000 Subject: [PATCH 3/3] Rename the parameter to table_options and avoid introduction of new API function OpenWithTableSuffix --- README.md | 3 ++- main.go | 10 ++-------- scope_private.go | 12 ++++++------ 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 5919b188..4aa1137a 100644 --- a/README.md +++ b/README.md @@ -113,7 +113,6 @@ 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 @@ -137,12 +136,14 @@ db.SingularTable(true) ```go // Create table db.CreateTable(&User{}) +db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&User{}) // Drop table db.DropTable(&User{}) // Automating Migration db.AutoMigrate(&User{}) +db.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(&User{}) db.AutoMigrate(&User{}, &Product{}, &Order{}) // Feel free to change your struct, AutoMigrate will keep your database up-to-date. // AutoMigrate will ONLY add *new columns* and *new indexes*, diff --git a/main.go b/main.go index 8470a8c7..aba51fc4 100644 --- a/main.go +++ b/main.go @@ -32,16 +32,11 @@ 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 @@ -74,7 +69,6 @@ func OpenWithTableSuffix(dialect, tableSuffix string, args ...interface{}) (DB, logger: defaultLogger, callback: DefaultCallback, source: source, - tableSuffix:tableSuffix, values: map[string]interface{}{}, db: dbSql, } @@ -376,7 +370,7 @@ func (s *DB) RecordNotFound() bool { // Migrations func (s *DB) CreateTable(value interface{}) *DB { - return s.clone().NewScope(value).Set("gorm:table_suffix", s.tableSuffix).createTable().db + return s.clone().NewScope(value).createTable().db } func (s *DB) DropTable(value interface{}) *DB { @@ -396,7 +390,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().Set("gorm:table_suffix", s.tableSuffix).autoMigrate().db + db = db.NewScope(value).NeedPtr().autoMigrate().db } return db } diff --git a/scope_private.go b/scope_private.go index 2ab340b2..e7efd9a8 100644 --- a/scope_private.go +++ b/scope_private.go @@ -443,14 +443,14 @@ func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope { } /** - Return the table suffix string or an empty string if the table suffix does not exist + Return the table options string or an empty string if the table options does not exist */ -func (scope *Scope) getTableSuffix() string{ - tableSuffix, ok := scope.Get("gorm:table_suffix") +func (scope *Scope) getTableOptions() string{ + tableOptions, ok := scope.Get("gorm:table_options") if !ok { return "" } - return tableSuffix.(string) + return tableOptions.(string) } func (scope *Scope) createJoinTable(field *StructField) { @@ -469,7 +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) %s", scope.Quote(joinTable), strings.Join(sqlTypes, ","), scope.getTableSuffix())).Error) + scope.Err(scope.NewDB().Exec(fmt.Sprintf("CREATE TABLE %v (%v) %s", scope.Quote(joinTable), strings.Join(sqlTypes, ","), scope.getTableOptions())).Error) } scope.NewDB().Table(joinTable).AutoMigrate(joinTableHandler) } @@ -494,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) %s", scope.QuotedTableName(), strings.Join(tags, ","), primaryKeyStr, scope.getTableSuffix())).Exec() + scope.Raw(fmt.Sprintf("CREATE TABLE %v (%v %v) %s", scope.QuotedTableName(), strings.Join(tags, ","), primaryKeyStr, scope.getTableOptions())).Exec() return scope }