Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/cli/generate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ use serde::Serialize;
use crate::cli::{OutputFormat, ensure_backend_initialized, load_backend, print_json};
use crate::db::diff::breaking::{BreakingChange, MitigationStrategy, analyze_breaking_changes};
use crate::db::diff::diff_namespaces;
use crate::db::migrate::{MigrationPlan, PostgresRenderer, RenderConfig};
use crate::db::migrate::{
MigrationPlan, PostgresRenderer, RenderConfig, compute_inverse_operations,
};
use crate::db::pglite::SchemaLoader;
use crate::db::state::{Migration, StateBackend, StateHash};

Expand Down Expand Up @@ -70,6 +72,8 @@ pub struct GenerateOutput {
/// Breaking changes with details.
#[serde(skip_serializing_if = "Vec::is_empty")]
pub breaking_changes: Vec<BreakingChangeOutput>,
/// Whether the migration can be reversed with `down`.
pub is_reversible: bool,
}

/// Breaking change output for JSON serialization.
Expand Down Expand Up @@ -117,6 +121,11 @@ impl std::fmt::Display for GenerateOutput {
" To state: {}",
&self.target_state_hash[..16.min(self.target_state_hash.len())]
)?;
writeln!(
f,
" Reversible: {}",
if self.is_reversible { "yes" } else { "no" }
)?;

if self.has_breaking_changes {
writeln!(f)?;
Expand All @@ -127,6 +136,12 @@ impl std::fmt::Display for GenerateOutput {
}
}

if !self.is_reversible {
writeln!(f)?;
writeln!(f, "NOTE: This migration contains irreversible operations.")?;
writeln!(f, " Running 'tern down' will not be possible.")?;
}

if self.dry_run {
writeln!(f)?;
writeln!(f, "This was a dry run. No migration was recorded.")?;
Expand Down Expand Up @@ -195,6 +210,7 @@ impl Generate {
target_state_hash: target_hash.to_hex(),
dry_run: self.dry_run,
breaking_changes: vec![],
is_reversible: true, // Empty migration is trivially reversible
};
print_json(&output);
}
Expand Down Expand Up @@ -232,10 +248,15 @@ impl Generate {
// Get the breaking changes for the migration
let breaking_changes = analysis.into_changes();

// Compute inverse operations for the down migration
let inverse_result = compute_inverse_operations(&plan.operations);
let down_operations = inverse_result.operations;

// Create the migration
let migration = Migration::new(
&self.description,
plan.operations.clone(),
down_operations,
source_hash,
target_hash,
breaking_changes.clone(),
Expand All @@ -250,13 +271,14 @@ impl Generate {
let output = GenerateOutput {
migration_id: migration.id.to_hex(),
description: self.description.clone(),
operation_count: migration.operations.len(),
operation_count: migration.up_operations.len(),
has_breaking_changes: !breaking_changes.is_empty(),
has_destructive_changes: destructive_count > 0,
source_state_hash: source_hash.to_hex(),
target_state_hash: target_hash.to_hex(),
dry_run: self.dry_run,
breaking_changes: breaking_change_outputs,
is_reversible: migration.is_reversible(),
};

// Record the migration (unless dry run)
Expand Down Expand Up @@ -308,6 +330,7 @@ mod tests {
operation_count: 3,
has_breaking_changes: false,
has_destructive_changes: false,
is_reversible: true,
source_state_hash: "0".repeat(64),
target_state_hash: "1".repeat(64),
dry_run: false,
Expand All @@ -328,6 +351,7 @@ mod tests {
operation_count: 1,
has_breaking_changes: false,
has_destructive_changes: false,
is_reversible: true,
source_state_hash: "0".repeat(64),
target_state_hash: "1".repeat(64),
dry_run: true,
Expand All @@ -346,6 +370,7 @@ mod tests {
operation_count: 1,
has_breaking_changes: false,
has_destructive_changes: false,
is_reversible: true,
source_state_hash: "src".to_string(),
target_state_hash: "tgt".to_string(),
dry_run: false,
Expand Down
1 change: 1 addition & 0 deletions src/cli/history/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ mod tests {
let m = Migration::new(
"Second migration",
vec![],
vec![],
current_hash,
crate::db::state::StateHash::from_bytes([1u8; 32]),
vec![],
Expand Down
11 changes: 9 additions & 2 deletions src/cli/import/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use serde::Serialize;
use crate::cli::{OutputFormat, ensure_backend_initialized, load_backend, print_json};
use crate::db::diff::breaking::analyze_breaking_changes;
use crate::db::diff::diff_namespaces;
use crate::db::migrate::{MigrationPlan, PostgresRenderer, RenderConfig};
use crate::db::migrate::{
MigrationPlan, PostgresRenderer, RenderConfig, compute_inverse_operations,
};
use crate::db::query::{PostgresCatalog, load_namespace};
use crate::db::state::{Migration, StateBackend, StateHash};
use crate::db::{self};
Expand Down Expand Up @@ -256,9 +258,14 @@ impl Import {
let source_hash = StateHash::from_namespace(&expected_schema);
let target_hash = StateHash::from_namespace(&live_schema);

// Compute inverse operations for the down migration
let inverse_result = compute_inverse_operations(&plan.operations);
let down_operations = inverse_result.operations;

let migration = Migration::new(
&self.description,
plan.operations.clone(),
down_operations,
source_hash,
target_hash,
breaking_changes,
Expand All @@ -280,7 +287,7 @@ impl Import {
has_changes: true,
migration_id,
description: self.description,
operation_count: migration.operations.len(),
operation_count: migration.up_operations.len(),
dry_run: self.dry_run,
summary,
};
Expand Down
6 changes: 3 additions & 3 deletions src/cli/inspect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ fn inspect_json_migration(path: &PathBuf) -> miette::Result<InspectOutput> {
.into_diagnostic()
.wrap_err("Failed to parse migration JSON")?;

// Try to generate SQL from operations
let sql_statements = if !migration.operations.is_empty() {
// Try to generate SQL from up_operations
let sql_statements = if !migration.up_operations.is_empty() {
use crate::db::migrate::{MigrationPlan, PostgresRenderer, RenderConfig};
let plan = MigrationPlan::from_operations(migration.operations.clone());
let plan = MigrationPlan::from_operations(migration.up_operations.clone());
let renderer = PostgresRenderer::new(RenderConfig::default());
let script = plan.render(&renderer);
Some(
Expand Down
Loading