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 =
49 crate::db::connect_with_transport(url, &crate::db::TransportConfig::default())
50 .await?;
51 schema::introspect(&target_client, schema_name).await?
52 }
53 };
54
55 let diffs = schema::diff(¤t, &target_snapshot);
56 let generated_sql = schema::generate_ddl(&diffs);
57 let has_changes = !diffs.is_empty();
58
59 Ok(DiffReport {
60 diffs,
61 generated_sql,
62 has_changes,
63 })
64}
65
66pub async fn execute_db(
73 client: &DbClient,
74 config: &WaypointConfig,
75 target: DiffTarget,
76) -> Result<DiffReport> {
77 let schema_name = client.resolve_schema(&config.migrations.schema).await?;
78
79 let current = schema::introspect_db(client, &schema_name).await?;
80
81 let target_snapshot = match target {
82 DiffTarget::Database(ref url) => {
83 let target_client = crate::db::connect_for_url(url, config).await?;
87 let target_schema = match target_client.dialect_kind() {
95 DialectKind::Mysql => target_client.current_database().await?,
96 DialectKind::Postgres => {
97 target_client
98 .resolve_schema(&config.migrations.schema)
99 .await?
100 }
101 };
102 schema::introspect_db(&target_client, &target_schema).await?
103 }
104 };
105
106 let diffs = schema::diff(¤t, &target_snapshot);
107 let generated_sql = schema::generate_ddl(&diffs);
108 let has_changes = !diffs.is_empty();
109
110 Ok(DiffReport {
111 diffs,
112 generated_sql,
113 has_changes,
114 })
115}