Skip to content

Commit

Permalink
Add ignoreTables flag (#59)
Browse files Browse the repository at this point in the history
* Add ignore tables flag to config

* Regenerate mocks

* Add ignore table functionality

* Update docs

* filter tables always, regardless of useAllTables flag; cleanup

---------

Co-authored-by: Thomas Karner <[email protected]>
  • Loading branch information
jamieaitken and KarnerTh authored Jun 16, 2024
1 parent 3679672 commit cbb2a7d
Show file tree
Hide file tree
Showing 14 changed files with 198 additions and 43 deletions.
33 changes: 33 additions & 0 deletions analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package analyzer
import (
"errors"
"fmt"
"regexp"
"sort"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -143,6 +144,13 @@ func (a analyzer) GetTables(db database.Connector, selectedSchemas []string) ([]

logrus.WithField("count", len(tables)).Info("Got tables")

if len(a.config.IgnoreTables()) > 0 {
tables, err = a.removeIgnoredTables(tables)
if err != nil {
return []database.TableDetail{}, err
}
}

if a.config.UseAllTables() {
return tables, nil
}
Expand All @@ -164,6 +172,31 @@ func (a analyzer) GetTables(db database.Connector, selectedSchemas []string) ([]
}), nil
}

func (a analyzer) removeIgnoredTables(tables []database.TableDetail) ([]database.TableDetail, error) {
var tablesWithoutIgnored []database.TableDetail

for _, table := range tables {
ignoreTable := false
for _, ignore := range a.config.IgnoreTables() {
match, err := regexp.MatchString(ignore, table.Name)
if err != nil {
return nil, err
}

if match {
ignoreTable = true
break
}
}

if !ignoreTable {
tablesWithoutIgnored = append(tablesWithoutIgnored, table)
}
}

return tablesWithoutIgnored, nil
}

func (a analyzer) GetColumnsAndConstraints(db database.Connector, selectedTables []database.TableDetail) ([]database.TableResult, error) {
var tableResults []database.TableResult
a.loadingSpinner.Start("Getting columns and constraints")
Expand Down
43 changes: 42 additions & 1 deletion analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ func TestAnalyzer_GetTables(t *testing.T) {
configMock.On("SelectedTables").Return([]string{}).Once()
connectorMock.On("GetTables", []string{"validSchema"}).Return([]database.TableDetail{{Schema: "validSchema", Name: "tableA"}, {Schema: "validSchema", Name: "tableB"}}, nil).Once()
configMock.On("UseAllTables").Return(true).Once()
configMock.On("IgnoreTables").Return([]string{}).Once()

// Act
result, err := analyzer.GetTables(&connectorMock, []string{"validSchema"})
Expand All @@ -178,13 +179,53 @@ func TestAnalyzer_GetTables(t *testing.T) {
assert.Equal(t, "tableB", result[1].Name)
})

t.Run("Use all available tables whilst ignoring some", func(t *testing.T) {
// Arrange
analyzer, configMock, _, _ := getAnalyzerWithMocks()
connectorMock := mocks.Connector{}
configMock.On("SelectedTables").Return([]string{}).Once()
connectorMock.On("GetTables", []string{"validSchema"}).Return([]database.TableDetail{
{
Schema: "validSchema",
Name: "tableA",
},
{
Schema: "validSchema",
Name: "tableB",
},
{
Schema: "validSchema",
Name: "tableA_2024_06",
},
{
Schema: "validSchema",
Name: "tableC",
},
}, nil).Once()
configMock.On("UseAllTables").Return(true)
configMock.On("IgnoreTables").Return([]string{"_20\\d{2}_0[1-9]|1[0-2]", "tableB"})

// Act
result, err := analyzer.GetTables(&connectorMock, []string{"validSchema"})

// Assert
configMock.AssertExpectations(t)
connectorMock.AssertExpectations(t)
assert.Nil(t, err)
assert.Len(t, result, 2)
assert.Equal(t, "tableA", result[0].Name)
assert.Equal(t, "tableC", result[1].Name)

})

t.Run("Use value from questioner", func(t *testing.T) {
// Arrange
analyzer, configMock, _, questionerMock := getAnalyzerWithMocks()
connectorMock := mocks.Connector{}
configMock.On("SelectedTables").Return([]string{}).Once()
connectorMock.On("GetTables", []string{"validSchema"}).Return([]database.TableDetail{{Schema: "validSchema", Name: "tableA"}, {Schema: "validSchema", Name: "tableB"}}, nil).Once()
configMock.On("UseAllTables").Return(false).Once()
configMock.On("UseAllTables").Return(false)
configMock.On("IgnoreTables").Return([]string{})
questionerMock.On("AskTableQuestion", []string{"validSchema.tableA", "validSchema.tableB"}).Return([]string{"validSchema.tableA"}, nil).Once()

// Act
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func init() {
rootCmd.Flags().StringVar(&runConfig, "runConfig", "", "run configuration (replaces global configuration)")
rootCmd.Flags().Bool(config.ShowAllConstraintsKey, false, "show all constraints, even though the table of the resulting constraint was not selected")
rootCmd.Flags().Bool(config.UseAllTablesKey, false, "use all available tables")
rootCmd.Flags().StringSlice(config.IgnoreTables, []string{""}, "ignore the given tables (supports regex)")
rootCmd.Flags().Bool(config.UseAllSchemasKey, false, "use all available schemas")
rootCmd.Flags().Bool(config.DebugKey, false, "show debug logs")
rootCmd.Flags().Bool(config.OmitConstraintLabelsKey, false, "omit the constraint labels")
Expand All @@ -80,6 +81,7 @@ func init() {

bindFlagToViper(config.ShowAllConstraintsKey)
bindFlagToViper(config.UseAllTablesKey)
bindFlagToViper(config.IgnoreTables)
bindFlagToViper(config.UseAllSchemasKey)
bindFlagToViper(config.DebugKey)
bindFlagToViper(config.OmitConstraintLabelsKey)
Expand Down
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "github.com/spf13/viper"
const (
ShowAllConstraintsKey = "showAllConstraints"
UseAllTablesKey = "useAllTables"
IgnoreTables = "ignoreTables"
SelectedTablesKey = "selectedTables"
SchemaKey = "schema"
ConnectionStringKey = "connectionString"
Expand All @@ -26,6 +27,7 @@ type config struct{}
type MermerdConfig interface {
ShowAllConstraints() bool
UseAllTables() bool
IgnoreTables() []string
Schemas() []string
ConnectionString() string
OutputFileName() string
Expand Down Expand Up @@ -54,6 +56,10 @@ func (c config) UseAllTables() bool {
return viper.GetBool(UseAllTablesKey)
}

func (c config) IgnoreTables() []string {
return viper.GetStringSlice(IgnoreTables)
}

func (c config) Schemas() []string {
return viper.GetStringSlice(SchemaKey)
}
Expand Down
4 changes: 4 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ relationshipLabels:
useAllSchemas: true
showSchemaPrefix: true
schemaPrefixSeparator: "_"
ignoreTables:
- city
- customer
# These connection strings are available as suggestions in the cli (use tab to access)
connectionStringSuggestions:
Expand Down Expand Up @@ -83,4 +86,5 @@ connectionStringSuggestions:
},
},
)
assert.ElementsMatch(t, []string{"city", "customer"}, config.IgnoreTables())
}
11 changes: 5 additions & 6 deletions mocks/Analyzer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions mocks/Connector.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions mocks/ConnectorFactory.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions mocks/Diagram.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions mocks/LoadingSpinner.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 21 additions & 6 deletions mocks/MermerdConfig.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions mocks/Questioner.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cbb2a7d

Please sign in to comment.