-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use the new seeding library to seed rulesets
Signed-off-by: Sam Lucidi <slucidi@redhat.com>
- Loading branch information
Showing
23 changed files
with
1,046 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package v7 | ||
|
||
import ( | ||
liberr "github.com/jortel/go-utils/error" | ||
"github.com/jortel/go-utils/logr" | ||
"github.com/konveyor/tackle2-hub/migration/v6/model" | ||
"gorm.io/gorm" | ||
) | ||
|
||
var log = logr.WithName("migration|v7") | ||
|
||
type Migration struct{} | ||
|
||
func (r Migration) Apply(db *gorm.DB) (err error) { | ||
// note: sqlite can't add a unique column, so we add UUID as a optional column, | ||
// and then mark the column unique and create the index via auto-migrate. | ||
type TagCategory struct { | ||
model.TagCategory | ||
UUID string | ||
} | ||
|
||
type Tag struct { | ||
model.Tag | ||
UUID string | ||
} | ||
|
||
type JobFunction struct { | ||
model.JobFunction | ||
UUID string | ||
} | ||
|
||
type RuleSet struct { | ||
model.RuleSet | ||
UUID string | ||
} | ||
|
||
err = db.AutoMigrate(&TagCategory{}, &Tag{}, &JobFunction{}, &RuleSet{}) | ||
if err != nil { | ||
err = liberr.Wrap(err) | ||
return | ||
} | ||
|
||
err = db.AutoMigrate(r.Models()...) | ||
if err != nil { | ||
err = liberr.Wrap(err) | ||
return | ||
} | ||
|
||
err = db.Delete(&model.RuleSet{}, "CreateUser = ?", "").Error | ||
if err != nil { | ||
err = liberr.Wrap(err) | ||
return | ||
} | ||
|
||
return | ||
} | ||
|
||
func (r Migration) Models() []interface{} { | ||
return model.All() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package model | ||
|
||
type Application struct { | ||
Model | ||
BucketOwner | ||
Name string `gorm:"index;unique;not null"` | ||
Description string | ||
Review *Review `gorm:"constraint:OnDelete:CASCADE"` | ||
Repository JSON `gorm:"type:json"` | ||
Binary string | ||
Facts []Fact `gorm:"constraint:OnDelete:CASCADE"` | ||
Comments string | ||
Tasks []Task `gorm:"constraint:OnDelete:CASCADE"` | ||
Tags []Tag `gorm:"many2many:ApplicationTags"` | ||
Identities []Identity `gorm:"many2many:ApplicationIdentity;constraint:OnDelete:CASCADE"` | ||
BusinessServiceID *uint `gorm:"index"` | ||
BusinessService *BusinessService | ||
OwnerID *uint `gorm:"index"` | ||
Owner *Stakeholder `gorm:"foreignKey:OwnerID"` | ||
Contributors []Stakeholder `gorm:"many2many:ApplicationContributors;constraint:OnDelete:CASCADE"` | ||
Analyses []Analysis `gorm:"constraint:OnDelete:CASCADE"` | ||
MigrationWaveID *uint `gorm:"index"` | ||
MigrationWave *MigrationWave | ||
Ticket *Ticket `gorm:"constraint:OnDelete:CASCADE"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package model | ||
|
||
// | ||
// ApplicationTag represents a row in the join table for the | ||
// many-to-many relationship between Applications and Tags. | ||
type ApplicationTag struct { | ||
ApplicationID uint `gorm:"primaryKey"` | ||
TagID uint `gorm:"primaryKey"` | ||
Source string `gorm:"primaryKey;not null"` | ||
Application Application `gorm:"constraint:OnDelete:CASCADE"` | ||
Tag Tag `gorm:"constraint:OnDelete:CASCADE"` | ||
} | ||
|
||
// | ||
// TableName must return "ApplicationTags" to ensure compatibility | ||
// with the autogenerated join table name. | ||
func (ApplicationTag) TableName() string { | ||
return "ApplicationTags" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package model | ||
|
||
type BusinessService struct { | ||
Model | ||
Name string `gorm:"index;unique;not null"` | ||
Description string | ||
Applications []Application `gorm:"constraint:OnDelete:SET NULL"` | ||
StakeholderID *uint `gorm:"index"` | ||
Stakeholder *Stakeholder | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package model | ||
|
||
type JobFunction struct { | ||
Model | ||
UUID string `gorm:"uniqueIndex"` | ||
Username string | ||
Name string `gorm:"index;unique;not null"` | ||
Stakeholders []Stakeholder `gorm:"constraint:OnDelete:SET NULL"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package model | ||
|
||
import "time" | ||
|
||
type MigrationWave struct { | ||
Model | ||
Name string `gorm:"uniqueIndex:MigrationWaveA"` | ||
StartDate time.Time `gorm:"uniqueIndex:MigrationWaveA"` | ||
EndDate time.Time `gorm:"uniqueIndex:MigrationWaveA"` | ||
Applications []Application `gorm:"constraint:OnDelete:SET NULL"` | ||
Stakeholders []Stakeholder `gorm:"many2many:MigrationWaveStakeholders;constraint:OnDelete:CASCADE"` | ||
StakeholderGroups []StakeholderGroup `gorm:"many2many:MigrationWaveStakeholderGroups;constraint:OnDelete:CASCADE"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package model | ||
|
||
import "github.com/konveyor/tackle2-hub/migration/v6/model" | ||
|
||
// | ||
// JSON field (data) type. | ||
type JSON = []byte | ||
|
||
type Model = model.Model | ||
type TechDependency = model.TechDependency | ||
type Incident = model.Incident | ||
type Analysis = model.Analysis | ||
type Issue = model.Issue | ||
type Bucket = model.Bucket | ||
type BucketOwner = model.BucketOwner | ||
type Dependency = model.Dependency | ||
type File = model.File | ||
type Fact = model.Fact | ||
type Identity = model.Identity | ||
type Import = model.Import | ||
type ImportSummary = model.ImportSummary | ||
type ImportTag = model.ImportTag | ||
type Proxy = model.Proxy | ||
type Review = model.Review | ||
type Setting = model.Setting | ||
type Task = model.Task | ||
type TaskGroup = model.TaskGroup | ||
type TaskReport = model.TaskReport | ||
type Ticket = model.Ticket | ||
type Tracker = model.Tracker | ||
type TTL = model.TTL | ||
type DependencyCyclicError = model.DependencyCyclicError | ||
|
||
// | ||
// All builds all models. | ||
// Models are enumerated such that each are listed after | ||
// all the other models on which they may depend. | ||
func All() []interface{} { | ||
return []interface{}{ | ||
Application{}, | ||
TechDependency{}, | ||
Incident{}, | ||
Analysis{}, | ||
Issue{}, | ||
Bucket{}, | ||
BusinessService{}, | ||
Dependency{}, | ||
File{}, | ||
Fact{}, | ||
Identity{}, | ||
Import{}, | ||
ImportSummary{}, | ||
ImportTag{}, | ||
JobFunction{}, | ||
MigrationWave{}, | ||
Proxy{}, | ||
Review{}, | ||
Setting{}, | ||
RuleSet{}, | ||
Rule{}, | ||
Stakeholder{}, | ||
StakeholderGroup{}, | ||
Tag{}, | ||
TagCategory{}, | ||
Task{}, | ||
TaskGroup{}, | ||
TaskReport{}, | ||
Ticket{}, | ||
Tracker{}, | ||
ApplicationTag{}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package model | ||
|
||
import "gorm.io/gorm" | ||
|
||
// | ||
// RuleSet - Analysis ruleset. | ||
type RuleSet struct { | ||
Model | ||
UUID string `gorm:"uniqueIndex"` | ||
Kind string | ||
Name string `gorm:"uniqueIndex;not null"` | ||
Description string | ||
Custom bool | ||
Repository JSON `gorm:"type:json"` | ||
ImageID uint `gorm:"index" ref:"file"` | ||
Image *File | ||
IdentityID *uint `gorm:"index"` | ||
Identity *Identity | ||
Rules []Rule `gorm:"constraint:OnDelete:CASCADE"` | ||
DependsOn []RuleSet `gorm:"many2many:RuleSetDependencies;constraint:OnDelete:CASCADE"` | ||
} | ||
|
||
// | ||
// BeforeUpdate hook to avoid cyclic dependencies. | ||
func (r *RuleSet) BeforeUpdate(db *gorm.DB) (err error) { | ||
seen := make(map[uint]bool) | ||
var nextDeps []RuleSet | ||
var nextRuleSetIDs []uint | ||
for _, dep := range r.DependsOn { | ||
nextRuleSetIDs = append(nextRuleSetIDs, dep.ID) | ||
} | ||
for len(nextRuleSetIDs) != 0 { | ||
result := db.Preload("DependsOn").Where("ID IN ?", nextRuleSetIDs).Find(&nextDeps) | ||
if result.Error != nil { | ||
err = result.Error | ||
return | ||
} | ||
nextRuleSetIDs = nextRuleSetIDs[:0] | ||
for _, nextDep := range nextDeps { | ||
for _, dep := range nextDep.DependsOn { | ||
if seen[dep.ID] { | ||
continue | ||
} | ||
if dep.ID == r.ID { | ||
err = DependencyCyclicError{} | ||
return | ||
} | ||
seen[dep.ID] = true | ||
nextRuleSetIDs = append(nextRuleSetIDs, dep.ID) | ||
} | ||
} | ||
} | ||
|
||
return | ||
} | ||
|
||
// | ||
// Rule - Analysis rule. | ||
type Rule struct { | ||
Model | ||
Name string | ||
Description string | ||
Labels JSON `gorm:"type:json"` | ||
RuleSetID uint `gorm:"uniqueIndex:RuleA;not null"` | ||
RuleSet *RuleSet | ||
FileID *uint `gorm:"uniqueIndex:RuleA" ref:"file"` | ||
File *File | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package model | ||
|
||
type Stakeholder struct { | ||
Model | ||
Name string `gorm:"not null;"` | ||
Email string `gorm:"index;unique;not null"` | ||
Groups []StakeholderGroup `gorm:"many2many:StakeholderGroupStakeholder;constraint:OnDelete:CASCADE"` | ||
BusinessServices []BusinessService `gorm:"constraint:OnDelete:SET NULL"` | ||
JobFunctionID *uint `gorm:"index"` | ||
JobFunction *JobFunction | ||
Owns []Application `gorm:"foreignKey:OwnerID;constraint:OnDelete:SET NULL"` | ||
Contributes []Application `gorm:"many2many:ApplicationContributors;constraint:OnDelete:CASCADE"` | ||
MigrationWaves []MigrationWave `gorm:"many2many:MigrationWaveStakeholders;constraint:OnDelete:CASCADE"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package model | ||
|
||
type StakeholderGroup struct { | ||
Model | ||
Name string `gorm:"index;unique;not null"` | ||
Username string | ||
Description string | ||
Stakeholders []Stakeholder `gorm:"many2many:StakeholderGroupStakeholder;constraint:OnDelete:CASCADE"` | ||
MigrationWaves []MigrationWave `gorm:"many2many:MigrationWaveStakeholderGroups;constraint:OnDelete:CASCADE"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package model | ||
|
||
type Tag struct { | ||
Model | ||
UUID string `gorm:"uniqueIndex"` | ||
Name string `gorm:"uniqueIndex:tagA;not null"` | ||
Username string | ||
CategoryID uint `gorm:"uniqueIndex:tagA;index;not null"` | ||
Category TagCategory | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package model | ||
|
||
type TagCategory struct { | ||
Model | ||
UUID string `gorm:"uniqueIndex"` | ||
Name string `gorm:"index;unique;not null"` | ||
Username string | ||
Rank uint | ||
Color string | ||
Tags []Tag `gorm:"foreignKey:CategoryID;constraint:OnDelete:CASCADE"` | ||
} |
Oops, something went wrong.