autumn-cli 0.6.0

CLI tool for the Autumn web framework
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//! `autumn config` — inspect and mutate runtime configuration values.
//!
//! All commands connect directly to the configured Postgres database over a
//! native `tokio_postgres` connection (see issue #1243) — no `psql` binary
//! required — following the same profile-aware URL-resolution strategy as
//! the app.
//!
//! # Commands
//!
//! ```text
//! autumn config list                    # list all overrides (key, value, updated_at)
//! autumn config get <key>               # print the current stored value for a key
//! autumn config set <key> <value>       # set (or replace) a key
//! autumn config unset <key>             # remove the override, restoring the default
//! autumn config history <key>           # show the change history for a key
//! ```

use autumn_web::config::{AutumnConfig, Env, OsEnv};

use crate::pg::{self, ResultExt as _};

/// Options for `autumn config list`.
pub struct ListOptions;

/// Options for `autumn config get <key>`.
pub struct GetOptions {
    pub key: String,
}

/// Options for `autumn config set <key> <value>`.
pub struct SetOptions {
    pub key: String,
    pub value: String,
    pub actor: Option<String>,
}

/// Options for `autumn config unset <key>`.
pub struct UnsetOptions {
    pub key: String,
    pub actor: Option<String>,
}

/// Options for `autumn config history <key>`.
pub struct HistoryOptions {
    pub key: String,
    pub limit: usize,
}

const LIST_SQL: &str = "SELECT key, raw_value, updated_at::text AS updated_at \
     FROM autumn_runtime_config_values \
     ORDER BY key;";

const GET_SQL: &str = "SELECT key, raw_value, updated_at::text AS updated_at \
     FROM autumn_runtime_config_values \
     WHERE key = $1;";

// Per-key advisory lock: serializes concurrent set/unset on the same key so
// T2 blocks here until T1 commits, and the next statement then sees T1's
// committed row under READ COMMITTED's per-statement snapshot.
const CONFIG_LOCK_SQL: &str = "SELECT pg_advisory_xact_lock(1, hashtext($1));";

const CONFIG_GET_PRIOR_SQL: &str =
    "SELECT raw_value FROM autumn_runtime_config_values WHERE key = $1;";

const CONFIG_UPSERT_SQL: &str = "INSERT INTO autumn_runtime_config_values (key, raw_value, updated_at) \
        VALUES ($1, $2, NOW()) \
        ON CONFLICT (key) DO UPDATE \
            SET raw_value = EXCLUDED.raw_value, \
                updated_at = EXCLUDED.updated_at;";

const CONFIG_SET_AUDIT_SQL: &str = "INSERT INTO autumn_runtime_config_changes \
    (key, old_value, new_value, actor) VALUES ($1, $2, $3, $4);";

const CONFIG_UNSET_DELETE_SQL: &str = "DELETE FROM autumn_runtime_config_values \
    WHERE key = $1 \
    RETURNING raw_value;";

const CONFIG_UNSET_AUDIT_SQL: &str = "INSERT INTO autumn_runtime_config_changes \
    (key, old_value, new_value, actor) VALUES ($1, $2, NULL, $3);";

const CONFIG_HISTORY_SQL: &str = "SELECT id::text, key, old_value, new_value, actor, changed_at::text AS changed_at \
     FROM autumn_runtime_config_changes \
     WHERE key = $1 \
     ORDER BY changed_at DESC \
     LIMIT $2;";

// ── Public entry points ────────────────────────────────────────────────────────

/// Run `autumn config list`.
pub fn run_list(_opts: &ListOptions) {
    let url = resolve_database_url();
    let rows = pg::block_on_or_die("config list", async {
        let client = pg::connect("config list", &url).await?;
        client.query(LIST_SQL, &[]).await.pg()
    });
    pg::print_table(
        &["key", "raw_value", "updated_at"],
        &rows.iter().map(pg::row_to_strings).collect::<Vec<_>>(),
    );
}

/// Run `autumn config get <key>`.
pub fn run_get(opts: &GetOptions) {
    let url = resolve_database_url();
    let rows = pg::block_on_or_die("config get", async {
        let client = pg::connect("config get", &url).await?;
        client.query(GET_SQL, &[&opts.key]).await.pg()
    });
    if rows.is_empty() {
        eprintln!("\u{2717} Key '{}' has no active override.", opts.key);
        std::process::exit(1);
    }
    pg::print_table(
        &["key", "raw_value", "updated_at"],
        &rows.iter().map(pg::row_to_strings).collect::<Vec<_>>(),
    );
}

/// Run `autumn config set <key> <value>`.
pub fn run_set(opts: &SetOptions) {
    let url = resolve_database_url();
    let actor = opts.actor.as_deref().unwrap_or("cli");
    pg::block_on_or_die("config set", async {
        let mut client = pg::connect("config set", &url).await?;
        let txn = client.transaction().await.pg()?;
        // Acquire the per-key advisory lock before reading the prior value
        // so concurrent writers on a brand-new key are serialised: T2 blocks
        // here until T1 commits, and the next statement then sees T1's
        // committed row under READ COMMITTED's per-statement snapshot.
        txn.execute(CONFIG_LOCK_SQL, &[&opts.key]).await.pg()?;
        let prior_rows = txn.query(CONFIG_GET_PRIOR_SQL, &[&opts.key]).await.pg()?;
        let old_value: Option<String> = prior_rows.first().map(|r| r.get::<_, String>(0));
        txn.execute(CONFIG_UPSERT_SQL, &[&opts.key, &opts.value])
            .await
            .pg()?;
        txn.execute(
            CONFIG_SET_AUDIT_SQL,
            &[&opts.key, &old_value, &opts.value, &actor],
        )
        .await
        .pg()?;
        txn.commit().await.pg()
    });

    eprintln!(
        "\u{2713} Set '{key}' = '{value}'",
        key = opts.key,
        value = opts.value
    );
}

/// Run `autumn config unset <key>`.
pub fn run_unset(opts: &UnsetOptions) {
    let url = resolve_database_url();
    let actor = opts.actor.as_deref().unwrap_or("cli");
    pg::block_on_or_die("config unset", async {
        let mut client = pg::connect("config unset", &url).await?;
        let txn = client.transaction().await.pg()?;
        // Same per-key advisory lock as `set`, taken before the DELETE
        // RETURNING captures old_value, keeping set/unset audit history
        // ordered under concurrent writes.
        txn.execute(CONFIG_LOCK_SQL, &[&opts.key]).await.pg()?;
        let removed = txn
            .query(CONFIG_UNSET_DELETE_SQL, &[&opts.key])
            .await
            .pg()?;
        if let Some(row) = removed.first() {
            let old_value: String = row.get(0);
            txn.execute(CONFIG_UNSET_AUDIT_SQL, &[&opts.key, &old_value, &actor])
                .await
                .pg()?;
        }
        txn.commit().await.pg()
    });
    eprintln!(
        "\u{2713} Unset '{key}' (reverted to compile-time default)",
        key = opts.key
    );
}

/// Run `autumn config history <key>`.
pub fn run_history(opts: &HistoryOptions) {
    let url = resolve_database_url();
    let limit = i64::try_from(opts.limit).unwrap_or(i64::MAX);
    let rows = pg::block_on_or_die("config history", async {
        let client = pg::connect("config history", &url).await?;
        client
            .query(CONFIG_HISTORY_SQL, &[&opts.key, &limit])
            .await
            .pg()
    });
    pg::print_table(
        &["id", "key", "old_value", "new_value", "actor", "changed_at"],
        &rows.iter().map(pg::row_to_strings).collect::<Vec<_>>(),
    );
}

// ── Database URL resolution (mirrors token.rs) ────────────────────────────────

pub fn resolve_database_url() -> String {
    if let Some(url) = resolve_primary_database_url_with_env(&OsEnv) {
        return url;
    }

    eprintln!("\u{2717} No database URL found.");
    eprintln!(
        "  Set database.primary_url (or database.url) in autumn.toml, autumn-<profile>.toml, \
         or set AUTUMN_DATABASE__PRIMARY_URL / AUTUMN_DATABASE__URL / DATABASE_URL."
    );
    std::process::exit(1);
}

pub fn resolve_primary_database_url_with_env(env: &dyn Env) -> Option<String> {
    resolve_primary_database_url_from_env_var(|key| env.var(key)).or_else(|| {
        AutumnConfig::load_with_env(env).ok().and_then(|config| {
            config
                .database
                .effective_primary_url()
                .map(ToOwned::to_owned)
        })
    })
}

fn resolve_primary_database_url_from_env_var<F>(env_var: F) -> Option<String>
where
    F: Fn(&str) -> Result<String, std::env::VarError>,
{
    for var in [
        "AUTUMN_DATABASE__PRIMARY_URL",
        "AUTUMN_DATABASE__URL",
        "DATABASE_URL",
    ] {
        if let Ok(url) = env_var(var)
            && !url.is_empty()
        {
            return Some(url);
        }
    }

    None
}

#[cfg(test)]
fn resolve_primary_database_url_from_sources<F>(
    env_var: F,
    table: Option<&toml::Table>,
) -> Option<String>
where
    F: Fn(&str) -> Result<String, std::env::VarError>,
{
    if let Some(url) = resolve_primary_database_url_from_env_var(env_var) {
        return Some(url);
    }

    let database = table?.get("database").and_then(toml::Value::as_table)?;
    for key in ["primary_url", "url"] {
        if let Some(url) = database
            .get(key)
            .and_then(toml::Value::as_str)
            .filter(|url| !url.is_empty())
        {
            return Some(url.to_owned());
        }
    }

    None
}

// ── Tests ──────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn lock_sql_acquires_per_key_advisory_lock() {
        assert!(
            CONFIG_LOCK_SQL.contains("pg_advisory_xact_lock(1, hashtext($1))"),
            "set/unset should acquire the per-key advisory lock before reading/writing"
        );
    }

    #[test]
    fn unset_delete_sql_deletes_and_returns_prior_value() {
        assert!(CONFIG_UNSET_DELETE_SQL.contains("DELETE FROM autumn_runtime_config_values"));
        assert!(CONFIG_UNSET_DELETE_SQL.contains("RETURNING raw_value"));
    }

    #[test]
    fn config_set_sql_upserts() {
        assert!(
            CONFIG_UPSERT_SQL.contains("ON CONFLICT"),
            "config set must use INSERT ... ON CONFLICT"
        );
    }

    // ── Issue #1243: no more psql shell-out ─────────────────────────────────

    #[test]
    fn no_sql_constant_uses_psql_variable_syntax() {
        for sql in [
            LIST_SQL,
            GET_SQL,
            CONFIG_LOCK_SQL,
            CONFIG_GET_PRIOR_SQL,
            CONFIG_UPSERT_SQL,
            CONFIG_SET_AUDIT_SQL,
            CONFIG_UNSET_DELETE_SQL,
            CONFIG_UNSET_AUDIT_SQL,
            CONFIG_HISTORY_SQL,
        ] {
            assert!(
                !sql.contains(":'"),
                "SQL must use native $n placeholders, not psql variable substitution: {sql}"
            );
        }
    }

    #[test]
    fn no_sql_constant_uses_textual_transaction_control() {
        for sql in [CONFIG_UPSERT_SQL, CONFIG_UNSET_DELETE_SQL] {
            assert!(!sql.contains("BEGIN;"), "no textual BEGIN: {sql}");
            assert!(!sql.contains("COMMIT;"), "no textual COMMIT: {sql}");
        }
    }

    #[test]
    fn module_does_not_shell_out_to_psql() {
        let src = include_str!("config.rs");
        assert!(
            !src.contains("Command::new(\"psql\")"),
            "config.rs must not shell out to the psql binary (issue #1243)"
        );
    }

    #[test]
    fn resolve_prefers_primary_url_env_var() {
        let env = |key: &str| match key {
            "AUTUMN_DATABASE__PRIMARY_URL" => Ok("postgres://primary".to_owned()),
            "AUTUMN_DATABASE__URL" => Ok("postgres://legacy".to_owned()),
            "DATABASE_URL" => Ok("postgres://fallback".to_owned()),
            _ => Err(std::env::VarError::NotPresent),
        };
        let url = resolve_primary_database_url_from_sources(env, None).unwrap();
        assert_eq!(url, "postgres://primary");
    }

    #[test]
    fn resolve_falls_back_to_legacy_env_var() {
        let env = |key: &str| match key {
            "AUTUMN_DATABASE__URL" => Ok("postgres://legacy".to_owned()),
            _ => Err(std::env::VarError::NotPresent),
        };
        let url = resolve_primary_database_url_from_sources(env, None).unwrap();
        assert_eq!(url, "postgres://legacy");
    }

    #[test]
    fn resolve_falls_back_to_database_url_env_var() {
        let env = |key: &str| match key {
            "DATABASE_URL" => Ok("postgres://fallback".to_owned()),
            _ => Err(std::env::VarError::NotPresent),
        };
        let url = resolve_primary_database_url_from_sources(env, None).unwrap();
        assert_eq!(url, "postgres://fallback");
    }

    #[test]
    fn resolve_reads_primary_url_from_toml() {
        let table = toml::from_str::<toml::Table>(
            r#"
            [database]
            primary_url = "postgres://from-toml"
            "#,
        )
        .unwrap();
        let env = |_: &str| Err(std::env::VarError::NotPresent);
        let url = resolve_primary_database_url_from_sources(env, Some(&table)).unwrap();
        assert_eq!(url, "postgres://from-toml");
    }

    #[test]
    fn resolve_reads_url_from_toml_when_primary_url_absent() {
        let table = toml::from_str::<toml::Table>(
            r#"
            [database]
            url = "postgres://legacy-toml"
            "#,
        )
        .unwrap();
        let env = |_: &str| Err(std::env::VarError::NotPresent);
        let url = resolve_primary_database_url_from_sources(env, Some(&table)).unwrap();
        assert_eq!(url, "postgres://legacy-toml");
    }

    #[test]
    fn resolve_reads_url_from_active_profile_file() {
        let tmp = tempfile::tempdir().unwrap();
        std::fs::write(tmp.path().join("autumn.toml"), "").unwrap();
        std::fs::write(
            tmp.path().join("autumn-dev.toml"),
            r#"
            [database]
            url = "postgres://profile-file"
            "#,
        )
        .unwrap();

        let env = autumn_web::config::MockEnv::new()
            .with("AUTUMN_MANIFEST_DIR", tmp.path().to_str().unwrap())
            .with("AUTUMN_ENV", "dev");

        let url = resolve_primary_database_url_with_env(&env).unwrap();
        assert_eq!(url, "postgres://profile-file");
    }

    #[test]
    fn resolve_returns_none_when_no_source_found() {
        let env = |_: &str| Err(std::env::VarError::NotPresent);
        let url = resolve_primary_database_url_from_sources(env, None);
        assert!(url.is_none());
    }
}