Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add allOrNothing: lint option #250

Merged
merged 2 commits into from
Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 60 additions & 10 deletions config/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ type Rule interface {

// RequireTableComment checks table comment
type RequireTableComment struct {
Enabled bool `yaml:"enabled"`
Exclude []string `yaml:"exclude"`
Enabled bool `yaml:"enabled"`
AllOrNothing bool `yaml:"allOrNothing"`
Exclude []string `yaml:"exclude"`
}

// IsEnabled return Rule is enabled or not
Expand All @@ -51,11 +52,13 @@ func (r RequireTableComment) IsEnabled() bool {
func (r RequireTableComment) Check(s *schema.Schema, exclude []string) []RuleWarn {
warns := []RuleWarn{}
if !r.IsEnabled() {
return warns
return []RuleWarn{}
}
msg := "table comment required."

nt := s.NormalizeTableNames(r.Exclude)
commented := false

for _, t := range s.Tables {
if contains(exclude, t.Name) {
continue
Expand All @@ -68,14 +71,20 @@ func (r RequireTableComment) Check(s *schema.Schema, exclude []string) []RuleWar
Target: t.Name,
Message: msg,
})
continue
}
commented = true
}
if r.AllOrNothing && !commented {
return []RuleWarn{}
}
return warns
}

// RequireColumnComment checks column comment
type RequireColumnComment struct {
Enabled bool `yaml:"enabled"`
AllOrNothing bool `yaml:"allOrNothing"`
Exclude []string `yaml:"exclude"`
ExcludeTables []string `yaml:"excludeTables"`
}
Expand All @@ -89,11 +98,13 @@ func (r RequireColumnComment) IsEnabled() bool {
func (r RequireColumnComment) Check(s *schema.Schema, exclude []string) []RuleWarn {
warns := []RuleWarn{}
if !r.IsEnabled() {
return warns
return []RuleWarn{}
}
msg := "column comment required."

nt := s.NormalizeTableNames(r.ExcludeTables)
commented := false

for _, t := range s.Tables {
if contains(exclude, t.Name) {
continue
Expand All @@ -111,15 +122,21 @@ func (r RequireColumnComment) Check(s *schema.Schema, exclude []string) []RuleWa
Target: target,
Message: msg,
})
continue
}
commented = true
}
}
if r.AllOrNothing && !commented {
return []RuleWarn{}
}
return warns
}

// RequireIndexComment checks index comment
type RequireIndexComment struct {
Enabled bool `yaml:"enabled"`
AllOrNothing bool `yaml:"allOrNothing"`
Exclude []string `yaml:"exclude"`
ExcludeTables []string `yaml:"excludeTables"`
}
Expand All @@ -133,11 +150,13 @@ func (r RequireIndexComment) IsEnabled() bool {
func (r RequireIndexComment) Check(s *schema.Schema, exclude []string) []RuleWarn {
warns := []RuleWarn{}
if !r.IsEnabled() {
return warns
return []RuleWarn{}
}
msg := "index comment required."

nt := s.NormalizeTableNames(r.ExcludeTables)
commented := false

for _, t := range s.Tables {
if contains(exclude, t.Name) {
continue
Expand All @@ -155,15 +174,21 @@ func (r RequireIndexComment) Check(s *schema.Schema, exclude []string) []RuleWar
Target: target,
Message: msg,
})
continue
}
commented = true
}
}
if r.AllOrNothing && !commented {
return []RuleWarn{}
}
return warns
}

// RequireConstraintComment checks constraint comment
type RequireConstraintComment struct {
Enabled bool `yaml:"enabled"`
AllOrNothing bool `yaml:"allOrNothing"`
Exclude []string `yaml:"exclude"`
ExcludeTables []string `yaml:"excludeTables"`
}
Expand All @@ -177,11 +202,13 @@ func (r RequireConstraintComment) IsEnabled() bool {
func (r RequireConstraintComment) Check(s *schema.Schema, exclude []string) []RuleWarn {
warns := []RuleWarn{}
if !r.IsEnabled() {
return warns
return []RuleWarn{}
}
msg := "constraint comment required."

nt := s.NormalizeTableNames(r.ExcludeTables)
commented := false

for _, t := range s.Tables {
if contains(exclude, t.Name) {
continue
Expand All @@ -199,15 +226,21 @@ func (r RequireConstraintComment) Check(s *schema.Schema, exclude []string) []Ru
Target: target,
Message: msg,
})
continue
}
commented = true
}
}
if r.AllOrNothing && !commented {
return []RuleWarn{}
}
return warns
}

// RequireTriggerComment checks trigger comment
type RequireTriggerComment struct {
Enabled bool `yaml:"enabled"`
AllOrNothing bool `yaml:"allOrNothing"`
Exclude []string `yaml:"exclude"`
ExcludeTables []string `yaml:"excludeTables"`
}
Expand All @@ -221,11 +254,13 @@ func (r RequireTriggerComment) IsEnabled() bool {
func (r RequireTriggerComment) Check(s *schema.Schema, exclude []string) []RuleWarn {
warns := []RuleWarn{}
if !r.IsEnabled() {
return warns
return []RuleWarn{}
}
msg := "trigger comment required."

nt := s.NormalizeTableNames(r.ExcludeTables)
commented := false

for _, t := range s.Tables {
if contains(exclude, t.Name) {
continue
Expand All @@ -243,16 +278,22 @@ func (r RequireTriggerComment) Check(s *schema.Schema, exclude []string) []RuleW
Target: target,
Message: msg,
})
continue
}
commented = true
}
}
if r.AllOrNothing && !commented {
return []RuleWarn{}
}
return warns
}

// UnrelatedTable checks isolated table
type UnrelatedTable struct {
Enabled bool `yaml:"enabled"`
Exclude []string `yaml:"exclude"`
Enabled bool `yaml:"enabled"`
AllOrNothing bool `yaml:"allOrNothing"`
Exclude []string `yaml:"exclude"`
}

// IsEnabled return Rule is enabled or not
Expand All @@ -264,11 +305,12 @@ func (r UnrelatedTable) IsEnabled() bool {
func (r UnrelatedTable) Check(s *schema.Schema, exclude []string) []RuleWarn {
warns := []RuleWarn{}
if !r.IsEnabled() {
return warns
return []RuleWarn{}
}
msgFmt := "unrelated (isolated) table exists. %s"

nt := s.NormalizeTableNames(r.Exclude)
related := false
ut := map[string]*schema.Table{}
for _, t := range s.Tables {
if contains(exclude, t.Name) {
Expand All @@ -279,10 +321,15 @@ func (r UnrelatedTable) Check(s *schema.Schema, exclude []string) []RuleWarn {
}
ut[t.Name] = t
}
before := len(ut)
for _, rl := range s.Relations {
delete(ut, rl.Table.Name)
delete(ut, rl.ParentTable.Name)
}
after := len(ut)
if before != after {
related = true
}
if len(ut) > 0 {
us := []string{}
for _, t := range ut {
Expand All @@ -293,6 +340,9 @@ func (r UnrelatedTable) Check(s *schema.Schema, exclude []string) []RuleWarn {
Message: fmt.Sprintf(msgFmt, us),
})
}
if r.AllOrNothing && !related {
return []RuleWarn{}
}
return warns
}

Expand Down
Loading