Skip to content

Business intelligence made simple

Added materialized views to table preview and schema page for Postgres - resolves #282, resolves #498, and resolves #513

Andrew Kane82addd4e0a22
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 364deca1..9804c5c4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 3.5.0 (unreleased)
+
+- Added materialized views to table preview and schema page for Postgres
+
## 3.4.0 (2026-04-20)
- Dropped support for Ruby < 3.3 and Rails < 7.2
diff --git a/lib/blazer/adapters/sql_adapter.rb b/lib/blazer/adapters/sql_adapter.rb
index e3fdd972..6ccfdd39 100644
--- a/lib/blazer/adapters/sql_adapter.rb
+++ b/lib/blazer/adapters/sql_adapter.rb
@@ -74,6 +74,8 @@ module Blazer
sql =
if sqlite?
"SELECT NULL, name FROM sqlite_master WHERE type IN ('table', 'view') ORDER BY name"
+ elsif postgresql?
+ add_schemas("SELECT * FROM (SELECT table_schema, table_name FROM information_schema.tables UNION ALL SELECT n.nspname AS table_schema, c.relname AS table_name FROM pg_class c INNER JOIN pg_namespace n ON c.relnamespace = n.oid WHERE c.relkind = 'm' AND has_table_privilege(c.oid, 'SELECT'))")
else
add_schemas("SELECT table_schema, table_name FROM information_schema.tables")
end
@@ -103,6 +105,8 @@ module Blazer
sql =
if sqlite?
"SELECT NULL, t.name, c.name, c.type, c.cid FROM sqlite_master t INNER JOIN pragma_table_info(t.name) c WHERE t.type IN ('table', 'view')"
+ elsif postgresql?
+ add_schemas("SELECT * FROM (SELECT table_schema, table_name, column_name, data_type, ordinal_position FROM information_schema.columns UNION ALL SELECT n.nspname AS table_schema, c.relname AS table_name, a.attname AS column_name, format_type(a.atttypid, a.atttypmod) AS data_type, a.attnum AS ordinal_position FROM pg_attribute a INNER JOIN pg_class c ON a.attrelid = c.oid INNER JOIN pg_namespace n ON c.relnamespace = n.oid WHERE a.attnum > 0 AND NOT a.attisdropped AND c.relkind = 'm' AND has_column_privilege(c.oid, a.attnum, 'SELECT'))")
else
add_schemas("SELECT table_schema, table_name, column_name, data_type, ordinal_position FROM information_schema.columns")
end
diff --git a/test/adapters/postgresql_test.rb b/test/adapters/postgresql_test.rb
index df2150a3..010a61fd 100644
--- a/test/adapters/postgresql_test.rb
+++ b/test/adapters/postgresql_test.rb
@@ -27,8 +27,7 @@ class PostgresqlTest < ActionDispatch::IntegrationTest
tables = ds.tables.map { |v| v[:table] }
assert_includes tables, "users"
assert_includes tables, "users_view"
- # TODO add
- refute_includes tables, "users_matview"
+ assert_includes tables, "users_matview"
end
def test_schema_method
@@ -40,8 +39,7 @@ class PostgresqlTest < ActionDispatch::IntegrationTest
]
assert_equal expected, columns["users"]
assert_equal expected, columns["users_view"]
- # TODO add
- assert_nil columns["users_matview"]
+ assert_equal expected, columns["users_matview"]
end
def test_run