Skip to content

Commit

Permalink
feat(db) workflow table with events (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
Djangss authored Dec 4, 2024
1 parent 47a34b7 commit 9ac7677
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
20 changes: 20 additions & 0 deletions server/internal/models/event.go
Original file line number Diff line number Diff line change
@@ -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"
)
8 changes: 8 additions & 0 deletions server/internal/models/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package models

import "gorm.io/gorm"

type Service struct {
gorm.Model
Label string `json:"label"`
}
27 changes: 27 additions & 0 deletions server/internal/models/workflow.go
Original file line number Diff line number Diff line change
@@ -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"`
}
24 changes: 22 additions & 2 deletions server/internal/pkg/db.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package db

import (
"AREA/internal/models"
"AREA/internal/utils"
"fmt"
"gorm.io/driver/mysql"
Expand All @@ -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")
Expand All @@ -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")
}

0 comments on commit 9ac7677

Please sign in to comment.