athena_rs 0.83.0

Database gateway API
Documentation
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
use actix_web::dev::ServiceResponse;
use actix_web::http::StatusCode as ActixStatusCode;
use actix_web::web::Bytes;
use actix_web::{
    App,
    body::to_bytes,
    test::{TestRequest, call_service, init_service},
};
use anyhow::{Context, Result, anyhow, bail};
use clap::{Args, Parser, Subcommand};
use colored::Colorize;
use reqwest::{Client as HttpClient, Response, StatusCode};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use std::collections::HashMap;
use std::fs::{self, File};
use std::io::Write;
use std::path::{Path, PathBuf};
#[cfg(feature = "cdc")]
use std::time::Duration;

use crate::api::pipelines::run_pipeline;
use crate::bootstrap::Bootstrap;
#[cfg(feature = "cdc")]
use crate::cdc::postgres::sequin::{
    AuditLogger, backfill_from_csv, load_table_configs, stream_events,
};
#[cfg(feature = "cdc")]
use crate::client::{AthenaClient, backend::BackendType};
#[cfg(feature = "cdc")]
use crate::config::Config;

const DEFAULT_CLIENTS_PATH: &str = "config/clients.json";
const DEFAULT_FETCH_URL: &str = "https://athena-db.com/gateway/fetch";

/// CLI definition for the shared Athena binary.
#[derive(Parser)]
#[command(name = "athena_rs")]
#[command(author, version, about = "Athena API server + CLI helpers")]
pub struct AthenaCli {
    /// Override the default configuration file path.
    #[arg(
        long = "config",
        alias = "config-path",
        global = true,
        value_name = "PATH",
        help = "Override the default configuration file path (default: config.yaml)"
    )]
    pub config_path: Option<PathBuf>,

    /// Override the pipeline definitions file path.
    #[arg(
        long,
        global = true,
        value_name = "PATH",
        default_value = "config/pipelines.yaml"
    )]
    pub pipelines_path: PathBuf,

    /// Override the port used when booting the API server.
    #[arg(long, global = true, value_name = "PORT")]
    pub port: Option<u16>,

    /// Only start the API server when explicitly requested.
    #[arg(long, global = true)]
    pub api_only: bool,

    /// Run only the CDC WebSocket server (for event broadcasting).
    #[cfg(feature = "cdc")]
    #[arg(long, global = true)]
    pub cdc_only: bool,

    /// Subcommand to run (use `--api-only` or `server` to start the API).
    #[command(subcommand)]
    pub command: Option<Command>,
}

/// Supported subcommands for Athena.
#[derive(Subcommand)]
pub enum Command {
    /// Start the Actix-based API server.
    Server,

    /// Run a pipeline definition inline.
    Pipeline(PipelineArgs),

    /// Manage saved Postgres client entries.
    Clients {
        #[command(subcommand)]
        command: ClientCommand,
    },

    /// Proxy a gateway fetch request through athena-db.com.
    Fetch(FetchArgs),

    /// Run CDC helpers (backfills + streaming).
    #[cfg(feature = "cdc")]
    Cdc {
        #[command(subcommand)]
        command: CdcCommand,
    },

    /// Diagnostic info about the current environment.
    Diag,

    /// Print the build version and exit.
    Version,
}

/// Arguments for the pipeline runner CLI.
#[derive(Args)]
pub struct PipelineArgs {
    /// The `X-Athena-Client` header value to route the pipeline.
    #[arg(short, long, value_name = "CLIENT")]
    pub client: String,

    /// Inline JSON body describing the pipeline request.
    #[arg(long, conflicts_with = "payload_file")]
    pub payload_json: Option<String>,

    /// Path to a file containing the JSON pipeline request.
    #[arg(long, value_name = "PATH", conflicts_with = "payload_json")]
    pub payload_file: Option<PathBuf>,

    /// Reference a prebuilt pipeline by name (merged with inline overrides).
    #[arg(long, value_name = "NAME")]
    pub pipeline: Option<String>,
}

/// Subcommands for client management.
#[derive(Subcommand)]
pub enum ClientCommand {
    /// List known clients and the configured default.
    List {
        #[arg(long, value_name = "PATH", default_value = DEFAULT_CLIENTS_PATH)]
        path: PathBuf,
    },

    /// Add or update a client entry.
    Add {
        #[arg(long, value_name = "PATH", default_value = DEFAULT_CLIENTS_PATH)]
        path: PathBuf,

        /// Logical client name.
        name: String,

        /// JDBC-style URI or supabase slug.
        uri: String,
    },

    /// Remove an existing client entry.
    Remove {
        #[arg(long, value_name = "PATH", default_value = DEFAULT_CLIENTS_PATH)]
        path: PathBuf,

        /// Logical client name to delete.
        name: String,
    },

    /// Update the default client used when none is provided.
    SetDefault {
        #[arg(long, value_name = "PATH", default_value = DEFAULT_CLIENTS_PATH)]
        path: PathBuf,

        /// Name of the client to make default.
        name: String,
    },
}

/// Arguments for the gateway fetch helper command.
#[derive(Args)]
pub struct FetchArgs {
    /// Override the clients file path.
    #[arg(long, value_name = "PATH", default_value = DEFAULT_CLIENTS_PATH)]
    pub clients_path: PathBuf,

    /// Override the default client (otherwise uses the stored default).
    #[arg(long)]
    pub client: Option<String>,

    /// Custom gateway fetch URL.
    #[arg(long, value_name = "URL", default_value = DEFAULT_FETCH_URL)]
    pub url: String,

    /// Inline JSON body to POST.
    #[arg(long, conflicts_with = "body_file")]
    pub body_json: Option<String>,

    /// File containing the JSON body.
    #[arg(long, value_name = "PATH", conflicts_with = "body_json")]
    pub body_file: Option<PathBuf>,
}

/// CDC command helpers.
#[cfg(feature = "cdc")]
#[derive(Subcommand)]
pub enum CdcCommand {
    /// Replay a CSV export of `sequin_events`.
    Backfill(CdcBackfillArgs),

    /// Stream the live `sequin_events` table and replay each change.
    Stream(CdcStreamArgs),
}

/// Arguments that control the backfill run.
#[cfg(feature = "cdc")]
#[derive(Args)]
pub struct CdcBackfillArgs {
    /// Path to the CSV export produced by Sequin.
    #[arg(long, value_name = "PATH", default_value = "src/cdc/sequin_events.csv")]
    pub csv_path: PathBuf,

    /// Optional YAML file describing primary keys per table.
    #[arg(long, value_name = "PATH")]
    pub table_config: Option<PathBuf>,

    /// Where the CDC cursor should be persisted.
    #[arg(long, value_name = "PATH", default_value = "cdc/state.json")]
    pub state_file: PathBuf,

    /// Athena backend name (native, supabase, postgrest, scylla, neon).
    #[arg(long)]
    pub backend: Option<String>,

    /// URL of the gateway/backend to connect to.
    #[arg(long, value_name = "URL")]
    pub url: String,

    /// Optional API key for the target backend.
    #[arg(long, value_name = "KEY")]
    pub key: Option<String>,

    /// Enable dry-run mode (skip executing SQL but still log and advance seq).
    #[arg(long)]
    pub dry_run: bool,

    /// Athena backend name for the audit log (defaults to postgres/native).
    #[arg(long)]
    pub audit_backend: Option<String>,

    /// URL of the audit logging backend (`athena_logging` client).
    #[arg(long, value_name = "URL")]
    pub audit_url: Option<String>,

    /// Optional API key for the audit logging backend.
    #[arg(long, value_name = "KEY")]
    pub audit_key: Option<String>,
}

/// Arguments that control the streaming processor.
#[cfg(feature = "cdc")]
#[derive(Args)]
pub struct CdcStreamArgs {
    /// Optional YAML file describing primary keys per table.
    #[arg(long, value_name = "PATH")]
    pub table_config: Option<PathBuf>,

    /// Where the CDC cursor should be persisted.
    #[arg(long, value_name = "PATH", default_value = "cdc/state.json")]
    pub state_file: PathBuf,

    /// Athena backend name (native, supabase, postgrest, scylla, neon).
    #[arg(long)]
    pub backend: Option<String>,

    /// URL of the gateway/backend to connect to.
    #[arg(long, value_name = "URL")]
    pub url: String,

    /// Optional API key for the target backend.
    #[arg(long, value_name = "KEY")]
    pub key: Option<String>,

    /// Enable dry-run mode (skip executing SQL but still log and advance seq).
    #[arg(long)]
    pub dry_run: bool,

    /// Athena backend name for the audit log (defaults to postgres/native).
    #[arg(long)]
    pub audit_backend: Option<String>,

    /// URL of the audit logging backend (`athena_logging` client).
    #[arg(long, value_name = "URL")]
    pub audit_url: Option<String>,

    /// Optional API key for the audit logging backend.
    #[arg(long, value_name = "KEY")]
    pub audit_key: Option<String>,

    /// Fully qualified sequin events table (schema.table or table).
    #[arg(long, default_value = "public.sequin_events")]
    pub sequin_table: String,

    /// Number of events to fetch per poll.
    #[arg(long, default_value = "512")]
    pub batch_size: usize,

    /// Seconds to wait between empty poll cycles.
    #[arg(long, default_value = "5")]
    pub poll_interval_secs: u64,
}

/// Represents the persisted clients configuration.
#[derive(Debug, Deserialize, Serialize, Default)]
struct ClientStore {
    default: Option<String>,
    clients: HashMap<String, String>,
}

impl ClientStore {
    fn load(path: &Path) -> Result<Self> {
        if path.exists() {
            let bytes = fs::read_to_string(path).context("reading clients file")?;
            let store: ClientStore =
                serde_json::from_str(&bytes).context("parsing clients file as JSON")?;
            Ok(store)
        } else {
            Ok(Self::default())
        }
    }

    fn persist(&self, path: &Path) -> Result<()> {
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).context("creating clients directory")?;
        }
        let mut file: File = File::create(path).context("creating clients file")?;
        serde_json::to_writer_pretty(&mut file, &self).context("serializing clients file")?;
        file.write_all(b"\n")
            .context("writing newline to clients file")?;
        Ok(())
    }
}

/// Run the pipeline CLI extension.
pub async fn run_pipeline_command(bootstrap: &Bootstrap, args: PipelineArgs) -> Result<()> {
    let mut body = match parse_json_input(args.payload_json, args.payload_file)? {
        Some(value) => value,
        None => json!({}),
    };

    if !body.is_object() {
        bail!("pipeline payload must be a JSON object");
    }

    if let Some(name) = args.pipeline.as_ref() {
        body["pipeline"] = json!(name);
    }

    if body.as_object().map_or(true, |map| map.is_empty()) && args.pipeline.is_none() {
        bail!("provide either --payload-json/--payload-file or --pipeline");
    }

    let mut service = init_service(
        App::new()
            .app_data(bootstrap.app_state.clone())
            .service(run_pipeline),
    )
    .await;

    let request = TestRequest::post()
        .uri("/pipelines")
        .insert_header(("X-Athena-Client", args.client.as_str()))
        .set_json(&body)
        .to_request();

    let response: ServiceResponse = call_service(&mut service, request).await;
    let status: ActixStatusCode = response.status();
    let body_bytes: Bytes = to_bytes(response.into_body())
        .await
        .map_err(|err| anyhow!("reading pipeline response body: {}", err))?;
    let body_text = String::from_utf8_lossy(&body_bytes);

    if status.is_success() {
        if let Ok(parsed) = serde_json::from_slice::<Value>(&body_bytes) {
            println!("{}", serde_json::to_string_pretty(&parsed)?);
        } else {
            println!("{}", body_text);
        }
    } else {
        eprintln!("pipeline request failed ({})", status);
        eprintln!("{}", body_text);
    }

    Ok(())
}

/// Run client management CLI commands.
pub fn run_clients_command(command: ClientCommand) -> Result<()> {
    match command {
        ClientCommand::List { path } => {
            let store: ClientStore = ClientStore::load(&path)?;
            println!("{}", "Clients".bold().underline());
            println!(
                "{} {}",
                "Clients file:".bright_cyan(),
                path.display().to_string().bright_white()
            );
            match store.default.as_deref() {
                Some(default) => println!(
                    "{} {}",
                    "Default client:".bright_cyan(),
                    default.bright_green().bold()
                ),
                None => println!("{} {}", "Default client:".bright_cyan(), "none".dimmed()),
            }
            let mut names: Vec<&String> = store.clients.keys().collect();
            names.sort();
            for name in names {
                if let Some(uri) = store.clients.get(name) {
                    let name_display = if store.default.as_deref() == Some(name.as_str()) {
                        format!("{} (default)", name)
                    } else {
                        name.to_string()
                    };
                    println!(
                        "  {} {} {}",
                        name_display.yellow().bold(),
                        "->".dimmed(),
                        uri.cyan()
                    );
                }
            }
            Ok(())
        }
        ClientCommand::Add { path, name, uri } => {
            let mut store: ClientStore = ClientStore::load(&path)?;
            store.clients.insert(name.clone(), uri.clone());
            if store.default.is_none() {
                store.default = Some(name.clone());
            }
            store.persist(&path)?;
            println!(
                "{} {} {} {} {}",
                "Saved client".bright_green().bold(),
                format!("'{}'", name).bright_yellow(),
                "->".dimmed(),
                uri.cyan(),
                format!("in {}", path.display().to_string().bright_cyan())
            );
            Ok(())
        }
        ClientCommand::Remove { path, name } => {
            let mut store: ClientStore = ClientStore::load(&path)?;
            if store.clients.remove(&name).is_none() {
                bail!("client '{}' not found", name);
            }
            if store.default.as_deref() == Some(name.as_str()) {
                store.default = None;
            }
            store.persist(&path)?;
            println!(
                "{} {} {} {}",
                "Removed client".bright_red().bold(),
                format!("'{}'", name).bright_yellow(),
                "from".white(),
                path.display().to_string().bright_cyan()
            );
            Ok(())
        }
        ClientCommand::SetDefault { path, name } => {
            let mut store: ClientStore = ClientStore::load(&path)?;
            if !store.clients.contains_key(&name) {
                bail!("client '{}' is not defined", name);
            }
            store.default = Some(name.clone());
            store.persist(&path)?;
            println!(
                "{} {} {} {} {}",
                "Default client set to".bright_green().bold(),
                format!("'{}'", name).bright_yellow(),
                "in".white(),
                path.display().to_string().bright_cyan(),
                "✅".bright_green()
            );
            Ok(())
        }
    }
}

/// Run the fetch helper command.
pub async fn run_fetch_command(args: FetchArgs) -> Result<()> {
    let body: Value =
        parse_json_input(args.body_json.clone(), args.body_file.clone())?.context("empty body")?;
    let store: ClientStore = ClientStore::load(&args.clients_path)?;
    let client_name: String = if let Some(name) = args.client {
        if !store.clients.contains_key(&name) {
            bail!(
                "client '{}' not found in {}",
                name,
                args.clients_path.display()
            );
        }
        name
    } else {
        store
            .default
            .clone()
            .ok_or_else(|| anyhow!("no client provided and no default set"))?
    };

    let http: HttpClient = HttpClient::new();
    let resp: Response = http
        .post(&args.url)
        .header("Content-Type", "application/json")
        .header("X-Athena-Client", client_name.clone())
        .json(&body)
        .send()
        .await
        .context("failed to execute fetch request")?;

    let status: StatusCode = resp.status();
    let text: String = resp.text().await.context("reading fetch response")?;
    if status.is_success() {
        if let Ok(parsed) = serde_json::from_str::<Value>(&text) {
            println!("{}", serde_json::to_string_pretty(&parsed)?);
        } else {
            println!("{}", text);
        }
    } else {
        eprintln!("fetch failed ({})", status);
        eprintln!("{}", text);
    }

    Ok(())
}

/// Run the CDC helper command.
#[cfg(feature = "cdc")]
pub async fn run_cdc_command(config: &Config, command: CdcCommand) -> Result<()> {
    match command {
        CdcCommand::Backfill(args) => run_cdc_backfill(config, args).await,
        CdcCommand::Stream(args) => run_cdc_stream(config, args).await,
    }
}

#[cfg(feature = "cdc")]
async fn run_cdc_backfill(config: &Config, args: CdcBackfillArgs) -> Result<()> {
    let configs = load_table_configs(args.table_config.as_deref())?;
    let client = build_cdc_client(args.backend.as_deref(), &args.url, args.key.as_deref()).await?;
    let audit_logger = build_audit_logger(
        config,
        args.audit_backend.as_deref(),
        args.audit_url.as_deref(),
        args.audit_key.as_deref(),
    )
    .await?;
    let state = backfill_from_csv(
        &client,
        &args.csv_path,
        &configs,
        args.state_file.as_path(),
        args.dry_run,
        audit_logger.as_ref(),
    )
    .await?;
    println!(
        "Backfill finished through seq {}",
        state.last_seq.unwrap_or(0)
    );
    Ok(())
}

#[cfg(feature = "cdc")]
async fn run_cdc_stream(config: &Config, args: CdcStreamArgs) -> Result<()> {
    let configs = load_table_configs(args.table_config.as_deref())?;
    let client = build_cdc_client(args.backend.as_deref(), &args.url, args.key.as_deref()).await?;
    let audit_logger = build_audit_logger(
        config,
        args.audit_backend.as_deref(),
        args.audit_url.as_deref(),
        args.audit_key.as_deref(),
    )
    .await?;
    let interval = Duration::from_secs(args.poll_interval_secs.max(1));
    let limit = args.batch_size.max(1);
    println!(
        "Starting CDC stream against {} (batch {}, interval {}s)...",
        args.sequin_table,
        limit,
        interval.as_secs()
    );
    stream_events(
        &client,
        &configs,
        &args.sequin_table,
        limit,
        interval,
        args.state_file.as_path(),
        args.dry_run,
        audit_logger.as_ref(),
    )
    .await
}

#[cfg(feature = "cdc")]
async fn build_cdc_client(
    backend: Option<&str>,
    url: &str,
    key: Option<&str>,
) -> Result<AthenaClient> {
    let backend = parse_backend_type(backend);
    build_client(backend, url, key).await
}

#[cfg(feature = "cdc")]
async fn build_client(backend: BackendType, url: &str, key: Option<&str>) -> Result<AthenaClient> {
    let mut builder = AthenaClient::builder().backend(backend).url(url);
    if let Some(key) = key {
        builder = builder.key(key);
    }
    AthenaClient::build(builder)
        .await
        .map_err(|err| anyhow!("failed to build Athena client: {}", err))
}

#[cfg(feature = "cdc")]
async fn build_audit_logger(
    config: &Config,
    backend: Option<&str>,
    url: Option<&str>,
    key: Option<&str>,
) -> Result<Option<AuditLogger>> {
    let fallback_url = config
        .get_postgres_uri("athena_logging")
        .map(|value| value.clone());
    let target_url = url.map(|value| value.to_string()).or(fallback_url);
    if let Some(url) = target_url {
        let backend = backend
            .map(|value| parse_backend_type(Some(value)))
            .unwrap_or(BackendType::PostgreSQL);
        let client = build_client(backend, &url, key).await?;
        Ok(Some(AuditLogger::new(client, "cdc", "cdc")))
    } else {
        Ok(None)
    }
}

#[cfg(feature = "cdc")]
fn parse_backend_type(name: Option<&str>) -> BackendType {
    match name.unwrap_or("native").to_lowercase().as_str() {
        "supabase" => BackendType::Supabase,
        "postgrest" => BackendType::Postgrest,
        "scylla" => BackendType::Scylla,
        "neon" => BackendType::Neon,
        "postgresql" | "postgres" => BackendType::PostgreSQL,
        _ => BackendType::Native,
    }
}

/// Run a quick diagnostic summary.
pub fn run_diag_command() -> Result<()> {
    let info: Value = json!({
        "os": std::env::consts::OS,
        "arch": std::env::consts::ARCH,
        "family": std::env::consts::FAMILY,
        "cpu_count": num_cpus::get(),
        "rust_version": env!("CARGO_PKG_VERSION"),
        "hostname": std::env::var("HOSTNAME").unwrap_or_default(),
        "locale": std::env::var("LANG").unwrap_or_default(),
    });
    println!("{}", serde_json::to_string_pretty(&info)?);
    Ok(())
}

/// Print the version and exit.
pub fn run_version_command() {
    println!("{}", env!("CARGO_PKG_VERSION"));
}

fn parse_json_input(json: Option<String>, file: Option<PathBuf>) -> Result<Option<Value>> {
    if let Some(text) = json {
        let value: Value = serde_json::from_str(&text).context("parsing inline JSON payload")?;
        return Ok(Some(value));
    }
    if let Some(path) = file {
        let text: String = fs::read_to_string(&path).context("reading payload file")?;
        let value: Value = serde_json::from_str(&text).context("parsing payload file")?;
        return Ok(Some(value));
    }
    Ok(None)
}