-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathmigrations.go
More file actions
139 lines (128 loc) · 4.71 KB
/
Copy pathmigrations.go
File metadata and controls
139 lines (128 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package database
import (
"errors"
"fmt"
adaudit_domain "github.com/utmstack/utmstack/backend/modules/adaudit/domain"
alerts_domain "github.com/utmstack/utmstack/backend/modules/alerts/domain"
appconfig_domain "github.com/utmstack/utmstack/backend/modules/appconfig/domain"
audit_domain "github.com/utmstack/utmstack/backend/modules/audit/domain"
compliance_domain "github.com/utmstack/utmstack/backend/modules/compliance/domain"
dashboards_domain "github.com/utmstack/utmstack/backend/modules/dashboards/domain"
datasources_domain "github.com/utmstack/utmstack/backend/modules/datasources/domain"
iam_domain "github.com/utmstack/utmstack/backend/modules/iam/domain"
incidents_domain "github.com/utmstack/utmstack/backend/modules/incidents/domain"
integrations_domain "github.com/utmstack/utmstack/backend/modules/integrations/domain"
loganalyzer_domain "github.com/utmstack/utmstack/backend/modules/loganalyzer/domain"
notifications_domain "github.com/utmstack/utmstack/backend/modules/notifications/domain"
opensearch_domain "github.com/utmstack/utmstack/backend/modules/opensearch/domain"
arr_domain "github.com/utmstack/utmstack/backend/modules/soar/domain"
"gorm.io/gorm"
"github.com/threatwinds/go-sdk/catcher"
"github.com/golang-migrate/migrate/v4"
migratepg "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
)
func Models() []any {
return []any{
iam_domain.User{},
iam_domain.Authority{},
iam_domain.Permission{},
iam_domain.AuthorityPermission{},
iam_domain.UserAuthority{},
iam_domain.APIKey{},
iam_domain.RefreshToken{},
iam_domain.IdentityProviderConfig{},
audit_domain.AuditLog{},
appconfig_domain.Config{},
alerts_domain.UtmAlertTag{},
alerts_domain.UtmAlertTagRule{},
arr_domain.AlertResponseRuleExecution{},
arr_domain.AlertResponseActionTemplate{},
arr_domain.UtmIncidentVariable{},
arr_domain.UtmIncidentAction{},
arr_domain.UtmIncidentActionCommand{},
arr_domain.UtmIncidentJob{},
compliance_domain.UtmComplianceReportSchedule{},
compliance_domain.UtmComplianceControlStatusOverride{},
compliance_domain.UtmComplianceControlNote{},
opensearch_domain.UtmIndexPattern{},
integrations_domain.UtmModule{},
incidents_domain.UtmIncident{},
incidents_domain.UtmIncidentAlert{},
incidents_domain.UtmIncidentNote{},
incidents_domain.UtmIncidentHistory{},
notifications_domain.UtmNotification{},
datasources_domain.UtmAssetGroup{},
datasources_domain.Datasource{},
dashboards_domain.Dashboard{},
dashboards_domain.Visualization{},
dashboards_domain.DashboardVisualization{},
loganalyzer_domain.UtmLogAnalyzerQuery{},
adaudit_domain.ADUser{},
}
}
func MigrateDatabase(db *gorm.DB, migrationsURL string) error {
if err := db.Exec(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`).Error; err != nil {
catcher.Warn("could not create uuid-ossp extension", map[string]any{"error": err.Error()})
}
if migrationsURL != "" {
if err := runSQLMigrations(db, migrationsURL+"/pre", "schema_migrations_pre", "pre-AutoMigrate"); err != nil {
return err
}
}
models := Models()
if len(models) > 0 {
catcher.Info("running GORM AutoMigrate...", nil)
if err := db.AutoMigrate(models...); err != nil {
return err
}
catcher.Info("GORM AutoMigrate complete", nil)
}
if migrationsURL == "" {
return nil
}
return runSQLMigrations(db, migrationsURL, "schema_migrations", "post-AutoMigrate")
}
func runSQLMigrations(db *gorm.DB, migrationsURL, table, stage string) error {
sqlDB, err := db.DB()
if err != nil {
return err
}
driver, err := migratepg.WithInstance(sqlDB, &migratepg.Config{MigrationsTable: table})
if err != nil {
return err
}
m, err := migrate.NewWithDatabaseInstance(migrationsURL, "postgres", driver)
if err != nil {
return err
}
if err := applyUp(m, stage); err != nil {
var dirty migrate.ErrDirty
if !errors.As(err, &dirty) {
return err
}
// A previous run started version dirty.Version but failed mid-way.
// All our migrations are idempotent (IF EXISTS / ON CONFLICT), so it is
// safe to force the version back one step and retry.
catcher.Warn(fmt.Sprintf("%s SQL migration version %d is dirty — forcing back and retrying", stage, dirty.Version), nil)
if ferr := m.Force(dirty.Version - 1); ferr != nil {
return fmt.Errorf("could not force migration version after dirty state: %w", ferr)
}
if rerr := applyUp(m, stage); rerr != nil {
return rerr
}
}
return nil
}
func applyUp(m *migrate.Migrate, stage string) error {
switch err := m.Up(); {
case err == nil:
catcher.Info(stage+" SQL migrations applied", nil)
return nil
case errors.Is(err, migrate.ErrNoChange):
catcher.Info(stage+" SQL migrations already up to date", nil)
return nil
default:
return err
}
}