From 9ac76777279a8e00d3c1e6a6d933931096b5226d Mon Sep 17 00:00:00 2001 From: Mathieu Coulet <114529377+Djangss@users.noreply.github.com> Date: Wed, 4 Dec 2024 13:53:51 +0100 Subject: [PATCH] feat(db) workflow table with events (#121) --- server/internal/models/event.go | 20 ++++++++++++++++++++ server/internal/models/service.go | 8 ++++++++ server/internal/models/workflow.go | 27 +++++++++++++++++++++++++++ server/internal/pkg/db.go | 24 ++++++++++++++++++++++-- 4 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 server/internal/models/event.go create mode 100644 server/internal/models/service.go create mode 100644 server/internal/models/workflow.go diff --git a/server/internal/models/event.go b/server/internal/models/event.go new file mode 100644 index 0000000..b5e9545 --- /dev/null +++ b/server/internal/models/event.go @@ -0,0 +1,20 @@ +package models + +import "gorm.io/gorm" + +type WorkflowStatus string + +type Event struct { + gorm.Model + Name string `json:"name"` + Description string `json:"description"` + ServiceID uint `gorm:"foreignKey:ServiceID" json:"service_id"` + Parameters []Parameters `json:"parameters"` + Type EventType `gorm:"type:enum('action', 'reaction');not null" json:"type"` +} + +const ( + WorkflowStatusPending WorkflowStatus = "pending" + WorkflowStatusProcessed WorkflowStatus = "processed" + WorkflowStatusFailed WorkflowStatus = "failed" +) diff --git a/server/internal/models/service.go b/server/internal/models/service.go new file mode 100644 index 0000000..ac74e0a --- /dev/null +++ b/server/internal/models/service.go @@ -0,0 +1,8 @@ +package models + +import "gorm.io/gorm" + +type Service struct { + gorm.Model + Label string `json:"label"` +} diff --git a/server/internal/models/workflow.go b/server/internal/models/workflow.go new file mode 100644 index 0000000..7657e13 --- /dev/null +++ b/server/internal/models/workflow.go @@ -0,0 +1,27 @@ +package models + +import "gorm.io/gorm" + +type EventType string + +const ( + ActionEventType EventType = "action" + ReactionEventType EventType = "reaction" +) + +type Parameters struct { + gorm.Model + Name string `json:"name"` + Description string `json:"description"` + Type string `json:"type"` + EventID uint `gorm:"foreignKey:EventID" json:"event_id"` +} + +type Workflow struct { + gorm.Model + UserID uint `gorm:"foreignKey:UserID" json:"user_id"` + Name string `json:"name"` + Description string `json:"description"` + Status WorkflowStatus `gorm:"type:enum('pending', 'processed', 'failed')" json:"status"` + Events []Event `gorm:"many2many:workflow_events" json:"events"` +} diff --git a/server/internal/pkg/db.go b/server/internal/pkg/db.go index 5139365..8bc06d2 100644 --- a/server/internal/pkg/db.go +++ b/server/internal/pkg/db.go @@ -1,6 +1,7 @@ package db import ( + "AREA/internal/models" "AREA/internal/utils" "fmt" "gorm.io/driver/mysql" @@ -10,6 +11,20 @@ import ( var DB *gorm.DB +func migrateDB() error { + err := DB.AutoMigrate( + &models.User{}, + &models.Workflow{}, + &models.Event{}, + &models.Parameters{}, + &models.Service{}, + ) + if err != nil { + log.Fatalf("Failed to migrate DB: %v", err) + } + return err +} + func InitDB() { dbHost := utils.GetEnvVar("DB_HOST") dbPort := utils.GetEnvVar("DB_PORT") @@ -20,7 +35,12 @@ func InitDB() { err := error(nil) DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{}) if err != nil { - log.Fatalf("failed to initialize database, got error %v", err) + log.Fatalf("Failed to connect database: %v", err) + return + } + log.Println("Database connection established") + if migrateDB() != nil { + return } - fmt.Println("Database connection established") + log.Println("Migration done") }