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//! Copyright (c) systemprompt.io — Business Source License 1.1.
8//! See <https://systemprompt.io> for licensing details.
9
10#[must_use]
11pub fn quote_ident(ident: &str) -> String {
12    format!("\"{}\"", ident.replace('"', "\"\""))
13}
14
15#[must_use]
16pub fn quote_literal(value: &str) -> String {
17    format!("'{}'", value.replace('\'', "''"))
18}
19
20#[must_use]
21pub fn build_create_user_sql(user: &str, password: &str) -> String {
22    format!(
23        "CREATE USER {} WITH PASSWORD {}",
24        quote_ident(user),
25        quote_literal(password)
26    )
27}
28
29#[must_use]
30pub fn build_create_db_sql(database: &str, owner: &str) -> String {
31    format!(
32        "CREATE DATABASE {} OWNER {}",
33        quote_ident(database),
34        quote_ident(owner)
35    )
36}
37
38#[must_use]
39pub fn build_grant_sql(database: &str, user: &str) -> String {
40    format!(
41        "GRANT ALL PRIVILEGES ON DATABASE {} TO {}",
42        quote_ident(database),
43        quote_ident(user)
44    )
45}