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

+151 -25
View File
@@ -23,6 +23,7 @@ mod iris_tests;
mod db2;
mod ddl_plan;
mod structure_plan;
use crate::connection::{config_for_pool_key, AppState, PoolKind};
use crate::db;
@@ -297,6 +298,12 @@ pub struct TransferOwnershipPreview {
pub missing_owners: Vec<String>,
pub target_owner: String,
pub rebuild: Option<TransferRebuildPreview>,
/// Structure statements a structure-only transfer is about to run. Absent for every
/// other content mode (`dataOnly` has no DDL, `structureAndData` keeps its current
/// behavior). Built without executing any DDL, but not a frozen script: `start_transfer`
/// re-reads source and target metadata before executing.
#[serde(skip_serializing_if = "Option::is_none")]
pub structure: Option<TransferStructurePreview>,
}
/// SQL plan preview for a `drop_target_before_create` (rebuild) transfer.
@@ -309,6 +316,13 @@ pub struct TransferOwnershipPreview {
pub struct TransferRebuildPreview {
pub sql: String,
pub tables: Vec<TransferRebuildPreviewTable>,
/// The backup (rename) phase on its own, for callers that place the structure preview
/// between the rename and the cleanup phases instead of showing the combined `sql`.
#[serde(skip_serializing_if = "Option::is_none")]
pub backup_sql: Option<String>,
/// The cleanup (drop backups) phase on its own. `sql` stays the combined plan.
#[serde(skip_serializing_if = "Option::is_none")]
pub cleanup_sql: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
@@ -320,6 +334,31 @@ pub struct TransferRebuildPreviewTable {
pub backup_table: Option<String>,
}
/// SQL plan preview for a structure-only transfer.
///
/// Plans rather than executes: every statement comes from the generator the execution pass
/// uses for the same operation, so preview and execution can never disagree on the DDL they
/// render. It is still a preview — the target is re-inspected when the transfer starts.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TransferStructurePreview {
pub sql: String,
pub tables: Vec<TransferStructurePreviewTable>,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TransferStructurePreviewTable {
pub source_table: String,
pub target_table: String,
/// The target table already exists and this transfer plans no structure DDL for it,
/// mirroring the execution pass' skip when the target table is present.
pub preexisting: bool,
/// The structure the transfer plans for this table, or an explanatory comment when the
/// execution pass will skip the table entirely.
pub sql: String,
}
impl TransferRequest {
pub fn target_table_name(&self, source_table: &str) -> String {
match self.target_table_name_case {
@@ -7385,28 +7424,39 @@ async fn get_existing_postgres_sequence_names_for_transfer(
.collect())
}
/// Create owned PostgreSQL sequences before executing reused table DDL because
/// serial defaults still reference `nextval('...')` in `CREATE TABLE`.
async fn prepare_postgres_owned_sequences_for_transfer(
/// Read-only half of [`prepare_postgres_owned_sequences_for_transfer`]: resolves which
/// owned sequences the transfer must create and renders their `CREATE SEQUENCE` statements,
/// executing nothing.
///
/// The structure SQL preview calls this so it can show the same sequence DDL the transfer
/// pass will run (and the serial-column rewrite that DDL implies) without ever triggering a
/// statement. Nothing in here may execute DDL — that is what
/// [`execute_planned_postgres_owned_sequences_for_transfer`] is for.
struct PlannedPostgresOwnedSequences {
create_statements: Vec<String>,
owned_sequences: Vec<PostgresOwnedSequence>,
}
#[allow(clippy::too_many_arguments)]
async fn plan_postgres_owned_sequences_for_transfer(
state: &AppState,
request: &TransferRequest,
table: &str,
target_table: &str,
source_pool_key: &str,
target_pool_key: &str,
pg_compat_transfer: bool,
preserves_target_table_name: bool,
target_table_preexisting: bool,
) -> Result<Vec<PostgresOwnedSequence>, String> {
) -> Result<PlannedPostgresOwnedSequences, String> {
if !(request.create_table && pg_compat_transfer && preserves_target_table_name && !target_table_preexisting) {
return Ok(Vec::new());
return Ok(PlannedPostgresOwnedSequences { create_statements: Vec::new(), owned_sequences: Vec::new() });
}
let owned_sequences =
get_postgres_owned_sequences_for_transfer(state, source_pool_key, &request.source_schema, &[table.to_string()])
.await?;
if owned_sequences.is_empty() {
return Ok(Vec::new());
return Ok(PlannedPostgresOwnedSequences { create_statements: Vec::new(), owned_sequences });
}
let sequence_names = owned_sequences.iter().map(|sequence| sequence.name.clone()).collect::<Vec<_>>();
@@ -7425,6 +7475,7 @@ async fn prepare_postgres_owned_sequences_for_transfer(
.collect::<HashMap<_, _>>();
let sequence_if_not_exists = target_supports_if_not_exists_ddl(state, target_pool_key).await;
let mut create_statements = Vec::new();
for sequence in &owned_sequences {
let should_create = validate_existing_postgres_sequence(
sequence,
@@ -7435,18 +7486,69 @@ async fn prepare_postgres_owned_sequences_for_transfer(
let definition = definitions
.get(&sequence.name)
.ok_or_else(|| format!("PostgreSQL sequence definition not found: {}", sequence.name))?;
let create_sql = generate_postgres_transfer_sequence_create_ddl(
create_statements.push(generate_postgres_transfer_sequence_create_ddl(
definition,
&request.target_schema,
sequence_if_not_exists,
);
execute_on_pool(state, target_pool_key, &create_sql)
.await
.map_err(|e| format!("Failed to create PostgreSQL sequence for {target_table}: {e}"))?;
));
}
}
Ok(owned_sequences)
Ok(PlannedPostgresOwnedSequences { create_statements, owned_sequences })
}
/// Executes the `CREATE SEQUENCE` statements a [`PlannedPostgresOwnedSequences`] planned.
async fn execute_planned_postgres_owned_sequences_for_transfer(
state: &AppState,
target_pool_key: &str,
target_table: &str,
plan: &PlannedPostgresOwnedSequences,
) -> Result<(), String> {
for create_sql in &plan.create_statements {
execute_on_pool(state, target_pool_key, create_sql)
.await
.map_err(|e| format!("Failed to create PostgreSQL sequence for {target_table}: {e}"))?;
}
Ok(())
}
/// Create owned PostgreSQL sequences before executing reused table DDL because
/// serial defaults still reference `nextval('...')` in `CREATE TABLE`.
async fn prepare_postgres_owned_sequences_for_transfer(
state: &AppState,
request: &TransferRequest,
table: &str,
target_table: &str,
source_pool_key: &str,
target_pool_key: &str,
pg_compat_transfer: bool,
preserves_target_table_name: bool,
target_table_preexisting: bool,
) -> Result<Vec<PostgresOwnedSequence>, String> {
let plan = plan_postgres_owned_sequences_for_transfer(
state,
request,
table,
source_pool_key,
target_pool_key,
pg_compat_transfer,
preserves_target_table_name,
target_table_preexisting,
)
.await?;
execute_planned_postgres_owned_sequences_for_transfer(state, target_pool_key, target_table, &plan).await?;
Ok(plan.owned_sequences)
}
/// Bind a planned sequence to its column after the table exists. Shared with the structure
/// SQL preview so both render the identical statement.
fn postgres_owned_sequence_bind_sql(request: &TransferRequest, sequence: &PostgresOwnedSequence) -> String {
format!(
"ALTER SEQUENCE {} OWNED BY {}.{}",
postgres_sequence_qualified_name(&request.target_schema, &sequence.name),
qualified_table(&sequence.owner_table, &request.target_schema, &DatabaseType::Postgres, None),
quote_identifier(&sequence.owner_column, &DatabaseType::Postgres)
)
}
/// Bind created or reused sequences after the table exists so
@@ -7459,12 +7561,7 @@ async fn bind_postgres_owned_sequences_for_transfer(
owned_sequences: &[PostgresOwnedSequence],
) -> Result<(), String> {
for sequence in owned_sequences {
let owner_sql = format!(
"ALTER SEQUENCE {} OWNED BY {}.{}",
postgres_sequence_qualified_name(&request.target_schema, &sequence.name),
qualified_table(&sequence.owner_table, &request.target_schema, &DatabaseType::Postgres, None),
quote_identifier(&sequence.owner_column, &DatabaseType::Postgres)
);
let owner_sql = postgres_owned_sequence_bind_sql(request, sequence);
execute_on_pool(state, target_pool_key, &owner_sql)
.await
.map_err(|e| format!("Failed to bind PostgreSQL sequence for {target_table}: {e}"))?;
@@ -8535,18 +8632,28 @@ async fn build_rebuild_preview(
}
let mut phases: Vec<String> = Vec::new();
if !rename_statements.is_empty() {
phases.push(format!("-- 1. Backup existing target tables\n{}", rename_statements.join(";\n")));
let backup_sql = if rename_statements.is_empty() {
None
} else {
Some(format!("-- 1. Backup existing target tables\n{}", rename_statements.join(";\n")))
};
let cleanup_sql = if drop_statements.is_empty() {
None
} else {
Some(format!("-- 3. Drop backups after success\n{}", drop_statements.join(";\n")))
};
if let Some(sql) = &backup_sql {
phases.push(sql.clone());
}
phases.push(format!(
"-- 2. Recreate the {} selected table(s) from the source structure and transfer the selected data",
resolved.len()
));
if !drop_statements.is_empty() {
phases.push(format!("-- 3. Drop backups after success\n{}", drop_statements.join(";\n")));
if let Some(sql) = &cleanup_sql {
phases.push(sql.clone());
}
Ok(TransferRebuildPreview { sql: phases.join("\n\n"), tables })
Ok(TransferRebuildPreview { sql: phases.join("\n\n"), tables, backup_sql, cleanup_sql })
}
pub async fn preview_transfer_ownership(
@@ -8592,7 +8699,26 @@ pub async fn preview_transfer_ownership(
None
};
Ok(TransferOwnershipPreview { missing_owners, target_owner, rebuild })
// Structure-only transfers expose the statements their create pass will run. The other
// content modes keep their existing previews: data-only runs no DDL, and
// structure-and-data is deliberately unchanged here.
let structure = if matches!(request.content, TransferContent::StructureOnly) {
Some(
structure_plan::build_structure_preview(
state,
request,
source_db_type,
target_db_type,
source_pool_key,
target_pool_key,
)
.await?,
)
} else {
None
};
Ok(TransferOwnershipPreview { missing_owners, target_owner, rebuild, structure })
}
fn postgres_transfer_grant_statements_sql(
@@ -1,8 +1,10 @@
//! Prepare target CREATE TABLE and deferred foreign keys without executing DDL.
//!
//! The transfer pass and the ownership preview share this planning helper so both always
//! agree on the DDL that will run and the names it will use. Nothing here executes DDL:
//! execution stays with `execute_transfer_create_table_ddl_on_pool` and the caller.
//! The transfer pass and the structure-only SQL preview share this planning helper, so both
//! always agree on the DDL that will run and the names it will use. Nothing here executes
//! DDL: execution stays with `execute_transfer_create_table_ddl_on_pool` and the caller. The
//! preview is still a plan, not a frozen script — the transfer pass re-reads source and
//! target metadata (and re-runs its fail-closed checks) when it starts.
use super::*;
@@ -0,0 +1,603 @@
//! Read-only planning for the structure-only transfer SQL preview.
//!
//! A structure-only transfer used to start with no idea what it would run. This module
//! renders the statements the create pass is about to execute so the confirmation dialog can
//! show them first. It is strictly planning: target names, source metadata and the SQL are
//! read from the same helpers the execution pass uses (`ddl_plan::prepare_table_ddl`,
//! `plan_postgres_owned_sequences_for_transfer`, `generate_comment_ddl_with_column_quoting`,
//! `generate_postgres_index_ddl`, `generate_postgres_foreign_key_ddl`,
//! `generate_mysql_foreign_key_alter_statements`), and no statement is ever executed here.
//!
//! The result is a plan, not a frozen script: `start_transfer` re-reads source and target
//! metadata before it executes, and the existing execution-time fail-closed checks stay in
//! force. Callers must therefore never present the preview as "these exact statements will
//! run".
//!
//! Where the create pass normalizes before executing (splitting a reused PostgreSQL table
//! script, stripping inline foreign keys, dropping the post-table index/foreign-key statements
//! it re-applies from metadata), the preview runs the same normalization — a statement the
//! executor filters out must not be shown. Re-applied statements are shown once per step,
//! including the idempotent `COMMENT` the reused script may already contain.
use super::*;
/// Emitted when the request asks for no structure DDL at all (`createTable = false`).
const CREATE_TABLE_DISABLED_NOTE: &str =
"-- This transfer has structure DDL disabled (createTable = false): no structure statements will run.";
/// Emitted when the transfer also moves non-table schema objects whose DDL this preview
/// deliberately does not expand (views, routines, triggers, standalone sequences, ...).
const UNEXPANDED_OBJECTS_NOTE: &str = "\
-- Additional selected schema objects are transferred during execution.
-- Their generated DDL is not expanded in this preview.";
/// Plan the structure DDL a structure-only transfer is about to run.
///
/// Everything inside is read-only: metadata reads plus SQL rendering. The only statements
/// handed to `execute_on_pool` are catalog queries (`SELECT`), never DDL.
pub(super) async fn build_structure_preview(
state: &Arc<AppState>,
request: &TransferRequest,
source_db_type: &DatabaseType,
target_db_type: &DatabaseType,
source_pool_key: &str,
target_pool_key: &str,
) -> Result<TransferStructurePreview, String> {
if !request.create_table {
return Ok(TransferStructurePreview { sql: CREATE_TABLE_DISABLED_NOTE.to_string(), tables: Vec::new() });
}
let pg_compat_transfer = is_postgres_compat_transfer(source_db_type, target_db_type);
let index_if_not_exists = pg_compat_transfer && target_supports_if_not_exists_ddl(state, target_pool_key).await;
// Two execution paths create the target schema before any table does: the inline ensure in
// `create_transfer_target_table` (PostgreSQL-dialect target from another family) and
// `transfer_postgres_schema_dependencies` (PostgreSQL→PostgreSQL). Plan the same statement
// for either, deciding it with a read-only existence check instead of executing anything.
let create_schema_sql = if is_postgres_transfer_dialect(target_db_type)
&& !request.target_schema.trim().is_empty()
&& !postgres_schema_exists(state, target_pool_key, &request.target_schema).await?
{
Some(format!("CREATE SCHEMA {}", quote_identifier(&request.target_schema, target_db_type)))
} else {
None
};
// Same order and same foreign key metadata the create pass uses.
let (tables, known_foreign_keys) = sorted_source_tables_with_foreign_keys(state, request).await;
let mut sections: Vec<String> = Vec::new();
if let Some(create_schema_sql) = &create_schema_sql {
sections.push(format!(
"-- Ensure the target schema exists\n{}",
statement_block(std::slice::from_ref(create_schema_sql))
));
}
let mut planned_tables: Vec<TransferStructurePreviewTable> = Vec::with_capacity(tables.len());
let mut table_sections: Vec<String> = Vec::new();
let mut deferred_fk_alters: Vec<String> = Vec::new();
for table in &tables {
let ResolvedTransferTargetTable { name: target_table, preexisting } = resolve_transfer_target_table_name(
state,
request,
table,
target_pool_key,
target_db_type,
request.source_catalog.as_deref(),
request.target_catalog.as_deref(),
)
.await;
// A rebuild renames every preexisting target aside before the create pass runs, so
// those tables are created fresh — the same reset the create pass performs.
if preexisting && !request.drop_target_before_create {
let note = format!(
"-- {table} -> {target_table}: the target table already exists, so this transfer plans no structure \
DDL for it"
);
planned_tables.push(TransferStructurePreviewTable {
source_table: table.clone(),
target_table,
preexisting: true,
sql: note.clone(),
});
table_sections.push(note);
continue;
}
let planned = plan_table_structure(
state,
request,
table,
&target_table,
source_db_type,
target_db_type,
source_pool_key,
target_pool_key,
&known_foreign_keys,
pg_compat_transfer,
index_if_not_exists,
)
.await?;
deferred_fk_alters.extend(planned.deferred_fk_alters);
let section = format!("-- {} -> {}\n{}", table, target_table, statement_block(&planned.statements));
planned_tables.push(TransferStructurePreviewTable {
source_table: table.clone(),
target_table,
preexisting: false,
sql: section.clone(),
});
table_sections.push(section);
}
sections.extend(table_sections);
if !deferred_fk_alters.is_empty() {
// MySQL-family targets defer foreign keys so creation order never has to satisfy
// them; the create pass flushes these after every table exists.
sections.push(format!(
"-- Foreign keys added after every table exists, so creation order never has to satisfy them\n{}",
statement_block(&deferred_fk_alters)
));
}
sections.extend(unexpanded_schema_object_notes(source_db_type, target_db_type, request));
Ok(TransferStructurePreview { sql: sections.join("\n\n"), tables: planned_tables })
}
struct PlannedTableStructure {
statements: Vec<String>,
deferred_fk_alters: Vec<String>,
}
/// Render the statements the create pass runs for one table, in execution order.
#[allow(clippy::too_many_arguments)]
async fn plan_table_structure(
state: &Arc<AppState>,
request: &TransferRequest,
table: &str,
target_table: &str,
source_db_type: &DatabaseType,
target_db_type: &DatabaseType,
source_pool_key: &str,
target_pool_key: &str,
known_foreign_keys: &HashMap<String, Vec<db::ForeignKeyInfo>>,
pg_compat_transfer: bool,
index_if_not_exists: bool,
) -> Result<PlannedTableStructure, String> {
let preserves_target_table_name = target_table == table;
let columns = source_transfer_columns(state, request, table, source_pool_key).await?;
let table_comment = source_table_comment(state, request, table, source_db_type).await;
// Planned here, created by the execution pass.
let owned_sequences = plan_postgres_owned_sequences_for_transfer(
state,
request,
table,
source_pool_key,
target_pool_key,
pg_compat_transfer,
preserves_target_table_name,
false,
)
.await?;
// Shared with `create_transfer_target_table`: the same helper renders the DDL that runs.
let prepared = ddl_plan::prepare_table_ddl(
state,
request,
table,
target_table,
source_db_type,
target_db_type,
source_pool_key,
&columns,
table_comment.as_deref(),
known_foreign_keys,
)
.await?;
let ddl = if pg_compat_transfer && !owned_sequences.owned_sequences.is_empty() {
rewrite_postgres_serial_columns_for_transfer(
&prepared.ddl,
&owned_sequences.owned_sequences,
&request.target_schema,
)
} else {
prepared.ddl
};
let mut statements = owned_sequences.create_statements.clone();
// Normalize the DDL exactly like the executor does before running it: the reused
// PostgreSQL script spans several statements, has its inline foreign keys stripped, and
// drops the post-table index/foreign-key statements that are re-applied from structured
// metadata below. A statement the create pass filters out must never appear here.
statements.extend(transfer_ddl_statements(&ddl, target_db_type));
statements.extend(generate_comment_ddl_with_column_quoting(
&columns,
target_table,
&request.target_schema,
target_db_type,
table_comment.as_deref(),
request.quote_target_column_names,
));
for sequence in &owned_sequences.owned_sequences {
statements.push(postgres_owned_sequence_bind_sql(request, sequence));
}
if pg_compat_transfer && preserves_target_table_name {
// PostgreSQL-compatible targets restore indexes and foreign keys from structured
// source metadata once the table exists.
let indexes = get_postgres_indexes_for_transfer(
state,
source_pool_key,
&request.source_database,
&request.source_schema,
table,
)
.await?;
statements.extend(generate_postgres_index_ddl(
&indexes,
target_table,
&request.target_schema,
index_if_not_exists,
));
let foreign_keys = get_postgres_foreign_keys_for_transfer(
state,
source_pool_key,
&request.source_database,
&request.source_schema,
table,
)
.await?;
statements.extend(generate_postgres_foreign_key_ddl(
&foreign_keys,
target_table,
&request.source_schema,
&request.target_schema,
));
}
Ok(PlannedTableStructure { statements, deferred_fk_alters: prepared.deferred_fk_alters })
}
/// Read and de-duplicate the source columns the create pass uses, failing the same way the
/// pass does when the source returns nothing.
async fn source_transfer_columns(
state: &AppState,
request: &TransferRequest,
table: &str,
source_pool_key: &str,
) -> Result<Vec<db::ColumnInfo>, String> {
let raw = get_columns_for_transfer(
state,
source_pool_key,
&request.source_connection_id,
&request.source_database,
&request.source_schema,
table,
request.source_catalog.as_deref(),
)
.await?;
let mut seen = HashSet::new();
let columns = raw.into_iter().filter(|column| seen.insert(column.name.clone())).collect::<Vec<_>>();
if columns.is_empty() {
return Err(format!("No columns found for table {table}"));
}
Ok(columns)
}
/// Source table comment, via the same fallback chain the create pass uses. A metadata read
/// failure only drops the comment, exactly like execution.
async fn source_table_comment(
state: &Arc<AppState>,
request: &TransferRequest,
table: &str,
source_db_type: &DatabaseType,
) -> Option<String> {
if *source_db_type == DatabaseType::Postgres {
get_transfer_table_comment_isolated(
state.clone(),
request.source_connection_id.clone(),
request.source_database.clone(),
request.source_schema.clone(),
table.to_string(),
)
.await
.unwrap_or_default()
} else {
list_transfer_tables_isolated(
state.clone(),
request.source_connection_id.clone(),
request.source_database.clone(),
request.source_schema.clone(),
request.source_catalog.clone(),
*source_db_type,
table.to_string(),
1,
)
.await
.unwrap_or_default()
.into_iter()
.next()
.and_then(|info| info.comment)
}
}
/// Table creation order plus the foreign key metadata behind the deferred MySQL alters, using
/// the same helper (and the same external-catalog skip) the transfer pass uses.
async fn sorted_source_tables_with_foreign_keys(
state: &Arc<AppState>,
request: &TransferRequest,
) -> (Vec<String>, HashMap<String, Vec<db::ForeignKeyInfo>>) {
if request.tables.len() <= 1 {
return (request.tables.clone(), HashMap::new());
}
let skip_fk_sort = {
let configs = state.configs.read().await;
configs
.get(&request.source_connection_id)
.and_then(|config| resolve_external_transfer_catalog_for_config(request.source_catalog.as_deref(), config))
.is_some()
};
if skip_fk_sort {
return (request.tables.clone(), HashMap::new());
}
sort_tables_by_fk_dependency_with_foreign_keys(
state,
&request.source_connection_id,
&request.source_database,
&request.source_schema,
&request.tables,
true,
)
.await
.unwrap_or_else(|error| {
log::warn!("[transfer] structure preview could not sort tables by FK dependency: {error}");
(request.tables.clone(), HashMap::new())
})
}
async fn postgres_schema_exists(state: &AppState, target_pool_key: &str, target_schema: &str) -> Result<bool, String> {
let result = execute_on_pool(state, target_pool_key, &postgres_schema_exists_sql(target_schema)).await?;
Ok(query_result_has_rows(&result))
}
/// The statement block the preview never expands: selected non-table schema objects.
fn unexpanded_schema_object_notes(
source_db_type: &DatabaseType,
target_db_type: &DatabaseType,
request: &TransferRequest,
) -> Vec<String> {
if !should_transfer_schema_objects(source_db_type, target_db_type, &request.content, &request.objects) {
return Vec::new();
}
let mut notes = vec![UNEXPANDED_OBJECTS_NOTE.to_string()];
// An empty selection is the legacy PostgreSQL default (everything is transferred), so
// there is no concrete list to print — and the note above already says so.
for selection in &request.objects {
if selection.object_type == TransferObjectKind::Table || selection.names.is_empty() {
continue;
}
notes.push(format!("-- {}: {}", preview_object_kind_label(&selection.object_type), selection.names.join(", ")));
}
notes
}
fn preview_object_kind_label(kind: &TransferObjectKind) -> &'static str {
match kind {
TransferObjectKind::Table => "Tables",
TransferObjectKind::View => "Views",
TransferObjectKind::MaterializedView => "Materialized views",
TransferObjectKind::Procedure => "Procedures",
TransferObjectKind::Function => "Functions",
TransferObjectKind::Trigger => "Triggers",
TransferObjectKind::Sequence => "Sequences",
TransferObjectKind::Event => "Events",
}
}
fn statement_block(statements: &[String]) -> String {
statements
.iter()
.map(|statement| ensure_sql_statement_terminated(statement))
.filter(|statement| !statement.is_empty() && statement != ";")
.collect::<Vec<_>>()
.join("\n")
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
async fn sqlite_fixture() -> (tempfile::TempDir, Arc<AppState>, String, String) {
let directory = tempfile::tempdir().unwrap();
let storage = crate::persistence::test_storage::open(&directory.path().join("storage.db")).await.unwrap();
let state = Arc::new(AppState::new_with_plugin_dir(storage, directory.path().join("plugins")));
for id in ["source", "target"] {
let path = directory.path().join(format!("{id}.db"));
crate::db::sqlite::connect_path_create_if_missing(path.to_str().unwrap()).await.unwrap();
let config: ConnectionConfig = serde_json::from_value(json!({
"id": id, "name": id, "db_type": "sqlite", "host": path.to_str().unwrap(),
"port": 0, "username": "", "password": "", "database": null,
"one_time": false, "save_password": false, "read_only": false
}))
.unwrap();
state.configs.write().await.insert(id.to_string(), config);
}
let source_pool = ensure_transfer_pool(&state, "source", "main", None).await.unwrap();
let target_pool = ensure_transfer_pool(&state, "target", "main", None).await.unwrap();
for table in ["orders", "users"] {
execute_on_pool(&state, &source_pool, &format!("CREATE TABLE {table}(id INTEGER PRIMARY KEY, name TEXT)"))
.await
.unwrap();
}
(directory, state, source_pool, target_pool)
}
fn structure_request(overrides: serde_json::Value) -> TransferRequest {
let mut value = json!({
"transferId": uuid::Uuid::new_v4().to_string(),
"sourceConnectionId": "source", "sourceDatabase": "main", "sourceSchema": "main",
"targetConnectionId": "target", "targetDatabase": "main", "targetSchema": "main",
"tables": ["orders", "users"], "createTable": true, "content": "structureOnly",
"mode": "append", "batchSize": 10
});
let map = value.as_object_mut().unwrap();
for (key, override_value) in overrides.as_object().unwrap() {
map.insert(key.clone(), override_value.clone());
}
serde_json::from_value(value).unwrap()
}
async fn preview(
state: &Arc<AppState>,
request: &TransferRequest,
source_pool: &str,
target_pool: &str,
) -> Result<TransferOwnershipPreview, String> {
preview_transfer_ownership(
state,
request,
&DatabaseType::Sqlite,
&DatabaseType::Sqlite,
source_pool,
target_pool,
)
.await
}
async fn target_table_names(state: &AppState, target_pool: &str) -> Vec<String> {
let result = execute_read_on_pool(
state,
target_pool,
"SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name",
)
.await
.unwrap();
result.rows.iter().filter_map(|row| json_string_cell(row, 0)).collect()
}
#[tokio::test]
async fn structure_only_preview_plans_create_table_without_executing_ddl() {
let (_directory, state, source_pool, target_pool) = sqlite_fixture().await;
let request = structure_request(json!({}));
let preview = preview(&state, &request, &source_pool, &target_pool).await.unwrap();
let structure = preview.structure.expect("structure-only must expose its structure SQL plan");
assert_eq!(structure.tables.len(), 2, "every selected table must be planned: {:?}", structure.tables);
for table in &structure.tables {
assert!(!table.preexisting, "a fresh target has no preexisting tables: {table:?}");
assert!(table.sql.contains("CREATE TABLE"), "{}", table.sql);
}
assert!(structure.sql.contains("orders"), "{}", structure.sql);
assert!(structure.sql.contains("users"), "{}", structure.sql);
// Planning must not run any of it: the target database stays empty.
assert_eq!(target_table_names(&state, &target_pool).await, Vec::<String>::new());
}
#[tokio::test]
async fn structure_only_preview_handles_preexisting_target_consistently() {
let (_directory, state, source_pool, target_pool) = sqlite_fixture().await;
execute_on_pool(&state, &target_pool, "CREATE TABLE orders(id INTEGER PRIMARY KEY, legacy TEXT)")
.await
.unwrap();
execute_on_pool(&state, &target_pool, "INSERT INTO orders VALUES(7, 'original')").await.unwrap();
let request = structure_request(json!({ "tables": ["orders"] }));
let preview = preview(&state, &request, &source_pool, &target_pool).await.unwrap();
let structure = preview.structure.expect("structure-only must expose its structure SQL plan");
assert_eq!(structure.tables.len(), 1);
assert!(structure.tables[0].preexisting, "an existing target must be reported as preexisting: {structure:?}");
assert!(
!structure.sql.contains("CREATE TABLE"),
"execution skips create DDL for a preexisting target, so the preview must too: {}",
structure.sql
);
assert!(structure.sql.contains("already exists"), "{}", structure.sql);
// The preview must not have touched the preexisting table.
let rows = execute_read_on_pool(&state, &target_pool, "SELECT legacy FROM orders WHERE id = 7").await.unwrap();
assert_eq!(rows.rows[0][0], json!("original"));
}
#[tokio::test]
async fn structure_only_preview_without_create_table_reports_no_structure_ddl() {
let (_directory, state, source_pool, target_pool) = sqlite_fixture().await;
let request = structure_request(json!({ "createTable": false }));
let preview = preview(&state, &request, &source_pool, &target_pool).await.unwrap();
let structure = preview
.structure
.expect("structure-only always answers with a structure preview so clients can fail closed");
assert!(!structure.sql.contains("CREATE TABLE"), "{}", structure.sql);
assert!(structure.sql.contains("createTable = false"), "{}", structure.sql);
}
#[tokio::test]
async fn data_only_has_no_structure_preview() {
let (_directory, state, source_pool, target_pool) = sqlite_fixture().await;
let request = structure_request(json!({ "content": "dataOnly", "createTable": false }));
let preview = preview(&state, &request, &source_pool, &target_pool).await.unwrap();
assert!(preview.structure.is_none(), "data-only runs no structure DDL: {preview:?}");
assert!(preview.rebuild.is_none());
}
#[tokio::test]
async fn structure_and_data_keeps_existing_preview_behavior() {
let (_directory, state, source_pool, target_pool) = sqlite_fixture().await;
execute_on_pool(&state, &target_pool, "CREATE TABLE orders(id INTEGER PRIMARY KEY, legacy TEXT)")
.await
.unwrap();
let request = structure_request(json!({ "content": "structureAndData", "tables": ["orders"] }));
let preview = preview(&state, &request, &source_pool, &target_pool).await.unwrap();
assert!(
preview.structure.is_none(),
"structure-and-data keeps its current behavior in this phase: {preview:?}"
);
assert!(preview.rebuild.is_none());
assert!(preview.missing_owners.is_empty());
}
#[tokio::test]
async fn rebuild_preview_still_contains_existing_rename_and_cleanup_sql() {
let (_directory, state, source_pool, target_pool) = sqlite_fixture().await;
for table in ["orders", "users"] {
execute_on_pool(
&state,
&target_pool,
&format!("CREATE TABLE {table}(id INTEGER PRIMARY KEY, legacy TEXT)"),
)
.await
.unwrap();
}
let request = structure_request(json!({ "dropTargetBeforeCreate": true, "dropTargetConfirmed": true }));
let preview = preview(&state, &request, &source_pool, &target_pool).await.unwrap();
let rebuild = preview.rebuild.expect("a rebuild transfer must expose its SQL plan");
assert!(rebuild.sql.contains("-- 1. Backup existing target tables"), "{}", rebuild.sql);
assert!(rebuild.sql.contains("-- 2. Recreate the"), "{}", rebuild.sql);
assert!(rebuild.sql.contains("-- 3. Drop backups after success"), "{}", rebuild.sql);
assert!(rebuild.backup_sql.as_deref().unwrap_or_default().contains("RENAME"), "{rebuild:?}");
assert!(rebuild.cleanup_sql.as_deref().unwrap_or_default().contains("DROP TABLE"), "{rebuild:?}");
// The structure plan is rendered without repeating the rename/cleanup operations.
let structure = preview.structure.expect("structure-only must expose its structure SQL plan");
assert!(structure.sql.contains("CREATE TABLE"), "{}", structure.sql);
assert!(!structure.sql.contains("RENAME"), "{}", structure.sql);
assert!(!structure.sql.contains("DROP TABLE"), "{}", structure.sql);
for table in &structure.tables {
assert!(!table.preexisting, "a rebuild renames the old target aside, so it is created fresh: {table:?}");
}
// Still pure planning: nothing was renamed, nothing was dropped.
assert_eq!(target_table_names(&state, &target_pool).await, vec!["orders".to_string(), "users".to_string()]);
}
}
+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);
}