feat(transfer): add rebuild structure and data mode
This commit is contained in:
1 parent
2b2d1f0d95
commit
538aeed99c
37 files changed
+6720
-404
No files matched your search
@@ -88,6 +88,23 @@ pub async fn start_transfer(
|
||||
)));
|
||||
}
|
||||
|
||||
// `drop_target_before_create` rebuilds target tables. Gate it before responding so the
|
||||
// caller sees the error code rather than a progress stream that fails later.
|
||||
if req.drop_target_before_create {
|
||||
let target_db_type =
|
||||
transfer::get_db_type(&state.app, &req.target_connection_id).await.map_err(AppError::from)?;
|
||||
dbx_core::transfer_rebuild::ensure_drop_target_allowed(
|
||||
&state.app,
|
||||
&req.target_connection_id,
|
||||
&req.target_database,
|
||||
target_db_type,
|
||||
req.drop_target_before_create,
|
||||
req.drop_target_confirmed,
|
||||
)
|
||||
.await
|
||||
.map_err(AppError::from)?;
|
||||
}
|
||||
|
||||
let transfer_id = req.transfer_id.clone();
|
||||
|
||||
// Keep bounded replay state so a web EventSource opened after this POST
|
||||
@@ -202,6 +219,82 @@ pub async fn start_transfer(
|
||||
let mut failed_tables: Vec<String> = Vec::new();
|
||||
let mut pending_fk_alters: Vec<(String, String)> = Vec::new();
|
||||
|
||||
// When drop_target_before_create is true, perform a rename pre-pass in
|
||||
// parents_first=false order (children first) so that foreign keys on the
|
||||
// backup tables remain intact. The main create/insert pass then runs in
|
||||
// parents_first=true order (parents first).
|
||||
// Children-first order used by the rename pre-pass, kept for the post-loop backup
|
||||
// cleanup: a backup can still be referenced by another backup, so the drops must
|
||||
// follow the same order.
|
||||
let mut backup_drop_order: Vec<String> = Vec::new();
|
||||
let backup_names = if req.drop_target_before_create {
|
||||
// Re-sort tables in children-first order for the rename pre-pass
|
||||
let (tables_for_rename, _) = transfer::sort_tables_by_fk_dependency_with_foreign_keys(
|
||||
&app,
|
||||
&req.target_connection_id,
|
||||
&req.target_database,
|
||||
&req.target_schema,
|
||||
&tables,
|
||||
false, // parents_first=false: children first
|
||||
)
|
||||
.await
|
||||
.unwrap_or_else(|e| {
|
||||
log::warn!("[transfer] failed to sort tables for rename pre-pass, using original order: {e}");
|
||||
(tables.clone(), std::collections::HashMap::new())
|
||||
});
|
||||
backup_drop_order = tables_for_rename.clone();
|
||||
|
||||
let progress_channel_clone = progress_channel.clone();
|
||||
match transfer::rename_tables_to_backup(
|
||||
&app,
|
||||
&req,
|
||||
&tables_for_rename,
|
||||
target_db_type,
|
||||
&target_pool_key,
|
||||
|progress| {
|
||||
send_transfer_progress(&progress_channel_clone, &progress);
|
||||
},
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(names) => Some(names),
|
||||
Err(e) if e == "Cancelled" => {
|
||||
let progress = transfer::TransferProgress {
|
||||
transfer_id: req.transfer_id.clone(),
|
||||
table: "rename pre-pass".to_string(),
|
||||
table_index: 0,
|
||||
total_tables: tables.len(),
|
||||
rows_transferred: 0,
|
||||
total_rows: None,
|
||||
status: TransferStatus::Cancelled,
|
||||
error: None,
|
||||
terminal: true,
|
||||
};
|
||||
send_transfer_progress(&progress_channel, &progress);
|
||||
finish_transfer_channel(&state_clone, &req.transfer_id, &progress_channel).await;
|
||||
return;
|
||||
}
|
||||
Err(e) => {
|
||||
let progress = transfer::TransferProgress {
|
||||
transfer_id: req.transfer_id.clone(),
|
||||
table: "rename pre-pass".to_string(),
|
||||
table_index: 0,
|
||||
total_tables: tables.len(),
|
||||
rows_transferred: 0,
|
||||
total_rows: None,
|
||||
status: TransferStatus::Error,
|
||||
error: Some(e),
|
||||
terminal: true,
|
||||
};
|
||||
send_transfer_progress(&progress_channel, &progress);
|
||||
finish_transfer_channel(&state_clone, &req.transfer_id, &progress_channel).await;
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
if matches!(source_db_type, dbx_core::models::connection::DatabaseType::Postgres)
|
||||
&& matches!(target_db_type, dbx_core::models::connection::DatabaseType::Postgres)
|
||||
{
|
||||
@@ -285,6 +378,7 @@ pub async fn start_transfer(
|
||||
&target_pool_key,
|
||||
&known_foreign_keys,
|
||||
&mut pending_fk_alters,
|
||||
backup_names.as_ref(),
|
||||
|progress| {
|
||||
last_rows_transferred = progress.rows_transferred;
|
||||
last_total_rows = progress.total_rows;
|
||||
@@ -345,7 +439,9 @@ pub async fn start_transfer(
|
||||
// Add any foreign keys deferred during MySQL-family table creation now that
|
||||
// every selected table exists — see transfer_table's use of
|
||||
// strip_inline_foreign_key_constraint_lines for why these can't be created
|
||||
// inline (a foreign key cycle has no valid CREATE TABLE order at all).
|
||||
// inline (a foreign key cycle has no valid CREATE TABLE order at all). The
|
||||
// rename pre-pass already freed the backup constraint names, so these re-create
|
||||
// the original names without colliding.
|
||||
let mut failed_fk_tables: Vec<String> = Vec::new();
|
||||
let mut failed_fk_count = 0usize;
|
||||
for (table, alter_sql) in &pending_fk_alters {
|
||||
@@ -413,6 +509,33 @@ pub async fn start_transfer(
|
||||
if !object_outcome.failed.is_empty() {
|
||||
failed_tables.push(format!("schema objects ({})", object_outcome.failed.len()));
|
||||
}
|
||||
|
||||
// The rename pre-pass left one backup per rebuilt table. Drop them only now that
|
||||
// every table, every deferred foreign key and every selected schema object has
|
||||
// succeeded — a failure anywhere above keeps the originals recoverable under
|
||||
// their backup names.
|
||||
if let Some(backup_names) = backup_names.as_ref() {
|
||||
if failed_tables.is_empty() {
|
||||
if let Err(e) = transfer::drop_backup_tables(
|
||||
&app,
|
||||
&req,
|
||||
target_db_type,
|
||||
&target_pool_key,
|
||||
backup_names,
|
||||
&backup_drop_order,
|
||||
)
|
||||
.await
|
||||
{
|
||||
failed_tables.push(e);
|
||||
}
|
||||
} else {
|
||||
log::warn!(
|
||||
"[transfer] keeping {} backup table(s) because {} step(s) failed",
|
||||
backup_names.len(),
|
||||
failed_tables.len()
|
||||
);
|
||||
}
|
||||
}
|
||||
let skip_suffix = if !object_outcome.skipped.is_empty() && failed_tables.is_empty() {
|
||||
format!(",跳过 {} 个已存在对象", object_outcome.skipped.len())
|
||||
} else if !object_outcome.skipped.is_empty() {
|
||||
@@ -634,6 +757,8 @@ mod tests {
|
||||
target_catalog: None,
|
||||
tables: Vec::new(),
|
||||
create_table: false,
|
||||
drop_target_before_create: false,
|
||||
drop_target_confirmed: false,
|
||||
content: TransferContent::DataOnly,
|
||||
objects: Vec::new(),
|
||||
mode: TransferMode::Append,
|
||||
|
||||
Reference in new issue
Block a user