Skip to content

Commit

Permalink
Combine queries, fetch current_schema alongside views
Browse files Browse the repository at this point in the history
  • Loading branch information
smudge committed Jan 17, 2024
1 parent 5a3a81c commit bb11555
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions lib/scenic/adapters/postgres/views.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def views_from_postgres
c.relname as viewname,
pg_get_viewdef(c.oid) AS definition,
c.relkind AS kind,
n.nspname AS namespace
n.nspname AS namespace,
current_schema() AS current_schema
FROM pg_class c
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE
Expand All @@ -41,7 +42,7 @@ def views_from_postgres
end

def to_scenic_view(result)
namespace, viewname = result.values_at "namespace", "viewname"
namespace, viewname, current_schema = result.values_at "namespace", "viewname", "current_schema"

if namespace != current_schema
namespaced_viewname = "#{pg_identifier(namespace)}.#{pg_identifier(viewname)}"
Expand All @@ -56,10 +57,6 @@ def to_scenic_view(result)
)
end

def current_schema
@current_schema ||= connection.execute("SELECT CURRENT_SCHEMA()").first["current_schema"]
end

def pg_identifier(name)
return name if name =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/

Expand Down

0 comments on commit bb11555

Please sign in to comment.