fix: sort tables by FK dependency in transfer and export
This commit is contained in:
1 parent
71db24813e
commit
c4c427fb67
11 files changed
+292
-6
No files matched your search
@@ -702,7 +702,36 @@ pub async fn build_export_sql_insert(Json(req): Json<BuildExportSqlInsertRequest
|
||||
}
|
||||
|
||||
pub async fn build_database_sql_export(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(req): Json<BuildDatabaseSqlExportRequest>,
|
||||
) -> Result<Json<String>, AppError> {
|
||||
dbx_core::database_export::build_database_sql_export(req.options).map(Json).map_err(AppError)
|
||||
let mut options = req.options;
|
||||
// Sort tables by FK dependency when connection info is available.
|
||||
if let (Some(ref conn_id), Some(ref database), Some(ref schema)) =
|
||||
(&options.connection_id, &options.database, &options.schema)
|
||||
{
|
||||
if options.tables.len() > 1 {
|
||||
let table_names: Vec<String> = options.tables.iter().filter_map(|t| t.table_name.clone()).collect();
|
||||
if table_names.len() > 1 {
|
||||
if let Ok(sorted_names) = dbx_core::transfer::sort_tables_by_fk_dependency(
|
||||
&state.app,
|
||||
conn_id,
|
||||
database,
|
||||
schema,
|
||||
&table_names,
|
||||
true,
|
||||
)
|
||||
.await
|
||||
{
|
||||
options.tables.sort_by_key(|t| {
|
||||
sorted_names
|
||||
.iter()
|
||||
.position(|n| Some(n.as_str()) == t.table_name.as_deref())
|
||||
.unwrap_or(usize::MAX)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
dbx_core::database_export::build_database_sql_export(options).map(Json).map_err(AppError)
|
||||
}
|
||||
@@ -79,6 +79,20 @@ pub async fn start_transfer(
|
||||
};
|
||||
|
||||
let tables = req.tables.clone();
|
||||
// Sort by FK dependency so referenced tables are transferred first.
|
||||
let tables = transfer::sort_tables_by_fk_dependency(
|
||||
&app,
|
||||
&req.source_connection_id,
|
||||
&req.source_database,
|
||||
&req.source_schema,
|
||||
&tables,
|
||||
true,
|
||||
)
|
||||
.await
|
||||
.unwrap_or_else(|e| {
|
||||
log::warn!("[transfer] failed to sort tables by FK dependency, using original order: {e}");
|
||||
tables
|
||||
});
|
||||
for (i, table) in tables.iter().enumerate() {
|
||||
if transfer::is_cancelled(&req.transfer_id).await {
|
||||
let progress = transfer::TransferProgress {
|
||||
@@ -195,3 +209,30 @@ pub async fn cancel_transfer(
|
||||
transfer::set_cancelled(&req.transfer_id).await;
|
||||
Json(serde_json::json!({ "cancelled": true }))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct SortTablesByFkRequest {
|
||||
pub connection_id: String,
|
||||
pub database: String,
|
||||
pub schema: String,
|
||||
pub tables: Vec<String>,
|
||||
pub parents_first: bool,
|
||||
}
|
||||
|
||||
pub async fn sort_tables_by_fk_dependency(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(req): Json<SortTablesByFkRequest>,
|
||||
) -> Result<Json<Vec<String>>, AppError> {
|
||||
transfer::sort_tables_by_fk_dependency(
|
||||
&state.app,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&req.schema,
|
||||
&req.tables,
|
||||
req.parents_first,
|
||||
)
|
||||
.await
|
||||
.map(Json)
|
||||
.map_err(AppError)
|
||||
}
|
||||
Reference in new issue
Block a user