Skip to main content

systemprompt_cli/commands/admin/setup/
ddl.rs

1//! Bootstrap DDL statement builders for `PostgreSQL` provisioning.
2//!
3//! `CREATE USER` / `CREATE DATABASE` / `GRANT` run before the target database
4//! exists, so they cannot bind parameters; identifiers and literals are
5//! escaped here instead. Statement execution stays in the setup wizard.
6
7/// Escapes and double-quotes a `PostgreSQL` identifier (role or database name).
8#[must_use]
9pub fn quote_ident(ident: &str) -> String {
10    format!("\"{}\"", ident.replace('"', "\"\""))
11}
12
13/// Escapes and single-quotes a `PostgreSQL` string literal.
14#[must_use]
15pub fn quote_literal(value: &str) -> String {
16    format!("'{}'", value.replace('\'', "''"))
17}
18
19#[must_use]
20pub fn build_create_user_sql(user: &str, password: &str) -> String {
21    format!(
22        "CREATE USER {} WITH PASSWORD {}",
23        quote_ident(user),
24        quote_literal(password)
25    )
26}
27
28#[must_use]
29pub fn build_create_db_sql(database: &str, owner: &str) -> String {
30    format!(
31        "CREATE DATABASE {} OWNER {}",
32        quote_ident(database),
33        quote_ident(owner)
34    )
35}
36
37#[must_use]
38pub fn build_grant_sql(database: &str, user: &str) -> String {
39    format!(
40        "GRANT ALL PRIVILEGES ON DATABASE {} TO {}",
41        quote_ident(database),
42        quote_ident(user)
43    )
44}