Skip to content

Commit

Permalink
allow not unique constraint name for postgres (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
KarnerTh authored May 30, 2023
1 parent dc118b6 commit 8a5916a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
17 changes: 17 additions & 0 deletions database/database_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ func TestDatabaseIntegrations(t *testing.T) {
{Schema: schema, Name: "test_2_enum"},
{Schema: schema, Name: "test_3_a"},
}
if testCase.dbType == Postgres {
expectedResult = append(expectedResult, []TableDetail{
{Schema: schema, Name: "test_not_unique_constraint_name_a"},
{Schema: schema, Name: "test_not_unique_constraint_name_b"},
{Schema: schema, Name: "test_not_unique_constraint_name_c"},
}...)
}

assert.Nil(t, err)
assert.ElementsMatch(t, expectedResult, tables)
})
Expand Down Expand Up @@ -282,6 +290,15 @@ func TestDatabaseIntegrations(t *testing.T) {
{Schema: secondSchema, Name: "test_3_b"},
{Schema: secondSchema, Name: "test_3_c"},
}

if testCase.dbType == Postgres {
expectedResult = append(expectedResult, []TableDetail{
{Schema: testCase.schema, Name: "test_not_unique_constraint_name_a"},
{Schema: testCase.schema, Name: "test_not_unique_constraint_name_b"},
{Schema: testCase.schema, Name: "test_not_unique_constraint_name_c"},
}...)
}

assert.Nil(t, err)
assert.ElementsMatch(t, expectedResult, tables)
})
Expand Down
3 changes: 2 additions & 1 deletion database/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ func (c *postgresConnector) GetConstraints(tableName TableDetail) ([]ConstraintR
on kc2.constraint_name = tc.constraint_name and
tc.constraint_type = 'PRIMARY KEY'
where kc.constraint_name = c.constraint_name
and kc.column_name = kcu.column_name)
and kc.column_name = kcu.column_name
and kc.table_name = fk.table_name)
, false) "isPrimary",
(select COUNT(*) > 1 "hasMultiplePk"
from information_schema.table_constraints tc
Expand Down
21 changes: 21 additions & 0 deletions database/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,24 @@ func TestPostgresEnums(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, "apple,banana", enumValues)
}

func TestPostgresNotUniqueConstraintName(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}

// Arrange
tableName := TableDetail{Schema: "public", Name: "test_not_unique_constraint_name_b"}
connector, _ := NewConnectorFactory().NewConnector(testConnectionPostgres.connectionString)
if err := connector.Connect(); err != nil {
logrus.Error(err)
t.FailNow()
}

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

// Assert
// we only need to check if an error is thrown
assert.Nil(t, err)
}
1 change: 1 addition & 0 deletions test/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ services:
- ./db-table-setup.sql:/docker-entrypoint-initdb.d/1.sql
- ./postgres/postgres-enum-setup.sql:/docker-entrypoint-initdb.d/2.sql
- ./postgres/postgres-multiple-databases.sql:/docker-entrypoint-initdb.d/3.sql
- ./postgres/postgres-not-unique-constraint-name.sql:/docker-entrypoint-initdb.d/4.sql
mermerd-mysql-test-db:
image: mysql:8.0
command: --default-authentication-plugin=mysql_native_password
Expand Down
18 changes: 18 additions & 0 deletions test/postgres/postgres-not-unique-constraint-name.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- Test case for https://github.com/KarnerTh/mermerd/issues/36
create table test_not_unique_constraint_name_a
(
id int primary key
);
create table test_not_unique_constraint_name_b
(
id int primary key
);
create table test_not_unique_constraint_name_c
(
id int primary key
);

alter table test_not_unique_constraint_name_b
add constraint not_unique_constraint_name foreign key (id) references test_not_unique_constraint_name_a (id);
alter table test_not_unique_constraint_name_c
add constraint not_unique_constraint_name foreign key (id) references test_not_unique_constraint_name_a (id);

0 comments on commit 8a5916a

Please sign in to comment.