Skip to content

Commit

Permalink
Add CHECK constraints to internal schema
Browse files Browse the repository at this point in the history
Add knowledge of `CHECK` constraints to `pgroll`'s internal schema
representation.
  • Loading branch information
andrew-farries committed Jan 17, 2024
1 parent 7b5fafd commit 9d63479
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ type Table struct {

// ForeignKeys is a map of all foreign keys defined on the table
ForeignKeys map[string]ForeignKey `json:"foreignKeys"`

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

type Column struct {
Expand Down Expand Up @@ -85,6 +88,17 @@ type ForeignKey struct {
ReferencedColumns []string `json:"referencedColumns"`
}

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

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

// The definition of the check constraint
Definition string `json:"definition"`
}

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

0 comments on commit 9d63479

Please sign in to comment.