feat(schema-diff): preserve dialect-specific DDL semantics
This commit is contained in:
1 parent
820b8cd26f
commit
002a2c85c0
146 files changed
+36532
-1375
No files matched your search
@@ -0,0 +1,342 @@
|
||||
// ============================================================================
|
||||
// 12.4 — CLI/Tauri/dbx-web API Contract Verification
|
||||
//
|
||||
// These tests verify that public API types maintain backward-compatible
|
||||
// serialization contracts (field names, optional field handling) and
|
||||
// that function signatures haven't changed.
|
||||
// ============================================================================
|
||||
|
||||
use dbx_core::data_compare::{DataComparePreparation, DataCompareSyncPlan};
|
||||
use dbx_core::models::connection::DatabaseType;
|
||||
use dbx_core::schema_diff::{prepare_schema_diff, SchemaDiffPreparation, SchemaDiffPreparationOptions};
|
||||
use dbx_core::sql_risk::{classify_sql_risk, SqlRisk};
|
||||
use dbx_core::types::{ColumnInfo, TableInfo};
|
||||
|
||||
// ============================================================================
|
||||
// Compile-time checks: core function signatures compile
|
||||
// ============================================================================
|
||||
|
||||
/// Verify prepare_schema_diff accepts SchemaDiffPreparationOptions and returns SchemaDiffPreparation
|
||||
#[test]
|
||||
fn prepare_schema_diff_function_signature() {
|
||||
let options = SchemaDiffPreparationOptions {
|
||||
source_tables: vec![TableInfo {
|
||||
name: "t".to_string(),
|
||||
table_type: "TABLE".to_string(),
|
||||
comment: None,
|
||||
parent_schema: None,
|
||||
parent_name: None,
|
||||
}],
|
||||
target_tables: vec![TableInfo {
|
||||
name: "t".to_string(),
|
||||
table_type: "TABLE".to_string(),
|
||||
comment: None,
|
||||
parent_schema: None,
|
||||
parent_name: None,
|
||||
}],
|
||||
source_details: vec![],
|
||||
target_details: vec![],
|
||||
source_functions: vec![],
|
||||
target_functions: vec![],
|
||||
source_sequences: vec![],
|
||||
target_sequences: vec![],
|
||||
source_rules: vec![],
|
||||
target_rules: vec![],
|
||||
source_owners: vec![],
|
||||
target_owners: vec![],
|
||||
database_type: DatabaseType::Postgres,
|
||||
target_schema: None,
|
||||
ignore_comments: false,
|
||||
cascade_delete: false,
|
||||
compare_column_order: false,
|
||||
detect_renames: false,
|
||||
detect_table_renames: false,
|
||||
rename_threshold: 0.5,
|
||||
enable_rollback: false,
|
||||
batch_patterns: vec![],
|
||||
source_dialect: None,
|
||||
target_dialect: None,
|
||||
compatibility_threshold: 0.5,
|
||||
source_permissions: vec![],
|
||||
target_permissions: vec![],
|
||||
shard_strategy: None,
|
||||
resource_constraint: None,
|
||||
field_mappings: vec![],
|
||||
};
|
||||
let _result: SchemaDiffPreparation = prepare_schema_diff(options);
|
||||
}
|
||||
|
||||
/// Verify generate_schema_sync_sql accepts all arg types
|
||||
#[test]
|
||||
fn generate_schema_sync_sql_function_signature() {
|
||||
let _sql = dbx_core::schema_diff::generate_schema_sync_sql(
|
||||
&[],
|
||||
&[],
|
||||
&[],
|
||||
&[],
|
||||
&[],
|
||||
DatabaseType::Postgres,
|
||||
None,
|
||||
false,
|
||||
None,
|
||||
&[],
|
||||
);
|
||||
}
|
||||
|
||||
/// Verify classify_sql_risk function signature
|
||||
#[test]
|
||||
fn classify_sql_risk_function_signature() {
|
||||
let _risk: SqlRisk = classify_sql_risk("SELECT 1", "postgres").unwrap();
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Serialization contract: field naming conventions
|
||||
// ============================================================================
|
||||
|
||||
/// SchemaDiffPreparation fields use camelCase in JSON
|
||||
#[test]
|
||||
fn schema_diff_preparation_field_names() {
|
||||
let result = prepare_schema_diff(SchemaDiffPreparationOptions {
|
||||
source_tables: vec![TableInfo {
|
||||
name: "t".to_string(),
|
||||
table_type: "TABLE".to_string(),
|
||||
comment: None,
|
||||
parent_schema: None,
|
||||
parent_name: None,
|
||||
}],
|
||||
target_tables: vec![TableInfo {
|
||||
name: "t".to_string(),
|
||||
table_type: "TABLE".to_string(),
|
||||
comment: None,
|
||||
parent_schema: None,
|
||||
parent_name: None,
|
||||
}],
|
||||
source_details: vec![],
|
||||
target_details: vec![],
|
||||
source_functions: vec![],
|
||||
target_functions: vec![],
|
||||
source_sequences: vec![],
|
||||
target_sequences: vec![],
|
||||
source_rules: vec![],
|
||||
target_rules: vec![],
|
||||
source_owners: vec![],
|
||||
target_owners: vec![],
|
||||
database_type: DatabaseType::Postgres,
|
||||
target_schema: None,
|
||||
ignore_comments: false,
|
||||
cascade_delete: false,
|
||||
compare_column_order: false,
|
||||
detect_renames: false,
|
||||
detect_table_renames: false,
|
||||
rename_threshold: 0.5,
|
||||
enable_rollback: false,
|
||||
batch_patterns: vec![],
|
||||
source_dialect: None,
|
||||
target_dialect: None,
|
||||
compatibility_threshold: 0.5,
|
||||
source_permissions: vec![],
|
||||
target_permissions: vec![],
|
||||
shard_strategy: None,
|
||||
resource_constraint: None,
|
||||
field_mappings: vec![],
|
||||
});
|
||||
|
||||
let json = serde_json::to_value(&result).unwrap();
|
||||
let obj = json.as_object().unwrap();
|
||||
let keys: Vec<&str> = obj.keys().map(|k| k.as_str()).collect();
|
||||
|
||||
// All keys must be camelCase (no underscores)
|
||||
for key in &keys {
|
||||
assert!(!key.contains('_'), "Key '{}' should be camelCase, not snake_case", key);
|
||||
}
|
||||
|
||||
// Core fields must be present
|
||||
assert!(keys.contains(&"diffs"), "diffs field must be present");
|
||||
assert!(keys.contains(&"syncSql"), "syncSql field must be present");
|
||||
}
|
||||
|
||||
/// DataComparePreparation fields use camelCase
|
||||
#[test]
|
||||
fn data_compare_preparation_field_names() {
|
||||
use dbx_core::data_compare::{DataCompareResult, DataCompareRow};
|
||||
use serde_json::Value;
|
||||
|
||||
let prep = DataComparePreparation {
|
||||
result: DataCompareResult {
|
||||
added: vec![DataCompareRow {
|
||||
key: "1".to_string(),
|
||||
key_values: [("id".to_string(), Value::Number(1.into()))].into(),
|
||||
values: [("name".to_string(), Value::String("Alice".to_string()))].into(),
|
||||
}],
|
||||
removed: vec![],
|
||||
modified: vec![],
|
||||
},
|
||||
sync_statements: vec!["INSERT INTO t VALUES (1)".to_string()],
|
||||
sync_sql: "INSERT INTO t VALUES (1)".to_string(),
|
||||
};
|
||||
|
||||
let json = serde_json::to_value(&prep).unwrap();
|
||||
let obj = json.as_object().unwrap();
|
||||
for key in obj.keys() {
|
||||
assert!(!key.contains('_'), "Key '{}' should be camelCase", key);
|
||||
}
|
||||
assert!(obj.contains_key("result"), "result must be present");
|
||||
}
|
||||
|
||||
/// DataCompareSyncPlan fields use camelCase
|
||||
#[test]
|
||||
fn data_compare_sync_plan_field_names() {
|
||||
let plan = DataCompareSyncPlan {
|
||||
insert_count: 0,
|
||||
update_count: 0,
|
||||
delete_count: 0,
|
||||
statement_count: 0,
|
||||
sync_statements: vec![],
|
||||
sync_sql: String::new(),
|
||||
};
|
||||
|
||||
let json = serde_json::to_value(&plan).unwrap();
|
||||
let obj = json.as_object().unwrap();
|
||||
for key in obj.keys() {
|
||||
assert!(!key.contains('_'), "Key '{}' should be camelCase", key);
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Type serialization roundtrip: types used in API boundaries
|
||||
// ============================================================================
|
||||
|
||||
/// Core types must serialize/deserialize consistently
|
||||
#[test]
|
||||
fn core_types_serialization_roundtrip() {
|
||||
let table = TableInfo {
|
||||
name: "users".to_string(),
|
||||
table_type: "BASE TABLE".to_string(),
|
||||
comment: Some("user table".to_string()),
|
||||
parent_schema: Some("public".to_string()),
|
||||
parent_name: None,
|
||||
};
|
||||
let json = serde_json::to_value(&table).unwrap();
|
||||
let deserialized: TableInfo = serde_json::from_value(json).unwrap();
|
||||
assert_eq!(table.name, deserialized.name);
|
||||
assert_eq!(table.comment, deserialized.comment);
|
||||
}
|
||||
|
||||
/// ColumnInfo must serialize/deserialize consistently
|
||||
#[test]
|
||||
fn column_info_serialization_roundtrip() {
|
||||
let col = ColumnInfo {
|
||||
name: "id".to_string(),
|
||||
data_type: "int".to_string(),
|
||||
is_nullable: false,
|
||||
column_default: None,
|
||||
is_primary_key: true,
|
||||
extra: None,
|
||||
comment: None,
|
||||
numeric_precision: Some(10),
|
||||
numeric_scale: Some(0),
|
||||
character_maximum_length: None,
|
||||
enum_values: None,
|
||||
character_set: None,
|
||||
collation: None,
|
||||
};
|
||||
let json = serde_json::to_value(&col).unwrap();
|
||||
let deserialized: ColumnInfo = serde_json::from_value(json).unwrap();
|
||||
assert_eq!(col.name, deserialized.name);
|
||||
assert_eq!(col.numeric_precision, deserialized.numeric_precision);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// DatabaseType serialization consistency
|
||||
// ============================================================================
|
||||
|
||||
#[test]
|
||||
fn database_type_serialization() {
|
||||
let json = serde_json::to_value(DatabaseType::Postgres).unwrap();
|
||||
assert_eq!(json, "postgres", "DatabaseType serializes using snake_case");
|
||||
|
||||
let json = serde_json::to_value(DatabaseType::Mysql).unwrap();
|
||||
assert_eq!(json, "mysql", "DatabaseType serializes using snake_case");
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Tauri command argument compatibility: Option parameters
|
||||
// ============================================================================
|
||||
|
||||
/// Verify that the types used as Tauri command args serialize/deserialize properly.
|
||||
/// Tauri passes these as JSON from the frontend, so serde roundtrip must work.
|
||||
#[test]
|
||||
fn option_types_work_in_tauri_command_boundary() {
|
||||
// Simulate how Tauri passes optional params from JS frontend
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct TauriLikeCommand {
|
||||
diffs: Vec<dbx_core::schema_diff::TableDiff>,
|
||||
function_diffs: Option<Vec<dbx_core::schema_diff::FunctionDiff>>,
|
||||
cascade_delete: Option<bool>,
|
||||
target_schema: Option<String>,
|
||||
}
|
||||
|
||||
// Without optional params (frontend omitting them)
|
||||
let input = serde_json::json!({
|
||||
"diffs": [],
|
||||
"functionDiffs": null,
|
||||
"cascadeDelete": null,
|
||||
"targetSchema": null
|
||||
});
|
||||
let cmd: TauriLikeCommand = serde_json::from_value(input).unwrap();
|
||||
assert!(cmd.function_diffs.is_none());
|
||||
assert!(cmd.cascade_delete.is_none());
|
||||
|
||||
// With explicit values
|
||||
let input = serde_json::json!({
|
||||
"diffs": [],
|
||||
"cascadeDelete": true
|
||||
});
|
||||
let cmd: TauriLikeCommand = serde_json::from_value(input).unwrap();
|
||||
assert_eq!(cmd.cascade_delete, Some(true));
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Web API request compatibility
|
||||
// ============================================================================
|
||||
|
||||
/// Verify GenerateSchemaSyncSqlRequest contract matches the core function signature
|
||||
#[test]
|
||||
fn web_api_schema_sync_request_fields() {
|
||||
#[derive(serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[allow(dead_code)]
|
||||
struct WebApiRequest {
|
||||
diffs: Vec<dbx_core::schema_diff::TableDiff>,
|
||||
function_diffs: Option<Vec<dbx_core::schema_diff::FunctionDiff>>,
|
||||
sequence_diffs: Option<Vec<dbx_core::schema_diff::SequenceDiff>>,
|
||||
rule_diffs: Option<Vec<dbx_core::schema_diff::RuleDiff>>,
|
||||
owner_diffs: Option<Vec<dbx_core::schema_diff::OwnerDiff>>,
|
||||
database_type: DatabaseType,
|
||||
target_schema: Option<String>,
|
||||
cascade_delete: Option<bool>,
|
||||
}
|
||||
|
||||
let json = serde_json::json!({
|
||||
"diffs": [],
|
||||
"databaseType": "postgres"
|
||||
});
|
||||
let req: WebApiRequest = serde_json::from_value(json).unwrap();
|
||||
assert!(req.diffs.is_empty());
|
||||
assert_eq!(req.database_type, DatabaseType::Postgres);
|
||||
assert!(req.target_schema.is_none());
|
||||
assert!(req.cascade_delete.is_none());
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// SqlRisk enum backward compat: JSON serialization
|
||||
// ============================================================================
|
||||
|
||||
#[test]
|
||||
fn sql_risk_json_representation() {
|
||||
assert_eq!(serde_json::to_value(SqlRisk::ReadOnly).unwrap(), "ReadOnly");
|
||||
assert_eq!(serde_json::to_value(SqlRisk::Write).unwrap(), "Write");
|
||||
assert_eq!(serde_json::to_value(SqlRisk::Ddl).unwrap(), "Ddl");
|
||||
assert_eq!(serde_json::to_value(SqlRisk::Transaction).unwrap(), "Transaction");
|
||||
}
|
||||
@@ -0,0 +1,273 @@
|
||||
use dbx_core::models::connection::DatabaseType;
|
||||
use dbx_core::schema_diff::{prepare_schema_diff, RollbackGraph, SchemaDiffPreparationOptions, TableSchemaDetail};
|
||||
use dbx_core::types::{ColumnInfo, TableInfo};
|
||||
|
||||
fn table(name: &str) -> TableInfo {
|
||||
TableInfo {
|
||||
name: name.to_string(),
|
||||
table_type: "BASE TABLE".to_string(),
|
||||
comment: None,
|
||||
parent_schema: None,
|
||||
parent_name: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn col(name: &str, data_type: &str) -> ColumnInfo {
|
||||
ColumnInfo {
|
||||
name: name.to_string(),
|
||||
data_type: data_type.to_string(),
|
||||
is_nullable: false,
|
||||
column_default: None,
|
||||
is_primary_key: false,
|
||||
extra: None,
|
||||
comment: None,
|
||||
numeric_precision: None,
|
||||
numeric_scale: None,
|
||||
character_maximum_length: None,
|
||||
enum_values: None,
|
||||
character_set: None,
|
||||
collation: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn detail(name: &str, columns: Vec<ColumnInfo>) -> TableSchemaDetail {
|
||||
TableSchemaDetail {
|
||||
name: name.to_string(),
|
||||
columns,
|
||||
indexes: vec![],
|
||||
foreign_keys: vec![],
|
||||
triggers: vec![],
|
||||
ddl: None,
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 12.3 — Bidirectional Diff E2E Tests
|
||||
// ============================================================================
|
||||
|
||||
/// Create scenario: source (desired) has [id, name, email], target (current) has [id, name]
|
||||
/// Forward: ADD column email → Rollback: DROP column email
|
||||
#[test]
|
||||
fn bidirectional_diff_add_rollback_identity() {
|
||||
let options = SchemaDiffPreparationOptions {
|
||||
source_tables: vec![table("users")],
|
||||
target_tables: vec![table("users")],
|
||||
source_details: vec![detail(
|
||||
"users",
|
||||
vec![col("id", "int"), col("name", "varchar(64)"), col("email", "varchar(128)")],
|
||||
)],
|
||||
target_details: vec![detail("users", vec![col("id", "int"), col("name", "varchar(64)")])],
|
||||
database_type: DatabaseType::Postgres,
|
||||
enable_rollback: true,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = prepare_schema_diff(options);
|
||||
|
||||
assert!(!result.diffs.is_empty(), "Should detect new column in source");
|
||||
assert!(result.rollback_sync_sql.is_some(), "Rollback SQL should be generated");
|
||||
|
||||
let forward_sql = &result.sync_sql;
|
||||
let rollback_sql = result.rollback_sync_sql.as_ref().unwrap();
|
||||
|
||||
// Forward should ADD column to target, rollback should DROP column
|
||||
assert!(forward_sql.contains("ADD"), "Forward SQL should contain ADD");
|
||||
assert!(rollback_sql.contains("DROP"), "Rollback SQL should contain DROP");
|
||||
}
|
||||
|
||||
/// Delete scenario: source (desired) has [id], target (current) has [id, name, status]
|
||||
/// Forward: DROP column name, status → Rollback: ADD column name, status
|
||||
#[test]
|
||||
fn bidirectional_diff_delete_rollback_identity() {
|
||||
let options = SchemaDiffPreparationOptions {
|
||||
source_tables: vec![table("users")],
|
||||
target_tables: vec![table("users")],
|
||||
source_details: vec![detail("users", vec![col("id", "int")])],
|
||||
target_details: vec![detail(
|
||||
"users",
|
||||
vec![col("id", "int"), col("name", "varchar(64)"), col("status", "varchar(16)")],
|
||||
)],
|
||||
database_type: DatabaseType::Postgres,
|
||||
enable_rollback: true,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = prepare_schema_diff(options);
|
||||
|
||||
assert!(!result.diffs.is_empty(), "Should detect extra columns in target");
|
||||
assert!(result.rollback_sync_sql.is_some(), "Rollback SQL should be generated");
|
||||
|
||||
let forward_sql = &result.sync_sql;
|
||||
let rollback_sql = result.rollback_sync_sql.as_ref().unwrap();
|
||||
|
||||
// Forward should contain DROP for removed columns
|
||||
assert!(forward_sql.contains("DROP"), "Forward SQL should contain DROP");
|
||||
assert!(!rollback_sql.is_empty(), "Rollback SQL should not be empty");
|
||||
}
|
||||
|
||||
/// Modify scenario: source (desired) has varchar(64), target (current) has varchar(128)
|
||||
/// Forward: ALTER column type → Rollback: ALTER column back
|
||||
#[test]
|
||||
fn bidirectional_diff_modify_rollback_identity() {
|
||||
let options = SchemaDiffPreparationOptions {
|
||||
source_tables: vec![table("users")],
|
||||
target_tables: vec![table("users")],
|
||||
source_details: vec![detail("users", vec![col("name", "varchar(64)")])],
|
||||
target_details: vec![detail("users", vec![col("name", "varchar(128)")])],
|
||||
database_type: DatabaseType::Postgres,
|
||||
enable_rollback: true,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = prepare_schema_diff(options);
|
||||
|
||||
assert!(!result.diffs.is_empty(), "Should detect modified column");
|
||||
assert_eq!(result.diffs[0].diff_type, "modified");
|
||||
|
||||
// Should have rollback SQL
|
||||
assert!(result.rollback_sync_sql.is_some(), "Rollback SQL should be generated");
|
||||
}
|
||||
|
||||
/// RollbackGraph consistency: forward ∘ rollback = identity
|
||||
#[test]
|
||||
fn rollback_graph_consistency() {
|
||||
let options = SchemaDiffPreparationOptions {
|
||||
source_tables: vec![table("users")],
|
||||
target_tables: vec![table("users")],
|
||||
source_details: vec![detail("users", vec![col("id", "int"), col("name", "varchar(64)")])],
|
||||
target_details: vec![detail(
|
||||
"users",
|
||||
vec![col("id", "int"), col("name", "varchar(64)"), col("email", "varchar(128)")],
|
||||
)],
|
||||
database_type: DatabaseType::Postgres,
|
||||
enable_rollback: true,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = prepare_schema_diff(options);
|
||||
|
||||
assert!(result.rollback_graph.is_some(), "RollbackGraph should be generated");
|
||||
let graph = result.rollback_graph.as_ref().unwrap();
|
||||
assert!(graph.is_consistent, "RollbackGraph should be consistent");
|
||||
assert!(graph.consistency_issues.is_empty(), "RollbackGraph should have no consistency issues");
|
||||
}
|
||||
|
||||
/// RollbackGraph forward + rollback roundtrip: verify rollback_nodes are generated
|
||||
#[test]
|
||||
fn rollback_graph_generates_rollback_nodes() {
|
||||
let options = SchemaDiffPreparationOptions {
|
||||
source_tables: vec![table("users")],
|
||||
target_tables: vec![table("users")],
|
||||
source_details: vec![detail("users", vec![col("id", "int"), col("status", "varchar(16)")])],
|
||||
target_details: vec![detail("users", vec![col("id", "int"), col("status", "varchar(32)")])],
|
||||
database_type: DatabaseType::Postgres,
|
||||
enable_rollback: true,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = prepare_schema_diff(options);
|
||||
assert!(result.rollback_graph.is_some());
|
||||
|
||||
let graph = result.rollback_graph.as_ref().unwrap();
|
||||
assert!(!graph.rollback_nodes.is_empty(), "rollback_nodes should be non-empty");
|
||||
assert!(graph.is_consistent, "RollbackGraph should be consistent");
|
||||
}
|
||||
|
||||
/// Multiple table E2E: modify one table, add/remove columns in others
|
||||
#[test]
|
||||
fn bidirectional_diff_multi_table_e2e() {
|
||||
let options = SchemaDiffPreparationOptions {
|
||||
source_tables: vec![table("orders"), table("items"), table("archive")],
|
||||
target_tables: vec![table("orders"), table("items"), table("archive")],
|
||||
source_details: vec![
|
||||
// desired: orders with larger decimal + status column added
|
||||
detail("orders", vec![col("id", "int"), col("total", "decimal(12,2)"), col("status", "varchar(16)")]),
|
||||
// desired: items with larger name
|
||||
detail("items", vec![col("id", "int"), col("name", "varchar(128)")]),
|
||||
// desired: archive with only id
|
||||
detail("archive", vec![col("id", "int")]),
|
||||
],
|
||||
target_details: vec![
|
||||
// current: orders with smaller decimal, no status
|
||||
detail("orders", vec![col("id", "int"), col("total", "decimal(10,2)")]),
|
||||
// current: items with smaller name
|
||||
detail("items", vec![col("id", "int"), col("name", "varchar(64)")]),
|
||||
// current: archive with extra column
|
||||
detail("archive", vec![col("id", "int"), col("data", "text")]),
|
||||
],
|
||||
database_type: DatabaseType::Postgres,
|
||||
enable_rollback: true,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = prepare_schema_diff(options);
|
||||
|
||||
assert!(!result.diffs.is_empty(), "Should detect differences across multiple tables");
|
||||
assert!(result.rollback_sync_sql.is_some(), "Rollback SQL should be generated");
|
||||
|
||||
let forward_sql = &result.sync_sql;
|
||||
let rollback_sql = result.rollback_sync_sql.as_ref().unwrap();
|
||||
|
||||
assert!(!forward_sql.is_empty(), "Forward SQL should be non-empty");
|
||||
assert!(!rollback_sql.is_empty(), "Rollback SQL should be non-empty");
|
||||
|
||||
// Forward and rollback should be different
|
||||
assert_ne!(forward_sql, rollback_sql, "Forward and rollback SQL should differ");
|
||||
}
|
||||
|
||||
/// Rename detection: verify rename candidates are populated with matching column structures
|
||||
#[test]
|
||||
fn bidirectional_diff_with_rename_detection() {
|
||||
let options = SchemaDiffPreparationOptions {
|
||||
source_tables: vec![table("users")],
|
||||
target_tables: vec![table("user_profiles")],
|
||||
source_details: vec![detail("users", vec![col("id", "int"), col("name", "varchar(64)")])],
|
||||
target_details: vec![detail("user_profiles", vec![col("id", "int"), col("name", "varchar(64)")])],
|
||||
database_type: DatabaseType::Postgres,
|
||||
detect_renames: true,
|
||||
detect_table_renames: true,
|
||||
rename_threshold: 0.5,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = prepare_schema_diff(options);
|
||||
|
||||
// With identical column structure, rename should at least produce diffs
|
||||
assert!(!result.diffs.is_empty(), "Should detect difference between differently-named tables");
|
||||
|
||||
// With identical column structures and table rename detection on, candidates must be populated
|
||||
assert!(
|
||||
!result.rename_candidates.is_empty(),
|
||||
"Rename candidates should be detected for identical column structures"
|
||||
);
|
||||
assert!(result.rename_candidates[0].score > 0.0, "Rename score should be positive");
|
||||
}
|
||||
|
||||
/// RollbackGraph validate_consistency directly
|
||||
#[test]
|
||||
fn rollback_graph_direct_consistency_check() {
|
||||
use dbx_core::schema_diff::DependencyGraph;
|
||||
|
||||
let forward_diff = dbx_core::schema_diff::TableDiff {
|
||||
diff_type: "added".to_string(),
|
||||
object_type: None,
|
||||
name: "users".to_string(),
|
||||
columns: Some(vec![]),
|
||||
indexes: None,
|
||||
foreign_keys: None,
|
||||
triggers: None,
|
||||
ddl: Some("CREATE TABLE users (id INT)".to_string()),
|
||||
target_ddl: None,
|
||||
source_table_comment: None,
|
||||
target_table_comment: None,
|
||||
sync_sql: None,
|
||||
};
|
||||
|
||||
let dep_graph =
|
||||
DependencyGraph { nodes: std::collections::HashMap::new(), topological_order: vec!["users".to_string()] };
|
||||
|
||||
let mut graph = RollbackGraph::from_forward_diffs(&[forward_diff], &[], &dep_graph);
|
||||
graph.validate_consistency();
|
||||
assert!(graph.is_consistent, "Simple add should produce consistent rollback graph");
|
||||
assert!(graph.consistency_issues.is_empty());
|
||||
}
|
||||
@@ -0,0 +1,248 @@
|
||||
use dbx_core::models::connection::DatabaseType;
|
||||
use dbx_core::schema_diff::{
|
||||
generate_schema_sync_sql, prepare_schema_diff, SchemaDiffPreparationOptions, TableSchemaDetail,
|
||||
};
|
||||
use dbx_core::sql_dialect::descriptor::DialectKind;
|
||||
use dbx_core::types::{ColumnInfo, TableInfo};
|
||||
|
||||
fn table(name: &str) -> TableInfo {
|
||||
TableInfo {
|
||||
name: name.to_string(),
|
||||
table_type: "BASE TABLE".to_string(),
|
||||
comment: None,
|
||||
parent_schema: None,
|
||||
parent_name: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn col(name: &str, data_type: &str) -> ColumnInfo {
|
||||
ColumnInfo {
|
||||
name: name.to_string(),
|
||||
data_type: data_type.to_string(),
|
||||
is_nullable: false,
|
||||
column_default: None,
|
||||
is_primary_key: false,
|
||||
extra: None,
|
||||
comment: None,
|
||||
numeric_precision: None,
|
||||
numeric_scale: None,
|
||||
character_maximum_length: None,
|
||||
enum_values: None,
|
||||
character_set: None,
|
||||
collation: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn detail(name: &str, columns: Vec<ColumnInfo>) -> TableSchemaDetail {
|
||||
TableSchemaDetail {
|
||||
name: name.to_string(),
|
||||
columns,
|
||||
indexes: vec![],
|
||||
foreign_keys: vec![],
|
||||
triggers: vec![],
|
||||
ddl: None,
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 12.2 — Cross-dialect full-chain integration tests
|
||||
// ============================================================================
|
||||
|
||||
/// MySQL → PostgreSQL: diff a schema between MySQL source and PG target,
|
||||
/// verify SQL output uses PostgreSQL syntax and identifiers.
|
||||
#[test]
|
||||
fn mysql_to_postgresql_full_chain_diff_and_sql_generation() {
|
||||
let options = SchemaDiffPreparationOptions {
|
||||
source_tables: vec![table("users")],
|
||||
target_tables: vec![table("users")],
|
||||
source_details: vec![detail(
|
||||
"users",
|
||||
vec![col("id", "int"), col("name", "varchar(64)"), col("email", "varchar(128)")],
|
||||
)],
|
||||
target_details: vec![detail(
|
||||
"users",
|
||||
vec![col("id", "serial"), col("name", "text"), col("email", "varchar(128)"), col("age", "int")],
|
||||
)],
|
||||
database_type: DatabaseType::Postgres,
|
||||
source_dialect: Some(DialectKind::Mysql),
|
||||
target_dialect: Some(DialectKind::Postgres),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = prepare_schema_diff(options);
|
||||
|
||||
// Should detect column type changes and an extra column in target
|
||||
assert!(!result.diffs.is_empty(), "Should detect differences between MySQL and PG schemas");
|
||||
|
||||
// SQL should use PostgreSQL double-quote identifiers
|
||||
let pg_sql = &result.sync_sql;
|
||||
assert!(pg_sql.contains('"'), "PostgreSQL SQL should use double-quoted identifiers");
|
||||
assert!(!pg_sql.contains('`'), "PostgreSQL SQL should NOT use backtick identifiers");
|
||||
|
||||
// Verify compatibility warnings if type differences exist
|
||||
// varchar(64) → text may or may not generate a warning depending on threshold
|
||||
}
|
||||
|
||||
/// PostgreSQL → SQLite: diff a schema between PG source and SQLite target,
|
||||
/// verify SQL output uses SQLite-compatible syntax.
|
||||
#[test]
|
||||
fn postgresql_to_sqlite_full_chain_diff_and_sql_generation() {
|
||||
let options = SchemaDiffPreparationOptions {
|
||||
source_tables: vec![table("products")],
|
||||
target_tables: vec![table("products")],
|
||||
source_details: vec![detail(
|
||||
"products",
|
||||
vec![col("id", "serial"), col("name", "varchar(100)"), col("price", "numeric(10,2)")],
|
||||
)],
|
||||
target_details: vec![detail(
|
||||
"products",
|
||||
vec![col("id", "integer"), col("name", "text"), col("price", "real"), col("category", "text")],
|
||||
)],
|
||||
database_type: DatabaseType::Sqlite,
|
||||
source_dialect: Some(DialectKind::Postgres),
|
||||
target_dialect: Some(DialectKind::Sqlite),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = prepare_schema_diff(options);
|
||||
|
||||
assert!(!result.diffs.is_empty(), "Should detect differences between PG and SQLite schemas");
|
||||
|
||||
// SQLite identifiers are double-quoted or unquoted
|
||||
let sqlite_sql = &result.sync_sql;
|
||||
// SQLite uses CREATE TABLE IF NOT EXISTS etc.
|
||||
assert!(!sqlite_sql.contains("::"), "SQLite SQL should not contain PG type casts");
|
||||
}
|
||||
|
||||
/// MySQL → SQLite: full chain cross-dialect comparison
|
||||
#[test]
|
||||
fn mysql_to_sqlite_cross_dialect_diff() {
|
||||
let options = SchemaDiffPreparationOptions {
|
||||
source_tables: vec![table("events")],
|
||||
target_tables: vec![table("events")],
|
||||
source_details: vec![detail(
|
||||
"events",
|
||||
vec![
|
||||
col("id", "bigint unsigned"),
|
||||
col("event_type", "enum('click','view')"),
|
||||
col("created_at", "datetime"),
|
||||
],
|
||||
)],
|
||||
target_details: vec![detail(
|
||||
"events",
|
||||
vec![col("id", "integer"), col("event_type", "text"), col("created_at", "text"), col("source", "text")],
|
||||
)],
|
||||
database_type: DatabaseType::Sqlite,
|
||||
source_dialect: Some(DialectKind::Mysql),
|
||||
target_dialect: Some(DialectKind::Sqlite),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = prepare_schema_diff(options);
|
||||
assert!(!result.diffs.is_empty(), "MySQL enum → SQLite text should be detected as a difference");
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 12.2 — Same-dialect consistency: diff should be empty when schemas match
|
||||
// ============================================================================
|
||||
|
||||
#[test]
|
||||
fn identical_mysql_schemas_produce_no_diff() {
|
||||
let options = SchemaDiffPreparationOptions {
|
||||
source_tables: vec![table("users")],
|
||||
target_tables: vec![table("users")],
|
||||
source_details: vec![detail("users", vec![col("id", "int"), col("name", "varchar(64)")])],
|
||||
target_details: vec![detail("users", vec![col("id", "int"), col("name", "varchar(64)")])],
|
||||
database_type: DatabaseType::Mysql,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = prepare_schema_diff(options);
|
||||
assert!(result.diffs.is_empty(), "Identical schemas should produce no diffs");
|
||||
assert!(result.sync_sql.is_empty(), "Identical schemas should produce no SQL");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn identical_postgres_schemas_produce_no_diff() {
|
||||
let options = SchemaDiffPreparationOptions {
|
||||
source_tables: vec![table("users")],
|
||||
target_tables: vec![table("users")],
|
||||
source_details: vec![detail("users", vec![col("id", "int"), col("name", "text")])],
|
||||
target_details: vec![detail("users", vec![col("id", "int"), col("name", "text")])],
|
||||
database_type: DatabaseType::Postgres,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = prepare_schema_diff(options);
|
||||
assert!(result.diffs.is_empty(), "Identical schemas should produce no diffs");
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 12.2 — Cross-dialect generate_schema_sync_sql verification
|
||||
// ============================================================================
|
||||
|
||||
#[test]
|
||||
fn generate_schema_sync_sql_mysql_output_format() {
|
||||
let diffs = vec![dbx_core::schema_diff::TableDiff {
|
||||
diff_type: "added".to_string(),
|
||||
object_type: None,
|
||||
name: "users".to_string(),
|
||||
columns: Some(vec![]),
|
||||
indexes: None,
|
||||
foreign_keys: None,
|
||||
triggers: None,
|
||||
ddl: Some("CREATE TABLE `users` (`id` INT NOT NULL, `name` VARCHAR(64) NOT NULL)".to_string()),
|
||||
target_ddl: None,
|
||||
source_table_comment: None,
|
||||
target_table_comment: None,
|
||||
sync_sql: None,
|
||||
}];
|
||||
|
||||
let sql = generate_schema_sync_sql(&diffs, &[], &[], &[], &[], DatabaseType::Mysql, None, false, None, &[]);
|
||||
|
||||
assert!(sql.contains("CREATE TABLE"), "MySQL SQL should contain CREATE TABLE");
|
||||
assert!(sql.contains('`'), "MySQL SQL should use backtick identifiers");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generate_schema_sync_sql_postgres_output_format() {
|
||||
let diffs = vec![dbx_core::schema_diff::TableDiff {
|
||||
diff_type: "added".to_string(),
|
||||
object_type: None,
|
||||
name: "users".to_string(),
|
||||
columns: Some(vec![]),
|
||||
indexes: None,
|
||||
foreign_keys: None,
|
||||
triggers: None,
|
||||
ddl: Some("CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT NOT NULL)".to_string()),
|
||||
target_ddl: None,
|
||||
source_table_comment: None,
|
||||
target_table_comment: None,
|
||||
sync_sql: None,
|
||||
}];
|
||||
|
||||
let sql = generate_schema_sync_sql(&diffs, &[], &[], &[], &[], DatabaseType::Postgres, None, false, None, &[]);
|
||||
assert!(sql.contains("CREATE TABLE"), "PostgreSQL SQL should contain CREATE TABLE");
|
||||
assert!(sql.contains("SERIAL"), "PostgreSQL SQL should use SERIAL type");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generate_schema_sync_sql_sqlite_output_format() {
|
||||
let diffs = vec![dbx_core::schema_diff::TableDiff {
|
||||
diff_type: "added".to_string(),
|
||||
object_type: None,
|
||||
name: "users".to_string(),
|
||||
columns: Some(vec![]),
|
||||
indexes: None,
|
||||
foreign_keys: None,
|
||||
triggers: None,
|
||||
ddl: Some("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL)".to_string()),
|
||||
target_ddl: None,
|
||||
source_table_comment: None,
|
||||
target_table_comment: None,
|
||||
sync_sql: None,
|
||||
}];
|
||||
|
||||
let sql = generate_schema_sync_sql(&diffs, &[], &[], &[], &[], DatabaseType::Sqlite, None, false, None, &[]);
|
||||
assert!(sql.contains("CREATE TABLE"), "SQLite SQL should contain CREATE TABLE");
|
||||
}
|
||||
@@ -0,0 +1,274 @@
|
||||
// ============================================================================
|
||||
// 12.5 — Performance Benchmarks
|
||||
//
|
||||
// These benchmarks measure schema diff performance with large table sets.
|
||||
// Run with: cargo test --release --test performance_benchmarks -- --ignored
|
||||
// ============================================================================
|
||||
|
||||
use std::time::Instant;
|
||||
|
||||
use dbx_core::models::connection::DatabaseType;
|
||||
use dbx_core::schema_diff::{
|
||||
prepare_schema_diff, SchemaDiffPreparationOptions, ShardBy, ShardStrategy, TableSchemaDetail,
|
||||
};
|
||||
use dbx_core::types::{ColumnInfo, TableInfo};
|
||||
|
||||
fn generate_tables(count: usize, prefix: &str) -> Vec<TableInfo> {
|
||||
(0..count)
|
||||
.map(|i| TableInfo {
|
||||
name: format!("{}_{}", prefix, i),
|
||||
table_type: "BASE TABLE".to_string(),
|
||||
comment: None,
|
||||
parent_schema: None,
|
||||
parent_name: None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn generate_details(tables: &[TableInfo], columns_per_table: usize) -> Vec<TableSchemaDetail> {
|
||||
tables
|
||||
.iter()
|
||||
.map(|t| TableSchemaDetail {
|
||||
name: t.name.clone(),
|
||||
columns: (0..columns_per_table)
|
||||
.map(|j| ColumnInfo {
|
||||
name: format!("col_{}", j),
|
||||
data_type: if j % 3 == 0 { "int".to_string() } else { "varchar(64)".to_string() },
|
||||
is_nullable: j % 2 == 0,
|
||||
column_default: if j == 0 { Some("0".to_string()) } else { None },
|
||||
is_primary_key: j == 0,
|
||||
extra: None,
|
||||
comment: None,
|
||||
numeric_precision: if j % 3 == 0 { Some(10) } else { None },
|
||||
numeric_scale: if j % 3 == 0 { Some(0) } else { None },
|
||||
character_maximum_length: if j % 3 != 0 { Some(64) } else { None },
|
||||
enum_values: None,
|
||||
character_set: None,
|
||||
collation: None,
|
||||
})
|
||||
.collect(),
|
||||
indexes: vec![],
|
||||
foreign_keys: vec![],
|
||||
triggers: vec![],
|
||||
ddl: None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Benchmark: diff a small number of tables (baseline)
|
||||
#[ignore]
|
||||
#[test]
|
||||
fn benchmark_small_schema_diff() {
|
||||
let table_count = 10;
|
||||
let columns_per_table = 5;
|
||||
let source_tables = generate_tables(table_count, "src");
|
||||
let target_tables = generate_tables(table_count, "tgt");
|
||||
let source_details = generate_details(&source_tables, columns_per_table);
|
||||
let target_details = generate_details(&target_tables, columns_per_table);
|
||||
|
||||
let options = SchemaDiffPreparationOptions {
|
||||
source_tables,
|
||||
target_tables,
|
||||
source_details,
|
||||
target_details,
|
||||
database_type: DatabaseType::Postgres,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let start = Instant::now();
|
||||
let result = prepare_schema_diff(options);
|
||||
let elapsed = start.elapsed();
|
||||
|
||||
assert!(!result.diffs.is_empty(), "Should detect differences");
|
||||
eprintln!("[BENCHMARK] small_schema_diff ({} tables, {} cols): {:?}", table_count, columns_per_table, elapsed);
|
||||
}
|
||||
|
||||
/// Benchmark: diff a medium number of tables
|
||||
#[ignore]
|
||||
#[test]
|
||||
fn benchmark_medium_schema_diff() {
|
||||
let table_count = 100;
|
||||
let columns_per_table = 10;
|
||||
let source_tables = generate_tables(table_count, "src");
|
||||
let target_tables = generate_tables(table_count, "tgt");
|
||||
let source_details = generate_details(&source_tables, columns_per_table);
|
||||
let target_details = generate_details(&target_tables, columns_per_table);
|
||||
|
||||
let options = SchemaDiffPreparationOptions {
|
||||
source_tables,
|
||||
target_tables,
|
||||
source_details,
|
||||
target_details,
|
||||
database_type: DatabaseType::Postgres,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let start = Instant::now();
|
||||
let result = prepare_schema_diff(options);
|
||||
let elapsed = start.elapsed();
|
||||
|
||||
assert!(!result.diffs.is_empty(), "Should detect differences");
|
||||
eprintln!("[BENCHMARK] medium_schema_diff ({} tables, {} cols): {:?}", table_count, columns_per_table, elapsed);
|
||||
}
|
||||
|
||||
/// Benchmark: diff a large number of tables
|
||||
#[ignore]
|
||||
#[test]
|
||||
fn benchmark_large_schema_diff() {
|
||||
let table_count = 200;
|
||||
let columns_per_table = 10;
|
||||
let source_tables = generate_tables(table_count, "src");
|
||||
let target_tables = generate_tables(table_count, "tgt");
|
||||
let source_details = generate_details(&source_tables, columns_per_table);
|
||||
let target_details = generate_details(&target_tables, columns_per_table);
|
||||
|
||||
let options = SchemaDiffPreparationOptions {
|
||||
source_tables,
|
||||
target_tables,
|
||||
source_details,
|
||||
target_details,
|
||||
database_type: DatabaseType::Postgres,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let start = Instant::now();
|
||||
let result = prepare_schema_diff(options);
|
||||
let elapsed = start.elapsed();
|
||||
|
||||
assert!(!result.diffs.is_empty(), "Should detect differences");
|
||||
eprintln!("[BENCHMARK] large_schema_diff ({} tables, {} cols): {:?}", table_count, columns_per_table, elapsed);
|
||||
}
|
||||
|
||||
/// Benchmark: diff 1000 tables (simulates the 1000-table requirement)
|
||||
#[ignore]
|
||||
#[test]
|
||||
fn benchmark_thousand_table_schema_diff() {
|
||||
let table_count = 1000;
|
||||
let columns_per_table = 5;
|
||||
let source_tables = generate_tables(table_count, "src");
|
||||
let target_tables = generate_tables(table_count, "tgt");
|
||||
let source_details = generate_details(&source_tables, columns_per_table);
|
||||
let target_details = generate_details(&target_tables, columns_per_table);
|
||||
|
||||
let options = SchemaDiffPreparationOptions {
|
||||
source_tables,
|
||||
target_tables,
|
||||
source_details,
|
||||
target_details,
|
||||
database_type: DatabaseType::Postgres,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let start = Instant::now();
|
||||
let result = prepare_schema_diff(options);
|
||||
let elapsed = start.elapsed();
|
||||
|
||||
assert!(!result.diffs.is_empty(), "Should detect differences");
|
||||
eprintln!(
|
||||
"[BENCHMARK] thousand_table_schema_diff ({} tables, {} cols): {:?}",
|
||||
table_count, columns_per_table, elapsed
|
||||
);
|
||||
}
|
||||
|
||||
/// Benchmark: shard-parallel speedup comparison
|
||||
#[ignore]
|
||||
#[test]
|
||||
fn benchmark_shard_parallel_speedup() {
|
||||
let table_count = 200;
|
||||
let columns_per_table = 8;
|
||||
let source_tables = generate_tables(table_count, "src");
|
||||
let target_tables = generate_tables(table_count, "tgt");
|
||||
let source_details = generate_details(&source_tables, columns_per_table);
|
||||
let target_details = generate_details(&target_tables, columns_per_table);
|
||||
|
||||
// Single-threaded (shard_count = 1)
|
||||
let options_single = SchemaDiffPreparationOptions {
|
||||
source_tables: source_tables.clone(),
|
||||
target_tables: target_tables.clone(),
|
||||
source_details: source_details.clone(),
|
||||
target_details: target_details.clone(),
|
||||
database_type: DatabaseType::Postgres,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let start_single = Instant::now();
|
||||
let _result_single = prepare_schema_diff(options_single);
|
||||
let single_time = start_single.elapsed();
|
||||
|
||||
// Shard-parallel (shard_count = 4)
|
||||
let shard_options = SchemaDiffPreparationOptions {
|
||||
source_tables: source_tables.clone(),
|
||||
target_tables: target_tables.clone(),
|
||||
source_details: source_details.clone(),
|
||||
target_details: target_details.clone(),
|
||||
database_type: DatabaseType::Postgres,
|
||||
shard_strategy: Some(ShardStrategy { shard_count: 4, shard_by: ShardBy::RoundRobin }),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let start_parallel = Instant::now();
|
||||
let _result_parallel = prepare_schema_diff(shard_options);
|
||||
let parallel_time = start_parallel.elapsed();
|
||||
|
||||
eprintln!(
|
||||
"[BENCHMARK] shard_speedup: single={:?}, parallel(4 shards)={:?}, speedup={:.2}x",
|
||||
single_time,
|
||||
parallel_time,
|
||||
single_time.as_secs_f64() / parallel_time.as_secs_f64().max(0.0001)
|
||||
);
|
||||
|
||||
assert!(
|
||||
parallel_time <= single_time * 3, // Allow some overhead; shard_count=4 may not be 4x faster on small data
|
||||
"Parallel should not be significantly slower than single-threaded"
|
||||
);
|
||||
}
|
||||
|
||||
/// Benchmark: DependencyGraph build time
|
||||
#[ignore]
|
||||
#[test]
|
||||
fn benchmark_dependency_graph_build() {
|
||||
use dbx_core::schema_diff::DependencyGraph;
|
||||
use dbx_core::types::ForeignKeyInfo;
|
||||
|
||||
let details: Vec<TableSchemaDetail> = (0..100)
|
||||
.map(|i| {
|
||||
let mut fks = vec![];
|
||||
if i > 0 {
|
||||
fks.push(ForeignKeyInfo {
|
||||
name: format!("fk_{}_to_{}", i, i - 1),
|
||||
column: "parent_id".to_string(),
|
||||
ref_schema: None,
|
||||
ref_table: format!("tgt_{}", i - 1),
|
||||
ref_column: "id".to_string(),
|
||||
on_update: None,
|
||||
on_delete: None,
|
||||
});
|
||||
}
|
||||
TableSchemaDetail {
|
||||
name: format!("tgt_{}", i),
|
||||
columns: vec![],
|
||||
indexes: vec![],
|
||||
foreign_keys: fks,
|
||||
triggers: vec![],
|
||||
ddl: None,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
let tables: Vec<TableInfo> = (0..100)
|
||||
.map(|i| TableInfo {
|
||||
name: format!("tgt_{}", i),
|
||||
table_type: "BASE TABLE".to_string(),
|
||||
comment: None,
|
||||
parent_schema: None,
|
||||
parent_name: None,
|
||||
})
|
||||
.collect();
|
||||
|
||||
let start = Instant::now();
|
||||
let graph = DependencyGraph::build(&details, &tables);
|
||||
let elapsed = start.elapsed();
|
||||
|
||||
assert_eq!(graph.nodes.len(), 100);
|
||||
eprintln!("[BENCHMARK] dependency_graph_build (100 tables): {:?}", elapsed);
|
||||
}
|
||||
@@ -0,0 +1,297 @@
|
||||
use dbx_core::data_compare::{DataCompareFromTablesPreparation, DataComparePreparation, DataCompareResult};
|
||||
use dbx_core::models::connection::DatabaseType;
|
||||
use dbx_core::schema_diff::{
|
||||
generate_schema_sync_sql, prepare_schema_diff, SchemaDiffPreparation, SchemaDiffPreparationOptions, TableDiff,
|
||||
TableSchemaDetail,
|
||||
};
|
||||
use dbx_core::sql_risk::{classify_sql_risk, SqlRisk};
|
||||
use dbx_core::types::{ColumnInfo, TableInfo};
|
||||
|
||||
// ============================================================================
|
||||
// 12.1 — SchemaDiffPreparation backward-compatibility regression tests
|
||||
// ============================================================================
|
||||
|
||||
fn basic_table_info(name: &str) -> TableInfo {
|
||||
TableInfo {
|
||||
name: name.to_string(),
|
||||
table_type: "BASE TABLE".to_string(),
|
||||
comment: None,
|
||||
parent_schema: None,
|
||||
parent_name: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn col(name: &str, data_type: &str) -> ColumnInfo {
|
||||
ColumnInfo {
|
||||
name: name.to_string(),
|
||||
data_type: data_type.to_string(),
|
||||
is_nullable: false,
|
||||
column_default: None,
|
||||
is_primary_key: false,
|
||||
extra: None,
|
||||
comment: None,
|
||||
numeric_precision: None,
|
||||
numeric_scale: None,
|
||||
character_maximum_length: None,
|
||||
enum_values: None,
|
||||
character_set: None,
|
||||
collation: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn detail(name: &str, columns: Vec<ColumnInfo>) -> TableSchemaDetail {
|
||||
TableSchemaDetail {
|
||||
name: name.to_string(),
|
||||
columns,
|
||||
indexes: vec![],
|
||||
foreign_keys: vec![],
|
||||
triggers: vec![],
|
||||
ddl: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Verify that new optional fields in SchemaDiffPreparation are well-formed
|
||||
/// when using basic (non-enhanced) options, ensuring backward compatibility
|
||||
/// for existing callers that only use the original fields.
|
||||
#[test]
|
||||
fn schema_diff_preparation_new_fields_defaults_backward_compatible() {
|
||||
let options = SchemaDiffPreparationOptions {
|
||||
source_tables: vec![basic_table_info("users")],
|
||||
target_tables: vec![basic_table_info("users")],
|
||||
source_details: vec![detail("users", vec![col("name", "varchar(64)")])],
|
||||
target_details: vec![detail("users", vec![col("name", "varchar(128)")])],
|
||||
database_type: DatabaseType::Postgres,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = prepare_schema_diff(options);
|
||||
|
||||
// Original required fields must be present
|
||||
assert!(!result.diffs.is_empty(), "diffs should be non-empty");
|
||||
assert!(!result.sync_sql.is_empty(), "sync_sql should be non-empty");
|
||||
|
||||
// New optional fields from Phase 4+ must have sensible defaults
|
||||
assert!(result.rollback_sync_sql.is_none(), "rollback_sync_sql should default to None");
|
||||
assert!(result.rename_candidates.is_empty(), "rename_candidates should default to empty");
|
||||
assert!(result.rollback_graph.is_none(), "rollback_graph should default to None");
|
||||
assert!(result.compatibility_warnings.is_empty(), "compatibility_warnings should default to empty");
|
||||
assert!(result.permission_diffs.is_empty(), "permission_diffs should default to empty");
|
||||
assert!(result.permission_sync_sql.is_none(), "permission_sync_sql should default to None");
|
||||
|
||||
// dependency_graph is always built by prepare_schema_diff regardless of options
|
||||
// (it's used internally for topological ordering)
|
||||
assert!(result.dependency_graph.is_some(), "dependency_graph should be present (always built for ordering)");
|
||||
}
|
||||
|
||||
/// Verify that SchemaDiffPreparation serializes to JSON with backward-compatible
|
||||
/// field names (camelCase) and optional fields skipped when None/empty.
|
||||
#[test]
|
||||
fn schema_diff_preparation_json_serialization_skips_optional_fields() {
|
||||
let options = SchemaDiffPreparationOptions {
|
||||
source_tables: vec![basic_table_info("users")],
|
||||
target_tables: vec![basic_table_info("users")],
|
||||
source_details: vec![detail("users", vec![col("name", "varchar(64)")])],
|
||||
target_details: vec![detail("users", vec![col("name", "varchar(128)")])],
|
||||
database_type: DatabaseType::Postgres,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = prepare_schema_diff(options);
|
||||
let json = serde_json::to_value(&result).expect("Serialization should succeed");
|
||||
|
||||
// Original fields must be present
|
||||
assert!(json.get("diffs").is_some(), "diffs must be present");
|
||||
assert!(json.get("syncSql").is_some(), "syncSql must be present");
|
||||
|
||||
// New optional fields should NOT appear when None/empty
|
||||
assert!(json.get("rollbackSyncSql").is_none(), "rollbackSyncSql should be absent");
|
||||
assert!(json.get("renameCandidates").is_none(), "renameCandidates should be absent");
|
||||
assert!(json.get("rollbackGraph").is_none(), "rollbackGraph should be absent");
|
||||
assert!(json.get("compatibilityWarnings").is_none(), "compatibilityWarnings should be absent");
|
||||
assert!(json.get("permissionDiffs").is_none(), "permissionDiffs should be absent");
|
||||
assert!(json.get("permissionSyncSql").is_none(), "permissionSyncSql should be absent");
|
||||
|
||||
// dependency_graph is always populated by prepare_schema_diff
|
||||
assert!(json.get("dependencyGraph").is_some(), "dependencyGraph is always built by prepare_schema_diff");
|
||||
}
|
||||
|
||||
/// Verify that an old-format JSON (without new fields) can be deserialized
|
||||
/// into SchemaDiffPreparation with defaults.
|
||||
#[test]
|
||||
fn schema_diff_preparation_deserializes_old_format_with_defaults() {
|
||||
let old_json = serde_json::json!({
|
||||
"diffs": [],
|
||||
"syncSql": "SELECT 1"
|
||||
});
|
||||
|
||||
let result: SchemaDiffPreparation = serde_json::from_value(old_json).expect("Old-format JSON should deserialize");
|
||||
|
||||
assert!(result.diffs.is_empty());
|
||||
assert_eq!(result.sync_sql, "SELECT 1");
|
||||
assert!(result.rollback_sync_sql.is_none());
|
||||
assert!(result.rename_candidates.is_empty());
|
||||
assert!(result.dependency_graph.is_none());
|
||||
}
|
||||
|
||||
/// Verify that generate_schema_sync_sql output is invariant under default options
|
||||
/// — i.e., callers that don't pass rollback/rename/permission options still get
|
||||
/// identical SQL output.
|
||||
#[test]
|
||||
fn generate_schema_sync_sql_output_invariant() {
|
||||
let diffs = vec![TableDiff {
|
||||
diff_type: "added".to_string(),
|
||||
object_type: None,
|
||||
name: "users".to_string(),
|
||||
columns: Some(vec![]),
|
||||
indexes: None,
|
||||
foreign_keys: None,
|
||||
triggers: None,
|
||||
ddl: Some("CREATE TABLE users (id INT)".to_string()),
|
||||
target_ddl: None,
|
||||
source_table_comment: None,
|
||||
target_table_comment: None,
|
||||
sync_sql: None,
|
||||
}];
|
||||
|
||||
let sql = generate_schema_sync_sql(&diffs, &[], &[], &[], &[], DatabaseType::Postgres, None, false, None, &[]);
|
||||
assert!(sql.contains("CREATE TABLE"), "SQL should contain CREATE TABLE");
|
||||
assert!(sql.contains("users"), "SQL should contain table name");
|
||||
}
|
||||
|
||||
/// Verify that with all enhancement flags off, the output of
|
||||
/// prepare_schema_diff matches the same as if they didn't exist.
|
||||
#[test]
|
||||
fn prepare_schema_diff_with_default_options_matches_basic_diff() {
|
||||
let options = SchemaDiffPreparationOptions {
|
||||
source_tables: vec![basic_table_info("orders")],
|
||||
target_tables: vec![basic_table_info("orders")],
|
||||
source_details: vec![detail("orders", vec![col("id", "int"), col("status", "varchar(16)")])],
|
||||
target_details: vec![detail("orders", vec![col("id", "int"), col("status", "varchar(32)")])],
|
||||
database_type: DatabaseType::Postgres,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = prepare_schema_diff(options);
|
||||
assert!(!result.diffs.is_empty());
|
||||
assert_eq!(result.diffs.len(), 1);
|
||||
assert_eq!(result.diffs[0].diff_type, "modified");
|
||||
assert!(!result.sync_sql.is_empty());
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 12.1 — SqlRisk backward-compatibility regression tests
|
||||
// ============================================================================
|
||||
|
||||
/// Verify that SqlRisk::ReadOnly classification is unchanged
|
||||
#[test]
|
||||
fn sql_risk_readonly_classification_invariant() {
|
||||
assert_eq!(classify_sql_risk("SELECT * FROM users", "postgres").unwrap(), SqlRisk::ReadOnly);
|
||||
assert_eq!(classify_sql_risk("SELECT id, name FROM users WHERE id = 1", "mysql").unwrap(), SqlRisk::ReadOnly);
|
||||
assert_eq!(classify_sql_risk("SHOW TABLES", "mysql").unwrap(), SqlRisk::ReadOnly);
|
||||
assert_eq!(classify_sql_risk("DESCRIBE users", "mysql").unwrap(), SqlRisk::ReadOnly);
|
||||
assert_eq!(classify_sql_risk("EXPLAIN SELECT * FROM users", "postgres").unwrap(), SqlRisk::ReadOnly);
|
||||
assert_eq!(classify_sql_risk("WITH cte AS (SELECT 1) SELECT * FROM cte", "postgres").unwrap(), SqlRisk::ReadOnly);
|
||||
}
|
||||
|
||||
/// Verify that SqlRisk::Write classification is unchanged
|
||||
#[test]
|
||||
fn sql_risk_write_classification_invariant() {
|
||||
assert_eq!(classify_sql_risk("INSERT INTO users VALUES (1)", "postgres").unwrap(), SqlRisk::Write);
|
||||
assert_eq!(classify_sql_risk("UPDATE users SET name = 'x'", "postgres").unwrap(), SqlRisk::Write);
|
||||
assert_eq!(classify_sql_risk("DELETE FROM users WHERE id = 1", "postgres").unwrap(), SqlRisk::Write);
|
||||
}
|
||||
|
||||
/// Verify that SqlRisk::Ddl classification is unchanged
|
||||
#[test]
|
||||
fn sql_risk_ddl_classification_invariant() {
|
||||
assert_eq!(classify_sql_risk("CREATE TABLE users (id INT)", "postgres").unwrap(), SqlRisk::Ddl);
|
||||
assert_eq!(classify_sql_risk("DROP TABLE users", "postgres").unwrap(), SqlRisk::Ddl);
|
||||
assert_eq!(classify_sql_risk("ALTER TABLE users ADD COLUMN age INT", "postgres").unwrap(), SqlRisk::Ddl);
|
||||
assert_eq!(classify_sql_risk("TRUNCATE TABLE users", "postgres").unwrap(), SqlRisk::Ddl);
|
||||
}
|
||||
|
||||
/// Verify that multi-statement returns highest risk (unchanged)
|
||||
#[test]
|
||||
fn sql_risk_multi_statement_invariant() {
|
||||
assert_eq!(classify_sql_risk("SELECT 1; INSERT INTO users VALUES (1)", "postgres").unwrap(), SqlRisk::Write);
|
||||
assert_eq!(classify_sql_risk("SELECT 1; CREATE TABLE t (id INT)", "postgres").unwrap(), SqlRisk::Ddl);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 12.1 — DataCompare result format compatibility tests
|
||||
// ============================================================================
|
||||
|
||||
/// Verify that DataCompareFromTablesPreparation serializes with backward-compatible
|
||||
/// field names and new optional fields are skipped when None.
|
||||
#[test]
|
||||
fn data_compare_from_tables_preparation_optional_fields_default() {
|
||||
let prep = DataCompareFromTablesPreparation {
|
||||
result: DataCompareResult { added: vec![], removed: vec![], modified: vec![] },
|
||||
sync_statements: vec![],
|
||||
sync_sql: String::new(),
|
||||
pre_sync_statements: vec![],
|
||||
source_row_count: 0,
|
||||
target_row_count: 0,
|
||||
source_truncated: false,
|
||||
target_truncated: false,
|
||||
degradation_level: None,
|
||||
sampling_rate: None,
|
||||
confidence_score: None,
|
||||
verification_method: None,
|
||||
source_checksums: None,
|
||||
target_checksums: None,
|
||||
};
|
||||
|
||||
let json = serde_json::to_value(&prep).expect("Serialization should succeed");
|
||||
|
||||
// Core fields must be present
|
||||
assert!(json.get("result").is_some(), "result must be present");
|
||||
assert!(json.get("syncSql").is_some(), "syncSql must be present");
|
||||
|
||||
// New optional fields should be absent when None
|
||||
assert!(json.get("degradationLevel").is_none(), "degradationLevel should be absent");
|
||||
assert!(json.get("samplingRate").is_none(), "samplingRate should be absent");
|
||||
assert!(json.get("confidenceScore").is_none(), "confidenceScore should be absent");
|
||||
assert!(json.get("verificationMethod").is_none(), "verificationMethod should be absent");
|
||||
assert!(json.get("sourceChecksums").is_none(), "sourceChecksums should be absent");
|
||||
assert!(json.get("targetChecksums").is_none(), "targetChecksums should be absent");
|
||||
}
|
||||
|
||||
/// Verify that an old-format JSON can be deserialized into DataCompareFromTablesPreparation
|
||||
#[test]
|
||||
fn data_compare_from_tables_preparation_deserializes_old_format() {
|
||||
let old_json = serde_json::json!({
|
||||
"result": { "added": [], "removed": [], "modified": [] },
|
||||
"syncStatements": [],
|
||||
"syncSql": "",
|
||||
"preSyncStatements": [],
|
||||
"sourceRowCount": 100,
|
||||
"targetRowCount": 100,
|
||||
"sourceTruncated": false,
|
||||
"targetTruncated": false
|
||||
});
|
||||
|
||||
let prep: DataCompareFromTablesPreparation =
|
||||
serde_json::from_value(old_json).expect("Old-format JSON should deserialize");
|
||||
|
||||
assert_eq!(prep.source_row_count, 100);
|
||||
assert_eq!(prep.target_row_count, 100);
|
||||
assert!(prep.degradation_level.is_none());
|
||||
assert!(prep.sampling_rate.is_none());
|
||||
assert!(prep.confidence_score.is_none());
|
||||
}
|
||||
|
||||
/// Verify that DataComparePreparation (the original struct) remains unchanged
|
||||
#[test]
|
||||
fn data_compare_preparation_format_stable() {
|
||||
let prep = DataComparePreparation {
|
||||
result: DataCompareResult { added: vec![], removed: vec![], modified: vec![] },
|
||||
sync_statements: vec!["INSERT INTO t VALUES (1)".to_string()],
|
||||
sync_sql: "INSERT INTO t VALUES (1)".to_string(),
|
||||
};
|
||||
|
||||
let json = serde_json::to_value(&prep).expect("Serialization should succeed");
|
||||
assert_eq!(json["syncStatements"][0], "INSERT INTO t VALUES (1)");
|
||||
assert_eq!(json["syncSql"], "INSERT INTO t VALUES (1)");
|
||||
assert!(json.get("result").is_some());
|
||||
}
|
||||
Reference in new issue
Block a user