feat(transfer): add rebuild structure and data mode

This commit is contained in:
HolyeTyrone authored and GitHub committed 2026-09-08 16:46:39 +08:00
1 parent 2b2d1f0d95
commit 538aeed99c
37 files changed
+6720 -404

No files matched your search

+82 -20
View File
@@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
use crate::models::connection::DatabaseType;
use crate::sql_dialect::{
is_schema_aware, profile_for, qualified_table_name, quote_table_data_identifier, quote_table_identifier,
uses_connection_identifier_quote,
uses_connection_identifier_quote, DdlDialectProfile,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
@@ -415,35 +415,28 @@ pub fn build_drop_table_sql(options: TableAdminSqlOptions) -> String {
&options.table_name,
options.identifier_quote.as_deref(),
);
// IoTDB is the one engine whose drop target is a derived path pattern rather than the
// qualified name, so it cannot be expressed as a profile template.
if matches!(options.database_type, Some(DatabaseType::Iotdb)) {
return format!("DELETE TIMESERIES {};", iotdb_timeseries_pattern(&table));
} else if matches!(options.database_type, Some(DatabaseType::InfluxDb)) {
return format!("DROP MEASUREMENT {};", table);
}
// CASCADE is valid for PostgreSQL-family dialects; keep default RESTRICT behavior elsewhere.
let cascade = if options.cascade.unwrap_or(false) && supports_drop_table_cascade(options.database_type) {
" CASCADE"
} else {
""
};
format!("DROP TABLE {table}{cascade};")
// Unknown database type: fall back to the ANSI shape rather than guessing a profile.
let Some(database_type) = options.database_type else {
return format!("DROP TABLE {table}{cascade};");
};
DdlDialectProfile::render_template(
profile_for(database_type).drop_table_template,
&[("table", &table), ("cascade", cascade)],
)
}
fn supports_drop_table_cascade(database_type: Option<DatabaseType>) -> bool {
matches!(
database_type,
Some(
DatabaseType::Postgres
| DatabaseType::Redshift
| DatabaseType::Gaussdb
| DatabaseType::Kwdb
| DatabaseType::Kingbase
| DatabaseType::Highgo
| DatabaseType::Uxdb
| DatabaseType::Vastbase
| DatabaseType::OpenGauss
)
)
database_type.is_some_and(|database_type| profile_for(database_type).drop_table_supports_cascade)
}
pub fn build_drop_table_child_object_sql(options: DropTableChildObjectSqlOptions) -> Result<String, String> {
@@ -1012,7 +1005,11 @@ fn is_postgres_like_rename(database_type: DatabaseType) -> bool {
fn is_oracle_like_rename(database_type: DatabaseType) -> bool {
// 神通 Oscar 实测支持 `ALTER TABLE old RENAME TO new`(PG 风格,与 Dameng/Oracle 一致)。
matches!(database_type, DatabaseType::Oracle | DatabaseType::Dameng | DatabaseType::Oscar)
// OceanBase Oracle 模式同样接受该语法。
matches!(
database_type,
DatabaseType::Oracle | DatabaseType::Dameng | DatabaseType::OceanbaseOracle | DatabaseType::Oscar
)
}
fn is_postgres_like_structure_copy(database_type: DatabaseType) -> bool {
@@ -1803,6 +1800,57 @@ mod tests {
}
}
/// `build_drop_table_sql` renders `DdlDialectProfile::drop_table_template`, so the six
/// profile families plus DuckDB (which resolves to `conservative_ansi`, not the SQLite
/// family) must all produce a valid statement.
#[test]
fn builds_drop_table_sql_per_profile_family() {
let drop_sql = |database_type: DatabaseType, schema: Option<&str>, cascade: Option<bool>| {
build_drop_table_sql(TableAdminSqlOptions {
database_type: Some(database_type),
schema: schema.map(str::to_string),
table_name: "events".to_string(),
cascade,
identifier_quote: None,
})
};
assert_eq!(drop_sql(DatabaseType::Mysql, None, None), "DROP TABLE `events`;");
assert_eq!(drop_sql(DatabaseType::Postgres, Some("public"), None), "DROP TABLE \"public\".\"events\";");
assert_eq!(drop_sql(DatabaseType::Oracle, Some("APP"), None), "DROP TABLE \"APP\".\"events\";");
assert_eq!(drop_sql(DatabaseType::SqlServer, Some("dbo"), None), "DROP TABLE [dbo].[events];");
assert_eq!(drop_sql(DatabaseType::Sqlite, None, None), "DROP TABLE \"events\";");
assert_eq!(drop_sql(DatabaseType::DuckDb, None, None), "DROP TABLE \"events\";");
// No database type: the ANSI shape, with the default double-quote from
// `qualified_name_with_quote`, and no CASCADE.
assert_eq!(
build_drop_table_sql(TableAdminSqlOptions {
database_type: None,
schema: None,
table_name: "events".to_string(),
cascade: Some(true),
identifier_quote: None,
}),
"DROP TABLE \"events\";"
);
// CASCADE stays limited to the PostgreSQL-family profiles. Oracle spells it
// `CASCADE CONSTRAINTS` and is deliberately excluded; Firebird, Vertica and Exasol
// share the PostgreSQL profile but opt out.
assert_eq!(
drop_sql(DatabaseType::Postgres, Some("public"), Some(true)),
"DROP TABLE \"public\".\"events\" CASCADE;"
);
assert_eq!(drop_sql(DatabaseType::OpenGauss, None, Some(true)), "DROP TABLE \"events\" CASCADE;");
assert_eq!(drop_sql(DatabaseType::Firebird, None, Some(true)), "DROP TABLE \"events\";");
assert_eq!(drop_sql(DatabaseType::Vertica, None, Some(true)), "DROP TABLE \"events\";");
assert_eq!(drop_sql(DatabaseType::Oracle, None, Some(true)), "DROP TABLE \"events\";");
assert_eq!(drop_sql(DatabaseType::DuckDb, None, Some(true)), "DROP TABLE \"events\";");
// InfluxDB drops a measurement; the profile template carries the whole shape.
assert_eq!(drop_sql(DatabaseType::InfluxDb, None, None), "DROP MEASUREMENT \"events\";");
}
#[test]
fn builds_mysql_auto_increment_sql_and_rejects_invalid_values() {
for value in ["1", "9007199254740993", "18446744073709551615"] {
@@ -2614,6 +2662,20 @@ mod tests {
.unwrap(),
"ALTER VIEW \"SYSDBA\".\"ACTIVE_USERS\" RENAME TO \"ENABLED_USERS\";"
);
// OceanBase in Oracle mode accepts the same statement; without it the transfer
// rename-then-drop path has no way to back up a target table.
assert!(supports_object_rename(Some(DatabaseType::OceanbaseOracle), DatabaseObjectType::Table));
assert_eq!(
build_rename_object_sql(RenameObjectSqlOptions {
database_type: Some(DatabaseType::OceanbaseOracle),
object_type: DatabaseObjectType::Table,
schema: Some("APP".to_string()),
old_name: "ORDERS".to_string(),
new_name: "ORDERS__DBX_BAK".to_string(),
})
.unwrap(),
"ALTER TABLE \"APP\".\"ORDERS\" RENAME TO \"ORDERS__DBX_BAK\";"
);
}
#[test]
+1
View File
@@ -106,6 +106,7 @@ pub mod temporal_format;
pub mod text_export;
pub mod token_usage;
pub mod transfer;
pub mod transfer_rebuild;
pub mod two_phase_commit;
pub mod types;
pub mod update;
+21
View File
@@ -436,6 +436,9 @@ fn wrap_if_not_exists(sql: &str, db_type: DatabaseType) -> String {
let (prefix, suffix) = sql.split_at(idx);
format!("{prefix} IF NOT EXISTS{suffix}")
} else if upper.starts_with("DROP TABLE") {
if !profile.drop_table_supports_if_exists {
return sql.to_string();
}
if upper.contains("IF EXISTS") {
return sql.to_string();
}
@@ -1604,6 +1607,24 @@ mod tests {
assert!(result.contains("DROP TABLE IF EXISTS"), "Got: {result}");
}
/// Oracle before 23c rejects `DROP TABLE IF EXISTS`, so the wrapper must leave the
/// statement alone instead of generating invalid SQL.
#[test]
fn idempotent_drop_table_skips_if_exists_where_unsupported() {
for db_type in [DatabaseType::Oracle, DatabaseType::Db2, DatabaseType::Access] {
let result = apply_idempotent_strategy("DROP TABLE old_users;", db_type, IdempotentStrategy::IfNotExists);
assert_eq!(result, "DROP TABLE old_users;", "{db_type:?} must not gain IF EXISTS");
}
// The ConditionalCheck strategy falls back to the guarded comment form instead.
let conditional = apply_idempotent_strategy(
"DROP TABLE old_users;",
DatabaseType::Oracle,
IdempotentStrategy::ConditionalCheck,
);
assert!(!conditional.contains("IF EXISTS"), "Got: {conditional}");
assert!(conditional.contains("-- Conditional"), "Got: {conditional}");
}
#[test]
fn idempotent_drop_index_if_exists() {
let sql = "DROP INDEX idx_email;";
+148 -6
View File
@@ -118,6 +118,12 @@ pub struct DdlDialectProfile {
pub drop_index_uses_on_table: bool,
/// `DROP FOREIGN KEY` (MySQL) vs `DROP CONSTRAINT`.
pub drop_fk_as_foreign_key: bool,
/// `DROP TABLE t CASCADE` is accepted. Oracle's `CASCADE CONSTRAINTS` is a
/// different clause and does not count.
pub drop_table_supports_cascade: bool,
/// `DROP TABLE IF EXISTS t` is valid grammar. False for Oracle (pre-23c),
/// Access, Firebird, Db2, Informix, SAP HANA and Teradata.
pub drop_table_supports_if_exists: bool,
/// Index method placement.
pub index_type_placement: IndexTypePlacement,
/// `INCLUDE (cols)` on indexes.
@@ -165,6 +171,9 @@ pub struct DdlDialectProfile {
pub rule_drop_template: Option<&'static str>,
/// `ALTER {object_type} {name} OWNER TO {owner};`
pub owner_alter_template: Option<&'static str>,
/// `DROP TABLE {table}{cascade};` — the caller quotes `{table}` itself. Engines
/// that drop a differently-named object override the whole shape.
pub drop_table_template: &'static str,
/// Optional session lock-timeout preamble for generated scripts.
pub lock_timeout_sql: Option<&'static str>,
/// Data-driven base-type rewrites for this target (empty → rely on matrix / normalize only).
@@ -222,6 +231,9 @@ const SQLSERVER_SEQ_ALTER: &str =
"ALTER SEQUENCE {name} RESTART WITH {start_value} INCREMENT BY {increment} MINVALUE {min_value} MAXVALUE {max_value} {cycle};";
const RULE_DROP: &str = "DROP RULE IF EXISTS {rule_name} ON {table_name}{cascade};";
const OWNER_ALTER: &str = "ALTER {object_type} {name} OWNER TO {owner};";
/// Shared ANSI `DROP TABLE` shape. `{if_exists}` is deliberately absent: whether a
/// dialect accepts `IF EXISTS` is a capability flag, not part of the rendered shape.
const DROP_TABLE: &str = "DROP TABLE {table}{cascade};";
// ---------------------------------------------------------------------------
// Type maps (data only — no per-database logic at call sites)
@@ -325,6 +337,8 @@ fn mysql_family(db: DatabaseType) -> DdlDialectProfile {
foreign_keys_inline_in_create: false,
drop_index_uses_on_table: true,
drop_fk_as_foreign_key: true,
drop_table_supports_cascade: false,
drop_table_supports_if_exists: true,
index_type_placement: IndexTypePlacement::UsingBeforeOn,
index_supports_include: false,
index_supports_filter: false,
@@ -354,6 +368,7 @@ fn mysql_family(db: DatabaseType) -> DdlDialectProfile {
sequence_drop_template: None,
rule_drop_template: None,
owner_alter_template: None,
drop_table_template: DROP_TABLE,
lock_timeout_sql: Some("SET SESSION lock_wait_timeout = 3;"),
type_map: &[],
}
@@ -371,6 +386,8 @@ fn postgres_family(db: DatabaseType) -> DdlDialectProfile {
foreign_keys_inline_in_create: false,
drop_index_uses_on_table: false,
drop_fk_as_foreign_key: false,
drop_table_supports_cascade: true,
drop_table_supports_if_exists: true,
index_type_placement: IndexTypePlacement::UsingSuffix,
index_supports_include: true,
index_supports_filter: true,
@@ -400,6 +417,7 @@ fn postgres_family(db: DatabaseType) -> DdlDialectProfile {
sequence_drop_template: Some(SEQ_DROP),
rule_drop_template: Some(RULE_DROP),
owner_alter_template: Some(OWNER_ALTER),
drop_table_template: DROP_TABLE,
lock_timeout_sql: Some("SET lock_timeout = '3s';"),
type_map: &[],
}
@@ -420,6 +438,9 @@ fn oracle_family(db: DatabaseType) -> DdlDialectProfile {
foreign_keys_inline_in_create: false,
drop_index_uses_on_table: false,
drop_fk_as_foreign_key: false,
// Oracle spells it `CASCADE CONSTRAINTS`, and `IF EXISTS` only landed in 23c.
drop_table_supports_cascade: false,
drop_table_supports_if_exists: false,
index_type_placement: IndexTypePlacement::None,
index_supports_include: false,
index_supports_filter: false,
@@ -450,6 +471,7 @@ fn oracle_family(db: DatabaseType) -> DdlDialectProfile {
sequence_drop_template: Some(SEQ_DROP),
rule_drop_template: None,
owner_alter_template: None,
drop_table_template: DROP_TABLE,
lock_timeout_sql: Some("ALTER SESSION SET DDL_LOCK_TIMEOUT = 3;"),
type_map: &[],
}
@@ -467,6 +489,9 @@ fn sqlserver_family(db: DatabaseType) -> DdlDialectProfile {
foreign_keys_inline_in_create: false,
drop_index_uses_on_table: false,
drop_fk_as_foreign_key: false,
drop_table_supports_cascade: false,
// `DROP TABLE IF EXISTS` is valid on SQL Server 2016+.
drop_table_supports_if_exists: true,
index_type_placement: IndexTypePlacement::TypePrefix,
index_supports_include: true,
index_supports_filter: true,
@@ -496,6 +521,7 @@ fn sqlserver_family(db: DatabaseType) -> DdlDialectProfile {
sequence_drop_template: Some(SEQ_DROP),
rule_drop_template: None,
owner_alter_template: None,
drop_table_template: DROP_TABLE,
lock_timeout_sql: Some("SET LOCK_TIMEOUT 3000;"),
type_map: &[],
}
@@ -513,6 +539,8 @@ fn sqlite_family(db: DatabaseType) -> DdlDialectProfile {
foreign_keys_inline_in_create: true,
drop_index_uses_on_table: false,
drop_fk_as_foreign_key: false,
drop_table_supports_cascade: false,
drop_table_supports_if_exists: true,
index_type_placement: IndexTypePlacement::None,
index_supports_include: false,
index_supports_filter: true,
@@ -542,6 +570,7 @@ fn sqlite_family(db: DatabaseType) -> DdlDialectProfile {
sequence_drop_template: None,
rule_drop_template: None,
owner_alter_template: None,
drop_table_template: DROP_TABLE,
lock_timeout_sql: None,
type_map: SQLITE_TYPE_MAP,
}
@@ -559,6 +588,9 @@ fn access_profile() -> DdlDialectProfile {
foreign_keys_inline_in_create: false,
drop_index_uses_on_table: false,
drop_fk_as_foreign_key: false,
// Jet SQL has neither CASCADE nor IF EXISTS on DROP TABLE.
drop_table_supports_cascade: false,
drop_table_supports_if_exists: false,
index_type_placement: IndexTypePlacement::None,
index_supports_include: false,
index_supports_filter: false,
@@ -588,6 +620,7 @@ fn access_profile() -> DdlDialectProfile {
sequence_drop_template: None,
rule_drop_template: None,
owner_alter_template: None,
drop_table_template: DROP_TABLE,
lock_timeout_sql: None,
type_map: ACCESS_TYPE_MAP,
}
@@ -605,6 +638,10 @@ fn h2_profile() -> DdlDialectProfile {
foreign_keys_inline_in_create: false,
drop_index_uses_on_table: false,
drop_fk_as_foreign_key: false,
// H2 accepts CASCADE, but it stays off to keep the cascade set aligned with
// the frontend preview list in `dbAdminSql.ts`.
drop_table_supports_cascade: false,
drop_table_supports_if_exists: true,
index_type_placement: IndexTypePlacement::None,
index_supports_include: false,
index_supports_filter: false,
@@ -634,6 +671,7 @@ fn h2_profile() -> DdlDialectProfile {
sequence_drop_template: None,
rule_drop_template: None,
owner_alter_template: None,
drop_table_template: DROP_TABLE,
lock_timeout_sql: None,
type_map: &[],
}
@@ -656,6 +694,10 @@ fn conservative_ansi(db: DatabaseType) -> DdlDialectProfile {
foreign_keys_inline_in_create: false,
drop_index_uses_on_table: false,
drop_fk_as_foreign_key: false,
drop_table_supports_cascade: false,
// Most engines in this bucket accept `IF EXISTS`; `profile_for` opts out the
// ones that do not.
drop_table_supports_if_exists: true,
index_type_placement: IndexTypePlacement::None,
index_supports_include: false,
index_supports_filter: false,
@@ -685,6 +727,7 @@ fn conservative_ansi(db: DatabaseType) -> DdlDialectProfile {
sequence_drop_template: None,
rule_drop_template: None,
owner_alter_template: None,
drop_table_template: DROP_TABLE,
lock_timeout_sql: None,
type_map: &[],
}
@@ -698,6 +741,14 @@ fn clickhouse_profile() -> DdlDialectProfile {
p
}
fn influxdb_profile() -> DdlDialectProfile {
let mut p = conservative_ansi(DatabaseType::InfluxDb);
// InfluxDB drops a measurement, not a table, and has no IF EXISTS form.
p.drop_table_template = "DROP MEASUREMENT {table};";
p.drop_table_supports_if_exists = false;
p
}
/// Resolve DDL profile for a concrete target database type.
///
/// This is the **only** place that maps [`DatabaseType`] → profile data.
@@ -709,8 +760,19 @@ pub fn profile_for(db_type: DatabaseType) -> DdlDialectProfile {
Mysql | Doris | StarRocks | Goldendb | Sundb | Databend | Gbase | ManticoreSearch => mysql_family(db_type),
// PostgreSQL family
Postgres | Redshift | Gaussdb | Kingbase | Highgo | Vastbase | OpenGauss | Kwdb | Firebird | Vertica
| Exasol | Uxdb => postgres_family(db_type),
Postgres | Redshift | Gaussdb | Kingbase | Highgo | Vastbase | OpenGauss | Kwdb | Uxdb => {
postgres_family(db_type)
}
// PostgreSQL-shaped DDL, but not the PostgreSQL `DROP TABLE` grammar:
// Firebird has neither CASCADE nor IF EXISTS; Vertica and Exasol are kept off
// CASCADE so the set stays aligned with the frontend preview list.
Firebird | Vertica | Exasol => {
let mut profile = postgres_family(db_type);
profile.drop_table_supports_cascade = false;
profile.drop_table_supports_if_exists = db_type != Firebird;
profile
}
// Oracle family
Oracle | Dameng | OceanbaseOracle | Yashandb | Xugu | Iris => oracle_family(db_type),
@@ -728,13 +790,23 @@ pub fn profile_for(db_type: DatabaseType) -> DdlDialectProfile {
ClickHouse => clickhouse_profile(),
DuckDb | Questdb | SapHana | Teradata | Snowflake | Trino | PrestoSql | Hive | Kyuubi | Impala | Argo
| Spark | Db2 | Informix | Bigquery | Spanner | Kylin | Ignite | Ignite3 | Oscar | Tdengine | Iotdb
| Databricks | Jdbc => conservative_ansi(db_type),
// Engines whose DROP TABLE grammar has no `IF EXISTS`.
Db2 | Informix | SapHana | Teradata => {
let mut profile = conservative_ansi(db_type);
profile.drop_table_supports_if_exists = false;
profile
}
DuckDb | Questdb | Snowflake | Trino | PrestoSql | Hive | Kyuubi | Impala | Argo | Spark | Bigquery
| Spanner | Kylin | Ignite | Ignite3 | Oscar | Tdengine | Iotdb | Databricks | Jdbc => {
conservative_ansi(db_type)
}
InfluxDb => influxdb_profile(),
// Non-tabular / not applicable for relational CREATE TABLE
Redis | MongoDb | DynamoDb | Elasticsearch | Easysearch | Meilisearch | Qdrant | Milvus | Weaviate
| ChromaDb | Neo4j | Cassandra | Etcd | ZooKeeper | Nacos | Consul | InfluxDb | InfluxDb3 | VictoriaMetrics
| ChromaDb | Neo4j | Cassandra | Etcd | ZooKeeper | Nacos | Consul | InfluxDb3 | VictoriaMetrics
| MessageQueue | Mqtt | Hbase => conservative_ansi(db_type),
}
}
@@ -810,4 +882,74 @@ mod tests {
);
assert_eq!(rendered, "DROP FUNCTION IF EXISTS \"public\".\"f\" CASCADE;");
}
#[test]
fn drop_table_if_exists_support_matches_dialect_grammar() {
for db in [
DatabaseType::Mysql,
DatabaseType::Postgres,
DatabaseType::SqlServer,
DatabaseType::Sqlite,
DatabaseType::DuckDb,
DatabaseType::ClickHouse,
DatabaseType::H2,
DatabaseType::Snowflake,
] {
assert!(profile_for(db).drop_table_supports_if_exists, "{db:?} does support DROP TABLE IF EXISTS");
}
// Oracle only gained `IF EXISTS` in 23c; the rest have no such grammar at all.
for db in [
DatabaseType::Oracle,
DatabaseType::Dameng,
DatabaseType::OceanbaseOracle,
DatabaseType::Access,
DatabaseType::Firebird,
DatabaseType::Db2,
DatabaseType::Informix,
DatabaseType::SapHana,
DatabaseType::Teradata,
DatabaseType::InfluxDb,
] {
assert!(!profile_for(db).drop_table_supports_if_exists, "{db:?} does not support DROP TABLE IF EXISTS");
}
}
#[test]
fn drop_table_cascade_support_is_limited_to_the_postgres_grammar() {
for db in [
DatabaseType::Postgres,
DatabaseType::Redshift,
DatabaseType::Gaussdb,
DatabaseType::Kwdb,
DatabaseType::Kingbase,
DatabaseType::Highgo,
DatabaseType::Uxdb,
DatabaseType::Vastbase,
DatabaseType::OpenGauss,
] {
assert!(profile_for(db).drop_table_supports_cascade, "{db:?} should keep DROP TABLE … CASCADE");
}
// Oracle's `CASCADE CONSTRAINTS` is a different clause; Firebird, Vertica and Exasol
// share the PostgreSQL profile but stay out of the cascade set so the backend matches
// the frontend preview list in `dbAdminSql.ts`.
for db in [
DatabaseType::Oracle,
DatabaseType::Firebird,
DatabaseType::Vertica,
DatabaseType::Exasol,
DatabaseType::Mysql,
DatabaseType::Sqlite,
DatabaseType::DuckDb,
DatabaseType::SqlServer,
DatabaseType::H2,
] {
assert!(!profile_for(db).drop_table_supports_cascade, "{db:?} must not emit DROP TABLE … CASCADE");
}
}
#[test]
fn drop_table_template_carries_the_whole_statement_shape() {
assert_eq!(profile_for(DatabaseType::Postgres).drop_table_template, "DROP TABLE {table}{cascade};");
assert_eq!(profile_for(DatabaseType::InfluxDb).drop_table_template, "DROP MEASUREMENT {table};");
}
}
File diff suppressed because it is too large. Load diff
+420
View File
@@ -0,0 +1,420 @@
//! 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.
use super::*;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(super) struct PreparedTableDdl {
pub ddl: String,
pub reused_source_ddl: bool,
pub deferred_fk_alters: Vec<String>,
}
#[allow(clippy::too_many_arguments)]
pub(super) async fn prepare_table_ddl(
state: &AppState,
request: &TransferRequest,
table: &str,
target_table: &str,
source_db_type: &DatabaseType,
target_db_type: &DatabaseType,
source_pool_key: &str,
columns: &[db::ColumnInfo],
table_comment: Option<&str>,
known_foreign_keys: &HashMap<String, Vec<db::ForeignKeyInfo>>,
) -> Result<PreparedTableDdl, String> {
let rebuild = request.drop_target_before_create;
let foreign_keys = if let Some(foreign_keys) = known_foreign_keys.get(table) {
foreign_keys.clone()
} else if rebuild {
// Strict for rebuilds: silently dropping foreign keys the inspection failed on
// would produce a rebuilt table that enforces fewer constraints than the source.
crate::schema::list_foreign_keys_core(
state,
&request.source_connection_id,
&request.source_database,
&request.source_schema,
table,
)
.await
.map_err(|e| format!("Failed to inspect source foreign keys for table '{table}' before rebuilding: {e}"))?
} else if supports_deferred_mysql_foreign_keys(target_db_type) {
// Ordinary MySQL-family targets keep the legacy best-effort behavior: fall back
// to the generated DDL without foreign keys when the inspection fails. Other
// targets never use the foreign keys, so no query is issued at all.
match crate::schema::list_foreign_keys_core(
state,
&request.source_connection_id,
&request.source_database,
&request.source_schema,
table,
)
.await
{
Ok(foreign_keys) => foreign_keys,
Err(e) => {
log::warn!("[transfer] failed to inspect source foreign keys for {table}: {e}");
Vec::new()
}
}
} else {
Vec::new()
};
let (source_driver_profile, target_driver_profile) = {
let configs = state.configs.read().await;
(
configs.get(&request.source_connection_id).and_then(|config| config.driver_profile.clone()),
configs.get(&request.target_connection_id).and_then(|config| config.driver_profile.clone()),
)
};
let can_reuse = can_reuse_source_table_ddl(
source_db_type,
target_db_type,
source_driver_profile.as_deref(),
target_driver_profile.as_deref(),
target_table == table,
) && (request.quote_target_column_names
|| !matches!(target_db_type, DatabaseType::Gaussdb | DatabaseType::OpenGauss));
// A rebuild recreates the source structure exactly. When the structure comes from
// the source DDL (the `can_reuse` path) the column list is only used for data
// mapping, so an empty list is fine — the DDL is read directly from the source. Only
// when the DDL must be *generated* from the column list does incomplete metadata
// become fatal, because there would be nothing to build the CREATE TABLE from.
if rebuild && !can_reuse {
let incomplete = columns.is_empty()
|| columns.iter().any(|column| column.name.trim().is_empty() || column.data_type.trim().is_empty());
if incomplete {
return Err(format!(
"Cannot rebuild table '{target_table}': the source returned incomplete column metadata, and the \
target cannot reuse the source DDL."
));
}
}
let mut reused_source_ddl = false;
let ddl = if can_reuse {
let (source_ddl, source_ddl_was_read) = if let Some(catalog) =
resolve_external_transfer_catalog(request.source_catalog.as_deref(), source_db_type)
{
// Doris/StarRocks external catalog: read DDL directly via
// SHOW CREATE TABLE catalog.database.table using the existing source
// pool (bare MySQL — addresses any catalog).
let pool = {
let pool =
state.pool_handle(source_pool_key).await.ok_or_else(|| "Source pool not found".to_string())?;
let PoolKind::Mysql(p, _) = &pool else {
return Err("Source pool must be MySQL-family for catalog DDL".to_string());
};
p.clone()
};
match db::doris::get_catalog_table_ddl(&pool, catalog, &request.source_database, table).await {
Ok(ddl) => (ddl, true),
Err(err) => {
log::warn!("[transfer] catalog DDL read failed for {table} in catalog '{catalog}': {err}; falling back to generated DDL");
(
generate_create_table_ddl_with_column_quoting(
columns,
target_table,
&request.source_schema,
&request.target_schema,
target_db_type,
source_db_type,
table_comment,
request.target_catalog.as_deref(),
request.quote_target_column_names,
),
false,
)
}
}
} else {
match crate::schema::get_table_ddl_core(
state,
&request.source_connection_id,
&request.source_database,
&request.source_schema,
table,
None,
)
.await
{
Ok(ddl) => (ddl, true),
Err(e) if rebuild => {
// A rebuild cannot fall back to generated DDL when the source DDL
// it promised to reproduce cannot be read.
return Err(format!("Failed to read source DDL for table '{table}' before rebuilding: {e}"));
}
Err(_) => (
generate_create_table_ddl_with_column_quoting(
columns,
target_table,
&request.source_schema,
&request.target_schema,
target_db_type,
source_db_type,
table_comment,
request.target_catalog.as_deref(),
request.quote_target_column_names,
),
false,
),
}
};
if contains_oceanbase_mysql_table_options(&source_ddl)
&& !db::oceanbase_mysql::is_profile(target_db_type, target_driver_profile.as_deref())
{
generate_create_table_ddl_with_column_quoting(
columns,
target_table,
&request.source_schema,
&request.target_schema,
target_db_type,
source_db_type,
table_comment,
request.target_catalog.as_deref(),
request.quote_target_column_names,
)
} else {
reused_source_ddl = source_ddl_was_read;
rewrite_transfer_source_table_ddl(
&source_ddl,
&request.source_schema,
&request.target_schema,
source_db_type,
target_db_type,
)
}
} else {
generate_create_table_ddl_with_column_quoting(
columns,
target_table,
&request.source_schema,
&request.target_schema,
target_db_type,
source_db_type,
table_comment,
request.target_catalog.as_deref(),
request.quote_target_column_names,
)
};
// MySQL-family targets: defer the foreign keys to ALTER statements so the table
// creation order never has to satisfy foreign key dependencies (a foreign key cycle
// has no valid CREATE TABLE order at all). The rebuild path above guarantees the
// metadata behind `foreign_keys` is trustworthy before anything is stripped.
let mut ddl = ddl;
let mut deferred_fk_alters = Vec::new();
if supports_deferred_mysql_foreign_keys(target_db_type) && !foreign_keys.is_empty() {
ddl = strip_inline_foreign_key_constraint_lines(&ddl);
deferred_fk_alters =
generate_mysql_foreign_key_alter_statements(&foreign_keys, request, target_table, target_db_type);
}
Ok(PreparedTableDdl { ddl, reused_source_ddl, deferred_fk_alters })
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
async fn state_fixture() -> (Arc<AppState>, tempfile::TempDir) {
let directory = tempfile::tempdir().unwrap();
let storage = crate::storage::Storage::open(&directory.path().join("storage.db")).await.unwrap();
let state = Arc::new(AppState::new_with_plugin_dir(storage, directory.path().join("plugins")));
(state, directory)
}
fn request(rebuild: bool) -> TransferRequest {
serde_json::from_value(json!({
"transferId": "ddl-plan-test",
"sourceConnectionId": "source", "sourceDatabase": "main", "sourceSchema": "main",
"targetConnectionId": "target", "targetDatabase": "main", "targetSchema": "main",
"tables": ["child", "Parent"], "createTable": true, "content": "structureAndData",
"mode": "append", "batchSize": 10, "dropTargetBeforeCreate": rebuild,
"dropTargetConfirmed": true
}))
.unwrap()
}
fn columns() -> Vec<db::ColumnInfo> {
vec![
db::ColumnInfo {
name: "id".into(),
data_type: "INTEGER".into(),
is_primary_key: true,
..Default::default()
},
db::ColumnInfo { name: "parent_id".into(), data_type: "INTEGER".into(), ..Default::default() },
]
}
#[tokio::test]
async fn prepared_sqlite_ddl_preserves_source_foreign_keys_without_mutating_source() {
let (state, directory) = state_fixture().await;
let path = directory.path().join("source.db");
let pool = crate::db::sqlite::connect_path_create_if_missing(path.to_str().unwrap()).await.unwrap();
pool.with_connection(|connection| {
connection
.execute_batch(
"PRAGMA foreign_keys = ON;
CREATE TABLE Parent (id INTEGER PRIMARY KEY);
CREATE TABLE child (id INTEGER PRIMARY KEY, parent_id INTEGER REFERENCES Parent(id) ON DELETE CASCADE);
INSERT INTO Parent VALUES (7);
INSERT INTO child VALUES (11, 7);",
)
.map_err(|error| error.to_string())
})
.unwrap();
let config: ConnectionConfig = serde_json::from_value(json!({
"id": "source", "name": "source", "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("source".into(), config);
let source_pool_key = ensure_transfer_pool(&state, "source", "main", None).await.unwrap();
let columns = crate::db::sqlite::get_columns(&pool, "main", "child").await.unwrap();
let prepared = prepare_table_ddl(
&state,
&request(true),
"child",
"child",
&DatabaseType::Sqlite,
&DatabaseType::Sqlite,
&source_pool_key,
&columns,
None,
&HashMap::new(),
)
.await
.unwrap();
let source_rows = crate::db::sqlite::execute_query(&pool, "SELECT id, parent_id FROM child").await.unwrap();
assert_eq!(source_rows.rows, vec![vec![json!(11), json!(7)]]);
let target =
crate::db::sqlite::connect_path_create_if_missing(directory.path().join("target.db").to_str().unwrap())
.await
.unwrap();
target
.with_connection(|connection| {
connection
.execute_batch("PRAGMA foreign_keys = ON; CREATE TABLE Parent (id INTEGER PRIMARY KEY);")
.map_err(|error| error.to_string())?;
connection.execute_batch(&prepared.ddl).map_err(|error| error.to_string())
})
.unwrap();
let foreign_keys = crate::db::sqlite::list_foreign_keys(&target, "main", "child").await.unwrap();
assert_eq!(foreign_keys.len(), 1, "source FK must survive the prepared CREATE TABLE: {}", prepared.ddl);
assert_eq!(foreign_keys[0].ref_table, "Parent");
assert!(crate::db::sqlite::execute_query(&target, "INSERT INTO child VALUES (1, 999)").await.is_err());
assert!(prepared.reused_source_ddl);
}
#[tokio::test]
async fn rebuild_rejects_failed_foreign_key_inspection() {
let (state, _directory) = state_fixture().await;
let error = prepare_table_ddl(
&state,
&request(true),
"child",
"child",
&DatabaseType::Sqlite,
&DatabaseType::Mysql,
"missing-source-pool",
&columns(),
None,
&HashMap::new(),
)
.await
.expect_err("a rebuild cannot discard foreign keys when source metadata is unavailable");
assert!(error.contains("child"), "{error}");
assert!(error.to_ascii_lowercase().contains("foreign key"), "{error}");
}
#[tokio::test]
async fn ordinary_foreign_key_metadata_failure_preserves_generated_fallback() {
let (state, _directory) = state_fixture().await;
let prepared = prepare_table_ddl(
&state,
&request(false),
"child",
"child",
&DatabaseType::Sqlite,
&DatabaseType::Mysql,
"missing-source-pool",
&columns(),
None,
&HashMap::new(),
)
.await
.unwrap();
assert!(!prepared.reused_source_ddl);
assert!(prepared.ddl.contains("CREATE TABLE"));
}
#[tokio::test]
async fn deferred_foreign_key_uses_actual_target_and_case_converted_parent() {
let (state, _directory) = state_fixture().await;
let mut request = request(true);
request.target_table_name_case = TransferTableNameCase::Lower;
let known = HashMap::from([(
"child".into(),
vec![db::ForeignKeyInfo {
name: "fk_child_parent".into(),
column: "parent_id".into(),
ref_schema: Some("main".into()),
ref_table: "Parent".into(),
ref_column: "id".into(),
on_delete: Some("CASCADE".into()),
on_update: Some("RESTRICT".into()),
}],
)]);
let prepared = prepare_table_ddl(
&state,
&request,
"child",
"resolved_child",
&DatabaseType::Sqlite,
&DatabaseType::Mysql,
"missing-source-pool",
&columns(),
None,
&known,
)
.await
.unwrap();
assert_eq!(prepared.deferred_fk_alters, vec![
"ALTER TABLE `resolved_child` ADD CONSTRAINT `fk_child_parent` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT"
]);
assert!(prepared.ddl.contains("`resolved_child`"), "{}", prepared.ddl);
}
#[tokio::test]
async fn rebuild_requires_complete_column_metadata() {
let (state, _directory) = state_fixture().await;
let incomplete = vec![db::ColumnInfo { name: "id".into(), ..Default::default() }];
for columns in [&[][..], incomplete.as_slice()] {
let result = prepare_table_ddl(
&state,
&request(true),
"child",
"child",
&DatabaseType::Sqlite,
&DatabaseType::Mysql,
"missing-source-pool",
columns,
None,
&HashMap::from([("child".into(), Vec::new())]),
)
.await;
assert!(result.is_err(), "incomplete source metadata must fail before any target DDL: {result:?}");
}
}
}
@@ -0,0 +1,116 @@
use super::*;
use serde_json::json;
async fn sqlite_fixture() -> (tempfile::TempDir, Arc<AppState>, TransferRequest, String, String) {
let directory = tempfile::tempdir().unwrap();
let storage = crate::storage::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();
execute_on_pool(&state, &source_pool, &format!("INSERT INTO {table} VALUES(1, 'source')")).await.unwrap();
execute_on_pool(&state, &target_pool, &format!("CREATE TABLE {table}(id INTEGER PRIMARY KEY, old_value TEXT)"))
.await
.unwrap();
execute_on_pool(&state, &target_pool, &format!("INSERT INTO {table} VALUES(99, 'original')")).await.unwrap();
}
let request: TransferRequest = serde_json::from_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": "structureAndData",
"mode": "append", "batchSize": 1, "dropTargetBeforeCreate": true,
"dropTargetConfirmed": true
}))
.unwrap();
(directory, state, request, source_pool, target_pool)
}
#[tokio::test]
async fn transfer_rebuild_preflights_every_backup_collision_before_renaming_any_table() {
let (_directory, state, request, _, target_pool) = sqlite_fixture().await;
let backup =
crate::transfer_rebuild::backup_table_name(DatabaseType::Sqlite, &request.transfer_id, "main.users", "users")
.unwrap();
execute_on_pool(&state, &target_pool, &format!("CREATE TABLE \"{backup}\"(id INTEGER)")).await.unwrap();
let result =
rename_tables_to_backup(&state, &request, &request.tables, DatabaseType::Sqlite, &target_pool, |_| {}).await;
assert!(result.is_err(), "a preexisting backup must block the operation");
let original = execute_read_on_pool(&state, &target_pool, "SELECT old_value FROM orders WHERE id=99").await;
assert!(original.is_ok(), "a collision on users must not rename orders: {original:?}");
assert_eq!(original.unwrap().rows[0][0], json!("original"));
}
#[tokio::test]
async fn transfer_rebuild_cancellation_remains_a_cancellation_after_backup() {
let (_directory, state, mut request, source_pool, target_pool) = sqlite_fixture().await;
request.tables = vec!["orders".into()];
let backups =
rename_tables_to_backup(&state, &request, &request.tables, DatabaseType::Sqlite, &target_pool, |_| {})
.await
.unwrap();
set_cancelled(&request.transfer_id).await;
let result = transfer_table(
&state,
&request,
"orders",
0,
&DatabaseType::Sqlite,
&DatabaseType::Sqlite,
&source_pool,
&target_pool,
&HashMap::new(),
&mut Vec::new(),
Some(&backups),
|_| {},
)
.await;
clear_cancelled(&request.transfer_id).await;
assert_eq!(result.unwrap_err(), "Cancelled", "recovery details must not change the cancellation discriminator");
}
#[tokio::test]
async fn transfer_rebuild_preview_plans_without_executing_ddl() {
let (_directory, state, request, source_pool, target_pool) = sqlite_fixture().await;
let preview = preview_transfer_ownership(
&state,
&request,
&DatabaseType::Sqlite,
&DatabaseType::Sqlite,
&source_pool,
&target_pool,
)
.await
.unwrap();
let rebuild = preview.rebuild.expect("a rebuild transfer must expose its SQL plan");
assert_eq!(rebuild.tables.len(), 2, "every source table must be mapped: {:?}", rebuild.tables);
for table in &rebuild.tables {
assert!(table.backup_table.is_some(), "preexisting targets must carry a backup name: {table:?}");
}
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);
// The preview must be pure planning: neither table may have been renamed aside.
for table in ["orders", "users"] {
let rows =
execute_read_on_pool(&state, &target_pool, &format!("SELECT old_value FROM {table} WHERE id=99")).await;
assert!(rows.is_ok(), "preview must not rename {table}: {rows:?}");
assert_eq!(rows.unwrap().rows[0][0], json!("original"));
}
}
File diff suppressed because it is too large. Load diff