1pub mod advisor;
45pub mod checksum;
46pub mod commands;
47pub mod config;
48pub mod db;
49pub mod dependency;
50pub mod dialect;
51pub mod directive;
52pub mod engines;
53pub mod error;
54pub mod guard;
55pub mod history;
56pub mod hooks;
57pub mod migration;
58pub mod multi;
59pub mod placeholder;
60pub mod preflight;
61pub mod reversal;
62pub mod safety;
63pub mod schema;
64pub mod sql_parser;
65pub mod tls;
66
67use std::path::PathBuf;
68
69use config::WaypointConfig;
70use db::DbClient;
71use error::Result;
72
73#[cfg(feature = "postgres")]
74use tokio_postgres::Client;
75
76pub use advisor::AdvisorReport;
77pub use commands::changelog::ChangelogReport;
78pub use commands::check_conflicts::ConflictReport;
79pub use commands::diff::DiffReport;
80pub use commands::drift::DriftReport;
81pub use commands::explain::ExplainReport;
82pub use commands::info::{MigrationInfo, MigrationState};
83pub use commands::lint::LintReport;
84pub use commands::migrate::MigrateReport;
85pub use commands::repair::RepairReport;
86pub use commands::safety::SafetyCommandReport;
87pub use commands::simulate::SimulationReport;
88pub use commands::snapshot::{RestoreReport, SnapshotReport};
89pub use commands::undo::{UndoReport, UndoTarget};
90pub use commands::validate::ValidateReport;
91pub use config::CliOverrides;
92pub use dialect::{DatabaseDialect, DialectKind};
93pub use multi::MultiWaypoint;
94pub use preflight::PreflightReport;
95pub use safety::SafetyReport;
96
97pub struct Waypoint {
102 pub config: WaypointConfig,
103 client: DbClient,
104}
105
106impl Waypoint {
107 pub async fn new(config: WaypointConfig) -> Result<Self> {
113 let conn_string = config.connection_string()?;
114 let client = db::connect_for_url(&conn_string, &config).await?;
115 Ok(Self { config, client })
116 }
117
118 #[cfg(feature = "postgres")]
123 pub fn with_client(config: WaypointConfig, client: Client) -> Self {
124 Self {
125 config,
126 client: DbClient::with_postgres(client),
127 }
128 }
129
130 pub fn with_db_client(config: WaypointConfig, client: DbClient) -> Self {
132 Self { config, client }
133 }
134
135 pub fn client(&self) -> &DbClient {
137 &self.client
138 }
139
140 #[cfg(feature = "postgres")]
146 pub fn postgres_client(&self) -> Result<&Client> {
147 self.client.as_postgres()
148 }
149
150 pub async fn migrate(&self, target_version: Option<&str>) -> Result<MigrateReport> {
152 self.migrate_with_options(target_version, false).await
153 }
154
155 pub async fn migrate_with_options(
159 &self,
160 target_version: Option<&str>,
161 force: bool,
162 ) -> Result<MigrateReport> {
163 match self.client.dialect_kind() {
164 #[cfg(feature = "postgres")]
165 DialectKind::Postgres => {
166 commands::migrate::execute_with_options(
167 self.client.as_postgres()?,
168 &self.config,
169 target_version,
170 force,
171 )
172 .await
173 }
174 #[cfg(not(feature = "postgres"))]
175 DialectKind::Postgres => Err(error::WaypointError::ConfigError(
176 "PostgreSQL support is not compiled in (enable the `postgres` feature)".into(),
177 )),
178 #[cfg(feature = "mysql")]
179 DialectKind::Mysql => {
180 commands::migrate::execute_mysql_with_options(
181 &self.client,
182 &self.config,
183 target_version,
184 force,
185 )
186 .await
187 }
188 #[cfg(not(feature = "mysql"))]
189 DialectKind::Mysql => Err(error::WaypointError::ConfigError(
190 "MySQL support is not compiled in (enable the `mysql` feature)".into(),
191 )),
192 }
193 }
194
195 pub async fn info(&self) -> Result<Vec<MigrationInfo>> {
197 commands::info::execute_db(&self.client, &self.config).await
198 }
199
200 pub async fn validate(&self) -> Result<ValidateReport> {
202 commands::validate::execute_db(&self.client, &self.config).await
203 }
204
205 pub async fn repair(&self) -> Result<RepairReport> {
207 commands::repair::execute_db(&self.client, &self.config).await
208 }
209
210 pub async fn baseline(&self, version: Option<&str>, description: Option<&str>) -> Result<()> {
212 commands::baseline::execute_db(&self.client, &self.config, version, description).await
213 }
214
215 pub async fn undo(&self, target: UndoTarget) -> Result<UndoReport> {
217 commands::undo::execute_db(&self.client, &self.config, target).await
218 }
219
220 pub async fn clean(&self, allow_clean: bool) -> Result<Vec<String>> {
222 commands::clean::execute_db(&self.client, &self.config, allow_clean).await
223 }
224
225 pub fn lint(locations: &[PathBuf], disabled_rules: &[String]) -> Result<LintReport> {
227 commands::lint::execute(locations, disabled_rules)
228 }
229
230 pub fn changelog(
232 locations: &[PathBuf],
233 from: Option<&str>,
234 to: Option<&str>,
235 ) -> Result<ChangelogReport> {
236 commands::changelog::execute(locations, from, to)
237 }
238
239 pub async fn diff(&self, target: commands::diff::DiffTarget) -> Result<DiffReport> {
241 commands::diff::execute_db(&self.client, &self.config, target).await
242 }
243
244 pub async fn drift(&self) -> Result<DriftReport> {
246 commands::drift::execute_db(&self.client, &self.config).await
247 }
248
249 pub async fn snapshot(
251 &self,
252 snapshot_config: &commands::snapshot::SnapshotConfig,
253 ) -> Result<SnapshotReport> {
254 commands::snapshot::execute_snapshot_db(&self.client, &self.config, snapshot_config).await
255 }
256
257 pub async fn restore(
259 &self,
260 snapshot_config: &commands::snapshot::SnapshotConfig,
261 snapshot_id: &str,
262 ) -> Result<RestoreReport> {
263 commands::snapshot::execute_restore_db(
264 &self.client,
265 &self.config,
266 snapshot_config,
267 snapshot_id,
268 )
269 .await
270 }
271
272 pub async fn explain(&self) -> Result<ExplainReport> {
274 commands::explain::execute_db(&self.client, &self.config).await
275 }
276
277 pub async fn preflight(&self) -> Result<PreflightReport> {
279 preflight::run_preflight_db(&self.client, &self.config.preflight).await
280 }
281
282 pub fn check_conflicts(locations: &[PathBuf], base_branch: &str) -> Result<ConflictReport> {
284 commands::check_conflicts::execute(locations, base_branch)
285 }
286
287 pub async fn safety(&self) -> Result<SafetyCommandReport> {
289 commands::safety::execute_db(&self.client, &self.config).await
290 }
291
292 pub async fn advise(&self) -> Result<AdvisorReport> {
294 commands::advisor::execute_db(&self.client, &self.config).await
295 }
296
297 pub async fn simulate(&self) -> Result<SimulationReport> {
299 commands::simulate::execute_db(&self.client, &self.config).await
300 }
301}