46 lines
1.3 KiB
Go
46 lines
1.3 KiB
Go
package integration
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
integrationbiz "kra/internal/biz/integration"
|
|
"kra/pkg/database/migration"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func Migrations() []migration.Step {
|
|
return []migration.Step{
|
|
{ID: "202608200001_data_infrastructure", Migrate: func(db *gorm.DB) error {
|
|
return migration.CreateMissingTables(db, &ConfigPO{})
|
|
}},
|
|
{ID: "202608210001_communication_integration_defaults", Migrate: ensureCommunicationIntegrationConfigs},
|
|
}
|
|
}
|
|
|
|
func ensureCommunicationIntegrationConfigs(db *gorm.DB) error {
|
|
defaults := []struct{ kind, provider string }{
|
|
{integrationbiz.IntegrationKindMQ, "emqx"},
|
|
{integrationbiz.IntegrationKindMQ, "rabbitmq"},
|
|
{integrationbiz.IntegrationKindWebSocket, "melody"},
|
|
}
|
|
for _, item := range defaults {
|
|
var row ConfigPO
|
|
err := db.Where("kind = ? AND provider = ?", item.kind, item.provider).First(&row).Error
|
|
if gorm.ErrRecordNotFound == err {
|
|
values, marshalErr := json.Marshal(integrationbiz.DefaultIntegrationConfig(item.kind, item.provider))
|
|
if marshalErr != nil {
|
|
return marshalErr
|
|
}
|
|
if err = db.Create(&ConfigPO{Kind: item.kind, Provider: item.provider, Enabled: false, Config: string(values)}).Error; err != nil {
|
|
return err
|
|
}
|
|
continue
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|