Skip to content

Commit

Permalink
05-Database create tables
Browse files Browse the repository at this point in the history
  • Loading branch information
bonfy committed Sep 11, 2018
1 parent cf3fa72 commit 2306009
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config.yml
18 changes: 18 additions & 0 deletions cmd/db_init/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"log"

"github.com/bonfy/go-mega-code/model"
_ "github.com/jinzhu/gorm/dialects/mysql"
)

func main() {
log.Println("DB Init ...")
db := model.ConnectToDB()
defer db.Close()
model.SetDB(db)

db.DropTableIfExists(model.User{}, model.Post{})
db.CreateTable(model.User{}, model.Post{})
}
6 changes: 6 additions & 0 deletions config.yml.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
mysql:
charset: utf8
db: dbname
host: localhost
password: password
user: root
36 changes: 36 additions & 0 deletions config/g.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package config

import (
"fmt"

"github.com/spf13/viper"
)

func init() {
projectName := "go-mega"
getConfig(projectName)
}

func getConfig(projectName string) {
viper.SetConfigName("config") // name of config file (without extension)

viper.AddConfigPath(".") // optionally look for config in the working directory
viper.AddConfigPath(fmt.Sprintf("$HOME/.%s", projectName)) // call multiple times to add many search paths
viper.AddConfigPath(fmt.Sprintf("/data/docker/config/%s", projectName)) // path to look for the config file in

err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("Fatal error config file: %s", err))
}
}

// GetMysqlConnectingString func
func GetMysqlConnectingString() string {
usr := viper.GetString("mysql.user")
pwd := viper.GetString("mysql.password")
host := viper.GetString("mysql.host")
db := viper.GetString("mysql.db")
charset := viper.GetString("mysql.charset")

return fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?charset=%s&parseTime=true", usr, pwd, host, db, charset)
}
27 changes: 27 additions & 0 deletions model/g.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package model

import (
"log"

"github.com/bonfy/go-mega-code/config"
"github.com/jinzhu/gorm"
)

var db *gorm.DB

// SetDB func
func SetDB(database *gorm.DB) {
db = database
}

// ConnectToDB func
func ConnectToDB() *gorm.DB {
connectingStr := config.GetMysqlConnectingString()
log.Println("Connet to db...")
db, err := gorm.Open("mysql", connectingStr)
if err != nil {
panic("Failed to connect database")
}
db.SingularTable(true)
return db
}
11 changes: 9 additions & 2 deletions model/post.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package model

import (
"time"
)

// Post struct
type Post struct {
User
Body string
ID int `gorm:"primary_key"`
UserID int
User User
Body string `gorm:"varchar(180)"`
Timestamp *time.Time `sql:"DEFAULT:current_timestamp"`
}
7 changes: 6 additions & 1 deletion model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@ package model

// User struct
type User struct {
Username string
ID int `gorm:"primary_key"`
Username string `gorm:"varchar(64)"`
Email string `gorm:"varchar(12)"`
PasswordHash string `gorm:"varchar(128)"`
Posts []Post
Followers []*User `gorm:"many2many:follower;association_jointable_foreignkey:follower_id"`
}

1 comment on commit 2306009

@Kabochar
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mysql8.0 无法创建数据库。 create DATABASE go-mega; 中 go-mega 可以改为使用 下划线 go_mega

Please sign in to comment.