Skip to content

Commit

Permalink
Add UNIQUE constraints to internal schema
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-farries committed Jan 18, 2024
1 parent 70a7c8e commit 5b7a2f3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pkg/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ type Table struct {

// CheckConstraints is a map of all check constraints defined on the table
CheckConstraints map[string]CheckConstraint `json:"checkConstraints"`

// UniqueConstraints is a map of all unique constraints defined on the table
UniqueConstraints map[string]UniqueConstraint `json:"uniqueConstraints"`
}

type Column struct {
Expand Down Expand Up @@ -99,6 +102,14 @@ type CheckConstraint struct {
Definition string `json:"definition"`
}

type UniqueConstraint struct {
// Name is the name of the unique constraint in postgres
Name string `json:"name"`

// The columns that the unique constraint is defined on
Columns []string `json:"columns"`
}

func (s *Schema) GetTable(name string) *Table {
if s.Tables == nil {
return nil
Expand Down
17 changes: 17 additions & 0 deletions pkg/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,23 @@ BEGIN
AND cc_constraint.contype = 'c'
GROUP BY cc_constraint.oid
) AS cc_details
),
'uniqueConstraints', (
SELECT json_object_agg(uc_details.conname, json_build_object(
'name', uc_details.conname,
'columns', uc_details.columns
))
FROM (
SELECT
uc_constraint.conname,
array_agg(uc_attr.attname ORDER BY uc_constraint.conkey::int[]) AS columns,
pg_get_constraintdef(uc_constraint.oid) AS definition
FROM pg_constraint AS uc_constraint
INNER JOIN pg_attribute uc_attr ON uc_attr.attrelid = uc_constraint.conrelid AND uc_attr.attnum = ANY(uc_constraint.conkey)
WHERE uc_constraint.conrelid = t.oid
AND uc_constraint.contype = 'u'
GROUP BY uc_constraint.oid
) AS uc_details
),
'foreignKeys', (
SELECT json_object_agg(fk_details.conname, json_build_object(
Expand Down

0 comments on commit 5b7a2f3

Please sign in to comment.