diff --git a/db/model/item.go b/db/model/item.go new file mode 100644 index 0000000..6cc8136 --- /dev/null +++ b/db/model/item.go @@ -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 +} diff --git a/db/model/item_tagging.go b/db/model/item_tagging.go new file mode 100644 index 0000000..6b78077 --- /dev/null +++ b/db/model/item_tagging.go @@ -0,0 +1,6 @@ +package model + +type ItemTagging struct { + ItemID int + TagID int +} diff --git a/db/model/ldap_user.go b/db/model/ldap_user.go new file mode 100644 index 0000000..f3b4ec7 --- /dev/null +++ b/db/model/ldap_user.go @@ -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 +} diff --git a/db/model/lending.go b/db/model/lending.go new file mode 100644 index 0000000..48782e0 --- /dev/null +++ b/db/model/lending.go @@ -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 +} diff --git a/db/model/passsword_reset_user.go b/db/model/passsword_reset_user.go new file mode 100644 index 0000000..a8fb7f8 --- /dev/null +++ b/db/model/passsword_reset_user.go @@ -0,0 +1,11 @@ +package model + +import ( + "time" +) + +type PasswordResetUser struct { + UserID int `gorm:"primaryKey"` + ResetHash string + ExpiredAt time.Time +} diff --git a/db/model/password_user.go b/db/model/password_user.go new file mode 100644 index 0000000..ce05ec7 --- /dev/null +++ b/db/model/password_user.go @@ -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 +} diff --git a/db/model/reservation.go b/db/model/reservation.go new file mode 100644 index 0000000..e5411fd --- /dev/null +++ b/db/model/reservation.go @@ -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 +} diff --git a/db/model/tag.go b/db/model/tag.go new file mode 100644 index 0000000..ccb46d1 --- /dev/null +++ b/db/model/tag.go @@ -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 +} diff --git a/db/model/user.go b/db/model/user.go new file mode 100644 index 0000000..4baa783 --- /dev/null +++ b/db/model/user.go @@ -0,0 +1,7 @@ +package model + +type User struct { + ID int `gorm:"primaryKey;autoIncrement:true"` + Nickname string + Role int +} diff --git a/docker-compose.yml b/docker-compose.yml index a20043c..4ee2c11 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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 diff --git a/go.mod b/go.mod index 775aa71..b8d76d5 100644 --- a/go.mod +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum index 6439826..fd073bb 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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= diff --git a/test/db_test/db_test.go b/test/db_test/db_test.go new file mode 100644 index 0000000..35f8180 --- /dev/null +++ b/test/db_test/db_test.go @@ -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 +} diff --git a/test/db_test/item.go b/test/db_test/item.go new file mode 100644 index 0000000..74fe285 --- /dev/null +++ b/test/db_test/item.go @@ -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) + } + } +} diff --git a/test/db_test/setup.go b/test/db_test/setup.go new file mode 100644 index 0000000..b8aa583 --- /dev/null +++ b/test/db_test/setup.go @@ -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() +} diff --git a/test/db_test/tag.go b/test/db_test/tag.go new file mode 100644 index 0000000..e5dbd65 --- /dev/null +++ b/test/db_test/tag.go @@ -0,0 +1,56 @@ +package db + +import ( + "gorm.io/gorm" + "reflect" + model "ritscc/kiri-tansu/db/model" + "testing" +) + +func test_db_tag(db *gorm.DB, t *testing.T) { + result := db.Create(&model.Tag{ + ID: 1, + Name: "book", + CreatedBy: 1, + UpdatedBy: 1, + }).Error + + if result != nil { + t.Error(result) + return + } + + testCases := []struct { + expected model.Tag + }{ + { + expected: model.Tag{ + ID: 1, + Name: "book", + CreatedBy: 1, + UpdatedBy: 1, + }, + }, + } + + for i, testCase := range testCases { + var actual model.Tag + 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.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) + } + } +} diff --git a/test/db_test/user.go b/test/db_test/user.go new file mode 100644 index 0000000..c2a1ff5 --- /dev/null +++ b/test/db_test/user.go @@ -0,0 +1,44 @@ + +package db + +import ( + "reflect" + model "ritscc/kiri-tansu/db/model" + "testing" + "gorm.io/gorm" +) + +func test_db_user(db *gorm.DB, t *testing.T) { + + result := db.Create(&model.User{ + ID: 1, + Nickname: "Hoge", + Role: 1, + }).Error + + if result != nil { + t.Error(result) + return + } + + testCases := []struct { + expected model.User + }{ + { + expected: model.User{ + ID: 1, + Nickname: "Hoge", + Role: 1, + }, + }, + } + + for i, testCase := range testCases { + var actual model.User + db.First(&actual) + expected := testCase.expected + if !reflect.DeepEqual(actual, expected) { + t.Errorf("errorNumber:%d actual: %v, expect: %v", i, actual, expected) + } + } +}