-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from 8acebuzzy/PXJ
migrate、gormGen model
- Loading branch information
Showing
7 changed files
with
145 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/ClubWeGo/commentmicro/dal/model" | ||
|
||
"gorm.io/driver/mysql" | ||
"gorm.io/gen" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func main() { | ||
|
||
g := gen.NewGenerator(gen.Config{ | ||
OutPath: "../../dal/query", | ||
Mode: gen.WithoutContext | gen.WithDefaultQuery | gen.WithQueryInterface, | ||
}) | ||
|
||
dsn := "tk:123456@tcp(127.0.0.1:3306)/simpletk?charset=utf8&parseTime=True&loc=Local" | ||
db, err := gorm.Open(mysql.Open(dsn)) | ||
if err != nil { | ||
log.Fatal(err) | ||
return | ||
} | ||
|
||
g.UseDB(db) | ||
|
||
g.ApplyBasic(model.User{}) | ||
|
||
// g.ApplyInterface(func(model.UserMethod) {}, model.User{}) | ||
|
||
g.Execute() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/ClubWeGo/commentmicro/dal/model" | ||
|
||
"gorm.io/driver/mysql" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func InitComment(db *gorm.DB) { | ||
err := db.AutoMigrate(&model.comment{}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func main() { | ||
var datetimePrecision = 2 | ||
dsn := "tk:123456@tcp(127.0.0.1:3306)/simpletk?charset=utf8&parseTime=True&loc=Local" | ||
db, err := gorm.Open(mysql.New(mysql.Config{ | ||
DSN: dsn, // data source name, refer https://github.com/go-sql-driver/mysql#dsn-data-source-name | ||
DefaultStringSize: 256, // add default size for string fields, by default, will use db type `longtext` for fields without size, not a primary key, no index defined and don't have default values | ||
DisableDatetimePrecision: true, // disable datetime precision support, which not supported before MySQL 5.6 | ||
DefaultDatetimePrecision: &datetimePrecision, // default datetime precision | ||
DontSupportRenameIndex: true, // drop & create index when rename index, rename index not supported before MySQL 5.7, MariaDB | ||
DontSupportRenameColumn: true, // use change when rename column, rename rename not supported before MySQL 8, MariaDB | ||
SkipInitializeWithVersion: false, // smart configure based on used version | ||
}), &gorm.Config{}) | ||
if err != nil { | ||
log.Fatal(err) | ||
return | ||
} | ||
|
||
InitComment(db) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package dal | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/ClubWeGo/commentmicro/dal/query" | ||
|
||
"gorm.io/driver/mysql" | ||
"gorm.io/gorm" | ||
) | ||
|
||
var DB *gorm.DB | ||
|
||
func InitDB(dsn string) { | ||
database, err := gorm.Open(mysql.Open(dsn)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
DB = database | ||
query.SetDefault(DB) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package model | ||
import( | ||
"gorm.io/gorm" | ||
) | ||
|
||
type comment struct{ | ||
gorm.Model | ||
videoid int64 `gorm:"type:varchar(128);not null;index"` \\视频id | ||
videouserid int64 `gorm:"type:varchar(128);not null;index"` \\视频主id | ||
commentuserid int64 `gorm:"not null"` | ||
content string `gorm:"type:varchar(256)"` | ||
is_follow bool | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
github.com/apache/thrift v0.13.0 h1:5hryIiq9gtn+MiLVn0wP37kb/uTeRZgN08WoCsAhIhI= | ||
github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ= | ||
github.com/bytedance/gopkg v0.0.0-20220531084716-665b4f21126f h1:2YCF3cgO6XCub0HIsLrA8ZGhmAPGZfOeSaGjT6Kx4Mc= | ||
github.com/bytedance/gopkg v0.0.0-20220531084716-665b4f21126f/go.mod h1:2ZlV9BaUH4+NXIBF0aMdKKAnHTzqH+iMU4KUjAbL23Q= | ||
github.com/chenzhuoyu/iasm v0.0.0-20220818063314-28c361dae733 h1:Hx6Jxqln+bHRrtjUdgrehhF3gtWVJ2S7bjO/YTNn8Fg= | ||
github.com/chenzhuoyu/iasm v0.0.0-20220818063314-28c361dae733/go.mod h1:wOQ0nsbeOLa2awv8bUYFW/EHXbjQMlZ10fAlXDB2sz8= | ||
github.com/choleraehyq/pid v0.0.15 h1:PejhUZowqxxssjwyaw4OZURRFjnvftZfeEWK9UoWPXU= | ||
github.com/choleraehyq/pid v0.0.15/go.mod h1:uhzeFgxJZWQsZulelVQZwdASxQ9TIPZYL4TPkQMtL/U= | ||
github.com/cloudwego/fastpb v0.0.3 h1:GZE0WzlnjQFE3+vkYFZd964PGT9AXOuvir+JGzuBSPM= | ||
github.com/cloudwego/fastpb v0.0.3/go.mod h1:/V13XFTq2TUkxj2qWReV8MwfPC4NnPcy6FsrojnsSG0= | ||
github.com/cloudwego/frugal v0.1.3 h1:tw3+hh4YMmtHFHRue3OGYjAnkxnZRHqeAyG18+7z5aI= | ||
github.com/cloudwego/frugal v0.1.3/go.mod h1:b981ViPYdhI56aFYsoMjl9kv6yeqYSO+iEz2jrhkCgI= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters