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 lint rule requireColumns #95

Merged
merged 1 commit into from
May 8, 2019
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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,17 @@ lint:
# exclude tables from warnings
exclude:
- user_options
# require columns
requireColumns:
enabled: true
columns:
-
name: created
-
name: updated
exclude:
- logs
- CamelizeTable
```

### Comments
Expand Down
50 changes: 50 additions & 0 deletions config/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Lint struct {
RequireColumnComment RequireColumnComment `yaml:"requireColumnComment"`
UnrelatedTable UnrelatedTable `yaml:"unrelatedTable"`
ColumnCount ColumnCount `yaml:"columnCount"`
RequireColumns RequireColumns `yaml:"requireColumns"`
}

// RuleWarn is struct of Rule error
Expand Down Expand Up @@ -154,3 +155,52 @@ func contains(s []string, e string) bool {
}
return false
}

// RequireColumns check required table columns
type RequireColumns struct {
Enabled bool `yaml:"enabled"`
Columns []RequireColumnsColumn `yaml:"columns"`
}

// RequireColumnsColumn is required column
type RequireColumnsColumn struct {
Name string `yaml:"name"`
Exclude []string `yaml:"exclude"`
}

// IsEnabled return Rule is enabled or not
func (r RequireColumns) IsEnabled() bool {
return r.Enabled
}

// Check the existence of a table columns
func (r *RequireColumns) Check(s *schema.Schema) []RuleWarn {
warns := []RuleWarn{}
for _, t := range s.Tables {
for _, cc := range r.Columns {
excluded := false
for _, tt := range cc.Exclude {
if t.Name == tt {
excluded = true
}
}
if excluded {
continue
}
exists := false
msgFmt := "column '%s' required."
for _, c := range t.Columns {
if c.Name == cc.Name {
exists = true
}
}
if !exists {
warns = append(warns, RuleWarn{
Target: t.Name,
Message: fmt.Sprintf(msgFmt, cc.Name),
})
}
}
}
return warns
}
54 changes: 50 additions & 4 deletions config/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ func TestColumnCount(t *testing.T) {
}
s := newTestSchema()
warns := r.Check(s)
if len(warns) != 1 {
t.Errorf("actual %v\nwant %v", len(warns), 1)
want := 1
if len(warns) != want {
t.Errorf("actual %v\nwant %v", len(warns), want)
}
}

Expand All @@ -108,8 +109,53 @@ func TestColumnCountWithExclude(t *testing.T) {
}
s := newTestSchema()
warns := r.Check(s)
if len(warns) != 0 {
t.Errorf("actual %v\nwant %v", len(warns), 1)
want := 0
if len(warns) != want {
t.Errorf("actual %v\nwant %v", len(warns), want)
}
}

func TestRequireColumns(t *testing.T) {
r := RequireColumns{
Enabled: true,
Columns: []RequireColumnsColumn{
RequireColumnsColumn{
Name: "a2",
Exclude: []string{},
},
RequireColumnsColumn{
Name: "b2",
Exclude: []string{},
},
},
}
s := newTestSchema()
warns := r.Check(s)
want := 4
if len(warns) != want {
t.Errorf("actual %v\nwant %v", len(warns), want)
}
}

func TestRequireColumnsWithExclude(t *testing.T) {
r := RequireColumns{
Enabled: true,
Columns: []RequireColumnsColumn{
RequireColumnsColumn{
Name: "a2",
Exclude: []string{"b", "c"},
},
RequireColumnsColumn{
Name: "b2",
Exclude: []string{"a", "c"},
},
},
}
s := newTestSchema()
warns := r.Check(s)
want := 0
if len(warns) != want {
t.Errorf("actual %v\nwant %v", len(warns), want)
}
}

Expand Down