Skip to content

Commit

Permalink
Support multiple key constraints on a single attribute (#51) (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
lnschroeder authored Nov 1, 2023
1 parent d96ab90 commit c439660
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 27 deletions.
8 changes: 4 additions & 4 deletions diagram/diagram_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ type ErdTableData struct {
}

type ErdColumnData struct {
Name string
DataType string
Description string
AttributeKey ErdAttributeKey
Name string
DataType string
Description string
AttributeKeys []ErdAttributeKey
}

type ErdConstraintData struct {
Expand Down
21 changes: 11 additions & 10 deletions diagram/diagram_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,30 @@ func tableNameInSlice(slice []ErdTableData, tableName string) bool {
return false
}

func getAttributeKey(column database.ColumnResult) ErdAttributeKey {
func getAttributeKeys(column database.ColumnResult) []ErdAttributeKey {
var attributeKeys []ErdAttributeKey
if column.IsPrimary {
return primaryKey
attributeKeys = append(attributeKeys, primaryKey)
}

if column.IsForeign {
return foreignKey
attributeKeys = append(attributeKeys, foreignKey)
}

return none
return attributeKeys
}

func getColumnData(config config.MermerdConfig, column database.ColumnResult) ErdColumnData {
attributeKey := getAttributeKey(column)
attributeKeys := getAttributeKeys(column)
if config.OmitAttributeKeys() {
attributeKey = none
attributeKeys = []ErdAttributeKey(nil)
}

return ErdColumnData{
Name: column.Name,
DataType: column.DataType,
Description: getDescription(config.ShowDescriptions(), column),
AttributeKey: attributeKey,
Name: column.Name,
DataType: column.DataType,
Description: getDescription(config.ShowDescriptions(), column),
AttributeKeys: attributeKeys,
}
}

Expand Down
24 changes: 12 additions & 12 deletions diagram/diagram_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestGetRelation(t *testing.T) {
func TestGetAttributeKey(t *testing.T) {
testCases := []struct {
column database.ColumnResult
expectedAttributeResult ErdAttributeKey
expectedAttributeResult []ErdAttributeKey
}{
{
column: database.ColumnResult{
Expand All @@ -55,7 +55,7 @@ func TestGetAttributeKey(t *testing.T) {
IsPrimary: true,
IsForeign: false,
},
expectedAttributeResult: primaryKey,
expectedAttributeResult: []ErdAttributeKey{primaryKey},
},
{
column: database.ColumnResult{
Expand All @@ -64,7 +64,7 @@ func TestGetAttributeKey(t *testing.T) {
IsPrimary: false,
IsForeign: true,
},
expectedAttributeResult: foreignKey,
expectedAttributeResult: []ErdAttributeKey{foreignKey},
},
{
column: database.ColumnResult{
Expand All @@ -73,7 +73,7 @@ func TestGetAttributeKey(t *testing.T) {
IsPrimary: true,
IsForeign: true,
},
expectedAttributeResult: primaryKey,
expectedAttributeResult: []ErdAttributeKey{primaryKey, foreignKey},
},
{
column: database.ColumnResult{
Expand All @@ -82,7 +82,7 @@ func TestGetAttributeKey(t *testing.T) {
IsPrimary: false,
IsForeign: false,
},
expectedAttributeResult: none,
expectedAttributeResult: []ErdAttributeKey(nil),
},
}

Expand All @@ -92,7 +92,7 @@ func TestGetAttributeKey(t *testing.T) {
column := testCase.column

// Act
result := getAttributeKey(column)
result := getAttributeKeys(column)

// Assert
assert.Equal(t, testCase.expectedAttributeResult, result)
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestGetColumnData(t *testing.T) {
configMock.AssertExpectations(t)
assert.Equal(t, columnName, result.Name)
assert.Equal(t, "<"+enumValues+"> "+expectedComment, result.Description)
assert.Equal(t, primaryKey, result.AttributeKey)
assert.Equal(t, []ErdAttributeKey{primaryKey}, result.AttributeKeys)
})

t.Run("Get all fields with enum values", func(t *testing.T) {
Expand All @@ -168,7 +168,7 @@ func TestGetColumnData(t *testing.T) {
configMock.AssertExpectations(t)
assert.Equal(t, columnName, result.Name)
assert.Equal(t, "<"+enumValues+">", result.Description)
assert.Equal(t, primaryKey, result.AttributeKey)
assert.Equal(t, []ErdAttributeKey{primaryKey}, result.AttributeKeys)
})

t.Run("Get all fields with column comments", func(t *testing.T) {
Expand All @@ -184,7 +184,7 @@ func TestGetColumnData(t *testing.T) {
configMock.AssertExpectations(t)
assert.Equal(t, columnName, result.Name)
assert.Equal(t, expectedComment, result.Description)
assert.Equal(t, primaryKey, result.AttributeKey)
assert.Equal(t, []ErdAttributeKey{primaryKey}, result.AttributeKeys)
})

t.Run("Get all fields except description", func(t *testing.T) {
Expand All @@ -200,7 +200,7 @@ func TestGetColumnData(t *testing.T) {
configMock.AssertExpectations(t)
assert.Equal(t, columnName, result.Name)
assert.Equal(t, "", result.Description)
assert.Equal(t, primaryKey, result.AttributeKey)
assert.Equal(t, []ErdAttributeKey{primaryKey}, result.AttributeKeys)
})

t.Run("Get all fields except attribute key", func(t *testing.T) {
Expand All @@ -216,7 +216,7 @@ func TestGetColumnData(t *testing.T) {
configMock.AssertExpectations(t)
assert.Equal(t, columnName, result.Name)
assert.Equal(t, "<"+enumValues+"> "+expectedComment, result.Description)
assert.Equal(t, none, result.AttributeKey)
assert.Equal(t, []ErdAttributeKey(nil), result.AttributeKeys)
})

t.Run("Get only minimal fields", func(t *testing.T) {
Expand All @@ -232,7 +232,7 @@ func TestGetColumnData(t *testing.T) {
configMock.AssertExpectations(t)
assert.Equal(t, columnName, result.Name)
assert.Equal(t, "", result.Description)
assert.Equal(t, none, result.AttributeKey)
assert.Equal(t, []ErdAttributeKey(nil), result.AttributeKeys)
})
}

Expand Down
7 changes: 6 additions & 1 deletion diagram/erd_template.gommd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ erDiagram
{{- range .Tables}}
{{.Name}} {
{{- range .Columns}}
{{.DataType}} {{.Name}} {{- if .AttributeKey}} {{.AttributeKey}}{{end -}} {{- if .Description}} "{{.Description}}"{{end -}}
{{.DataType}} {{.Name}} {{- if .AttributeKeys}} {{range $index, $attributeKey := .AttributeKeys}}
{{- if $index}},{{end -}}
{{$attributeKey}}
{{- end}}{{end}} {{if .Description -}}
"{{.Description}}"
{{- end}}
{{- end}}
}
{{end -}}
Expand Down

0 comments on commit c439660

Please sign in to comment.