-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: Implement new models and migration logic
- Implemented various model classes in /models, including User, Class, ClassUser, GroupBoard, GroupCode, GroupSchedule, Role, and Attendance. - Each model class is designed with appropriate GORM tags for effective database mapping. - Updated the migration logic in /migration/migration.go to include these new models. - Modified main.go to facilitate optional migration execution based on the RUN_MIGRATIONS environment variable. - Removed unnecessary logic for migration. Related issue: #15
- Loading branch information
Showing
14 changed files
with
148 additions
and
4 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 |
---|---|---|
@@ -1 +1,6 @@ | ||
ENV=prod | ||
MYSQL_USER= | ||
MYSQL_PASSWORD= | ||
MYSQL_DATABASE= | ||
MYSQL_HOST= | ||
MYSQL_PORT= | ||
RUN_MIGRATIONS= |
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 was deleted.
Oops, something went wrong.
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,41 @@ | ||
package migration | ||
|
||
import ( | ||
"fmt" | ||
"github.com/YJU-OKURA/project_minori-gin-deployment-repo/models" | ||
"gorm.io/driver/mysql" | ||
"gorm.io/gorm" | ||
"log" | ||
"os" | ||
) | ||
|
||
func InitDB() *gorm.DB { | ||
user := os.Getenv("MYSQL_USER") | ||
pass := os.Getenv("MYSQL_PASSWORD") | ||
dbName := os.Getenv("MYSQL_DATABASE") | ||
host := os.Getenv("MYSQL_HOST") | ||
port := os.Getenv("MYSQL_PORT") | ||
|
||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", user, pass, host, port, dbName) | ||
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) | ||
if err != nil { | ||
log.Fatalf("failed to connect to database: %v", err) | ||
} | ||
return db | ||
} | ||
|
||
func Migrate(db *gorm.DB) { | ||
err := db.AutoMigrate( | ||
&models.User{}, | ||
&models.Class{}, | ||
&models.ClassUser{}, | ||
&models.GroupBoard{}, | ||
&models.GroupCode{}, | ||
&models.GroupSchedule{}, | ||
&models.Role{}, | ||
&models.Attendance{}, | ||
) | ||
if err != nil { | ||
log.Fatalf("failed to migrate database: %v", err) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Empty file.
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,10 @@ | ||
package models | ||
|
||
type Attendance struct { | ||
Key string `gorm:"primaryKey;size:255"` | ||
CID uint `gorm:"not null"` // Class ID | ||
UID uint `gorm:"not null"` // User ID | ||
AttendanceID string `gorm:"size:255;not null"` | ||
IsAttendance string `gorm:"type:enum('Attendance', 'Tardy', 'Absence');default:'Absence';not null"` // 出席, 遅刻, 欠席 | ||
ClassUser ClassUser `gorm:"foreignKey:CID,UID"` | ||
} |
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,9 @@ | ||
package models | ||
|
||
type Class struct { | ||
ID uint `gorm:"primaryKey"` | ||
Name string `gorm:"size:30;not null"` | ||
Limitation *int | ||
Description *string `gorm:"size:255"` | ||
Image *string `gorm:"size:255"` | ||
} |
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,11 @@ | ||
package models | ||
|
||
type ClassUser struct { | ||
CID uint `gorm:"primaryKey"` | ||
UID uint `gorm:"primaryKey"` | ||
Nickname string `gorm:"size:50;not null"` | ||
IsFavorite bool | ||
RoleID int `gorm:"not null"` | ||
Class Class `gorm:"foreignKey:CID"` | ||
User User `gorm:"foreignKey:UID"` | ||
} |
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,17 @@ | ||
package models | ||
|
||
import "time" | ||
|
||
type GroupBoard struct { | ||
ID uint `gorm:"primaryKey"` | ||
Title string `gorm:"size:255;not null"` | ||
Content string `gorm:"type:text;not null"` | ||
Image string `gorm:"size:255"` | ||
CreatedAt time.Time `gorm:"not null;"` | ||
UpdatedAt time.Time `gorm:"not null;"` | ||
IsAnnounced bool `gorm:"not null;default:false"` | ||
CID uint `gorm:"not null"` // Class ID | ||
UID uint `gorm:"not null"` // User ID | ||
Class Class `gorm:"foreignKey:CID"` | ||
User User `gorm:"foreignKey:UID"` | ||
} |
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,11 @@ | ||
package models | ||
|
||
type GroupCode struct { | ||
ID string `gorm:"primaryKey;size:255"` | ||
Code string `gorm:"size:10;not null"` | ||
Secret string `gorm:"size:20"` | ||
CID uint `gorm:"not null"` // Class ID | ||
UID uint `gorm:"not null"` // User ID | ||
Class Class `gorm:"foreignKey:CID"` | ||
User User `gorm:"foreignKey:UID"` | ||
} |
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 models | ||
|
||
import "time" | ||
|
||
type GroupSchedule struct { | ||
ID uint `gorm:"primaryKey"` | ||
Title string `gorm:"size:255;not null"` | ||
StartedAt time.Time `gorm:"not null"` | ||
EndedAt time.Time `gorm:"not null"` | ||
CID uint `gorm:"not null"` // Class ID | ||
IsLive bool `gorm:"not null;default:false"` | ||
Class Class `gorm:"foreignKey:CID"` | ||
} |
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,6 @@ | ||
package models | ||
|
||
type Role struct { | ||
ID int `gorm:"primaryKey"` | ||
Role string `gorm:"type:enum('USER', 'ADMIN', 'ASSISTANT', 'APPLICANT', 'BLACKLIST');not null"` | ||
} |
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 |
---|---|---|
@@ -1 +1,13 @@ | ||
package models | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type User struct { | ||
ID uint `gorm:"primaryKey"` | ||
Name string `gorm:"size:50;not null"` | ||
Email string `gorm:"size:100;not null;"` | ||
Image string `gorm:"size:255;not null;"` | ||
CreatedAt time.Time `gorm:"not null;"` | ||
} |