Skip to content

Commit 02a6f59

Browse files
committed
fix wrong table name in constraints if schema prefix is used
1 parent b938250 commit 02a6f59

File tree

8 files changed

+40
-3
lines changed

8 files changed

+40
-3
lines changed

changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres
55
to [Semantic Versioning](https://semver.org/spec/v2.0.0.html) (after version 0.0.5).
66

7+
## [0.6.1] - 2023-03-09
8+
### Fixed
9+
- Fixed wrong table name in constraints if schema prefix was used
10+
711
## [0.6.0] - 2023-03-08
812
### Added
913
- Support schema prefix ([Issue #30](https://github.com/KarnerTh/mermerd/issues/30))
@@ -107,6 +111,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html) (after version 0.0
107111
### Added
108112
- Initial release of mermerd
109113

114+
[0.6.1]: https://github.com/KarnerTh/mermerd/releases/tag/v0.6.1
115+
110116
[0.6.0]: https://github.com/KarnerTh/mermerd/releases/tag/v0.6.0
111117

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

database/database_integration_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ func TestDatabaseIntegrations(t *testing.T) {
286286
assert.ElementsMatch(t, expectedResult, tables)
287287
})
288288

289-
t.Run("GetCrossSchemaConstraints", func(t *testing.T) {
289+
t.Run("Get Cross-Schema-Constraints", func(t *testing.T) {
290290
// Arrange
291291
tableName := TableDetail{Schema: "other_db", Name: "test_3_b"}
292292

@@ -302,6 +302,22 @@ func TestDatabaseIntegrations(t *testing.T) {
302302
assert.Equal(t, constraintResults[0].FkTable, "test_3_b")
303303
assert.Equal(t, constraintResults[0].PkTable, "test_3_a")
304304
})
305+
306+
t.Run("Get schema from FK and PK table", func(t *testing.T) {
307+
// Arrange
308+
tableName := TableDetail{Schema: "other_db", Name: "test_3_b"}
309+
310+
// Act
311+
constraintResults, err := connector.GetConstraints(tableName)
312+
313+
// Assert
314+
assert.Nil(t, err)
315+
assert.Len(t, constraintResults, 1)
316+
assert.Equal(t, constraintResults[0].FkTable, "test_3_b")
317+
assert.Equal(t, constraintResults[0].FkSchema, "other_db")
318+
assert.Equal(t, constraintResults[0].PkTable, "test_3_a")
319+
assert.Equal(t, constraintResults[0].PkSchema, testCase.schema)
320+
})
305321
})
306322
})
307323
}

database/mssql.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ func (c *mssqlConnector) GetColumns(tableName TableDetail) ([]ColumnResult, erro
129129
func (c *mssqlConnector) GetConstraints(tableName TableDetail) ([]ConstraintResult, error) {
130130
rows, err := c.db.Query(`
131131
select fk.table_name,
132+
fk.table_schema,
132133
pk.table_name,
134+
pk.table_schema,
133135
c.constraint_name,
134136
kcu.column_name,
135137
coalesce(
@@ -164,7 +166,9 @@ where c.CONSTRAINT_SCHEMA = @p1 and (fk.table_name = @p2 or pk.table_name = @p2)
164166
var constraint ConstraintResult
165167
err = rows.Scan(
166168
&constraint.FkTable,
169+
&constraint.FkSchema,
167170
&constraint.PkTable,
171+
&constraint.PkSchema,
168172
&constraint.ConstraintName,
169173
&constraint.ColumnName,
170174
&constraint.IsPrimary,

database/mysql.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ func (c *mySqlConnector) GetColumns(tableName TableDetail) ([]ColumnResult, erro
127127
func (c *mySqlConnector) GetConstraints(tableName TableDetail) ([]ConstraintResult, error) {
128128
rows, err := c.db.Query(`
129129
select c.TABLE_NAME,
130+
kcu.TABLE_SCHEMA,
130131
c.REFERENCED_TABLE_NAME,
132+
kcu.REFERENCED_TABLE_SCHEMA,
131133
c.CONSTRAINT_NAME,
132134
kcu.COLUMN_NAME,
133135
(
@@ -157,7 +159,9 @@ func (c *mySqlConnector) GetConstraints(tableName TableDetail) ([]ConstraintResu
157159
var constraint ConstraintResult
158160
err = rows.Scan(
159161
&constraint.FkTable,
162+
&constraint.FkSchema,
160163
&constraint.PkTable,
164+
&constraint.PkSchema,
161165
&constraint.ConstraintName,
162166
&constraint.ColumnName,
163167
&constraint.IsPrimary,

database/postgres.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ func (c *postgresConnector) GetColumns(tableName TableDetail) ([]ColumnResult, e
136136
func (c *postgresConnector) GetConstraints(tableName TableDetail) ([]ConstraintResult, error) {
137137
rows, err := c.db.Query(`
138138
select fk.table_name,
139+
fk.table_schema,
139140
pk.table_name,
141+
pk.table_schema,
140142
c.constraint_name,
141143
kcu.column_name,
142144
coalesce(
@@ -172,7 +174,9 @@ func (c *postgresConnector) GetConstraints(tableName TableDetail) ([]ConstraintR
172174
var constraint ConstraintResult
173175
err = rows.Scan(
174176
&constraint.FkTable,
177+
&constraint.FkSchema,
175178
&constraint.PkTable,
179+
&constraint.PkSchema,
176180
&constraint.ConstraintName,
177181
&constraint.ColumnName,
178182
&constraint.IsPrimary,

database/result.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ type ColumnResult struct {
2626
type ConstraintResultList []ConstraintResult
2727
type ConstraintResult struct {
2828
FkTable string
29+
FkSchema string
2930
PkTable string
31+
PkSchema string
3032
ConstraintName string
3133
ColumnName string
3234
IsPrimary bool

diagram/diagram_util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ func getConstraintData(config config.MermerdConfig, constraint database.Constrai
7272
}
7373

7474
return ErdConstraintData{
75-
PkTableName: constraint.PkTable,
76-
FkTableName: constraint.FkTable,
75+
PkTableName: getTableName(config, database.TableDetail{Schema: constraint.PkSchema, Name: constraint.PkTable}),
76+
FkTableName: getTableName(config, database.TableDetail{Schema: constraint.FkSchema, Name: constraint.FkTable}),
7777
Relation: getRelation(constraint),
7878
ConstraintLabel: constraintLabel,
7979
}

diagram/diagram_util_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ func TestGetConstraintData(t *testing.T) {
252252
// Arrange
253253
configMock := mocks.MermerdConfig{}
254254
configMock.On("OmitConstraintLabels").Return(true).Once()
255+
configMock.On("ShowSchemaPrefix").Return(false).Twice()
255256
constraint := database.ConstraintResult{ColumnName: "Column1"}
256257

257258
// Act

0 commit comments

Comments
 (0)