Skip to content

Commit

Permalink
Add plumbing to print auto migration plans
Browse files Browse the repository at this point in the history
  • Loading branch information
kazimuth committed Dec 16, 2024
1 parent 98c9399 commit d66684b
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/cli/src/subcommands/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ pub async fn exec(config: Config, args: &ArgMatches) -> Result<(), anyhow::Error
domain,
database_identity,
op,
update_summary,
} => {
let op = match op {
PublishOp::Created => "Created new",
Expand All @@ -180,6 +181,9 @@ pub async fn exec(config: Config, args: &ArgMatches) -> Result<(), anyhow::Error
} else {
println!("{} database with identity: {}", op, database_identity);
}
if let Some(update_summary) = update_summary {
println!("{}", update_summary);
}
}
PublishResult::TldNotRegistered { domain } => {
return Err(anyhow::anyhow!(
Expand Down
5 changes: 5 additions & 0 deletions crates/client-api-messages/src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ pub enum PublishResult {
/// or not.
database_identity: Identity,
op: PublishOp,

/// If the database was updated, may contain a string describing the update.
/// Contains ANSI escape codes for color.
/// Suitable for printing to the console.
update_summary: Option<String>,
},

// TODO: below variants are obsolete with control db module
Expand Down
10 changes: 7 additions & 3 deletions crates/client-api/src/routes/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ pub async fn publish<S: NodeDelegate + ControlStateDelegate>(
.await
.map_err(log_and_500)?;

if let Some(updated) = maybe_updated {
let update_summary = if let Some(updated) = maybe_updated {
match updated {
UpdateDatabaseResult::AutoMigrateError(errs) => {
return Err((StatusCode::BAD_REQUEST, format!("Database update rejected: {errs}")).into());
Expand All @@ -670,14 +670,18 @@ pub async fn publish<S: NodeDelegate + ControlStateDelegate>(
)
.into());
}
UpdateDatabaseResult::NoUpdateNeeded | UpdateDatabaseResult::UpdatePerformed => {}
UpdateDatabaseResult::NoUpdateNeeded => None,
UpdateDatabaseResult::UpdatePerformed(summary) => Some(summary),
}
}
} else {
None
};

Ok(axum::Json(PublishResult::Success {
domain: db_name.as_ref().map(ToString::to_string),
database_identity,
op,
update_summary,
}))
}

Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/host/host_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ impl HostController {
)
.await?;
match update_result {
UpdateDatabaseResult::NoUpdateNeeded | UpdateDatabaseResult::UpdatePerformed => {
UpdateDatabaseResult::NoUpdateNeeded | UpdateDatabaseResult::UpdatePerformed(_) => {
*guard = Some(host);
}
UpdateDatabaseResult::AutoMigrateError(e) => {
Expand Down
6 changes: 4 additions & 2 deletions crates/core/src/host/module_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,9 @@ pub struct WeakModuleHost {
#[derive(Debug)]
pub enum UpdateDatabaseResult {
NoUpdateNeeded,
UpdatePerformed,
/// The string is a printable summary of the update that happened.
/// Contains ANSI escape sequences for color.
UpdatePerformed(String),
AutoMigrateError(ErrorStream<AutoMigrateError>),
ErrorExecutingMigration(anyhow::Error),
}
Expand All @@ -457,7 +459,7 @@ impl UpdateDatabaseResult {
pub fn was_successful(&self) -> bool {
matches!(
self,
UpdateDatabaseResult::UpdatePerformed | UpdateDatabaseResult::NoUpdateNeeded
UpdateDatabaseResult::UpdatePerformed(_) | UpdateDatabaseResult::NoUpdateNeeded
)
}
}
Expand Down
7 changes: 6 additions & 1 deletion crates/core/src/host/wasm_common/module_host_actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use bytes::Bytes;
use spacetimedb_client_api_messages::timestamp::Timestamp;
use spacetimedb_primitives::TableId;
use spacetimedb_schema::auto_migrate::ponder_migrate;
use spacetimedb_schema::auto_migrate::pretty_print::pretty_print;
use spacetimedb_schema::def::ModuleDef;
use spacetimedb_schema::schema::{Schema, TableSchema};
use std::sync::Arc;
Expand Down Expand Up @@ -340,6 +341,10 @@ impl<T: WasmInstance> ModuleInstance for WasmModuleInstance<T> {
return Ok(UpdateDatabaseResult::AutoMigrateError(errs));
}
};
let summary = pretty_print(&plan).unwrap_or_else(|_| {
log::warn!("Failed to pretty-print migration plan: {plan:#?}");
"(plan not rendered, but succeeded)".to_string()
});
let stdb = &*self.replica_context().relational_db;

let program_hash = program.hash;
Expand All @@ -361,7 +366,7 @@ impl<T: WasmInstance> ModuleInstance for WasmModuleInstance<T> {
stdb.commit_tx(tx)?;
self.system_logger().info("Database updated");
log::info!("Database updated, {}", stdb.database_identity());
Ok(UpdateDatabaseResult::UpdatePerformed)
Ok(UpdateDatabaseResult::UpdatePerformed(summary))
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions crates/schema/src/auto_migrate/pretty_print.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! This module provides a function [`pretty_print`](pretty_print) that renders an automatic migration plan to a string.
use super::{AutoMigratePlan, IndexAlgorithm, TableDef};
use super::{IndexAlgorithm, MigratePlan, TableDef};
use crate::{auto_migrate::AutoMigrateStep, def::ConstraintData};
use colored::{self, ColoredString, Colorize};
use lazy_static::lazy_static;
Expand Down Expand Up @@ -33,7 +33,10 @@ pub fn strip_ansi_escape_codes(s: &str) -> String {

/// Pretty print a migration plan, resulting in a string (containing ANSI escape codes).
/// If you are printing
pub fn pretty_print(plan: &AutoMigratePlan) -> Result<String, fmt::Error> {
pub fn pretty_print(plan: &MigratePlan) -> Result<String, fmt::Error> {
let plan = match plan {
MigratePlan::Auto(plan) => plan,
};
let mut out = String::new();
let outr = &mut out;

Expand Down

0 comments on commit d66684b

Please sign in to comment.