Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[add] user_table's struct #6

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions db/model/item.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package model

import (
"time"
)

type Item struct {
ID int `gorm:"primaryKey;autoIncrement:true"`
Name string
Overview string
Icon byte
Type int
CreatedAt time.Time
CreatedBy int
UpdatedAt time.Time
UpdatedBy int
}
6 changes: 6 additions & 0 deletions db/model/item_tagging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package model

type ItemTagging struct {
ItemID int
TagID int
}
14 changes: 14 additions & 0 deletions db/model/ldap_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package model

import (
"time"
)

type LdapUser struct {
UserID int `gorm:"primaryKey"`
LdapID int
CreatedAt time.Time
CreatedBy int
UpdatedAt time.Time
UpdatedBy int
}
15 changes: 15 additions & 0 deletions db/model/lending.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package model

import (
"time"
)

type Lending struct {
UserID int `gorm:"primaryKey"`
ItemID int `gorm:"primaryKey"`
ReturnedAt time.Time
CreatedAt time.Time
CreatedBy int
UpdatedAt time.Time
UpdatedBy int
}
11 changes: 11 additions & 0 deletions db/model/passsword_reset_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package model

import (
"time"
)

type PasswordResetUser struct {
UserID int `gorm:"primaryKey"`
ResetHash string
ExpiredAt time.Time
}
14 changes: 14 additions & 0 deletions db/model/password_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package model

import (
"time"
)

type PasswordUser struct {
UserID int `gorm:"primaryKey"`
MailAddress string
Password string
IsLook bool
CreatedAt time.Time
UpdatedBy int
}
14 changes: 14 additions & 0 deletions db/model/reservation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package model

import (
"time"
)

type Reservation struct {
UserID int `gorm:"primaryKey"`
ItemID int `gorm:"primaryKey"`
CreatedAt time.Time
CreatedBy int
UpdatedAt time.Time
UpdatedBy int
}
14 changes: 14 additions & 0 deletions db/model/tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package model

import (
"time"
)

type Tag struct {
ID int `gorm:"primaryKey;autoIncrement:true"`
Name string
CreatedAt time.Time
CreatedBy int
UpdatedAt time.Time
UpdatedBy int
}
7 changes: 7 additions & 0 deletions db/model/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package model

type User struct {
ID int `gorm:"primaryKey;autoIncrement:true"`
Nickname string
Role int
}
13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ services:
MYSQL_DATABASE: "kiritan"
MYSQL_USER: "kiritan"
MYSQL_PASSWORD: "kiritan"
TZ: "Asia/Tokyo"
volumes:
- ./docker/mysql/data:/var/lib/mysql
- ./docker/mysql/my.conf:/etc/mysql/conf.d/my.conf

mysql_test:
image: "mysql:8"
ports:
- "3307:3306"
environment:
MYSQL_ROOT_PASSWORD: "kiritan"
MYSQL_DATABASE: "kiritan"
MYSQL_USER: "kiritan"
MYSQL_PASSWORD: "kiritan"
volumes:
- ./docker/mysql/my.conf:/etc/mysql/conf.d/my.conf
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ module ritscc/kiri-tansu

go 1.16

require github.com/golang-migrate/migrate/v4 v4.14.1 // indirect
require (
github.com/golang-migrate/migrate/v4 v4.14.1 // indirect
gorm.io/driver/mysql v1.0.6 // indirect
gorm.io/gorm v1.21.9 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM=
github.com/gocql/gocql v0.0.0-20190301043612-f6df8288f9b4/go.mod h1:4Fw1eo5iaEhDUs8XyuhSVCVy52Jq3L+/3GJgYkwc+/0=
Expand Down Expand Up @@ -180,6 +182,10 @@ github.com/jackc/pgx/v4 v4.0.0-20190421002000-1b8f0016e912/go.mod h1:no/Y67Jkk/9
github.com/jackc/pgx/v4 v4.0.0-pre1.0.20190824185557-6972a5742186/go.mod h1:X+GQnOEnf1dqHGpw7JmHqHc1NxDoalibchSk9/RWuDc=
github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.2 h1:eVKgfIdy9b6zbWBMgFpfDPoAMifwSZagU9HmEU6zgiI=
github.com/jinzhu/now v1.1.2/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
Expand Down Expand Up @@ -545,6 +551,10 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gorm.io/driver/mysql v1.0.6 h1:mA0XRPjIKi4bkE9nv+NKs6qj6QWOchqUSdWOcpd3x1E=
gorm.io/driver/mysql v1.0.6/go.mod h1:KdrTanmfLPPyAOeYGyG+UpDys7/7eeWT1zCq+oekYnU=
gorm.io/gorm v1.21.9 h1:INieZtn4P2Pw6xPJ8MzT0G4WUOsHq3RhfuDF1M6GW0E=
gorm.io/gorm v1.21.9/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
44 changes: 44 additions & 0 deletions test/db_test/db_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package db

import (
"log"
"testing"
_ "github.com/golang-migrate/migrate/v4/source/file"
gomysql "gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)

const (
DBNAME = "kiritan"
USERNAME = "kiritan"
PASSWORD = "kiritan"
IPADDRESS = "127.0.0.1"
PORTNUMBER = "3307"
)

func Test_DB(t *testing.T) {

MigrateBD()
defer ResetBD()

db, err := DatabaseConnection()
if err != nil {
log.Fatal(err)
return
}
test_db_user(db, t)
test_db_tag(db, t)
test_db_item(db, t)

}

func DatabaseConnection() (*gorm.DB, error) {
dsn := USERNAME + ":" + PASSWORD + "@tcp(" + IPADDRESS + ":" + PORTNUMBER + ")/" + DBNAME + "?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(gomysql.Open(dsn), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
SingularTable: true,
},
})
return db, err
}
68 changes: 68 additions & 0 deletions test/db_test/item.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package db

import (
"gorm.io/gorm"
"reflect"
model "ritscc/kiri-tansu/db/model"
"testing"
)

func test_db_item(db *gorm.DB, t *testing.T) {
result := db.Create(&model.Item{
ID: 1,
Name: "Vim",
Overview: "Vimの本です",
Type: 1,
CreatedBy: 1,
UpdatedBy: 1,
}).Error

if result != nil {
t.Error(result)
return
}

testCases := []struct {
expected model.Item
}{
{
expected: model.Item{
ID: 1,
Name: "Vim",
Overview: "Vimの本です",
Type: 1,
CreatedBy: 1,
UpdatedBy: 1,
},
},
}

for i, testCase := range testCases {
var actual model.Item
db.First(&actual)
expected := testCase.expected
if !reflect.DeepEqual(actual.ID, expected.ID) {
t.Errorf("errorNumber:%d actual: %v, expect: %v", i, actual, expected)
}

if !reflect.DeepEqual(actual.Name, expected.Name) {
t.Errorf("errorNumber:%d actual: %v, expect: %v", i, actual, expected)
}

if !reflect.DeepEqual(actual.Overview, expected.Overview) {
t.Errorf("errorNumber:%d actual: %v, expect: %v", i, actual, expected)
}

if !reflect.DeepEqual(actual.Type, expected.Type) {
t.Errorf("errorNumber:%d actual: %v, expect: %v", i, actual, expected)
}

if !reflect.DeepEqual(actual.CreatedBy, expected.CreatedBy) {
t.Errorf("errorNumber:%d actual: %v, expect: %v", i, actual, expected)
}

if !reflect.DeepEqual(actual.UpdatedBy, expected.UpdatedBy) {
t.Errorf("errorNumber:%d actual: %v, expect: %v", i, actual, expected)
}
}
}
73 changes: 73 additions & 0 deletions test/db_test/setup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package db

import (
"database/sql"
"fmt"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/mysql"
_ "github.com/golang-migrate/migrate/v4/source/file"
)

func MigrateBD() {
db, err := sql.Open("mysql", "kiritan:kiritan@tcp(localhost:3307)/kiritan")

if err != nil {
fmt.Println("DBの接続に失敗しました")
fmt.Printf("%s\n", err)
return
}

driver, err := mysql.WithInstance(db, &mysql.Config{})

if err != nil {
fmt.Println("Hoge")
fmt.Printf("%s\n", err)
return
}

m, err := migrate.NewWithDatabaseInstance("file://../../db/migration", "mysql", driver)
if err != nil {
fmt.Printf("%s\n", err)
return
}

err = m.Up()
if err != nil && err != migrate.ErrNoChange {
fmt.Printf("%s\n", err)
return
}

db.Close()
}

func ResetBD() {
db, err := sql.Open("mysql", "kiritan:kiritan@tcp(localhost:3307)/kiritan")

if err != nil {
fmt.Println("DBの接続に失敗しました")
fmt.Printf("%s\n", err)
return
}

driver, err := mysql.WithInstance(db, &mysql.Config{})

if err != nil {
fmt.Println("Hoge")
fmt.Printf("%s\n", err)
return
}

m, err := migrate.NewWithDatabaseInstance("file://../../db/migration", "mysql", driver)
if err != nil {
fmt.Printf("%s\n", err)
return
}

err = m.Down()
if err != nil && err != migrate.ErrNoChange {
fmt.Printf("%s\n", err)
return
}

db.Close()
}
Loading