Skip to content

Commit

Permalink
fix wrong table name in constraints if schema prefix is used
Browse files Browse the repository at this point in the history
  • Loading branch information
KarnerTh committed Mar 9, 2023
1 parent b938250 commit 02a6f59
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 3 deletions.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ 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.1] - 2023-03-09
### Fixed
- Fixed wrong table name in constraints if schema prefix was used

## [0.6.0] - 2023-03-08
### Added
- Support schema prefix ([Issue #30](https://github.com/KarnerTh/mermerd/issues/30))
Expand Down Expand Up @@ -107,6 +111,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html) (after version 0.0
### Added
- Initial release of mermerd

[0.6.1]: https://github.com/KarnerTh/mermerd/releases/tag/v0.6.1

[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
Expand Down
18 changes: 17 additions & 1 deletion database/database_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func TestDatabaseIntegrations(t *testing.T) {
assert.ElementsMatch(t, expectedResult, tables)
})

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

Expand All @@ -302,6 +302,22 @@ func TestDatabaseIntegrations(t *testing.T) {
assert.Equal(t, constraintResults[0].FkTable, "test_3_b")
assert.Equal(t, constraintResults[0].PkTable, "test_3_a")
})

t.Run("Get schema from FK and PK table", func(t *testing.T) {
// Arrange
tableName := TableDetail{Schema: "other_db", Name: "test_3_b"}

// Act
constraintResults, err := connector.GetConstraints(tableName)

// Assert
assert.Nil(t, err)
assert.Len(t, constraintResults, 1)
assert.Equal(t, constraintResults[0].FkTable, "test_3_b")
assert.Equal(t, constraintResults[0].FkSchema, "other_db")
assert.Equal(t, constraintResults[0].PkTable, "test_3_a")
assert.Equal(t, constraintResults[0].PkSchema, testCase.schema)
})
})
})
}
Expand Down
4 changes: 4 additions & 0 deletions database/mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ func (c *mssqlConnector) GetColumns(tableName TableDetail) ([]ColumnResult, erro
func (c *mssqlConnector) GetConstraints(tableName TableDetail) ([]ConstraintResult, error) {
rows, err := c.db.Query(`
select fk.table_name,
fk.table_schema,
pk.table_name,
pk.table_schema,
c.constraint_name,
kcu.column_name,
coalesce(
Expand Down Expand Up @@ -164,7 +166,9 @@ where c.CONSTRAINT_SCHEMA = @p1 and (fk.table_name = @p2 or pk.table_name = @p2)
var constraint ConstraintResult
err = rows.Scan(
&constraint.FkTable,
&constraint.FkSchema,
&constraint.PkTable,
&constraint.PkSchema,
&constraint.ConstraintName,
&constraint.ColumnName,
&constraint.IsPrimary,
Expand Down
4 changes: 4 additions & 0 deletions database/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ func (c *mySqlConnector) GetColumns(tableName TableDetail) ([]ColumnResult, erro
func (c *mySqlConnector) GetConstraints(tableName TableDetail) ([]ConstraintResult, error) {
rows, err := c.db.Query(`
select c.TABLE_NAME,
kcu.TABLE_SCHEMA,
c.REFERENCED_TABLE_NAME,
kcu.REFERENCED_TABLE_SCHEMA,
c.CONSTRAINT_NAME,
kcu.COLUMN_NAME,
(
Expand Down Expand Up @@ -157,7 +159,9 @@ func (c *mySqlConnector) GetConstraints(tableName TableDetail) ([]ConstraintResu
var constraint ConstraintResult
err = rows.Scan(
&constraint.FkTable,
&constraint.FkSchema,
&constraint.PkTable,
&constraint.PkSchema,
&constraint.ConstraintName,
&constraint.ColumnName,
&constraint.IsPrimary,
Expand Down
4 changes: 4 additions & 0 deletions database/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ func (c *postgresConnector) GetColumns(tableName TableDetail) ([]ColumnResult, e
func (c *postgresConnector) GetConstraints(tableName TableDetail) ([]ConstraintResult, error) {
rows, err := c.db.Query(`
select fk.table_name,
fk.table_schema,
pk.table_name,
pk.table_schema,
c.constraint_name,
kcu.column_name,
coalesce(
Expand Down Expand Up @@ -172,7 +174,9 @@ func (c *postgresConnector) GetConstraints(tableName TableDetail) ([]ConstraintR
var constraint ConstraintResult
err = rows.Scan(
&constraint.FkTable,
&constraint.FkSchema,
&constraint.PkTable,
&constraint.PkSchema,
&constraint.ConstraintName,
&constraint.ColumnName,
&constraint.IsPrimary,
Expand Down
2 changes: 2 additions & 0 deletions database/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ type ColumnResult struct {
type ConstraintResultList []ConstraintResult
type ConstraintResult struct {
FkTable string
FkSchema string
PkTable string
PkSchema string
ConstraintName string
ColumnName string
IsPrimary bool
Expand Down
4 changes: 2 additions & 2 deletions diagram/diagram_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func getConstraintData(config config.MermerdConfig, constraint database.Constrai
}

return ErdConstraintData{
PkTableName: constraint.PkTable,
FkTableName: constraint.FkTable,
PkTableName: getTableName(config, database.TableDetail{Schema: constraint.PkSchema, Name: constraint.PkTable}),
FkTableName: getTableName(config, database.TableDetail{Schema: constraint.FkSchema, Name: constraint.FkTable}),
Relation: getRelation(constraint),
ConstraintLabel: constraintLabel,
}
Expand Down
1 change: 1 addition & 0 deletions diagram/diagram_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ func TestGetConstraintData(t *testing.T) {
// Arrange
configMock := mocks.MermerdConfig{}
configMock.On("OmitConstraintLabels").Return(true).Once()
configMock.On("ShowSchemaPrefix").Return(false).Twice()
constraint := database.ConstraintResult{ColumnName: "Column1"}

// Act
Expand Down

0 comments on commit 02a6f59

Please sign in to comment.