78 lines
2.0 KiB
Go
78 lines
2.0 KiB
Go
package database
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
|
|
"hfb_sys/backend/internal/model"
|
|
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/logger"
|
|
)
|
|
|
|
// NewTestDB 创建用于测试的内存数据库
|
|
func NewTestDB() *gorm.DB {
|
|
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{
|
|
Logger: logger.Default.LogMode(logger.Silent),
|
|
})
|
|
if err != nil {
|
|
log.Fatalf("无法创建测试数据库: %v", err)
|
|
}
|
|
return db
|
|
}
|
|
|
|
// NewTestDBWithName 创建用于测试的命名内存数据库(支持多连接共享)
|
|
func NewTestDBWithName(name string) *gorm.DB {
|
|
dsn := fmt.Sprintf("file:%s?mode=memory&cache=shared", name)
|
|
db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{
|
|
Logger: logger.Default.LogMode(logger.Silent),
|
|
})
|
|
if err != nil {
|
|
log.Fatalf("无法创建测试数据库 %s: %v", name, err)
|
|
}
|
|
return db
|
|
}
|
|
|
|
// MigrateListingLifecycleTestSchema 创建商品生命周期测试所需的共享表结构。
|
|
func MigrateListingLifecycleTestSchema(db *gorm.DB) error {
|
|
return db.AutoMigrate(
|
|
&model.User{},
|
|
&model.GameAccount{},
|
|
&model.RentalListing{},
|
|
&model.ListingStatusEvent{},
|
|
)
|
|
}
|
|
|
|
// MigrateRentalTransactionTestSchema 在商品生命周期基础上创建订单、支付和交接测试所需的共享表结构。
|
|
func MigrateRentalTransactionTestSchema(db *gorm.DB) error {
|
|
if err := MigrateListingLifecycleTestSchema(db); err != nil {
|
|
return err
|
|
}
|
|
return db.AutoMigrate(
|
|
&model.RentalOrder{},
|
|
&model.SystemConfig{},
|
|
&model.PaymentOrder{},
|
|
&model.Notification{},
|
|
&model.AdminNotification{},
|
|
&model.HandoffRecord{},
|
|
&model.OrderCheckout{},
|
|
&model.WalletAccount{},
|
|
&model.WalletLedger{},
|
|
&model.RenterGrowthLedger{},
|
|
&model.AuditLog{},
|
|
&model.ChatConversation{},
|
|
&model.ChatParticipant{},
|
|
&model.ChatAdminConversationState{},
|
|
&model.ChatMessage{},
|
|
)
|
|
}
|
|
|
|
// MigrateRentalDisputeTestSchema 在交易链路基础上创建申诉测试所需的表结构。
|
|
func MigrateRentalDisputeTestSchema(db *gorm.DB) error {
|
|
if err := MigrateRentalTransactionTestSchema(db); err != nil {
|
|
return err
|
|
}
|
|
return db.AutoMigrate(&model.Dispute{})
|
|
}
|