Skip to content
Permalink
Browse files

05-Database create tables

  • Loading branch information...
bonfy committed Sep 11, 2018
1 parent cf3fa72 commit 2306009ddfbabd2f92406aa2cf233c2654e2fd7c
Showing with 103 additions and 3 deletions.
  1. +1 −0 .gitignore
  2. +18 −0 cmd/db_init/main.go
  3. +6 −0 config.yml.sample
  4. +36 −0 config/g.go
  5. +27 −0 model/g.go
  6. +9 −2 model/post.go
  7. +6 −1 model/user.go
@@ -0,0 +1 @@
config.yml
@@ -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{})
}
@@ -0,0 +1,6 @@
mysql:
charset: utf8
db: dbname
host: localhost
password: password
user: root
@@ -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)
}
@@ -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
}
@@ -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"`
}
@@ -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

@pumkinpie

This comment has been minimized.

Copy link

commented on 2306009 Apr 11, 2019

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

Please sign in to comment.
You can’t perform that action at this time.