mod args;
mod read;
use crate::cli::schema::{AdvisableTableArgs, Detail, SchemaCommand};
use crate::conn::Session;
use crate::error::{Failure, Result};
use crate::introspect::kinds::Kind;
use crate::introspect::{grants, Ceilings};
use crate::output::Envelope;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PolicyScope {
Change,
All,
}
impl PolicyScope {
pub fn parse(word: &str) -> Result<Self> {
match word {
"change" => Ok(Self::Change),
"all" => Ok(Self::All),
other => Err(Failure::config(
"config.unknown_policy_scope",
format!("`{other}` is not a reach a policy can have"),
)
.with_detail("detail", other)
.with_hint("set sql.policy_scope to change or to all")),
}
}
#[must_use]
pub const fn covers_discovery(self) -> bool {
matches!(self, Self::All)
}
}
#[derive(Debug, Clone)]
pub struct Target {
pub database: Option<String>,
pub ceilings: Ceilings,
pub read_only: bool,
pub policy_scope: PolicyScope,
pub index_advice: bool,
}
pub async fn execute(
command: &SchemaCommand,
session: &mut Session,
target: &Target,
) -> Result<Envelope> {
let name = command.published_name();
admit_discovery(name, target)?;
match command {
SchemaCommand::Map(args) => read::compact_map(name, args, session, target).await,
SchemaCommand::Databases => {
read::listing(name, Kind::Database, session, None, None, true).await
}
SchemaCommand::Tables(args) => read::tables(name, args, session, target).await,
SchemaCommand::Columns(args) => {
let deep = Detail::or_default(args.detail, Detail::Full);
read::on_table(name, Kind::Column, args, deep, session, target).await
}
SchemaCommand::Keys(args) => {
read::advised(name, Kind::ForeignKey, args, session, target).await
}
SchemaCommand::Indexes(args) => {
read::advised(name, Kind::Index, args, session, target).await
}
SchemaCommand::Checks(args) => {
let deep = Detail::or_default(args.detail, Detail::Full);
read::on_table(name, Kind::Check, args, deep, session, target).await
}
SchemaCommand::Views(args) => read::views(name, args, session, target).await,
SchemaCommand::Routines(args) => {
read::in_database(name, Kind::Routine, args, session, target).await
}
SchemaCommand::Triggers(args) => {
read::in_database(name, Kind::Trigger, args, session, target).await
}
SchemaCommand::Events(args) => read::events(name, args, session, target).await,
SchemaCommand::Sequences(args) => read::sequences(name, args, session, target).await,
SchemaCommand::Partitions(args) => {
let deep = Detail::or_default(args.detail, Detail::Summary);
read::on_table(name, Kind::Partition, args, deep, session, target).await
}
SchemaCommand::Search(args) => read::find(name, args, session, target).await,
SchemaCommand::Stats(args) => read::stats(name, args, session, target).await,
SchemaCommand::Ddl(args) => read::definition(name, args, session, target).await,
SchemaCommand::Count(args) => read::count(name, args, session, target).await,
SchemaCommand::Size(args) => read::size(name, args, session, target).await,
SchemaCommand::Grants => Ok(Envelope::new(
name,
grants::effective(session.connection()).await?,
)),
}
}
#[must_use]
pub fn advice_wanted(args: &AdvisableTableArgs, target: &Target) -> bool {
args.advise || target.index_advice
}
pub fn admit_discovery(name: &'static str, target: &Target) -> Result<()> {
if !(target.read_only && target.policy_scope.covers_discovery()) {
return Ok(());
}
Err(Failure::validation(
"safety.policy_covers_discovery",
format!("this destination refuses every operation, and `{name}` is one"),
)
.with_detail("detail", name)
.with_hint(
"set sql.policy_scope to change to keep discovery open, \
or drop the refusal of change altogether",
))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::introspect::map;
#[test]
fn the_database_comes_from_the_target_alone() {
let target = Target {
database: Some("in_force".to_owned()),
..open_target()
};
assert_eq!(target.database.clone(), Some("in_force".to_owned()));
assert_eq!(open_target().database, None);
}
fn open_target() -> Target {
Target {
database: None,
ceilings: ceilings(),
read_only: false,
policy_scope: PolicyScope::Change,
index_advice: false,
}
}
#[test]
fn discovery_is_refused_only_when_the_policy_was_widened_to_reach_it() {
assert!(admit_discovery("schema tables", &open_target()).is_ok());
let refusing_change = Target {
read_only: true,
..open_target()
};
assert!(
admit_discovery("schema tables", &refusing_change).is_ok(),
"refusing change must never take discovery away by itself"
);
let widened_but_writable = Target {
policy_scope: PolicyScope::All,
..open_target()
};
assert!(
admit_discovery("schema tables", &widened_but_writable).is_ok(),
"a wider reach refuses nothing while nothing is refused"
);
let closed = Target {
read_only: true,
policy_scope: PolicyScope::All,
..open_target()
};
let failure = admit_discovery("schema tables", &closed).expect_err("must refuse");
assert_eq!(failure.code(), "safety.policy_covers_discovery");
assert_eq!(failure.exit_number().code(), 3);
assert!(failure.message().contains("schema tables"));
assert!(failure
.hint()
.is_some_and(|hint| hint.contains("sql.policy_scope")));
}
#[test]
fn the_reach_of_the_policy_is_read_from_its_own_word() {
assert_eq!(
PolicyScope::parse("change").expect("published"),
PolicyScope::Change
);
assert_eq!(
PolicyScope::parse("all").expect("published"),
PolicyScope::All
);
assert!(!PolicyScope::Change.covers_discovery());
assert!(PolicyScope::All.covers_discovery());
let failure = PolicyScope::parse("sometimes").expect_err("outside the domain");
assert_eq!(failure.code(), "config.unknown_policy_scope");
}
fn ceilings() -> Ceilings {
Ceilings {
map: map::Budget {
tables: 200,
rows: 20_000,
},
search_rows: 200,
ranked_objects: 20,
countable_rows: 10_000_000,
}
}
}