Skip to content

Commit

Permalink
Add schema prefix support (#31)
Browse files Browse the repository at this point in the history
* [30] add schema prefix support

* format

* remove tparse from github action

* revert go version update
  • Loading branch information
KarnerTh committed Mar 8, 2023
1 parent 26a6da4 commit b938250
Show file tree
Hide file tree
Showing 19 changed files with 389 additions and 103 deletions.
2 changes: 2 additions & 0 deletions .mermerd.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ outputFileName: "my-db.mmd"
debug: false
omitConstraintLabels: false
omitAttributeKeys: false
showSchemaPrefix: true
schemaPrefixSeparator: "_"

# These connection strings are available as suggestions in the cli (use tab to access)
connectionStringSuggestions:
Expand Down
9 changes: 9 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres
to [Semantic Versioning](https://semver.org/spec/v2.0.0.html) (after version 0.0.5).

## [0.6.0] - 2023-03-08
### Added
- Support schema prefix ([Issue #30](https://github.com/KarnerTh/mermerd/issues/30))

### Changed
- updated dependencies

## [0.5.0] - 2022-12-28
### Added
- Support enum description ([Issue #15](https://github.com/KarnerTh/mermerd/issues/15))
Expand Down Expand Up @@ -100,6 +107,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html) (after version 0.0
### Added
- Initial release of mermerd

[0.6.0]: https://github.com/KarnerTh/mermerd/releases/tag/v0.6.0

[0.5.0]: https://github.com/KarnerTh/mermerd/releases/tag/v0.5.0

[0.4.1]: https://github.com/KarnerTh/mermerd/releases/tag/v0.4.1
Expand Down
4 changes: 4 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@ func init() {
rootCmd.Flags().Bool(config.OmitConstraintLabelsKey, false, "omit the constraint labels")
rootCmd.Flags().Bool(config.OmitAttributeKeysKey, false, "omit the attribute keys (PK, FK)")
rootCmd.Flags().Bool(config.ShowEnumValuesKey, false, "show enum values in description column")
rootCmd.Flags().Bool(config.ShowSchemaPrefix, false, "show schema prefix in table name")
rootCmd.Flags().BoolP(config.EncloseWithMermaidBackticksKey, "e", false, "enclose output with mermaid backticks (needed for e.g. in markdown viewer)")
rootCmd.Flags().StringP(config.ConnectionStringKey, "c", "", "connection string that should be used")
rootCmd.Flags().StringP(config.SchemaKey, "s", "", "schema that should be used")
rootCmd.Flags().StringP(config.OutputFileNameKey, "o", "result.mmd", "output file name")
rootCmd.Flags().String(config.SchemaPrefixSeparator, ".", "the separator that should be used between schema and table name")
rootCmd.Flags().StringSlice(config.SelectedTablesKey, []string{""}, "tables to include")

bindFlagToViper(config.ShowAllConstraintsKey)
Expand All @@ -88,6 +90,8 @@ func init() {
bindFlagToViper(config.OutputFileNameKey)
bindFlagToViper(config.SelectedTablesKey)
bindFlagToViper(config.ShowEnumValuesKey)
bindFlagToViper(config.ShowSchemaPrefix)
bindFlagToViper(config.SchemaPrefixSeparator)
}

func bindFlagToViper(key string) {
Expand Down
12 changes: 12 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const (
OmitAttributeKeysKey = "omitAttributeKeys"
ShowEnumValuesKey = "showEnumValues"
UseAllSchemasKey = "useAllSchemas"
ShowSchemaPrefix = "showSchemaPrefix"
SchemaPrefixSeparator = "schemaPrefixSeparator"
)

type config struct{}
Expand All @@ -34,6 +36,8 @@ type MermerdConfig interface {
OmitAttributeKeys() bool
ShowEnumValues() bool
UseAllSchemas() bool
ShowSchemaPrefix() bool
SchemaPrefixSeparator() string
}

func NewConfig() MermerdConfig {
Expand Down Expand Up @@ -91,3 +95,11 @@ func (c config) ShowEnumValues() bool {
func (c config) UseAllSchemas() bool {
return viper.GetBool(UseAllSchemasKey)
}

func (c config) ShowSchemaPrefix() bool {
return viper.GetBool(ShowSchemaPrefix)
}

func (c config) SchemaPrefixSeparator() string {
return viper.GetString(SchemaPrefixSeparator)
}
4 changes: 4 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ omitConstraintLabels: true
omitAttributeKeys: true
showEnumValues: true
useAllSchemas: true
showSchemaPrefix: true
schemaPrefixSeparator: "_"
# These connection strings are available as suggestions in the cli (use tab to access)
connectionStringSuggestions:
Expand All @@ -56,4 +58,6 @@ connectionStringSuggestions:
assert.True(t, config.OmitAttributeKeys())
assert.True(t, config.ShowEnumValues())
assert.True(t, config.UseAllSchemas())
assert.True(t, config.ShowSchemaPrefix())
assert.Equal(t, "_", config.SchemaPrefixSeparator())
}
2 changes: 1 addition & 1 deletion diagram/diagram.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (d diagram) Create(result *database.Result) error {
}

tableData[tableIndex] = ErdTableData{
Name: table.Table.Name,
Name: getTableName(d.config, table.Table),
Columns: columnData,
}
}
Expand Down
18 changes: 18 additions & 0 deletions diagram/diagram_util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package diagram

import (
"fmt"

"github.com/KarnerTh/mermerd/config"
"github.com/KarnerTh/mermerd/database"
)
Expand Down Expand Up @@ -76,3 +78,19 @@ func getConstraintData(config config.MermerdConfig, constraint database.Constrai
ConstraintLabel: constraintLabel,
}
}

func getTableName(config config.MermerdConfig, table database.TableDetail) string {
if !config.ShowSchemaPrefix() {
return table.Name
}

separator := config.SchemaPrefixSeparator()
name := fmt.Sprintf("%s%s%s", table.Schema, separator, table.Name)

// if fullstop is used the table name needs to be escaped with quote marks
if separator == "." {
return fmt.Sprintf("\"%s\"", name)
}

return name
}
47 changes: 47 additions & 0 deletions diagram/diagram_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,50 @@ func TestGetConstraintData(t *testing.T) {
assert.Equal(t, result.ConstraintLabel, "")
})
}

func TestGetTableName(t *testing.T) {
t.Run("Do not show schema prefix if config not active", func(t *testing.T) {
// Arrange
configMock := mocks.MermerdConfig{}
configMock.On("ShowSchemaPrefix").Return(false).Once()
tableDetail := database.TableDetail{Schema: "SchemaName", Name: "TableName"}

// Act
result := getTableName(&configMock, tableDetail)

// Assert
configMock.AssertExpectations(t)
assert.Equal(t, "TableName", result)
})

t.Run("Show schema prefix if config is active", func(t *testing.T) {
// Arrange
configMock := mocks.MermerdConfig{}
configMock.On("ShowSchemaPrefix").Return(true).Once()
configMock.On("SchemaPrefixSeparator").Return("_").Once()
tableDetail := database.TableDetail{Schema: "SchemaName", Name: "TableName"}

// Act
result := getTableName(&configMock, tableDetail)

// Assert
configMock.AssertExpectations(t)
assert.Equal(t, "SchemaName_TableName", result)
})

t.Run("Show escaped schema prefix if config is active and separator is a full stop", func(t *testing.T) {
// Arrange
configMock := mocks.MermerdConfig{}
configMock.On("ShowSchemaPrefix").Return(true).Once()
configMock.On("SchemaPrefixSeparator").Return(".").Once()
tableDetail := database.TableDetail{Schema: "SchemaName", Name: "TableName"}

// Act
result := getTableName(&configMock, tableDetail)

// Assert
configMock.AssertExpectations(t)
assert.Equal(t, "\"SchemaName.TableName\"", result)
})

}
2 changes: 2 additions & 0 deletions exampleRunConfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ debug: false
omitConstraintLabels: false
omitAttributeKeys: false
showEnumValues: false
showSchemaPrefix: true
schemaPrefixSeparator: "_"
52 changes: 25 additions & 27 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,50 @@ module github.com/KarnerTh/mermerd
go 1.19

require (
github.com/AlecAivazis/survey/v2 v2.3.5
github.com/briandowns/spinner v1.19.0
github.com/denisenkom/go-mssqldb v0.12.2
github.com/fatih/color v1.13.0
github.com/go-sql-driver/mysql v1.6.0
github.com/jackc/pgx/v4 v4.17.1
github.com/AlecAivazis/survey/v2 v2.3.6
github.com/briandowns/spinner v1.23.0
github.com/denisenkom/go-mssqldb v0.12.3
github.com/fatih/color v1.14.1
github.com/go-sql-driver/mysql v1.7.0
github.com/jackc/pgx/v4 v4.18.1
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.5.0
github.com/spf13/viper v1.12.0
github.com/stretchr/testify v1.8.0
github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.13.0 // indirect
github.com/jackc/pgconn v1.14.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.12.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.2 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgtype v1.14.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.7 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.4.0 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 // indirect
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 // indirect
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 // indirect
golang.org/x/text v0.3.7 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit b938250

Please sign in to comment.