VILLASweb-backend-go/database/database.go
2019-11-13 20:30:02 +01:00

94 lines
3 KiB
Go

package database
import (
"fmt"
"log"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
"log"
)
var DB_HOST string // host of the database system
var DB_NAME string // name of the production database
var DB_USER string // name of the database user
var DB_PASS string // database password
var DB_SSLMODE string // set to enable if database uses SSL
var AMQP_URL string // if set connect to AMQP broker using this URL
var DBpool *gorm.DB // database used by backend
// Initialize input command line flags
func init() {
flag.StringVar(&DB_HOST, "dbhost", "/var/run/postgresql", "Host of the PostgreSQL database (default is /var/run/postgresql for localhost DB on Ubuntu systems)")
flag.StringVar(&DB_NAME, "dbname", "villasdb", "Name of the database to use (default is villasdb)")
flag.StringVar(&DB_USER, "dbuser", "", "Username of database connection (default is <empty>)")
flag.StringVar(&DB_PASS, "dbpass", "", "Password of database connection (default is <empty>)")
flag.StringVar(&DB_SSLMODE, "dbsslmode", "disable", "SSL mode of DB (default is disable)") // TODO: change default for production
flag.StringVar(&AMQP_URL, "amqp", "", "If set, use this url to connect to an AMQP broker (default is disabled)")
flag.Parse()
fmt.Println("DB_HOST has value ", DB_HOST)
fmt.Println("DB_USER has value ", DB_USER)
fmt.Println("DB_NAME has value ", DB_NAME)
fmt.Println("DB_SSLMODE has value ", DB_SSLMODE)
fmt.Println("AMQP_URL has value ", AMQP_URL)
}
// Initialize connection to the database
func InitDB(dbname string, isTest bool) *gorm.DB {
dbinfo := fmt.Sprintf("host=%s sslmode=%s dbname=%s",
DB_HOST, DB_SSLMODE, dbname)
if DB_USER != "" && DB_PASS != "" {
dbinfo = fmt.Sprintf("host=%s sslmode=%s user=%s password=%s dbname=%s",
DB_HOST, DB_SSLMODE, DB_USER, DB_PASS, dbname)
}
db, err := gorm.Open("postgres", dbinfo)
if err != nil {
log.Fatal(err)
}
DBpool = db
if isTest {
// drop tables for testing case
DropTables(db)
}
log.Println("Database connection established")
return db
}
// Connection pool to already opened DB
func GetDB() *gorm.DB {
return DBpool
}
// Drop all the tables of the database
// TODO: Remove that function from the codebase and substitute the body
// to the Dummy*() where it is called
func DropTables(db *gorm.DB) {
db.DropTableIfExists(&Simulator{})
db.DropTableIfExists(&Signal{})
db.DropTableIfExists(&SimulationModel{})
db.DropTableIfExists(&File{})
db.DropTableIfExists(&Scenario{})
db.DropTableIfExists(&User{})
db.DropTableIfExists(&Dashboard{})
db.DropTableIfExists(&Widget{})
// The following statement deletes the many to many relationship between users and scenarios
db.DropTableIfExists("user_scenarios")
}
// AutoMigrate the models
func MigrateModels(db *gorm.DB) {
db.AutoMigrate(&Simulator{})
db.AutoMigrate(&Signal{})
db.AutoMigrate(&SimulationModel{})
db.AutoMigrate(&File{})
db.AutoMigrate(&Scenario{})
db.AutoMigrate(&User{})
db.AutoMigrate(&Dashboard{})
db.AutoMigrate(&Widget{})
}