test_data/initialization/postgre.go
2024-11-07 16:04:30 +08:00

50 lines
1.8 KiB
Go

package initialization
import (
"fmt"
"github.com/spf13/viper"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"log"
"testData/global"
"testData/model"
"time"
)
func InitPostgres() {
//dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Shanghai",
// "192.168.0.56",
// "tpdb",
// "TpMysql12#$",
// "test_data",
// "5432",
//)
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Shanghai",
viper.GetString("postgres.host"),
viper.GetString("postgres.user"),
viper.GetString("postgres.password"),
viper.GetString("postgres.dbname"),
viper.GetString("postgres.port"),
)
if db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
DisableForeignKeyConstraintWhenMigrating: true,
//Logger: logger.Default.LogMode(logger.Silent),
}); err != nil {
log.Printf("PostgreSQL Connection error: %s", err)
panic(err)
} else {
postgresDB, _ := db.DB()
postgresDB.SetMaxIdleConns(10)
postgresDB.SetMaxOpenConns(100)
postgresDB.SetConnMaxLifetime(time.Hour)
_ = db.AutoMigrate(&model.FileHandled{})
global.PostGreSQL = db
_ = global.PostGreSQL.AutoMigrate(&model.FileHandled{}, &model.Report{}, &model.FinalReportExcel{},
&model.Report{}, &model.FinalReport{}, &model.ReportSites{}, &model.BinFail{},
&model.WaferList{}, &model.WaferStock{}, &model.ABList{}, &model.ABListStock{}, &model.BPList{}, &model.BPListStock{},
&model.TRList{}, &model.TRListStock{}, &model.CPList{}, &model.CPListStock{}, &model.FTList{}, &model.FTListStock{},
&model.Warning{}, &model.ProductionControl{}, &model.PassProbabilityDiff{}, &model.BinControl{},
&model.SelectionDiffControl{}, &model.StackingMaterialsWarning{}, &model.HistogramSelection{}, &model.ScatterSelection{})
}
}