Skip to content

chore(schema_cache): add query for roles #404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/pgt_schema_cache/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
mod columns;
mod functions;
mod policies;
mod roles;
mod schema_cache;
mod schemas;
mod tables;
Expand All @@ -15,6 +16,7 @@ mod versions;
pub use columns::*;
pub use functions::{Behavior, Function, FunctionArg, FunctionArgs};
pub use policies::{Policy, PolicyCommand};
pub use roles::*;
pub use schema_cache::SchemaCache;
pub use schemas::Schema;
pub use tables::{ReplicaIdentity, Table};
Expand Down
7 changes: 7 additions & 0 deletions crates/pgt_schema_cache/src/queries/roles.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
select
rolname as "name!",
rolsuper as "is_super_user!",
rolcreatedb as "can_create_db!",
rolcanlogin as "can_login!",
rolbypassrls as "can_bypass_rls!"
from pg_catalog.pg_roles;
85 changes: 85 additions & 0 deletions crates/pgt_schema_cache/src/roles.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use crate::schema_cache::SchemaCacheItem;

#[derive(Debug, PartialEq, Eq)]
pub struct Role {
pub name: String,
pub is_super_user: bool,
pub can_create_db: bool,
pub can_login: bool,
pub can_bypass_rls: bool,
}

impl SchemaCacheItem for Role {
type Item = Role;

async fn load(pool: &sqlx::PgPool) -> Result<Vec<Self::Item>, sqlx::Error> {
sqlx::query_file_as!(Role, "src/queries/roles.sql")
.fetch_all(pool)
.await
}
}

#[cfg(test)]
mod tests {
use crate::SchemaCache;
use pgt_test_utils::test_database::get_new_test_db;
use sqlx::Executor;

#[tokio::test]
async fn loads_roles() {
let test_db = get_new_test_db().await;

let setup = r#"
do $$
begin
if not exists (
select from pg_catalog.pg_roles
where rolname = 'test_super'
) then
create role test_super superuser createdb login bypassrls;
end if;
if not exists (
select from pg_catalog.pg_roles
where rolname = 'test_nologin'
) then
create role test_nologin;
end if;
if not exists (
select from pg_catalog.pg_roles
where rolname = 'test_login'
) then
create role test_login login;
end if;
end $$;
"#;

test_db
.execute(setup)
.await
.expect("Failed to setup test database");

let cache = SchemaCache::load(&test_db)
.await
.expect("Failed to load Schema Cache");

let roles = &cache.roles;

let super_role = roles.iter().find(|r| r.name == "test_super").unwrap();
assert!(super_role.is_super_user);
assert!(super_role.can_create_db);
assert!(super_role.can_login);
assert!(super_role.can_bypass_rls);

let nologin_role = roles.iter().find(|r| r.name == "test_nologin").unwrap();
assert!(!nologin_role.is_super_user);
assert!(!nologin_role.can_create_db);
assert!(!nologin_role.can_login);
assert!(!nologin_role.can_bypass_rls);

let login_role = roles.iter().find(|r| r.name == "test_login").unwrap();
assert!(!login_role.is_super_user);
assert!(!login_role.can_create_db);
assert!(login_role.can_login);
assert!(!login_role.can_bypass_rls);
}
}
7 changes: 5 additions & 2 deletions crates/pgt_schema_cache/src/schema_cache.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use sqlx::postgres::PgPool;

use crate::Trigger;
use crate::columns::Column;
use crate::functions::Function;
use crate::policies::Policy;
use crate::schemas::Schema;
use crate::tables::Table;
use crate::types::PostgresType;
use crate::versions::Version;
use crate::{Role, Trigger};

#[derive(Debug, Default)]
pub struct SchemaCache {
Expand All @@ -19,11 +19,12 @@ pub struct SchemaCache {
pub columns: Vec<Column>,
pub policies: Vec<Policy>,
pub triggers: Vec<Trigger>,
pub roles: Vec<Role>,
}

impl SchemaCache {
pub async fn load(pool: &PgPool) -> Result<SchemaCache, sqlx::Error> {
let (schemas, tables, functions, types, versions, columns, policies, triggers) = futures_util::try_join!(
let (schemas, tables, functions, types, versions, columns, policies, triggers, roles) = futures_util::try_join!(
Schema::load(pool),
Table::load(pool),
Function::load(pool),
Expand All @@ -32,6 +33,7 @@ impl SchemaCache {
Column::load(pool),
Policy::load(pool),
Trigger::load(pool),
Role::load(pool)
)?;

Ok(SchemaCache {
Expand All @@ -43,6 +45,7 @@ impl SchemaCache {
columns,
policies,
triggers,
roles,
})
}

Expand Down