Skip to content

Commit

Permalink
Revert "Add support to list views (#358)" (#360)
Browse files Browse the repository at this point in the history
The state query changes from #358 are causing performance issues in an
upstream integration of `pgroll`.

Revert the PR until the cause of the regression is found and fixed.

This reverts commit f994a42.
  • Loading branch information
andrew-farries authored Jun 20, 2024
1 parent f994a42 commit 8234b9e
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 87 deletions.
19 changes: 0 additions & 19 deletions pkg/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ type Schema struct {
Name string `json:"name"`
// Tables is a map of virtual table name -> table mapping
Tables map[string]Table `json:"tables"`
// Views is a map of virtual view name -> view mapping
Views map[string]View `json:"views"`
}

type Table struct {
Expand Down Expand Up @@ -59,23 +57,6 @@ type Table struct {
UniqueConstraints map[string]UniqueConstraint `json:"uniqueConstraints"`
}

type View struct {
// OID for the view
OID string `json:"oid"`

// Name is the actual name in postgres
Name string `json:"name"`

// Optional comment for the view
Comment string `json:"comment"`

// Definition is the SQL definition of the view
Definition string `json:"definition"`

// Columns is a map of virtual column name -> column mapping
Columns map[string]Column `json:"columns"`
}

type Column struct {
// Name is the actual name in postgres
Name string `json:"name"`
Expand Down
38 changes: 3 additions & 35 deletions pkg/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ STABLE;
CREATE OR REPLACE FUNCTION %[1]s.read_schema(schemaname text) RETURNS jsonb
LANGUAGE plpgsql AS $$
DECLARE
result jsonb;
tables jsonb;
BEGIN
SELECT json_build_object(
'name', schemaname,
Expand Down Expand Up @@ -255,43 +255,11 @@ BEGIN
WHERE
ns.nspname = schemaname
AND t.relkind IN ('r', 'p') -- tables only (ignores views, materialized views & foreign tables)
),
'views', (
SELECT COALESCE(json_object_agg(v.relname, jsonb_build_object(
'name', v.relname,
'oid', v.oid,
'comment', descr.description,
'definition', pg_get_viewdef(v.oid, true),
'columns', (
SELECT COALESCE(json_object_agg(name, c), '{}'::json) FROM (
SELECT
attr.attname AS name,
format_type(attr.atttypid, attr.atttypmod) AS type,
descr.description AS comment
FROM
pg_attribute AS attr
LEFT JOIN pg_description AS descr ON attr.attrelid = descr.objoid
AND attr.attnum = descr.objsubid
WHERE
attr.attnum > 0
AND NOT attr.attisdropped
AND attr.attrelid = v.oid
ORDER BY
attr.attnum
) c
)
)), '{}'::json) FROM pg_class AS v
INNER JOIN pg_namespace AS ns ON v.relnamespace = ns.oid
LEFT JOIN pg_description AS descr ON v.oid = descr.objoid
AND descr.objsubid = 0
WHERE
ns.nspname = schemaname
AND v.relkind = 'v' -- views only
)
)
INTO result;
INTO tables;
RETURN result;
RETURN tables;
END;
$$;
Expand Down
34 changes: 1 addition & 33 deletions pkg/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,6 @@ func TestReadSchema(t *testing.T) {
wantSchema: &schema.Schema{
Name: "public",
Tables: map[string]schema.Table{},
Views: map[string]schema.View{},
},
},
{
Expand All @@ -330,27 +329,6 @@ func TestReadSchema(t *testing.T) {
ForeignKeys: map[string]schema.ForeignKey{},
},
},
Views: map[string]schema.View{},
},
},
{
name: "one view without columns",
createStmt: "CREATE VIEW public.view1 AS SELECT 1 AS foo",
wantSchema: &schema.Schema{
Name: "public",
Tables: map[string]schema.Table{},
Views: map[string]schema.View{
"view1": {
Name: "view1",
Definition: " SELECT 1 AS foo;",
Columns: map[string]schema.Column{
"foo": {
Name: "foo",
Type: "integer",
},
},
},
},
},
},
{
Expand All @@ -375,7 +353,6 @@ func TestReadSchema(t *testing.T) {
ForeignKeys: map[string]schema.ForeignKey{},
},
},
Views: map[string]schema.View{},
},
},
{
Expand Down Expand Up @@ -412,7 +389,6 @@ func TestReadSchema(t *testing.T) {
ForeignKeys: map[string]schema.ForeignKey{},
},
},
Views: map[string]schema.View{},
},
},
{
Expand Down Expand Up @@ -448,7 +424,6 @@ func TestReadSchema(t *testing.T) {
ForeignKeys: map[string]schema.ForeignKey{},
},
},
Views: map[string]schema.View{},
},
},
{
Expand Down Expand Up @@ -503,7 +478,6 @@ func TestReadSchema(t *testing.T) {
UniqueConstraints: map[string]schema.UniqueConstraint{},
},
},
Views: map[string]schema.View{},
},
},
{
Expand Down Expand Up @@ -558,7 +532,6 @@ func TestReadSchema(t *testing.T) {
UniqueConstraints: map[string]schema.UniqueConstraint{},
},
},
Views: map[string]schema.View{},
},
},
{
Expand Down Expand Up @@ -601,7 +574,6 @@ func TestReadSchema(t *testing.T) {
UniqueConstraints: map[string]schema.UniqueConstraint{},
},
},
Views: map[string]schema.View{},
},
},
{
Expand Down Expand Up @@ -649,7 +621,6 @@ func TestReadSchema(t *testing.T) {
},
},
},
Views: map[string]schema.View{},
},
},
{
Expand Down Expand Up @@ -697,7 +668,6 @@ func TestReadSchema(t *testing.T) {
},
},
},
Views: map[string]schema.View{},
},
},
{
Expand Down Expand Up @@ -733,7 +703,6 @@ func TestReadSchema(t *testing.T) {
UniqueConstraints: map[string]schema.UniqueConstraint{},
},
},
Views: map[string]schema.View{},
},
},
{
Expand All @@ -758,7 +727,6 @@ func TestReadSchema(t *testing.T) {
UniqueConstraints: map[string]schema.UniqueConstraint{},
},
},
Views: map[string]schema.View{},
},
},
}
Expand All @@ -777,7 +745,7 @@ func TestReadSchema(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(tt.wantSchema, gotSchema, cmpopts.IgnoreFields(schema.Table{}, "OID"), cmpopts.IgnoreFields(schema.View{}, "OID")); diff != "" {
if diff := cmp.Diff(tt.wantSchema, gotSchema, cmpopts.IgnoreFields(schema.Table{}, "OID")); diff != "" {
t.Errorf("expected schema mismatch (-want +got):\n%s", diff)
}
})
Expand Down

0 comments on commit 8234b9e

Please sign in to comment.