Merge branch 'orm-fix'

This commit is contained in:
smavros 2019-04-28 17:25:13 +02:00
commit 5401a6bb02
2 changed files with 71 additions and 49 deletions

View file

@ -96,10 +96,14 @@ func DummyPopulateDB(test_db *gorm.DB) {
checkErr(test_db.Create(&simr_A).Error) checkErr(test_db.Create(&simr_A).Error)
checkErr(test_db.Create(&simr_B).Error) checkErr(test_db.Create(&simr_B).Error)
sig_A := Signal{Name: "Signal_A"} outSig_A := Signal{Name: "outSignal_A"}
sig_B := Signal{Name: "Signal_B"} outSig_B := Signal{Name: "outSignal_B"}
checkErr(test_db.Create(&sig_A).Error) inSig_A := Signal{Name: "inSignal_A"}
checkErr(test_db.Create(&sig_B).Error) inSig_B := Signal{Name: "inSignal_B"}
checkErr(test_db.Create(&outSig_A).Error)
checkErr(test_db.Create(&outSig_B).Error)
checkErr(test_db.Create(&inSig_A).Error)
checkErr(test_db.Create(&inSig_B).Error)
smo_A := SimulationModel{Name: "SimModel_A"} smo_A := SimulationModel{Name: "SimModel_A"}
smo_B := SimulationModel{Name: "SimModel_B"} smo_B := SimulationModel{Name: "SimModel_B"}
@ -140,37 +144,46 @@ func DummyPopulateDB(test_db *gorm.DB) {
// For `belongs to` use the model with id=1 // For `belongs to` use the model with id=1
// For `has many` use the models with id=1 and id=2 // For `has many` use the models with id=1 and id=2
checkErr(test_db.Model(&smo_A).Association("BelongsToSimulation").Append(&simn_A).Error) // Project HM Visualization, Visualization BT Project
checkErr(test_db.Model(&smo_A).Association("BelongsToSimulator").Append(&simr_A).Error)
checkErr(test_db.Model(&smo_A).Association("OutputMapping").Append(&sig_A).Error)
checkErr(test_db.Model(&smo_A).Association("OutputMapping").Append(&sig_B).Error)
checkErr(test_db.Model(&smo_A).Association("InputMapping").Append(&sig_B).Error)
checkErr(test_db.Model(&smo_A).Association("InputMapping").Append(&sig_A).Error)
checkErr(test_db.Model(&simn_A).Association("User").Append(&usr_A).Error)
checkErr(test_db.Model(&simn_A).Association("Models").Append(&smo_A).Error)
checkErr(test_db.Model(&simn_A).Association("Models").Append(&smo_B).Error)
checkErr(test_db.Model(&simn_A).Association("Projects").Append(&proj_A).Error)
checkErr(test_db.Model(&simn_A).Association("Projects").Append(&proj_B).Error)
checkErr(test_db.Model(&proj_A).Association("Simulation").Append(&simn_A).Error)
checkErr(test_db.Model(&proj_A).Association("User").Append(&usr_A).Error)
checkErr(test_db.Model(&proj_A).Association("Visualizations").Append(&vis_A).Error)
checkErr(test_db.Model(&proj_A).Association("Visualizations").Append(&vis_B).Error)
checkErr(test_db.Model(&usr_A).Association("Projects").Append(&proj_A).Error)
checkErr(test_db.Model(&usr_A).Association("Projects").Append(&proj_B).Error)
checkErr(test_db.Model(&usr_A).Association("Simulations").Append(&simn_A).Error)
checkErr(test_db.Model(&usr_A).Association("Simulations").Append(&simn_B).Error)
checkErr(test_db.Model(&usr_A).Association("Files").Append(&file_A).Error)
checkErr(test_db.Model(&usr_A).Association("Files").Append(&file_B).Error)
checkErr(test_db.Model(&vis_A).Association("Project").Append(&proj_A).Error) checkErr(test_db.Model(&vis_A).Association("Project").Append(&proj_A).Error)
checkErr(test_db.Model(&vis_A).Association("User").Append(&usr_A).Error) checkErr(test_db.Model(&vis_B).Association("Project").Append(&proj_A).Error)
// User HM Project, Project BT User
checkErr(test_db.Model(&proj_A).Association("User").Append(&usr_A).Error)
checkErr(test_db.Model(&proj_B).Association("User").Append(&usr_A).Error)
// Simulation HM Project, Project BT Simulation
checkErr(test_db.Model(&proj_A).Association("Simulation").Append(&simn_A).Error)
checkErr(test_db.Model(&proj_B).Association("Simulation").Append(&simn_A).Error)
// User HM File, File BT User
checkErr(test_db.Model(&file_A).Association("User").Append(&usr_A).Error)
checkErr(test_db.Model(&file_B).Association("User").Append(&usr_A).Error)
// Simulation HM SimModel, SimModel BT Simulation
checkErr(test_db.Model(&smo_A).Association("BelongsToSimulation").Append(&simn_A).Error)
checkErr(test_db.Model(&smo_B).Association("BelongsToSimulation").Append(&simn_A).Error)
// User HM Simulation, Simulation BT User
checkErr(test_db.Model(&simn_A).Association("User").Append(&usr_A).Error)
checkErr(test_db.Model(&simn_B).Association("User").Append(&usr_A).Error)
// Visualization HM Widget
checkErr(test_db.Model(&vis_A).Association("Widgets").Append(&widg_A).Error) checkErr(test_db.Model(&vis_A).Association("Widgets").Append(&widg_A).Error)
checkErr(test_db.Model(&vis_A).Association("Widgets").Append(&widg_B).Error) checkErr(test_db.Model(&vis_A).Association("Widgets").Append(&widg_B).Error)
checkErr(test_db.Model(&file_A).Association("User").Append(&usr_A).Error) // SimModel HM Signal
checkErr(test_db.Model(&smo_A).Association("InputMapping").Append(&inSig_A).Error)
checkErr(test_db.Model(&smo_A).Association("InputMapping").Append(&inSig_B).Error)
checkErr(test_db.Model(&smo_A).Association("OutputMapping").Append(&outSig_A).Error)
checkErr(test_db.Model(&smo_A).Association("OutputMapping").Append(&outSig_B).Error)
// Visualization BT User
checkErr(test_db.Model(&vis_A).Association("User").Append(&usr_A).Error)
// Simulator BT SimModel
checkErr(test_db.Model(&smo_A).Association("BelongsToSimulator").Append(&simr_A).Error)
} }
// Erase tables and glose the testdb // Erase tables and glose the testdb

View file

@ -1,13 +1,14 @@
package common package common
import ( import (
"github.com/jinzhu/gorm" //"github.com/jinzhu/gorm"
"github.com/jinzhu/gorm/dialects/postgres" "github.com/jinzhu/gorm/dialects/postgres"
"time" "time"
) )
type Simulator struct { type Simulator struct {
gorm.Model //gorm.Model
ID uint `gorm:"primary_key;auto_increment"`
UUID string `gorm:"unique;not null"` UUID string `gorm:"unique;not null"`
Host string `gorm:"default:''"` Host string `gorm:"default:''"`
Modeltype string `gorm:"default:''"` Modeltype string `gorm:"default:''"`
@ -19,7 +20,8 @@ type Simulator struct {
} }
type File struct { type File struct {
gorm.Model //gorm.Model
ID uint `gorm:"primary_key;auto_increment"`
Name string `gorm:"not null"` Name string `gorm:"not null"`
Path string `gorm:"not null"` Path string `gorm:"not null"`
Type string `gorm:"not null"` Type string `gorm:"not null"`
@ -33,7 +35,8 @@ type File struct {
} }
type Project struct { type Project struct {
gorm.Model //gorm.Model
ID uint `gorm:"primary_key;auto_increment"`
Name string `gorm:"not null"` Name string `gorm:"not null"`
User User `gorm:"not null;association_autoupdate:false"` User User `gorm:"not null;association_autoupdate:false"`
@ -42,11 +45,12 @@ type Project struct {
Simulation Simulation `gorm:"not null;association_autoupdate:false"` Simulation Simulation `gorm:"not null;association_autoupdate:false"`
SimulationID uint `gorm:"not null"` SimulationID uint `gorm:"not null"`
Visualizations []Visualization //`gorm:"association_autoupdate:false"` Visualizations []Visualization `gorm:"association_autoupdate:false"`
} }
type Simulation struct { type Simulation struct {
gorm.Model //gorm.Model
ID uint `gorm:"primary_key;auto_increment"`
Name string `gorm:"not null"` Name string `gorm:"not null"`
Running bool `gorm:"default:false"` Running bool `gorm:"default:false"`
StartParameters postgres.Jsonb // TODO default value StartParameters postgres.Jsonb // TODO default value
@ -54,12 +58,13 @@ type Simulation struct {
User User `gorm:"not null;association_autoupdate:false"` User User `gorm:"not null;association_autoupdate:false"`
UserID uint `gorm:"not null"` UserID uint `gorm:"not null"`
Models []SimulationModel `gorm:"foreignkey:BelongsToSimulationID"` //;association_autoupdate:false"` Models []SimulationModel `gorm:"foreignkey:BelongsToSimulationID;association_autoupdate:false"`
Projects []Project `gorm:"association_autoupdate:false"` Projects []Project `gorm:"association_autoupdate:false"`
} }
type SimulationModel struct { type SimulationModel struct {
gorm.Model //gorm.Model
ID uint `gorm:"primary_key;auto_increment"`
Name string `gorm:"not null"` Name string `gorm:"not null"`
OutputLength int `gorm:"default:1"` OutputLength int `gorm:"default:1"`
InputLength int `gorm:"default:1"` InputLength int `gorm:"default:1"`
@ -72,24 +77,26 @@ type SimulationModel struct {
BelongsToSimulatorID uint `gorm:"not null"` BelongsToSimulatorID uint `gorm:"not null"`
// NOTE: order of signals is important // NOTE: order of signals is important
OutputMapping []Signal //`gorm:"association_autoupdate:false"` OutputMapping []Signal `gorm:""`
InputMapping []Signal //`gorm:"association_autoupdate:false"` InputMapping []Signal `gorm:""`
} }
type User struct { type User struct {
gorm.Model //gorm.Model
ID uint `gorm:"primary_key;auto_increment"`
Username string `gorm:"unique;not null"` Username string `gorm:"unique;not null"`
Password string `gorm:"not null"` Password string `gorm:"not null"`
Mail string `gorm:"default:''"` Mail string `gorm:"default:''"`
Role string `gorm:"default:'user'"` Role string `gorm:"default:'user'"`
Projects []Project //`gorm:"association_autoupdate:false"` Projects []Project `gorm:"association_autoupdate:false"`
Simulations []Simulation //`gorm:"association_autoupdate:false"` Simulations []Simulation `gorm:"association_autoupdate:false"`
Files []File //`gorm:"association_autoupdate:false"` Files []File `gorm:"association_autoupdate:false"`
} }
type Visualization struct { type Visualization struct {
gorm.Model //gorm.Model
ID uint `gorm:"primary_key;auto_increment"`
Name string `gorm:"not null"` Name string `gorm:"not null"`
Grid int `gorm:"default:15"` Grid int `gorm:"default:15"`
@ -99,11 +106,12 @@ type Visualization struct {
User User `gorm:"not null;association_autoupdate:false"` User User `gorm:"not null;association_autoupdate:false"`
UserID uint `gorm:"not null"` UserID uint `gorm:"not null"`
Widgets []Widget //`gorm:"association_autoupdate:false"` Widgets []Widget `gorm:""`
} }
type Signal struct { type Signal struct {
gorm.Model //gorm.Model
ID uint `gorm:"primary_key;auto_increment"`
Name string `gorm:"not null"` Name string `gorm:"not null"`
Unit string `gorm:"not null"` Unit string `gorm:"not null"`
SimulationModelID uint SimulationModelID uint
@ -111,7 +119,8 @@ type Signal struct {
} }
type Widget struct { type Widget struct {
gorm.Model //gorm.Model
ID uint `gorm:"primary_key;auto_increment"`
Name string `gorm:"not null"` Name string `gorm:"not null"`
Type string `gorm:"not null"` Type string `gorm:"not null"`
Width uint `gorm:"not null"` Width uint `gorm:"not null"`