diff --git a/.gitignore b/.gitignore index 628c0f0..35025ca 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ result.mmd mermerd coverage.html mermerd_test.db +go.work \ No newline at end of file diff --git a/Makefile b/Makefile index 907e1d4..f4e55c2 100644 --- a/Makefile +++ b/Makefile @@ -15,19 +15,24 @@ test-coverage: create-mocks: mockery --all -# https://github.com/mfridman/tparse is needed .PHONY: test-all -test-all: +test-all: test-setup go test $(test_target) -cover -json | tparse -all -# https://github.com/mfridman/tparse is needed .PHONY: test-unit -test-unit: +test-unit: test-setup go test --short $(test_target) -cover -json | tparse -all +.PHONY: test-setup +test-setup: + go install github.com/mfridman/tparse@v0.18.0 + cd test && docker-compose up -d + .PHONY: test-cleanup test-cleanup: go clean -testcache + cd test && docker-compose stop && docker-compose rm -f + rm mermerd_test.db 2&> /dev/null || true .PHONY: publish-package publish-package: diff --git a/cmd/root.go b/cmd/root.go index 897446f..9033d12 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -93,6 +93,7 @@ func init() { rootCmd.Flags().Bool(config.OmitConstraintLabelsKey, false, "omit the constraint labels") rootCmd.Flags().Bool(config.OmitAttributeKeysKey, false, "omit the attribute keys (PK, FK, UK)") rootCmd.Flags().Bool(config.ShowSchemaPrefix, false, "show schema prefix in table name") + rootCmd.Flags().Bool(config.ShowNameBeforeType, false, "show name before type for each table") 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") @@ -117,6 +118,7 @@ func init() { bindFlagToViper(config.ShowAllConstraintsKey) bindFlagToViper(config.ShowDescriptionsKey) bindFlagToViper(config.ShowSchemaPrefix) + bindFlagToViper(config.ShowNameBeforeType) bindFlagToViper(config.UseAllSchemasKey) bindFlagToViper(config.UseAllTablesKey) } diff --git a/config/config.go b/config/config.go index 1b6bfe0..7db3c64 100644 --- a/config/config.go +++ b/config/config.go @@ -19,6 +19,7 @@ const ( ShowAllConstraintsKey = "showAllConstraints" ShowDescriptionsKey = "showDescriptions" ShowSchemaPrefix = "showSchemaPrefix" + ShowNameBeforeType = "showNameBeforeType" UseAllSchemasKey = "useAllSchemas" UseAllTablesKey = "useAllTables" ) @@ -44,6 +45,7 @@ type MermerdConfig interface { ShowSchemaPrefix() bool UseAllSchemas() bool UseAllTables() bool + ShowNameBeforeType() bool } func NewConfig() MermerdConfig { @@ -122,3 +124,7 @@ func (c config) SchemaPrefixSeparator() string { func (c config) OutputMode() OutputModeType { return OutputModeType(viper.GetString(OutputMode)) } + +func (c config) ShowNameBeforeType() bool { + return viper.GetBool(ShowNameBeforeType) +} diff --git a/config/config_test.go b/config/config_test.go index c0fafff..a96a554 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -40,6 +40,7 @@ relationshipLabels: useAllSchemas: true showSchemaPrefix: true schemaPrefixSeparator: "_" +showNameBeforeType: true ignoreTables: - city - customer @@ -71,6 +72,7 @@ connectionStringSuggestions: assert.True(t, config.UseAllSchemas()) assert.True(t, config.ShowSchemaPrefix()) assert.Equal(t, "_", config.SchemaPrefixSeparator()) + assert.Equal(t, true, config.ShowNameBeforeType()) assert.ElementsMatch(t, config.RelationshipLabels(), []RelationshipLabel{ diff --git a/diagram/diagram.go b/diagram/diagram.go index f04f9bf..d8b7efe 100644 --- a/diagram/diagram.go +++ b/diagram/diagram.go @@ -62,6 +62,7 @@ func (d diagram) Create(wr io.Writer, result *database.Result) error { diagramData := ErdDiagramData{ EncloseWithMermaidBackticks: d.config.EncloseWithMermaidBackticks(), + ShowNameBeforeType: d.config.ShowNameBeforeType(), Tables: tableData, Constraints: constraints, } diff --git a/diagram/diagram_data.go b/diagram/diagram_data.go index 6fe72df..f463183 100644 --- a/diagram/diagram_data.go +++ b/diagram/diagram_data.go @@ -19,6 +19,7 @@ type ErdDiagramData struct { EncloseWithMermaidBackticks bool Tables []ErdTableData Constraints []ErdConstraintData + ShowNameBeforeType bool } type ErdTableData struct { diff --git a/diagram/erd_template.gommd b/diagram/erd_template.gommd index 0c5961b..3a3667b 100644 --- a/diagram/erd_template.gommd +++ b/diagram/erd_template.gommd @@ -3,7 +3,8 @@ erDiagram {{- range .Tables}} {{.Name}} { {{- range .Columns}} - {{.DataType}} {{.Name}} {{- if .AttributeKeys}} {{range $index, $attributeKey := .AttributeKeys}} + {{if $.ShowNameBeforeType}}{{.Name}} {{.DataType}}{{else}}{{.DataType}} {{.Name}} {{end -}} + {{- if .AttributeKeys}} {{range $index, $attributeKey := .AttributeKeys}} {{- if $index}},{{end -}} {{$attributeKey}} {{- end}}{{end}} {{if .Description -}} diff --git a/mocks/MermerdConfig.go b/mocks/MermerdConfig.go index 2445f72..dd9385f 100644 --- a/mocks/MermerdConfig.go +++ b/mocks/MermerdConfig.go @@ -276,6 +276,22 @@ func (_m *MermerdConfig) UseAllTables() bool { return r0 } + +// ShowNameBeforeType provides a mock function with given fields: +func (_m *MermerdConfig) ShowNameBeforeType() bool { + ret := _m.Called() + + var r0 bool + if rf, ok := ret.Get(0).(func() bool); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + + type mockConstructorTestingTNewMermerdConfig interface { mock.TestingT Cleanup(func()) diff --git a/test/docker-compose.yaml b/test/docker-compose.yaml index 96f4425..eeaf88a 100644 --- a/test/docker-compose.yaml +++ b/test/docker-compose.yaml @@ -1,4 +1,3 @@ -version: "3.3" services: mermerd-postgres-test-db: image: postgres:14-alpine @@ -29,6 +28,7 @@ services: - ./mysql/mysql-multiple-databases.sql:/docker-entrypoint-initdb.d/3.sql mermerd-mssql-test-db: image: mcr.microsoft.com/mssql/server:2019-latest + platform: linux/amd64 environment: ACCEPT_EULA: "Y" SA_PASSWORD: "securePassword1!"