Skip to main content

waypoint_core/
lib.rs

1pub mod checksum;
2pub mod commands;
3pub mod config;
4pub mod db;
5pub mod error;
6pub mod history;
7pub mod hooks;
8pub mod migration;
9pub mod placeholder;
10
11use config::WaypointConfig;
12use error::Result;
13use tokio_postgres::Client;
14
15pub use commands::info::{MigrationInfo, MigrationState};
16pub use commands::migrate::MigrateReport;
17pub use commands::repair::RepairReport;
18pub use commands::validate::ValidateReport;
19pub use config::CliOverrides;
20
21/// Main entry point for the Waypoint library.
22///
23/// Create a `Waypoint` instance with a config and use its methods to
24/// run migration commands programmatically.
25pub struct Waypoint {
26    pub config: WaypointConfig,
27    client: Client,
28}
29
30impl Waypoint {
31    /// Create a new Waypoint instance, connecting to the database.
32    ///
33    /// If `connect_retries` is configured, retries with exponential backoff.
34    pub async fn new(config: WaypointConfig) -> Result<Self> {
35        let conn_string = config.connection_string()?;
36        let client = db::connect_with_config(
37            &conn_string,
38            &config.database.ssl_mode,
39            config.database.connect_retries,
40            config.database.connect_timeout_secs,
41            config.database.statement_timeout_secs,
42        )
43        .await?;
44        Ok(Self { config, client })
45    }
46
47    /// Create a new Waypoint instance with an existing database client.
48    pub fn with_client(config: WaypointConfig, client: Client) -> Self {
49        Self { config, client }
50    }
51
52    /// Apply pending migrations.
53    pub async fn migrate(&self, target_version: Option<&str>) -> Result<MigrateReport> {
54        commands::migrate::execute(&self.client, &self.config, target_version).await
55    }
56
57    /// Show migration status information.
58    pub async fn info(&self) -> Result<Vec<MigrationInfo>> {
59        commands::info::execute(&self.client, &self.config).await
60    }
61
62    /// Validate applied migrations against local files.
63    pub async fn validate(&self) -> Result<ValidateReport> {
64        commands::validate::execute(&self.client, &self.config).await
65    }
66
67    /// Repair the schema history table.
68    pub async fn repair(&self) -> Result<RepairReport> {
69        commands::repair::execute(&self.client, &self.config).await
70    }
71
72    /// Baseline an existing database.
73    pub async fn baseline(&self, version: Option<&str>, description: Option<&str>) -> Result<()> {
74        commands::baseline::execute(&self.client, &self.config, version, description).await
75    }
76
77    /// Drop all objects in managed schemas.
78    pub async fn clean(&self, allow_clean: bool) -> Result<Vec<String>> {
79        commands::clean::execute(&self.client, &self.config, allow_clean).await
80    }
81}