feat(transfer): add SQL preview before structure-only transfer

This commit is contained in:
jearhe authored and GitHub committed 2026-10-05 02:02:53 +08:00
1 parent 1d9223eeed
commit bf68b93b05
22 files changed
+1332 -54

No files matched your search

+176 -3
View File
@@ -2,9 +2,10 @@ use dbx_core::connection::{AppState, PoolKind};
use dbx_core::db::postgres;
use dbx_core::models::connection::{ConnectionConfig, DatabaseType};
use dbx_core::transfer::{
drop_backup_tables, get_db_type, rename_tables_to_backup, transfer_postgres_schema_dependencies,
transfer_postgres_schema_objects, transfer_table, TransferContent, TransferMode, TransferObjectKind,
TransferObjectSelection, TransferOwnershipPolicy, TransferRequest, TransferTableNameCase,
drop_backup_tables, get_db_type, preview_transfer_ownership, rename_tables_to_backup,
transfer_postgres_schema_dependencies, transfer_postgres_schema_objects, transfer_table, TransferContent,
TransferMode, TransferObjectKind, TransferObjectSelection, TransferOwnershipPolicy, TransferRequest,
TransferTableNameCase,
};
use serde_json::json;
use std::sync::Arc;
@@ -2112,3 +2113,175 @@ async fn live_postgres_keyset_large_batch_copies_every_row() {
postgres::execute_batch(&target_pool, &[format!("DROP SCHEMA \"{target_schema}\" CASCADE")]).await.unwrap();
let _ = std::fs::remove_dir_all(dir);
}
/// The structure-only SQL preview is shown to the operator before anything runs, so it must
/// render the real PostgreSQL structure DDL — and it must not touch either database.
#[tokio::test]
#[ignore = "requires PostgreSQL URLs via DBX_LIVE_PG_TRANSFER_SOURCE_URL and DBX_LIVE_PG_TRANSFER_TARGET_URL"]
async fn live_postgres_structure_only_preview_renders_ddl_without_touching_the_target() {
let source_url = std::env::var("DBX_LIVE_PG_TRANSFER_SOURCE_URL").expect("DBX_LIVE_PG_TRANSFER_SOURCE_URL");
let target_url = std::env::var("DBX_LIVE_PG_TRANSFER_TARGET_URL").unwrap_or_else(|_| source_url.clone());
let source_pool = postgres::connect(&source_url, std::time::Duration::from_secs(5)).await.unwrap();
let target_pool = postgres::connect(&target_url, std::time::Duration::from_secs(5)).await.unwrap();
let source_database = query_scalar(&source_pool, "SELECT current_database()").await.as_str().unwrap().to_string();
let target_database = query_scalar(&target_pool, "SELECT current_database()").await.as_str().unwrap().to_string();
let suffix = uuid::Uuid::new_v4().simple().to_string();
let source_schema = format!("dbx_src_preview_{}", &suffix[..8]);
let target_schema = format!("dbx_dst_preview_{}", &suffix[..8]);
let missing_target_schema = format!("dbx_dst_missing_{}", &suffix[..8]);
postgres::execute_batch(
&source_pool,
&[
format!("CREATE SCHEMA \"{source_schema}\""),
format!("CREATE TABLE \"{source_schema}\".\"parent\" (\"id\" serial PRIMARY KEY)"),
format!(
"CREATE TABLE \"{source_schema}\".\"items\" (\"id\" serial PRIMARY KEY, \"parent_id\" integer REFERENCES \"{source_schema}\".\"parent\"(\"id\"), \"name\" text NOT NULL)"
),
format!("COMMENT ON TABLE \"{source_schema}\".\"items\" IS 'preview items'"),
format!("COMMENT ON COLUMN \"{source_schema}\".\"items\".\"name\" IS 'preview name'"),
format!("CREATE INDEX \"items_name_idx\" ON \"{source_schema}\".\"items\" (\"name\")"),
format!("INSERT INTO \"{source_schema}\".\"parent\" DEFAULT VALUES"),
],
)
.await
.unwrap();
postgres::execute_batch(&target_pool, &[format!("CREATE SCHEMA \"{target_schema}\"")]).await.unwrap();
let dir = std::env::temp_dir().join(format!("dbx-live-pg-structpreview-{suffix}"));
std::fs::create_dir_all(&dir).unwrap();
let storage = dbx_core::persistence::test_storage::open(&dir.join("storage.db")).await.unwrap();
let state = Arc::new(AppState::new(storage));
let source_connection_id = "live-pg-preview-source";
let target_connection_id = "live-pg-preview-target";
let source_pool_key = format!("{source_connection_id}:{source_database}");
let target_pool_key = format!("{target_connection_id}:{target_database}");
state
.update_connection_pools(|connections| {
connections.insert(source_pool_key.clone(), PoolKind::Postgres(source_pool.clone()));
connections.insert(target_pool_key.clone(), PoolKind::Postgres(target_pool.clone()));
})
.await;
state
.configs
.write()
.await
.insert(source_connection_id.to_string(), postgres_test_config(source_connection_id, &source_database));
state
.configs
.write()
.await
.insert(target_connection_id.to_string(), postgres_test_config(target_connection_id, &target_database));
let request = TransferRequest {
transfer_id: format!("live-pg-preview-{suffix}"),
source_connection_id: source_connection_id.to_string(),
source_database: source_database.clone(),
source_schema: source_schema.clone(),
source_catalog: None,
target_connection_id: target_connection_id.to_string(),
target_database: target_database.clone(),
target_schema: target_schema.clone(),
target_catalog: None,
tables: vec!["items".to_string()],
create_table: true,
drop_target_before_create: false,
drop_target_confirmed: false,
content: TransferContent::StructureOnly,
// A selected non-table object must be disclosed as not expanded, never silently dropped.
objects: vec![TransferObjectSelection {
object_type: TransferObjectKind::Function,
names: vec!["preview_probe_function".to_string()],
}],
mode: TransferMode::Append,
target_table_name_case: TransferTableNameCase::Preserve,
quote_target_column_names: true,
ownership_policy: TransferOwnershipPolicy::Preserve,
batch_size: 1000,
};
let source_db_type = get_db_type(&state, source_connection_id).await.unwrap();
let target_db_type = get_db_type(&state, target_connection_id).await.unwrap();
let preview = preview_transfer_ownership(
&state,
&request,
&source_db_type,
&target_db_type,
&source_pool_key,
&target_pool_key,
)
.await
.unwrap();
let structure = preview.structure.expect("a structure-only transfer must plan its structure SQL");
let sql = &structure.sql;
println!("--- structure-only preview ---\n{sql}\n--- end ---");
assert_eq!(structure.tables.len(), 1, "{:?}", structure.tables);
assert_eq!(structure.tables[0].source_table, "items");
assert!(!structure.tables[0].preexisting);
assert!(sql.contains("CREATE TABLE"), "{sql}");
assert!(sql.contains("items"), "{sql}");
assert!(
sql.contains("CREATE INDEX IF NOT EXISTS") && sql.contains("items_name_idx"),
"indexes must be planned: {sql}"
);
assert_eq!(
sql.matches("CREATE INDEX").count(),
1,
"the create pass filters the source script's index statement, so the preview must not show it: {sql}"
);
assert_eq!(
sql.matches("\nCREATE TABLE").count(),
1,
"the reused multi-statement script must be split, not repeated: {sql}"
);
assert!(sql.contains("FOREIGN KEY"), "foreign keys must be planned: {sql}");
assert!(sql.contains("COMMENT ON TABLE") && sql.contains("COMMENT ON COLUMN"), "comments must be planned: {sql}");
assert!(sql.contains("CREATE SEQUENCE"), "owned sequences must be planned: {sql}");
assert!(sql.contains("ALTER SEQUENCE") && sql.contains("OWNED BY"), "sequence binding must be planned: {sql}");
assert!(sql.contains("not expanded in this preview"), "unexpanded objects must be disclosed: {sql}");
assert!(sql.contains("-- Functions: preview_probe_function"), "{sql}");
// Planning only: nothing was created on the target, nothing changed on the source.
assert_eq!(
schema_count(&target_pool, &format!("tables WHERE table_schema = '{target_schema}'")).await,
"0",
"the preview must not create anything on the target"
);
assert_eq!(
schema_count(&source_pool, &format!("tables WHERE table_schema = '{source_schema}'")).await,
"2",
"the preview must not modify the source"
);
// A missing PostgreSQL target schema is created during execution, so the preview shows it.
let mut missing_schema_request = request.clone();
missing_schema_request.transfer_id = format!("live-pg-preview-missing-{suffix}");
missing_schema_request.target_schema = missing_target_schema.clone();
missing_schema_request.objects = Vec::new();
let missing_preview = preview_transfer_ownership(
&state,
&missing_schema_request,
&source_db_type,
&target_db_type,
&source_pool_key,
&target_pool_key,
)
.await
.unwrap();
let missing_sql = &missing_preview.structure.expect("structure-only must plan its structure SQL").sql;
assert!(
missing_sql.contains(&format!("CREATE SCHEMA \"{missing_target_schema}\"")),
"a missing target schema must be planned: {missing_sql}"
);
assert_eq!(
schema_count(&target_pool, &format!("schemata WHERE schema_name = '{missing_target_schema}'")).await,
"0",
"the preview must not create the target schema"
);
postgres::execute_batch(&source_pool, &[format!("DROP SCHEMA \"{source_schema}\" CASCADE")]).await.unwrap();
postgres::execute_batch(&target_pool, &[format!("DROP SCHEMA \"{target_schema}\" CASCADE")]).await.unwrap();
let _ = std::fs::remove_dir_all(dir);
}