首次提交:初始化项目代码
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"cloudnest/internal/domain/auth/entity"
|
||||
fileentity "cloudnest/internal/domain/file/entity"
|
||||
"cloudnest/internal/pkg/logger"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
gormlogger "gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
// MySQLClient provides MySQL database operations.
|
||||
// MySQLClient 提供 MySQL 数据库操作。
|
||||
//
|
||||
// This struct wraps the GORM database connection and provides methods
|
||||
// for database initialization and migration.
|
||||
//
|
||||
// 此结构体封装了 GORM 数据库连接,并提供了数据库初始化和迁移的方法。
|
||||
type MySQLClient struct {
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
// NewMySQLClient creates a new MySQL client.
|
||||
// NewMySQLClient 创建一个新的 MySQL 客户端。
|
||||
//
|
||||
// Parameters:
|
||||
// - dsn: The MySQL data source name in format "user:password@tcp(host:port)/database".
|
||||
//
|
||||
// Returns:
|
||||
// - *MySQLClient: A pointer to the new MySQLClient instance.
|
||||
// - error: An error if the connection fails.
|
||||
//
|
||||
// Note:
|
||||
// - The connection is established immediately.
|
||||
// - GORM logging is enabled at Info level.
|
||||
//
|
||||
// 参数:
|
||||
// - dsn: MySQL 数据源名称,格式为 "user:password@tcp(host:port)/database"。
|
||||
//
|
||||
// 返回值:
|
||||
// - *MySQLClient: 新创建的 MySQLClient 实例指针。
|
||||
// - error: 如果连接失败则返回错误。
|
||||
//
|
||||
// 注意:
|
||||
// - 连接会立即建立。
|
||||
// - GORM 日志级别为 Info。
|
||||
func NewMySQLClient(dsn string) (*MySQLClient, error) {
|
||||
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
|
||||
Logger: gormlogger.Default.LogMode(gormlogger.Info),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
logger.Info("mysql connected")
|
||||
return &MySQLClient{DB: db}, nil
|
||||
}
|
||||
|
||||
// Migrate runs database migrations for all registered models.
|
||||
// Migrate 对所有注册的模型运行数据库迁移。
|
||||
//
|
||||
// Returns:
|
||||
// - error: An error if migration fails.
|
||||
//
|
||||
// Note:
|
||||
// - This method should be called once during application startup.
|
||||
// - It automatically creates or updates database tables based on model definitions.
|
||||
//
|
||||
// 返回值:
|
||||
// - error: 如果迁移失败则返回错误。
|
||||
//
|
||||
// 注意:
|
||||
// - 此方法应在应用程序启动时调用一次。
|
||||
// - 它会根据模型定义自动创建或更新数据库表。
|
||||
func (c *MySQLClient) Migrate() error {
|
||||
if err := c.DB.AutoMigrate(&entity.User{}, &fileentity.Favorite{}, &fileentity.DeletedFile{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.setTableComment("users", "用户表")
|
||||
c.setTableComment("favorites", "用户收藏表")
|
||||
c.setTableComment("deleted_files", "回收站文件表")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *MySQLClient) setTableComment(tableName, comment string) {
|
||||
sql := "ALTER TABLE `" + tableName + "` COMMENT = '" + comment + "'"
|
||||
if err := c.DB.Exec(sql).Error; err != nil {
|
||||
logger.Error("failed to set table comment", "table", tableName, "error", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user