From 45f3c5024b9285c910c06e012d2f0699c0cfe470 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Thu, 9 May 2019 01:03:36 +0900 Subject: [PATCH] Add requireColumns lint rule --- README.md | 11 +++++++++ config/lint.go | 50 +++++++++++++++++++++++++++++++++++++++++ config/lint_test.go | 54 +++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 111 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 25ef9fd31..9c2540b49 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/config/lint.go b/config/lint.go index abbe1b814..d7d348ca8 100644 --- a/config/lint.go +++ b/config/lint.go @@ -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 @@ -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 +} diff --git a/config/lint_test.go b/config/lint_test.go index ab01a3202..94cc54b79 100644 --- a/config/lint_test.go +++ b/config/lint_test.go @@ -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) } } @@ -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) } }