waypoint_core/commands/
diff.rs1use serde::Serialize;
4
5#[cfg(feature = "postgres")]
6use tokio_postgres::Client;
7
8use crate::config::WaypointConfig;
9use crate::db::DbClient;
10use crate::dialect::DialectKind;
11use crate::error::Result;
12use crate::schema::{self, SchemaDiff};
13
14pub enum DiffTarget {
16 Database(String),
18}
19
20#[derive(Debug, Serialize)]
22pub struct DiffReport {
23 pub diffs: Vec<SchemaDiff>,
25 pub generated_sql: String,
27 pub has_changes: bool,
29}
30
31#[cfg(feature = "postgres")]
33#[deprecated(
34 since = "0.6.0",
35 note = "Unused PostgreSQL-only entry point superseded by `execute_db`, which handles both engines. Will be removed in 1.0."
36)]
37pub async fn execute(
38 client: &Client,
39 config: &WaypointConfig,
40 target: DiffTarget,
41) -> Result<DiffReport> {
42 let schema_name = &config.migrations.schema;
43
44 let current = schema::introspect(client, schema_name).await?;
45
46 let target_snapshot = match target {
47 DiffTarget::Database(ref url) => {
48 let target_client = crate::db::connect(url).await?;
49 schema::introspect(&target_client, schema_name).await?
50 }
51 };
52
53 let diffs = schema::diff(¤t, &target_snapshot);
54 let generated_sql = schema::generate_ddl(&diffs);
55 let has_changes = !diffs.is_empty();
56
57 Ok(DiffReport {
58 diffs,
59 generated_sql,
60 has_changes,
61 })
62}
63
64pub async fn execute_db(
71 client: &DbClient,
72 config: &WaypointConfig,
73 target: DiffTarget,
74) -> Result<DiffReport> {
75 let schema_name = client.resolve_schema(&config.migrations.schema).await?;
76
77 let current = schema::introspect_db(client, &schema_name).await?;
78
79 let target_snapshot = match target {
80 DiffTarget::Database(ref url) => {
81 let target_client = crate::db::connect_for_url(url, config).await?;
85 let target_schema = match target_client.dialect_kind() {
93 DialectKind::Mysql => target_client.current_database().await?,
94 DialectKind::Postgres => {
95 target_client
96 .resolve_schema(&config.migrations.schema)
97 .await?
98 }
99 };
100 schema::introspect_db(&target_client, &target_schema).await?
101 }
102 };
103
104 let diffs = schema::diff(¤t, &target_snapshot);
105 let generated_sql = schema::generate_ddl(&diffs);
106 let has_changes = !diffs.is_empty();
107
108 Ok(DiffReport {
109 diffs,
110 generated_sql,
111 has_changes,
112 })
113}