Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

feat(xugu): support native MySQL table and data transfers

Elias0cb4a7590eaf
diff --git a/apps/desktop/src/components/transfer/DataTransferDialog.vue b/apps/desktop/src/components/transfer/DataTransferDialog.vue
index 68e4930dc4..d46bd3f1db 100644
--- a/apps/desktop/src/components/transfer/DataTransferDialog.vue
+++ b/apps/desktop/src/components/transfer/DataTransferDialog.vue
@@ -161,11 +161,13 @@ const treeSelection = computed<Record<string, string[]>>({
});
const treeGroups = computed(() =>
- (Object.keys(objectGroups.value) as TransferObjectKind[]).map((kind) => ({
- kind,
- label: t(OBJECT_KIND_LABEL_KEY[kind]),
- items: objectGroups.value[kind] ?? [],
- })),
+ (Object.keys(objectGroups.value) as TransferObjectKind[])
+ .filter((kind) => !(sourceTransferType.value === "mysql" && targetTransferType.value === "xugu") || kind === "TABLE")
+ .map((kind) => ({
+ kind,
+ label: t(OBJECT_KIND_LABEL_KEY[kind]),
+ items: objectGroups.value[kind] ?? [],
+ })),
);
const treeDisabledGroups = computed<TransferObjectKind[]>(() => {
@@ -335,7 +337,7 @@ function requestedTableFilters(): Record<string, string> | undefined {
const sourceTransferType = computed(() => transferDatabaseTypeForConnection(store.getConfig(sourceConnectionId.value)));
const targetTransferType = computed(() => transferDatabaseTypeForConnection(store.getConfig(targetConnectionId.value)));
-const isXuguTransferPair = computed(() => sourceTransferType.value === "xugu" && targetTransferType.value === "xugu");
+const isXuguTransferPair = computed(() => targetTransferType.value === "xugu" && (sourceTransferType.value === "xugu" || sourceTransferType.value === "mysql"));
function structureOverwriteSupported(): boolean {
if (transferContent.value !== "structureOnly") return true;
diff --git a/apps/desktop/src/i18n/locales/en.ts b/apps/desktop/src/i18n/locales/en.ts
index 557a012215..9e7bdd21b3 100644
--- a/apps/desktop/src/i18n/locales/en.ts
+++ b/apps/desktop/src/i18n/locales/en.ts
@@ -8587,7 +8587,7 @@ export default {
objectTypeEvent: "Events",
objectDataOnlyDisabled: "Non-table objects are not transferred in data-only mode",
objectCrossFamilyDisabled: "Cross-database transfer supports sequences only (plain DDL; view query bodies cannot be translated)",
- xuguPairOnly: "Xugu transfer currently supports Xugu-to-Xugu only. Select Xugu connections for both source and target.",
+ xuguPairOnly: "Supported Xugu directions: Xugu to Xugu, or MySQL to Xugu (tables and data only).",
xuguAppendOverwriteOnly: "The first Xugu transfer phase supports append and overwrite only; upsert and rebuild are not supported yet.",
xuguPreserveTableNames: "Xugu structure transfer currently requires preserving source table-name casing to avoid mismatched foreign-key references.",
crossFamilyViewHint: "Cross-database view transfer: select the tables the view depends on as well, or make sure they already exist in the target database",
diff --git a/apps/desktop/src/i18n/locales/es.ts b/apps/desktop/src/i18n/locales/es.ts
index e92bdec5d3..569c853c7a 100644
--- a/apps/desktop/src/i18n/locales/es.ts
+++ b/apps/desktop/src/i18n/locales/es.ts
@@ -7530,7 +7530,7 @@ export default withEnglishFallback({
objectTypeEvent: "Eventos",
objectDataOnlyDisabled: "Los objetos que no son tablas no se transfieren en el modo de solo datos",
objectCrossFamilyDisabled: "La transferencia entre bases de datos solo admite secuencias (DDL simple; los cuerpos de las vistas no se traducen)",
- xuguPairOnly: "La transferencia de Xugu solo admite conexiones de Xugu a Xugu. Selecciona una conexión Xugu tanto para el origen como para el destino.",
+ xuguPairOnly: "Direcciones compatibles: Xugu a Xugu, o MySQL a Xugu (solo tablas y datos).",
xuguAppendOverwriteOnly: "La primera fase de transferencia de Xugu solo admite agregar y sobrescribir; aún no admite upsert ni reconstrucción.",
xuguPreserveTableNames: "La transferencia de estructura de Xugu requiere conservar las mayúsculas y minúsculas de los nombres de las tablas de origen para evitar referencias incorrectas en las claves foráneas.",
crossFamilyViewHint: "Transferencia de vistas entre bases de datos: seleccione también las tablas de las que depende la vista o asegúrese de que ya existen en la base de datos de destino",
diff --git a/apps/desktop/src/i18n/locales/it.ts b/apps/desktop/src/i18n/locales/it.ts
index 4d4996c674..9c6c608fc4 100644
--- a/apps/desktop/src/i18n/locales/it.ts
+++ b/apps/desktop/src/i18n/locales/it.ts
@@ -7343,7 +7343,7 @@ export default withEnglishFallback({
objectTypeEvent: "Eventi",
objectDataOnlyDisabled: "Gli oggetti non di tabella non vengono trasferiti nella modalità solo dati",
objectCrossFamilyDisabled: "Il trasferimento tra database supporta solo sequenze (DDL semplice; i corpi delle viste non vengono tradotti)",
- xuguPairOnly: "Il trasferimento Xugu supporta attualmente solo connessioni da Xugu a Xugu. Seleziona connessioni Xugu sia per l'origine sia per la destinazione.",
+ xuguPairOnly: "Direzioni supportate: da Xugu a Xugu o da MySQL a Xugu (solo tabelle e dati).",
xuguAppendOverwriteOnly: "La prima fase del trasferimento Xugu supporta solo l'aggiunta e la sovrascrittura; upsert e ricostruzione non sono ancora supportati.",
xuguPreserveTableNames: "Il trasferimento della struttura Xugu richiede di mantenere maiuscole e minuscole dei nomi delle tabelle di origine per evitare riferimenti errati alle chiavi esterne.",
crossFamilyViewHint: "Trasferimento di viste tra database: selezionare anche le tabelle da cui la vista dipende, o assicurarsi che esistano già nel database di destinazione",
diff --git a/apps/desktop/src/i18n/locales/ja.ts b/apps/desktop/src/i18n/locales/ja.ts
index 64c646d212..0bcc9499ed 100644
--- a/apps/desktop/src/i18n/locales/ja.ts
+++ b/apps/desktop/src/i18n/locales/ja.ts
@@ -7355,7 +7355,7 @@ export default withEnglishFallback({
objectTypeEvent: "イベント",
objectDataOnlyDisabled: "データのみモードではテーブル以外のオブジェクトは転送されません",
objectCrossFamilyDisabled: "データベース間転送はシーケンスのみサポートします(単純なDDL。ビューの本文は翻訳されません)",
- xuguPairOnly: "Xugu の転送は現在、Xugu から Xugu への接続のみをサポートしています。ソースとターゲットの両方に Xugu 接続を選択してください。",
+ xuguPairOnly: "対応する転送方向:Xugu から Xugu、または MySQL から Xugu(テーブル定義とデータのみ)。",
xuguAppendOverwriteOnly: "Xugu 転送の第 1 段階では追加と上書きのみをサポートしています。upsert と再構築はまだサポートされていません。",
xuguPreserveTableNames: "外部キー参照の不一致を防ぐため、Xugu の構造転送ではソーステーブル名の大文字・小文字を保持する必要があります。",
crossFamilyViewHint: "データベース間ビュー転送:ビューが依存するテーブルも選択するか、ターゲットDBに既に存在することを確認してください",
diff --git a/apps/desktop/src/i18n/locales/ko.ts b/apps/desktop/src/i18n/locales/ko.ts
index 996467b77a..c31f2c02d6 100644
--- a/apps/desktop/src/i18n/locales/ko.ts
+++ b/apps/desktop/src/i18n/locales/ko.ts
@@ -7373,7 +7373,7 @@ export default withEnglishFallback({
objectTypeEvent: "이벤트",
objectDataOnlyDisabled: "데이터 전용 모드에서는 테이블이 아닌 개체는 전송되지 않습니다",
objectCrossFamilyDisabled: "데이터베이스 간 전송은 시퀀스만 지원합니다(단순 DDL, 뷰 본문은 변환되지 않음)",
- xuguPairOnly: "현재 Xugu 전송은 Xugu 간 연결만 지원합니다. 원본과 대상 모두 Xugu 연결을 선택하세요.",
+ xuguPairOnly: "지원되는 전송 방향: Xugu에서 Xugu 또는 MySQL에서 Xugu(테이블 구조 및 데이터만).",
xuguAppendOverwriteOnly: "Xugu 전송 1단계에서는 추가와 덮어쓰기만 지원하며, upsert와 재구성은 아직 지원하지 않습니다.",
xuguPreserveTableNames: "외래 키 참조가 잘못되는 것을 방지하려면 Xugu 구조 전송 시 원본 테이블 이름의 대소문자를 유지해야 합니다.",
crossFamilyViewHint: "데이터베이스 간 뷰 전송: 뷰가 의존하는 테이블도 선택하거나 대상 DB에 이미 존재하는지 확인하세요",
diff --git a/apps/desktop/src/i18n/locales/pl.ts b/apps/desktop/src/i18n/locales/pl.ts
index 329ead1734..90c5bbe338 100644
--- a/apps/desktop/src/i18n/locales/pl.ts
+++ b/apps/desktop/src/i18n/locales/pl.ts
@@ -7071,7 +7071,7 @@ export default withEnglishFallback({
objectTypeEvent: "Zdarzenia",
objectDataOnlyDisabled: "Obiekty inne niż tabele nie są transferowane w trybie samych danych",
objectCrossFamilyDisabled: "Transfer między różnymi silnikami baz obsługuje tylko sekwencje (zwykły DDL; treść zapytań widoków nie może być tłumaczona)",
- xuguPairOnly: "Przesyłanie danych Xugu jest obecnie możliwe tylko między bazami Xugu. Wybierz połączenia Xugu jako źródło i cel.",
+ xuguPairOnly: "Obsługiwane kierunki: Xugu do Xugu lub MySQL do Xugu (tylko tabele i dane).",
xuguAppendOverwriteOnly: "Pierwszy etap transferu Xugu obsługuje tylko dopisywanie i nadpisywanie. Aktualizacja lub ponowne tworzenie nie są jeszcze obsługiwane.",
xuguPreserveTableNames: "Transfer struktury Xugu wymaga obecnie zachowania wielkości liter w nazwach tabel źródłowych, aby uniknąć błędnych odwołań kluczy obcych.",
crossFamilyViewHint: "Transfer widoków między różnymi bazami: zaznacz również tabele, od których zależy widok, lub upewnij się, że istnieją już w docelowej bazie",
diff --git a/apps/desktop/src/i18n/locales/pt-BR.ts b/apps/desktop/src/i18n/locales/pt-BR.ts
index 34ccebdfd8..6a50e2bad4 100644
--- a/apps/desktop/src/i18n/locales/pt-BR.ts
+++ b/apps/desktop/src/i18n/locales/pt-BR.ts
@@ -7343,7 +7343,7 @@ export default withEnglishFallback({
objectTypeEvent: "Eventos",
objectDataOnlyDisabled: "Objetos que não são tabelas não são transferidos no modo apenas dados",
objectCrossFamilyDisabled: "A transferência entre bancos suporta apenas sequências (DDL simples; corpos de views não são traduzidos)",
- xuguPairOnly: "No momento, a transferência do Xugu aceita apenas conexões de Xugu para Xugu. Selecione conexões Xugu para a origem e o destino.",
+ xuguPairOnly: "Direções compatíveis: Xugu para Xugu ou MySQL para Xugu (somente tabelas e dados).",
xuguAppendOverwriteOnly: "A primeira fase da transferência do Xugu aceita apenas inclusão e sobrescrita; upsert e reconstrução ainda não são compatíveis.",
xuguPreserveTableNames: "A transferência de estrutura do Xugu exige preservar maiúsculas e minúsculas nos nomes das tabelas de origem para evitar referências incorretas de chaves estrangeiras.",
crossFamilyViewHint: "Transferência de views entre bancos: selecione também as tabelas das quais a view depende ou garanta que elas já existam no banco de destino",
diff --git a/apps/desktop/src/i18n/locales/ru.ts b/apps/desktop/src/i18n/locales/ru.ts
index 5486271a6c..48707d6ce0 100644
--- a/apps/desktop/src/i18n/locales/ru.ts
+++ b/apps/desktop/src/i18n/locales/ru.ts
@@ -8807,7 +8807,7 @@ export default withEnglishFallback({
objectTypeEvent: "События",
objectDataOnlyDisabled: "Объекты, не являющиеся таблицами, не переносятся в режиме «только данные»",
objectCrossFamilyDisabled: "Межбазовый перенос поддерживает только последовательности (обычный DDL; тела запросов представлений не могут быть преобразованы)",
- xuguPairOnly: "Перенос Xugu пока поддерживает только соединения Xugu → Xugu. Выберите соединения Xugu для источника и назначения.",
+ xuguPairOnly: "Поддерживаемые направления: Xugu → Xugu или MySQL → Xugu (только таблицы и данные).",
xuguAppendOverwriteOnly: "На первом этапе переноса Xugu поддерживаются только добавление и перезапись; upsert и перестроение пока не поддерживаются.",
xuguPreserveTableNames: "При переносе структуры Xugu необходимо сохранять регистр символов в именах исходных таблиц, чтобы избежать ошибок во внешних ключах.",
crossFamilyViewHint: "Межбазовый перенос представлений: выберите также таблицы, от которых зависит представление, или убедитесь, что они уже существуют в целевой базе данных",
diff --git a/apps/desktop/src/i18n/locales/th.ts b/apps/desktop/src/i18n/locales/th.ts
index 30c36d0b75..c6554aaec0 100644
--- a/apps/desktop/src/i18n/locales/th.ts
+++ b/apps/desktop/src/i18n/locales/th.ts
@@ -9130,7 +9130,7 @@ export default withEnglishFallback({
objectTypeEvent: "Events",
objectDataOnlyDisabled: "ไม่ถ่ายโอน Object ที่ไม่ใช่ Table ใน Mode เฉพาะข้อมูล",
objectCrossFamilyDisabled: "การถ่ายโอนข้ามฐานข้อมูลรองรับเฉพาะ Sequences (DDL ธรรมดา ไม่สามารถแปล Body ของ View Query)",
- xuguPairOnly: "การถ่ายโอน Xugu ตอนนี้รองรับเฉพาะ Xugu ไป Xugu เลือกการเชื่อมต่อ Xugu สำหรับทั้ง Source และ Target",
+ xuguPairOnly: "ทิศทางที่รองรับ: Xugu ไป Xugu หรือ MySQL ไป Xugu (เฉพาะโครงสร้างตารางและข้อมูล)",
xuguAppendOverwriteOnly: "Phase แรกของการถ่ายโอน Xugu รองรับเฉพาะ Append และ Overwrite ยังไม่รองรับ Upsert และ Rebuild",
xuguPreserveTableNames: "การถ่ายโอน Structure ของ Xugu ตอนนี้ต้องคงตัวพิมพ์เล็กใหญ่ของชื่อ Table ต้นทางเพื่อหลีกเลี่ยง Reference Foreign Key ไม่ตรงกัน",
crossFamilyViewHint: "การถ่ายโอน View ข้ามฐานข้อมูล: เลือก Tables ที่ View ขึ้นต่อด้วย หรือยืนยันว่ามีอยู่ในฐานข้อมูลเป้าหมายแล้ว",
diff --git a/apps/desktop/src/i18n/locales/zh-CN.ts b/apps/desktop/src/i18n/locales/zh-CN.ts
index 3322a8e2d3..4ae67a6a82 100644
--- a/apps/desktop/src/i18n/locales/zh-CN.ts
+++ b/apps/desktop/src/i18n/locales/zh-CN.ts
@@ -8554,7 +8554,7 @@ export default withEnglishFallback({
objectTypeEvent: "事件",
objectDataOnlyDisabled: "仅数据模式不传输非表对象",
objectCrossFamilyDisabled: "跨库仅支持序列传输(纯DDL;视图查询体无法翻译)",
- xuguPairOnly: "虚谷数据传输当前仅支持虚谷到虚谷,请为源端和目标端都选择虚谷连接。",
+ xuguPairOnly: "虚谷支持的传输方向:虚谷到虚谷,或 MySQL 到虚谷(仅表结构和数据)。",
xuguAppendOverwriteOnly: "虚谷一期仅支持追加或覆盖;暂不支持更新插入和重建。",
xuguPreserveTableNames: "虚谷结构传输暂要求保留源表名大小写,以避免外键引用错位。",
crossFamilyViewHint: "跨库视图传输:请同时选择视图依赖的表,或确保目标库已存在这些表",
diff --git a/apps/desktop/src/i18n/locales/zh-TW.ts b/apps/desktop/src/i18n/locales/zh-TW.ts
index 48c9c61a13..14ba479378 100644
--- a/apps/desktop/src/i18n/locales/zh-TW.ts
+++ b/apps/desktop/src/i18n/locales/zh-TW.ts
@@ -6450,7 +6450,7 @@ export default withEnglishFallback({
objectTypeEvent: "事件",
objectDataOnlyDisabled: "僅資料模式不傳輸非表物件",
objectCrossFamilyDisabled: "跨庫僅支援序列傳輸(純DDL;檢視查詢體無法翻譯)",
- xuguPairOnly: "虛谷資料傳輸目前僅支援虛谷到虛谷,請為來源端和目標端都選擇虛谷連線。",
+ xuguPairOnly: "虛谷支援的傳輸方向:虛谷到虛谷,或 MySQL 到虛谷(僅表結構和資料)。",
xuguAppendOverwriteOnly: "虛谷第一階段僅支援追加或覆蓋;暫不支援更新插入和重建。",
xuguPreserveTableNames: "虛谷結構傳輸目前要求保留來源資料表名稱大小寫,以避免外鍵引用錯位。",
crossFamilyViewHint: "跨庫檢視傳輸:請同時選擇檢視依賴的表,或確保目標庫已存在這些表",
diff --git a/apps/desktop/src/lib/database/__tests__/transferObjectKinds.spec.ts b/apps/desktop/src/lib/database/__tests__/transferObjectKinds.spec.ts
index 9538b3b360..4b040dc7c9 100644
--- a/apps/desktop/src/lib/database/__tests__/transferObjectKinds.spec.ts
+++ b/apps/desktop/src/lib/database/__tests__/transferObjectKinds.spec.ts
@@ -64,10 +64,13 @@ describe("transferObjectKinds", () => {
expect(isSameTransferFamily("sqlserver", "mysql")).toBe(false);
});
- it("allows Xugu transfer only between two Xugu connections", () => {
+ it("allows native MySQL to Xugu tables without opening other Xugu directions", () => {
expect(isTransferPairSupported("xugu", "xugu")).toBe(true);
expect(isTransferPairSupported("xugu", "postgres")).toBe(false);
- expect(isTransferPairSupported("mysql", "xugu")).toBe(false);
+ expect(isTransferPairSupported("mysql", "xugu")).toBe(true);
+ expect(isTransferPairSupported("xugu", "mysql")).toBe(false);
+ expect(isTransferPairSupported("doris", "xugu")).toBe(false);
+ expect(isTransferPairSupported("starrocks", "xugu")).toBe(false);
expect(isTransferPairSupported("mysql", "postgres")).toBe(true);
expect(isTransferPairSupported(undefined, "xugu")).toBe(true);
});
diff --git a/apps/desktop/src/lib/database/transferObjectKinds.ts b/apps/desktop/src/lib/database/transferObjectKinds.ts
index 66bcff6a66..8a1dba6e87 100644
--- a/apps/desktop/src/lib/database/transferObjectKinds.ts
+++ b/apps/desktop/src/lib/database/transferObjectKinds.ts
@@ -31,10 +31,10 @@ export function isSameTransferFamily(a?: DatabaseType, b?: DatabaseType): boolea
return !!fa && fa === fb;
}
-/** Xugu's initial transfer implementation is deliberately limited to Xugu → Xugu. */
+/** Only native MySQL → Xugu tables and native Xugu → Xugu are validated. */
export function isTransferPairSupported(source?: DatabaseType, target?: DatabaseType): boolean {
if (!source || !target) return true;
- if (source === "xugu" || target === "xugu") return source === "xugu" && target === "xugu";
+ if (source === "xugu" || target === "xugu") return target === "xugu" && (source === "xugu" || source === "mysql");
return true;
}
diff --git a/crates/dbx-core/src/data/transfer.rs b/crates/dbx-core/src/data/transfer.rs
index 4b55e81e2f..fedd16db28 100644
--- a/crates/dbx-core/src/data/transfer.rs
+++ b/crates/dbx-core/src/data/transfer.rs
@@ -24,6 +24,7 @@ mod iris_tests;
mod db2;
mod ddl_plan;
+pub(crate) mod mysql_xugu;
pub(crate) mod oracle_table_contract;
mod overwrite_clear;
mod structure_plan;
@@ -1190,8 +1191,18 @@ pub fn validate_transfer_database_pair(
if !matches!(source_db_type, DatabaseType::Xugu) && !matches!(target_db_type, DatabaseType::Xugu) {
return Ok(());
}
- if !matches!((source_db_type, target_db_type), (DatabaseType::Xugu, DatabaseType::Xugu)) {
- return Err("虚谷数据传输当前仅支持虚谷到虚谷".to_string());
+ let mysql_to_xugu = mysql_xugu::is_pair(source_db_type, target_db_type);
+ if !matches!((source_db_type, target_db_type), (DatabaseType::Xugu, DatabaseType::Xugu)) && !mysql_to_xugu {
+ return Err("虚谷数据传输当前仅支持虚谷到虚谷,以及 MySQL 到虚谷的表传输".to_string());
+ }
+ if mysql_to_xugu
+ && request.objects.as_ref().is_some_and(|objects| {
+ objects
+ .iter()
+ .any(|selection| selection.object_type != TransferObjectKind::Table && !selection.names.is_empty())
+ })
+ {
+ return Err("MySQL 到虚谷当前仅支持表结构和表数据".into());
}
if request.objects.as_ref().is_some_and(|objects| {
objects.iter().any(|selection| !xugu_objects::supported_kinds().contains(&selection.object_type))
@@ -11293,6 +11304,9 @@ pub async fn preview_transfer_ownership(
}
validate_transfer_database_pair(request, source_db_type, target_db_type)?;
// PostgreSQL-compatible transfers report role ownership gaps for the confirmation flow.
+ if mysql_xugu::is_pair(source_db_type, target_db_type) {
+ mysql_xugu::preflight(state, request, source_pool_key, target_pool_key).await?;
+ }
let (missing_owners, target_owner) =
if request.create_table && is_postgres_compat_transfer(source_db_type, target_db_type) {
let has_prokind = postgres_transfer_catalog_capabilities(state, source_pool_key).await?.has_prokind;
@@ -12960,6 +12974,12 @@ where
return Err(format!("No columns found for table {table}"));
}
ensure_target_column_names_unique(request, table, &columns)?;
+ let mysql_to_xugu = mysql_xugu::is_pair(source_db_type, target_db_type);
+ let mysql_xugu_types = if mysql_to_xugu {
+ Some(columns.iter().map(mysql_xugu::column_type).collect::<Result<Vec<_>, _>>()?)
+ } else {
+ None
+ };
let is_doris_source = {
let configs = state.configs.read().await;
@@ -12991,6 +13011,12 @@ where
// preexisting-target validation and write SQL compare/emit these, reads keep `col_names`.
let mut target_col_names: Vec<String> = col_names.iter().map(|name| request.target_column_name(name)).collect();
let mut col_types: Vec<Option<String>> = writable_columns.iter().map(|c| Some(c.data_type.clone())).collect();
+ if let Some(types) = &mysql_xugu_types {
+ col_types = writable_columns
+ .iter()
+ .map(|column| columns.iter().position(|c| c.name == column.name).map(|index| types[index].clone()))
+ .collect();
+ }
let primary_key_columns = transfer_key_columns(&writable_columns, source_db_type);
if should_copy_data(&request.content) {
log::info!("[transfer] {} has {} columns, counting rows...", table, columns.len());
@@ -13149,14 +13175,15 @@ where
// whether the batch needs the `SET IDENTITY_INSERT` wrapper, and without the
// target metadata it stays false — SQL Server then rejects the explicit
// identity values with 544.
- let needs_target_columns = default_rows_only
+ let needs_target_columns = mysql_to_xugu
+ || default_rows_only
|| target_table_preexisting
|| (request.mode == TransferMode::Upsert && !transfer_upsert_falls_back_to_append(target_db_type))
|| matches!(
target_db_type,
DatabaseType::Postgres | DatabaseType::Dameng | DatabaseType::H2 | DatabaseType::SqlServer
);
- let target_columns = if needs_target_columns {
+ let mut target_columns = if needs_target_columns {
get_columns_for_transfer(
state,
target_pool_key,
@@ -13172,11 +13199,25 @@ where
Vec::new()
};
+ if mysql_to_xugu {
+ mysql_xugu::enrich_target_temporal_precision(
+ state,
+ target_pool_key,
+ &request.target_schema,
+ &target_table,
+ &mut target_columns,
+ )
+ .await?;
+ }
+
// Empty-column INSERTs are safe only when the target also computes every
// value. Reject incompatible data-only targets before an overwrite truncates
// them; otherwise ordinary columns could silently receive defaults instead.
let target_default_rows_only = mysql_generated_only_transfer(&target_columns, target_db_type, target_db_type)
|| xugu_rowversion_only_transfer(&target_columns, target_db_type, target_db_type);
+ if mysql_to_xugu {
+ mysql_xugu::validate_target(&writable_columns, &target_columns)?;
+ }
if default_rows_only && !target_default_rows_only {
return Err(format!(
"Target table '{target_table}' must contain only generated columns for default-row transfer"
@@ -13567,8 +13608,11 @@ where
request.source_catalog.as_deref(),
)
};
- let (sql, mysql_spatial_markers) =
- mysql_spatial_transfer_select_sql(sql, &col_names, &col_types, source_db_type, target_db_type);
+ let (sql, mysql_spatial_markers) = if mysql_to_xugu {
+ (mysql_xugu::project_page(&sql, &writable_columns)?, false)
+ } else {
+ mysql_spatial_transfer_select_sql(sql, &col_names, &col_types, source_db_type, target_db_type)
+ };
// Cap the result at `batch_size` (not the 10k default row limit), so a
// large batch is never truncated into looking like a short final page.
(
@@ -13578,6 +13622,9 @@ where
};
let has_more = result.has_more;
let row_count = result.rows.len();
+ if mysql_to_xugu {
+ mysql_xugu::validate_rows(&writable_columns, &result.rows)?;
+ }
if row_count == 0 {
// A `ctid` window can be empty — its rows were deleted, or the
@@ -13623,21 +13670,38 @@ where
row.clear();
}
}
- let write_statements = generate_transfer_write_sql_batches_with_column_quoting(
- &effective_mode,
- &write_col_names,
- &col_types,
- &result.rows,
- &target_table,
- &request.target_schema,
- target_db_type,
- &pk_columns,
- request.target_catalog.as_deref(),
- overrides_postgres_system_values,
- mysql_spatial_markers,
- request.quote_target_column_names,
- write_hard_limit,
- )?;
+ let write_statements = if mysql_to_xugu {
+ mysql_xugu::write_batches(
+ &write_col_names,
+ &col_types,
+ &result.rows,
+ &target_table,
+ &request.target_schema,
+ request.target_catalog.as_deref(),
+ SqlBatchLimits::for_database(
+ target_db_type,
+ max_transfer_write_rows(target_db_type, &effective_mode),
+ )
+ .with_hard_sql_bytes(write_hard_limit),
+ request.quote_target_column_names,
+ )?
+ } else {
+ generate_transfer_write_sql_batches_with_column_quoting(
+ &effective_mode,
+ &write_col_names,
+ &col_types,
+ &result.rows,
+ &target_table,
+ &request.target_schema,
+ target_db_type,
+ &pk_columns,
+ request.target_catalog.as_deref(),
+ overrides_postgres_system_values,
+ mysql_spatial_markers,
+ request.quote_target_column_names,
+ write_hard_limit,
+ )?
+ };
for (statement_index, batch_sql) in write_statements.iter().enumerate() {
execute_transfer_write_statement(
state,
@@ -13707,7 +13771,7 @@ where
&target_table,
&request.target_schema,
&write_col_names,
- target_table_preexisting,
+ target_table_preexisting || mysql_to_xugu,
)
.await?;
}
@@ -14123,6 +14187,11 @@ where
{
validate_transfer_database_pair(request, source_db_type, target_db_type)?;
let state = state.clone();
+ if mysql_xugu::is_pair(source_db_type, target_db_type) {
+ let mut table_request = request.clone();
+ table_request.tables = vec![table.to_string()];
+ mysql_xugu::preflight(&state, &table_request, source_pool_key, target_pool_key).await?;
+ }
let request = request.clone();
let table = table.to_string();
let source_db_type = *source_db_type;
@@ -15897,8 +15966,9 @@ CREATE TABLE "Other"."prefix""Source"."NAME" ("ID" INT);"#;
let err =
validate_transfer_database_pair(&request, &DatabaseType::Xugu, &DatabaseType::Postgres).unwrap_err();
assert!(err.contains("虚谷到虚谷"), "{err}");
- let err = validate_transfer_database_pair(&request, &DatabaseType::Mysql, &DatabaseType::Xugu).unwrap_err();
- assert!(err.contains("虚谷到虚谷"), "{err}");
+ assert!(validate_transfer_database_pair(&request, &DatabaseType::Mysql, &DatabaseType::Xugu).is_ok());
+ assert!(validate_transfer_database_pair(&request, &DatabaseType::Xugu, &DatabaseType::Mysql).is_err());
+ assert!(validate_transfer_database_pair(&request, &DatabaseType::Doris, &DatabaseType::Xugu).is_err());
for mode in [TransferMode::Append, TransferMode::Overwrite] {
let request = TransferRequest { mode, ..request.clone() };
diff --git a/crates/dbx-core/src/data/transfer/ddl_plan.rs b/crates/dbx-core/src/data/transfer/ddl_plan.rs
index 76a13249ca..aa0680e989 100644
--- a/crates/dbx-core/src/data/transfer/ddl_plan.rs
+++ b/crates/dbx-core/src/data/transfer/ddl_plan.rs
@@ -35,6 +35,18 @@ pub(super) async fn prepare_table_ddl(
table_comment: Option<&str>,
known_foreign_keys: &HashMap<String, Vec<db::ForeignKeyInfo>>,
) -> Result<PreparedTableDdl, String> {
+ if mysql_xugu::is_pair(source_db_type, target_db_type) {
+ let ddl = crate::schema::get_table_transfer_ddl_core(
+ state,
+ &request.source_connection_id,
+ &request.source_database,
+ &request.source_schema,
+ table,
+ )
+ .await
+ .map_err(|error| format!("Cannot read complete MySQL DDL for '{table}': {error}"))?;
+ return mysql_xugu::prepare(&ddl, columns, &request.target_schema, target_table);
+ }
let source_columns = if request.has_package_selection() {
Some(serde_json::to_value(columns).map_err(|error| error.to_string())?)
} else {
diff --git a/crates/dbx-core/src/data/transfer/mysql_xugu.rs b/crates/dbx-core/src/data/transfer/mysql_xugu.rs
new file mode 100644
index 0000000000..14505807fb
--- /dev/null
+++ b/crates/dbx-core/src/data/transfer/mysql_xugu.rs
@@ -0,0 +1,1155 @@
+//! Native MySQL → native Xugu only. Do not reuse MySQL DDL on Xugu, or change
+//! the generic type mapper: other transfer pairs have different contracts.
+use super::*;
+use sqlparser::{
+ ast::{ColumnOption, CreateTableOptions, Expr, IndexColumn, SqlOption, Statement, TableConstraint},
+ dialect::MySqlDialect,
+ parser::Parser,
+};
+
+pub(crate) fn is_pair(source: &DatabaseType, target: &DatabaseType) -> bool {
+ *source == DatabaseType::Mysql && *target == DatabaseType::Xugu
+}
+
+fn quote(name: &str) -> String {
+ format!("`{}`", name.replace('`', "``"))
+}
+
+/// The declared domain, not the values in the first page, decides the mapping.
+/// In particular TINYINT(1) is an integer (it can store 2), not a Boolean.
+pub(super) fn column_type(column: &db::ColumnInfo) -> Result<String, String> {
+ let raw = column.data_type.to_ascii_uppercase();
+ let base = raw.split(['(', ' ']).next().unwrap_or_default();
+ let unsigned = raw.split_whitespace().any(|part| part == "UNSIGNED" || part == "ZEROFILL");
+ let parameters = raw.split_once('(').and_then(|(_, rest)| rest.split_once(')')).map(|(args, _)| args);
+ let first = parameters.and_then(|p| p.split(',').next()).and_then(|p| p.trim().parse::<i32>().ok());
+ let length = column.character_maximum_length.or(first);
+ let fail = || {
+ format!(
+ "MySQL column '{}': type '{}' cannot be transferred to Xugu without loss",
+ column.name, column.data_type
+ )
+ };
+ let sized = |name: &str, length: Option<i32>, max: i32| -> Result<String, String> {
+ match length {
+ Some(n) if n > 0 && n <= max => Ok(format!("{name}({n})")),
+ _ => Err(fail()),
+ }
+ };
+ if column_is_auto_increment(column) && matches!(base, "TINYINT" | "SMALLINT" | "MEDIUMINT") {
+ return Ok("INTEGER".into());
+ }
+ Ok(match base {
+ "TINYINT" if unsigned => "SMALLINT".into(),
+ "TINYINT" => "TINYINT".into(),
+ "SMALLINT" if unsigned => "INTEGER".into(),
+ "SMALLINT" => "SMALLINT".into(),
+ "MEDIUMINT" => "INTEGER".into(),
+ "INT" | "INTEGER" if unsigned => "BIGINT".into(),
+ "INT" | "INTEGER" => "INTEGER".into(),
+ "BIGINT" if unsigned => "NUMERIC(20,0)".into(),
+ "BIGINT" => "BIGINT".into(),
+ "DECIMAL" | "NUMERIC" => {
+ let p = column.numeric_precision.or(first).unwrap_or(10);
+ let s = column
+ .numeric_scale
+ .or_else(|| parameters.and_then(|v| v.split(',').nth(1)).and_then(|v| v.trim().parse().ok()))
+ .unwrap_or(0);
+ if !(1..=38).contains(&p) || !(0..=p).contains(&s) {
+ return Err(fail());
+ }
+ format!("NUMERIC({p},{s})")
+ }
+ "FLOAT" if first.is_some_and(|n| n > 24) && parameters.is_some_and(|p| !p.contains(',')) => "DOUBLE".into(),
+ "FLOAT" => "FLOAT".into(),
+ "DOUBLE" | "REAL" => "DOUBLE".into(),
+ "BOOL" | "BOOLEAN" => "TINYINT".into(),
+ "BIT" => sized("BIT", first.or(Some(1)), 64)?,
+ "CHAR" => sized("CHAR", length, 60000)?,
+ "VARCHAR" => sized("VARCHAR", length, 60000)?,
+ "TINYTEXT" | "TEXT" | "MEDIUMTEXT" | "LONGTEXT" => "CLOB".into(),
+ "BINARY" | "VARBINARY" => sized("BINARY", length, 65535)?,
+ "TINYBLOB" | "BLOB" | "MEDIUMBLOB" | "LONGBLOB" => "BLOB".into(),
+ "JSON" => "JSON".into(),
+ "YEAR" => "SMALLINT".into(),
+ // MySQL TIME is a duration up to ±838 hours, not a time of day.
+ "TIME" => "INTERVAL HOUR(3) TO SECOND(6)".into(),
+ "DATE" => "DATE".into(),
+ "DATETIME" | "TIMESTAMP" => {
+ let scale = first.or(column.numeric_scale).unwrap_or(0);
+ if !(0..=6).contains(&scale) {
+ return Err(fail());
+ }
+ // MySQL TIMESTAMP's session-local value is copied explicitly. Do not
+ // introduce Xugu TIMESTAMP's implicit current-time default.
+ format!("DATETIME({scale})")
+ }
+ "ENUM" => sized("VARCHAR", length, 60000)?,
+ // SET constraints, GIS, generated columns, and arbitrary type names need
+ // their own proven conversion. Never fall back to the source spelling.
+ _ => return Err(fail()),
+ })
+}
+
+fn default_sql(expr: &Expr, column: &db::ColumnInfo, mapped: &str) -> Result<String, String> {
+ use sqlparser::ast::{UnaryOperator, Value};
+ match expr {
+ Expr::Nested(inner) => default_sql(inner, column, mapped),
+ Expr::Value(value) => match &value.value {
+ Value::Null => Ok("NULL".into()),
+ Value::Number(n, _) => Ok(n.clone()),
+ Value::SingleQuotedString(s) | Value::DoubleQuotedString(s) => {
+ validate_text_value(column, s)?;
+ Ok(value_literal(&serde_json::Value::String(s.clone()), Some(mapped)))
+ }
+ Value::Boolean(value) => Ok(if *value { "1" } else { "0" }.into()),
+ Value::HexStringLiteral(value) if is_binary_transfer_column_type(mapped) => Ok(format!("X'{value}'")),
+ Value::SingleQuotedByteStringLiteral(value)
+ if mapped.starts_with("BIT(") && value.bytes().all(|b| b == b'0' || b == b'1') =>
+ {
+ let width = mapped
+ .strip_prefix("BIT(")
+ .and_then(|n| n.strip_suffix(')'))
+ .and_then(|n| n.parse::<usize>().ok())
+ .ok_or("Invalid mapped Xugu BIT width")?;
+ if value.len() > width {
+ return Err(format!("MySQL BIT default exceeds the width of '{}'", column.name));
+ }
+ // MySQL left-pads a short numeric bit literal, whereas Xugu
+ // right-pads it. SHOW CREATE removes MySQL's leading zeros.
+ Ok(format!("B'{value:0>width$}'"))
+ }
+ _ => Err(format!("Unsupported MySQL default on column '{}'", column.name)),
+ },
+ Expr::UnaryOp { op: UnaryOperator::Minus | UnaryOperator::Plus, expr: inner } if matches!(inner.as_ref(), Expr::Value(value) if matches!(value.value, Value::Number(..))) => {
+ Ok(expr.to_string())
+ }
+ _ if is_current_timestamp(expr) && (mapped.starts_with("DATETIME") || mapped.starts_with("TIMESTAMP")) => {
+ // Xugu has no CURRENT_TIMESTAMP(p) overload. The DATETIME(p)
+ // column retains the declared precision of the default value.
+ Ok("CURRENT_TIMESTAMP".into())
+ }
+ _ => Err(format!("MySQL default expression on '{}' needs manual conversion: {expr}", column.name)),
+ }
+}
+
+fn is_current_timestamp(expr: &Expr) -> bool {
+ let text = expr.to_string().to_ascii_uppercase();
+ text == "CURRENT_TIMESTAMP"
+ || text == "CURRENT_TIMESTAMP()"
+ || (0..=6).any(|p| text == format!("CURRENT_TIMESTAMP({p})"))
+}
+
+fn key_columns(columns: &[IndexColumn]) -> Result<String, String> {
+ columns
+ .iter()
+ .map(|column| {
+ let Expr::Identifier(name) = &column.column.expr else {
+ return Err("MySQL prefix/expression indexes require manual conversion".into());
+ };
+ if column.operator_class.is_some() {
+ return Err("Unsupported MySQL index operator class".into());
+ }
+ let direction = if column.column.options.asc == Some(false) { " DESC" } else { "" };
+ Ok(format!("{}{direction}", quote(&name.value)))
+ })
+ .collect::<Result<Vec<_>, String>>()
+ .map(|v| v.join(", "))
+}
+
+/// Parse the entire SHOW CREATE result, fail closed on unhandled clauses, and
+/// use exact column names from its AST. This is shared by preview and execution.
+pub(super) fn prepare(
+ sql: &str,
+ columns: &[db::ColumnInfo],
+ schema: &str,
+ table: &str,
+) -> Result<ddl_plan::PreparedTableDdl, String> {
+ if sql.contains('\0') || schema.contains('\0') || table.contains('\0') {
+ return Err("MySQL DDL contains NUL characters; refusing unsafe Xugu SQL text".into());
+ }
+ if sql.chars().any(|ch| u32::from(ch) > 0xffff) {
+ return Err("MySQL DDL contains supplementary Unicode characters; refusing lossy Xugu transfer".into());
+ }
+ // MySQL hides partitioning and newer features inside executable comments.
+ // Ignoring such comments would silently lose source structure.
+ if sql.contains("/*!") {
+ return Err("MySQL executable DDL comments require manual conversion".into());
+ }
+ let mut statements =
+ Parser::parse_sql(&MySqlDialect {}, sql).map_err(|e| format!("Cannot parse complete MySQL table DDL: {e}"))?;
+ if statements.len() != 1 {
+ return Err("Expected exactly one MySQL CREATE TABLE statement".into());
+ }
+ let Statement::CreateTable(create) = statements.remove(0) else {
+ return Err("Expected MySQL CREATE TABLE".into());
+ };
+ if create.temporary || create.query.is_some() || create.like.is_some() || create.partition_by.is_some() {
+ return Err("MySQL temporary/partitioned/query-defined tables require manual conversion".into());
+ }
+ if columns.is_empty() || create.columns.len() != columns.len() {
+ return Err("Incomplete MySQL column metadata; refusing reduced table DDL".into());
+ }
+ let full_table = format!("{}.{}", quote(schema), quote(table));
+ let options = create.table_options.to_string();
+ let seed = Regex::new(r"(?i)(?:^|\s)AUTO_INCREMENT\s*=\s*(\d+)")
+ .unwrap()
+ .captures(&options)
+ .map(|c| c[1].parse::<i64>().map_err(|_| "MySQL auto-increment seed exceeds Xugu identity range".to_string()))
+ .transpose()?
+ .unwrap_or(1);
+ let mut lines = Vec::new();
+ let mut post = Vec::new();
+ for definition in &create.columns {
+ let column = columns
+ .iter()
+ .find(|c| c.name == definition.name.value)
+ .ok_or_else(|| format!("Missing MySQL metadata for column '{}'", definition.name.value))?;
+ let mapped = column_type(column)?;
+ let name = quote(&column.name);
+ let mut line = format!(" {name} {mapped}");
+ let auto_increment = definition.options.iter().any(|option| matches!(&option.option,
+ ColumnOption::DialectSpecific(tokens) if tokens.iter().map(ToString::to_string).collect::<String>().eq_ignore_ascii_case("AUTO_INCREMENT")));
+ if auto_increment {
+ if !matches!(mapped.as_str(), "INTEGER" | "BIGINT") {
+ return Err(format!("MySQL auto-increment column '{}' needs an INTEGER/BIGINT target", column.name));
+ }
+ line.push_str(&format!(" IDENTITY({seed},1)"));
+ }
+ for option in &definition.options {
+ match &option.option {
+ ColumnOption::NotNull => line.push_str(" NOT NULL"),
+ ColumnOption::Null => line.push_str(" NULL"),
+ ColumnOption::Default(expr) => {
+ line.push_str(&format!(" DEFAULT {}", default_sql(expr, column, &mapped)?))
+ }
+ ColumnOption::DialectSpecific(tokens)
+ if tokens
+ .iter()
+ .map(ToString::to_string)
+ .collect::<String>()
+ .eq_ignore_ascii_case("AUTO_INCREMENT") => {}
+ ColumnOption::Comment(comment) => {
+ post.push(format!("COMMENT ON COLUMN {full_table}.{name} IS {}", quote_string_literal(comment)))
+ }
+ ColumnOption::CharacterSet(_) | ColumnOption::Collation(_) => {}
+ _ => {
+ return Err(format!("MySQL column '{}' has an unsupported option: {}", column.name, option.option))
+ }
+ }
+ }
+ if column.data_type.to_ascii_uppercase().contains("UNSIGNED") {
+ line.push_str(&format!(" CHECK ({name} >= 0)"));
+ }
+ if column.data_type.to_ascii_uppercase().starts_with("ENUM") {
+ let values = column
+ .enum_values
+ .as_ref()
+ .filter(|v| !v.is_empty())
+ .ok_or_else(|| format!("Missing MySQL ENUM members for '{}'", column.name))?;
+ line.push_str(&format!(
+ " CHECK ({name} IN ({}))",
+ values.iter().map(|v| quote_string_literal(v)).collect::<Vec<_>>().join(", ")
+ ));
+ }
+ lines.push(line);
+ }
+ for constraint in &create.constraints {
+ match constraint {
+ TableConstraint::PrimaryKey(key) => lines.push(format!(" PRIMARY KEY ({})", key_columns(&key.columns)?)),
+ TableConstraint::Unique(key) => lines.push(format!(" UNIQUE ({})", key_columns(&key.columns)?)),
+ TableConstraint::Index(index) => {
+ if index.index_type.as_ref().is_some_and(|t| t.to_string() != "BTREE")
+ || !index.index_options.is_empty()
+ {
+ return Err("MySQL non-BTREE or option-bearing indexes require manual conversion".into());
+ }
+ let name = index.name.as_ref().ok_or("Missing MySQL index name")?;
+ // Xugu index names are schema-wide; MySQL index names are table-local.
+ let target_index_name = format!("{table}_{}", name.value);
+ if target_index_name.chars().count() > 128 {
+ return Err("Mapped Xugu index name exceeds 128 characters".into());
+ }
+ post.push(format!(
+ // Xugu takes a simple index name; the qualified ON table
+ // determines the owner. A schema-qualified index name is
+ // rejected with E19260 (unexpected '.', expecting ON).
+ "CREATE INDEX {} ON {full_table} ({})",
+ quote(&target_index_name),
+ key_columns(&index.columns)?
+ ));
+ }
+ // Do not silently drop FK/CHECK enforcement or invent a translated
+ // expression. These are explicit preflight failures in phase one.
+ _ => return Err(format!("MySQL table constraint requires manual conversion: {constraint}")),
+ }
+ }
+ // MySQL SHOW CREATE puts COMMENT='...' in the plain table options,
+ // unlike dialects that populate CreateTable.comment directly.
+ let table_comment = create.comment.as_ref().or_else(|| match &create.table_options {
+ CreateTableOptions::Plain(options) => options.iter().rev().find_map(|option| match option {
+ SqlOption::Comment(comment) => Some(comment),
+ _ => None,
+ }),
+ _ => None,
+ });
+ if let Some(comment) = table_comment {
+ post.push(format!("COMMENT ON TABLE {full_table} IS {}", quote_string_literal(&comment.to_string())));
+ }
+ let mut ddl = format!("-- MySQL engines, character sets and collations are not copied; target Xugu semantics apply.\nCREATE TABLE {full_table} (\n{}\n);", lines.join(",\n"));
+ for statement in post {
+ ddl.push_str(&format!("\n{statement};"));
+ }
+ Ok(ddl_plan::PreparedTableDdl {
+ ddl,
+ reused_source_ddl: false,
+ deferred_fk_alters: vec![],
+ deferred_fk_names: vec![],
+ source_evidence: None,
+ })
+}
+
+fn validate_text_value(column: &db::ColumnInfo, value: &str) -> Result<(), String> {
+ // The native SQL transport cannot carry an embedded zero byte in a text
+ // literal. Never remove it or pass a statement that may be truncated.
+ if value.contains('\0') {
+ return Err(format!(
+ "MySQL column '{}' contains a NUL character; native Xugu SQL text cannot preserve it",
+ column.name
+ ));
+ }
+ // The native Xugu SQL path may silently discard supplementary characters.
+ // Until a lossless wire encoding is proven, fail instead of reporting a
+ // successful transfer with changed text. This guard is pair-local.
+ if value.chars().any(|ch| u32::from(ch) > 0xffff) {
+ return Err(format!(
+ "MySQL column '{}' contains supplementary Unicode characters; native Xugu text transfer is not yet lossless",
+ column.name
+ ));
+ }
+ let kind = column.data_type.to_ascii_uppercase();
+ if trims_trailing_spaces(column) && value.ends_with(' ') {
+ return Err(format!(
+ "MySQL column '{}' contains trailing spaces; Xugu CHAR/VARCHAR would remove them",
+ column.name
+ ));
+ }
+ if matches!(kind.split('(').next(), Some("DATE" | "DATETIME" | "TIMESTAMP"))
+ && (value.starts_with("0000-") || value.get(5..7) == Some("00") || value.get(8..10) == Some("00"))
+ {
+ return Err(format!("MySQL zero/partial date in '{}': refusing to invent a replacement", column.name));
+ }
+ Ok(())
+}
+
+/// Check declared target capacity before append/overwrite. Values that happen
+/// to fit on today's first page do not establish schema compatibility.
+pub(super) fn validate_target(source: &[db::ColumnInfo], target: &[db::ColumnInfo]) -> Result<(), String> {
+ for column in source {
+ let matches = target.iter().filter(|t| t.name == column.name).collect::<Vec<_>>();
+ let [destination] = matches.as_slice() else {
+ return Err(format!("Missing or ambiguous Xugu target column '{}'", column.name));
+ };
+ let expected = column_type(column)?;
+ let wanted = normalize_xugu_type_name(&expected);
+ let actual = normalize_xugu_type_name(&destination.data_type);
+ let canonical = |name: &str| {
+ match name {
+ "INT" => "INTEGER",
+ "DECIMAL" | "NUMBER" => "NUMERIC",
+ "VARBINARY" => "BINARY",
+ _ => name,
+ }
+ .to_string()
+ };
+ if canonical(&wanted) != canonical(&actual) {
+ return Err(format!(
+ "Xugu target column '{}' must accept {expected}, found {}",
+ column.name, destination.data_type
+ ));
+ }
+ let params = |kind: &str| -> Vec<i32> {
+ kind.split_once('(')
+ .and_then(|(_, v)| v.split_once(')'))
+ .map(|(v, _)| v.split(',').filter_map(|s| s.trim().parse().ok()).collect())
+ .unwrap_or_default()
+ };
+ let wanted_params = params(&expected);
+ let actual_params = params(&destination.data_type);
+ if matches!(wanted.as_str(), "CHAR" | "VARCHAR" | "BINARY" | "BIT") {
+ let available = destination
+ .character_maximum_length
+ .or(if wanted == "BIT" { destination.numeric_precision } else { None })
+ .or_else(|| actual_params.first().copied());
+ if available.is_none_or(|n| n < wanted_params.first().copied().unwrap_or(1)) {
+ return Err(format!("Xugu target column '{}' is shorter than {expected}", column.name));
+ }
+ }
+ if wanted == "NUMERIC" {
+ let p = destination
+ .numeric_precision
+ .or_else(|| actual_params.first().copied())
+ .ok_or("Missing Xugu numeric precision")?;
+ let s = destination
+ .numeric_scale
+ .or_else(|| actual_params.get(1).copied())
+ .ok_or("Missing Xugu numeric scale")?;
+ if s < wanted_params[1] || p - s < wanted_params[0] - wanted_params[1] {
+ return Err(format!("Xugu target column '{}' cannot hold {expected}", column.name));
+ }
+ }
+ if wanted == "DATETIME" {
+ let available = destination.numeric_scale.or_else(|| actual_params.first().copied());
+ if available.is_none_or(|n| n < wanted_params[0]) {
+ return Err(format!(
+ "Xugu target column '{}' cannot preserve {expected} fractional seconds",
+ column.name
+ ));
+ }
+ }
+ if wanted == "HOUR TO SECOND" {
+ let leading = destination.numeric_precision.or_else(|| actual_params.first().copied());
+ // The two parenthesized qualifiers are intentionally parsed
+ // separately; splitting at the first ')' only sees HOUR(3).
+ let trailing = destination.numeric_scale.or_else(|| {
+ destination
+ .data_type
+ .to_ascii_uppercase()
+ .split_once("SECOND(")
+ .and_then(|(_, tail)| tail.split_once(')'))
+ .and_then(|(n, _)| n.trim().parse::<i32>().ok())
+ });
+ if leading.is_none_or(|n| n < 3) || trailing.is_none_or(|n| n < 6) {
+ return Err(format!(
+ "Xugu target column '{}' cannot preserve MySQL TIME range and fractional seconds",
+ column.name
+ ));
+ }
+ }
+ if column.is_nullable && !destination.is_nullable {
+ return Err(format!("Nullable MySQL column '{}' has a NOT NULL Xugu target", column.name));
+ }
+ if destination.extra.as_deref().is_some_and(|extra| extra.to_ascii_lowercase().contains("generated")) {
+ return Err(format!("Xugu target column '{}' is generated", column.name));
+ }
+ }
+ for column in target {
+ if !source.iter().any(|s| s.name == column.name) && !target_column_can_be_omitted(column, &DatabaseType::Xugu) {
+ return Err(format!("Required Xugu target column '{}' has no MySQL source", column.name));
+ }
+ }
+ Ok(())
+}
+
+/// The generic Xugu column model does not expose DATETIME/INTERVAL qualifiers.
+/// Read only the permission-scoped catalog for this transfer pair, including
+/// when the target was just created, before allowing data writes or truncation.
+pub(super) async fn enrich_target_temporal_precision(
+ state: &AppState,
+ pool: &str,
+ schema: &str,
+ table: &str,
+ columns: &mut [db::ColumnInfo],
+) -> Result<(), String> {
+ if !columns.iter().any(|c| matches!(normalize_xugu_type_name(&c.data_type).as_str(), "DATETIME" | "HOUR TO SECOND"))
+ {
+ return Ok(());
+ }
+ let sql = format!(
+ "SELECT c.COL_NAME,c.SCALE FROM ALL_COLUMNS c \
+ JOIN ALL_TABLES t ON c.DB_ID=t.DB_ID AND c.TABLE_ID=t.TABLE_ID \
+ JOIN ALL_SCHEMAS s ON t.DB_ID=s.DB_ID AND t.SCHEMA_ID=s.SCHEMA_ID \
+ WHERE s.DB_ID=CURRENT_DB_ID AND s.SCHEMA_NAME={} AND t.TABLE_NAME={}",
+ quote_string_literal(schema),
+ quote_string_literal(table)
+ );
+ let result = execute_read_on_pool(state, pool, &sql).await?;
+ for column in columns {
+ let kind = normalize_xugu_type_name(&column.data_type);
+ if !matches!(kind.as_str(), "DATETIME" | "HOUR TO SECOND") {
+ continue;
+ }
+ let matches = result
+ .rows
+ .iter()
+ .filter(|row| row.first().and_then(json_scalar_to_string).as_deref() == Some(column.name.as_str()))
+ .collect::<Vec<_>>();
+ let [row] = matches.as_slice() else {
+ return Err(format!("Missing or ambiguous Xugu temporal metadata for '{}'", column.name));
+ };
+ let scale = row
+ .get(1)
+ .and_then(json_scalar_to_string)
+ .and_then(|v| v.parse::<i32>().ok())
+ .filter(|n| *n >= 0)
+ .ok_or_else(|| format!("Missing Xugu temporal precision for '{}'", column.name))?;
+ if kind == "DATETIME" {
+ column.numeric_scale = Some(scale);
+ } else {
+ column.numeric_precision = Some(scale / 65536);
+ column.numeric_scale = Some(scale % 65536);
+ }
+ }
+ Ok(())
+}
+
+fn text_column(column: &db::ColumnInfo) -> bool {
+ matches!(
+ column.data_type.to_ascii_uppercase().split(['(', ' ']).next(),
+ Some("CHAR" | "VARCHAR" | "TINYTEXT" | "TEXT" | "MEDIUMTEXT" | "LONGTEXT" | "ENUM" | "SET" | "JSON")
+ )
+}
+
+fn trims_trailing_spaces(column: &db::ColumnInfo) -> bool {
+ matches!(column.data_type.to_ascii_uppercase().split(['(', ' ']).next(), Some("CHAR" | "VARCHAR" | "ENUM"))
+}
+
+fn unsafe_text_predicate(column: &db::ColumnInfo) -> String {
+ let name = quote(&column.name);
+ // A binary comparison is essential: source text collations must not ignore
+ // the zero byte. Binary columns use HEX projection and are not scanned here.
+ // Check both transport limits in one source scan before overwrite clears.
+ let trailing = if trims_trailing_spaces(column) {
+ format!(" OR RIGHT(CAST({name} AS BINARY), 1) = 0x20")
+ } else {
+ String::new()
+ };
+ format!("(LOCATE(0x00, CAST({name} AS BINARY)) > 0 OR HEX({name}) REGEXP '^(..)*F[0-4][89AB][0-9A-F][89AB][0-9A-F][89AB][0-9A-F]'{trailing})")
+}
+
+fn value_literal(value: &serde_json::Value, kind: Option<&str>) -> String {
+ if let Some(text) = value.as_str() {
+ if kind.is_some_and(|k| matches!(normalize_xugu_type_name(k).as_str(), "CHAR" | "VARCHAR" | "CLOB" | "JSON")) {
+ // Native Xugu ordinary string literals retain backslashes. The
+ // generic formatter's MySQL-style backslash escaping changes text
+ // and JSON escapes, so keep this correction local to this pair.
+ return quote_string_literal(text);
+ }
+ }
+ escape_value_typed(value, &DatabaseType::Xugu, kind)
+}
+
+#[allow(clippy::too_many_arguments)]
+pub(super) fn write_batches(
+ columns: &[String],
+ column_types: &[Option<String>],
+ rows: &[Vec<serde_json::Value>],
+ table: &str,
+ schema: &str,
+ catalog: Option<&str>,
+ limits: SqlBatchLimits,
+ quote_column_names: bool,
+) -> Result<Vec<String>, String> {
+ let value_rows = rows
+ .iter()
+ .map(|row| {
+ let values = row
+ .iter()
+ .enumerate()
+ .map(|(i, value)| value_literal(value, column_types.get(i).and_then(|t| t.as_deref())))
+ .collect::<Vec<_>>()
+ .join(", ");
+ format!("({values})")
+ })
+ .collect::<Vec<_>>();
+ // Reuse the existing statement-size limits, batching and Xugu VALUES
+ // syntax; only the pair's value literals differ.
+ generate_insert_sql_batches_from_value_rows(
+ columns,
+ &value_rows,
+ table,
+ schema,
+ &DatabaseType::Xugu,
+ catalog,
+ limits,
+ false,
+ quote_column_names,
+ )
+ .map(|batches| batches.into_iter().map(|(sql, _)| sql).collect())
+}
+
+fn source_scan_from(request: &TransferRequest, table: &str) -> Result<(String, Option<String>), String> {
+ match transfer_table_filter_for(request, table)? {
+ Some(TransferTableFilter::Query(query)) => Ok((format!("({query}) AS dbx_source"), None)),
+ Some(TransferTableFilter::Predicate(predicate)) => {
+ Ok((format!("{}.{}", quote(&request.source_database), quote(table)), Some(predicate)))
+ }
+ None => Ok((format!("{}.{}", quote(&request.source_database), quote(table)), None)),
+ }
+}
+
+/// Project wire-sensitive MySQL values to text on the source server. SQL NULL
+/// remains NULL; empty binary and JSON null stay distinct from SQL NULL.
+pub(super) fn project_page(sql: &str, columns: &[db::ColumnInfo]) -> Result<String, String> {
+ let prefix =
+ format!("SELECT {} FROM ", columns.iter().map(|column| quote(&column.name)).collect::<Vec<_>>().join(", "));
+ let tail = sql.strip_prefix(&prefix).ok_or("Unexpected generated MySQL projection; refusing to rewrite SQL")?;
+ let projection = columns
+ .iter()
+ .map(|column| {
+ let name = quote(&column.name);
+ let kind = column.data_type.to_ascii_uppercase();
+ let base = kind.split(['(', ' ']).next().unwrap_or_default();
+ let expr = match base {
+ "BIT" => {
+ let width = kind
+ .split_once('(')
+ .and_then(|(_, p)| p.trim_end_matches(')').parse::<u32>().ok())
+ .unwrap_or(1);
+ format!("LPAD(BIN({name} + 0), {width}, '0')")
+ }
+ "BINARY" | "VARBINARY" | "TINYBLOB" | "BLOB" | "MEDIUMBLOB" | "LONGBLOB" => {
+ format!("CASE WHEN {name} IS NULL THEN NULL ELSE CONCAT('0x', HEX({name})) END")
+ }
+ "DECIMAL" | "NUMERIC" | "BIGINT" | "TIME" | "DATE" | "DATETIME" | "TIMESTAMP" | "JSON" => {
+ format!("CAST({name} AS CHAR)")
+ }
+ _ => name.clone(),
+ };
+ format!("{expr} AS {name}")
+ })
+ .collect::<Vec<_>>()
+ .join(", ");
+ Ok(format!("SELECT {projection} FROM {tail}"))
+}
+
+pub(super) fn validate_rows(columns: &[db::ColumnInfo], rows: &[Vec<serde_json::Value>]) -> Result<(), String> {
+ for row in rows {
+ if row.len() != columns.len() {
+ return Err("Incomplete MySQL transfer row".into());
+ }
+ for (column, value) in columns.iter().zip(row) {
+ if let Some(value) = value.as_str() {
+ validate_text_value(column, value)?;
+ }
+ }
+ }
+ Ok(())
+}
+
+/// Doris/SelectDB/StarRocks/OceanBase-MySQL/TiDB/Manticore/Dolt connections are
+/// saved as `db_type=mysql` plus a `driver_profile` and deliberately surface as
+/// MySQL in the transfer dialog; this pair's contract is a native MySQL server
+/// only, so any of those profiles must be rejected before the first query.
+fn profile_allows_native_mysql_pair(config: &ConnectionConfig) -> bool {
+ let db_type = &config.db_type;
+ let profile = config.driver_profile.as_deref();
+ !(db::doris::is_profile(db_type, profile)
+ || db::starrocks::is_profile(db_type, profile)
+ || db::oceanbase_mysql::is_profile(db_type, profile)
+ || db::tidb::is_profile(db_type, profile)
+ || db::manticoresearch::is_profile(db_type, profile)
+ || db::dolt::is_config(config))
+}
+
+/// Run before the overwrite pre-pass, not just before CREATE TABLE. Reading an
+/// unsupported source must never clear an existing target first.
+pub(crate) fn preflight<'a>(
+ state: &'a AppState,
+ request: &'a TransferRequest,
+ source_pool: &'a str,
+ target_pool: &'a str,
+) -> futures::future::BoxFuture<'a, Result<(), String>> {
+ // Keep metadata's large async implementation out of the runner's future
+ // layout (including downstream consumers' default recursion limit).
+ Box::pin(async move {
+ let source_is_plain_mysql = {
+ let configs = state.configs.read().await;
+ configs.get(&request.source_connection_id).is_some_and(profile_allows_native_mysql_pair)
+ };
+ if !source_is_plain_mysql {
+ return Err("MySQL → Xugu currently requires a native MySQL server: MySQL-compatible product \
+ profiles (Doris/SelectDB/StarRocks/OceanBase/TiDB/Manticore/Dolt) are not enabled \
+ for this pair"
+ .into());
+ }
+ let version = execute_read_on_pool(
+ state,
+ source_pool,
+ "SELECT VERSION(), @@auto_increment_increment, @@auto_increment_offset",
+ )
+ .await?;
+ let row = version.rows.first().ok_or("Missing MySQL server metadata")?;
+ if row.first().and_then(json_scalar_to_string).is_none_or(|v| v.to_ascii_lowercase().contains("mariadb")) {
+ return Err("MySQL → Xugu currently requires a native MySQL server".into());
+ }
+ if row.get(1).and_then(json_scalar_to_string).as_deref() != Some("1")
+ || row.get(2).and_then(json_scalar_to_string).as_deref() != Some("1")
+ {
+ return Err("MySQL → Xugu currently requires auto_increment_increment=1 and auto_increment_offset=1".into());
+ }
+ for table in &request.tables {
+ let columns = get_columns_for_transfer(
+ state,
+ source_pool,
+ &request.source_connection_id,
+ &request.source_database,
+ &request.source_schema,
+ table,
+ None,
+ )
+ .await?;
+ let ddl = crate::schema::get_table_transfer_ddl_core(
+ state,
+ &request.source_connection_id,
+ &request.source_database,
+ &request.source_schema,
+ table,
+ )
+ .await?;
+ prepare(&ddl, &columns, &request.target_schema, table)?;
+ let target_name = request.target_table_name(table);
+ let target_tables = crate::schema::list_tables_core(
+ state,
+ &request.target_connection_id,
+ &request.target_database,
+ &request.target_schema,
+ Some(&target_name),
+ None,
+ None,
+ Some(&["TABLE".into()]),
+ None,
+ )
+ .await?;
+ if target_tables.iter().any(|t| t.name == target_name) {
+ let mut target_columns = get_columns_for_transfer(
+ state,
+ target_pool,
+ &request.target_connection_id,
+ &request.target_database,
+ &request.target_schema,
+ &target_name,
+ None,
+ )
+ .await?;
+ enrich_target_temporal_precision(
+ state,
+ target_pool,
+ &request.target_schema,
+ &target_name,
+ &mut target_columns,
+ )
+ .await?;
+ validate_target(&columns, &target_columns)?;
+ } else if !request.create_table || matches!(request.content, TransferContent::DataOnly) {
+ return Err(format!("Xugu target table '{target_name}' does not exist; target has not been modified"));
+ }
+ if !should_copy_data(&request.content) {
+ continue;
+ }
+ let (scan_from, filter) = source_scan_from(request, table)?;
+ let extra_filter = filter.as_ref().map(|p| format!(" AND ({p})")).unwrap_or_default();
+ let text_checks = columns.iter().filter(|c| text_column(c)).map(unsafe_text_predicate).collect::<Vec<_>>();
+ if !text_checks.is_empty() {
+ let sql =
+ format!("SELECT 1 FROM {scan_from} WHERE ({}){extra_filter} LIMIT 1", text_checks.join(" OR "));
+ if !execute_read_on_pool(state, source_pool, &sql).await?.rows.is_empty() {
+ return Err(format!("MySQL table '{table}' contains NUL, supplementary Unicode characters, or CHAR/VARCHAR trailing spaces; current native Xugu text transfer cannot preserve them. Target has not been modified"));
+ }
+ }
+ let predicates = columns
+ .iter()
+ .filter(|c| {
+ matches!(
+ c.data_type.to_ascii_uppercase().split('(').next(),
+ Some("DATE" | "DATETIME" | "TIMESTAMP")
+ )
+ })
+ .map(|c| {
+ let n = quote(&c.name);
+ format!("({n} IS NOT NULL AND (YEAR({n})=0 OR MONTH({n})=0 OR DAY({n})=0))")
+ })
+ .collect::<Vec<_>>();
+ if !predicates.is_empty() {
+ let sql =
+ format!("SELECT 1 FROM {scan_from} WHERE ({}){extra_filter} LIMIT 1", predicates.join(" OR "));
+ if !execute_read_on_pool(state, source_pool, &sql).await?.rows.is_empty() {
+ return Err(format!(
+ "MySQL table '{table}' contains zero/partial dates; target has not been modified"
+ ));
+ }
+ }
+ }
+ Ok(())
+ })
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use serde_json::json;
+
+ fn col(name: &str, kind: &str) -> db::ColumnInfo {
+ db::ColumnInfo { name: name.into(), data_type: kind.into(), is_nullable: true, ..Default::default() }
+ }
+
+ #[test]
+ fn native_pair_only() {
+ assert!(is_pair(&DatabaseType::Mysql, &DatabaseType::Xugu));
+ for engine in [DatabaseType::Postgres, DatabaseType::Doris, DatabaseType::StarRocks, DatabaseType::Xugu] {
+ assert!(!is_pair(&engine, &DatabaseType::Xugu));
+ assert!(!is_pair(&DatabaseType::Xugu, &engine));
+ }
+ }
+
+ fn profile_config(db_type: &str, driver_profile: Option<&str>) -> ConnectionConfig {
+ serde_json::from_value(json!({
+ "id": "test",
+ "name": "test",
+ "db_type": db_type,
+ "driver_profile": driver_profile,
+ "host": "",
+ "port": 3306,
+ "username": "",
+ "password": "",
+ }))
+ .expect("test connection config")
+ }
+
+ #[test]
+ fn native_mysql_pair_rejects_compatible_product_profiles() {
+ assert!(profile_allows_native_mysql_pair(&profile_config("mysql", None)));
+ for profile in ["doris", "selectdb", "starrocks", "oceanbase", "tidb", "manticoresearch", "dolt"] {
+ let config = profile_config("mysql", Some(profile));
+ assert!(!profile_allows_native_mysql_pair(&config), "profile {profile} must be rejected");
+ }
+ assert!(!profile_allows_native_mysql_pair(&profile_config("doris", None)));
+ }
+
+ #[test]
+ fn integer_domains_are_not_guessed_from_display_width() {
+ for (source, expected) in [
+ ("tinyint(1)", "TINYINT"),
+ ("tinyint unsigned", "SMALLINT"),
+ ("smallint unsigned", "INTEGER"),
+ ("mediumint unsigned", "INTEGER"),
+ ("int unsigned", "BIGINT"),
+ ("bigint unsigned", "NUMERIC(20,0)"),
+ ("bigint", "BIGINT"),
+ ("int(11) zerofill", "BIGINT"),
+ ("year", "SMALLINT"),
+ ] {
+ assert_eq!(column_type(&col("v", source)).unwrap(), expected, "{source}");
+ }
+ }
+
+ #[test]
+ fn precision_is_preserved_or_rejected_never_clamped() {
+ for (source, expected) in [
+ ("decimal(38,18)", "NUMERIC(38,18)"),
+ ("numeric(20,0)", "NUMERIC(20,0)"),
+ ("datetime(6)", "DATETIME(6)"),
+ ("timestamp(3)", "DATETIME(3)"),
+ ("time(6)", "INTERVAL HOUR(3) TO SECOND(6)"),
+ ("bit(64)", "BIT(64)"),
+ ("float(53)", "DOUBLE"),
+ ] {
+ assert_eq!(column_type(&col("v", source)).unwrap(), expected);
+ }
+ for source in
+ ["decimal(65,30)", "decimal(39,0)", "decimal(2,3)", "varchar(65535)", "geometry", "set('a','b')", "mystery"]
+ {
+ assert!(column_type(&col("v", source)).is_err(), "{source}");
+ }
+ }
+
+ #[test]
+ fn text_binary_json_types() {
+ for (source, expected) in [
+ ("char(10)", "CHAR(10)"),
+ ("varchar(60000)", "VARCHAR(60000)"),
+ ("longtext", "CLOB"),
+ ("tinytext", "CLOB"),
+ ("mediumtext", "CLOB"),
+ ("binary(16)", "BINARY(16)"),
+ ("varbinary(100)", "BINARY(100)"),
+ ("longblob", "BLOB"),
+ ("json", "JSON"),
+ ] {
+ assert_eq!(column_type(&col("v", source)).unwrap(), expected);
+ }
+ }
+
+ #[test]
+ fn complete_ddl_preserves_case_identity_seed_keys_comments_and_literals() {
+ let columns = vec![col("ID", "int"), col("Text", "varchar(40)")];
+ let ddl = prepare("CREATE TABLE `Source` (`ID` int NOT NULL AUTO_INCREMENT, `Text` varchar(40) DEFAULT 'Source.a' COMMENT 'it''s text', PRIMARY KEY (`ID`), UNIQUE KEY `uq_text` (`Text`), KEY `ix_text` (`Text`)) ENGINE=InnoDB AUTO_INCREMENT=501 DEFAULT CHARSET=utf8mb4", &columns, "Dst", "MixedCase").unwrap().ddl;
+ assert!(ddl.contains("CREATE TABLE `Dst`.`MixedCase`"), "{ddl}");
+ assert!(ddl.contains("IDENTITY(501,1)"), "{ddl}");
+ assert!(ddl.contains("DEFAULT 'Source.a'"), "{ddl}");
+ assert!(ddl.contains("PRIMARY KEY (`ID`)"), "{ddl}");
+ assert!(ddl.contains("UNIQUE (`Text`)"), "{ddl}");
+ assert!(ddl.contains("`MixedCase_ix_text`"), "{ddl}");
+ assert!(ddl.contains("IS 'it''s text'"), "{ddl}");
+ assert!(!ddl.contains("ENGINE="));
+ }
+
+ #[test]
+ fn mysql_plain_table_comment_options_are_preserved() {
+ for option in ["COMMENT='中文 it''s table'", "COMMENT '中文 it''s table'"] {
+ let ddl =
+ prepare(&format!("CREATE TABLE t (id int) ENGINE=InnoDB {option}"), &[col("id", "int")], "Dst", "t")
+ .unwrap()
+ .ddl;
+ assert!(ddl.contains("COMMENT ON TABLE `Dst`.`t` IS '中文 it''s table';"), "{ddl}");
+ }
+ }
+
+ #[test]
+ fn unsupported_structure_is_not_silently_reduced() {
+ for definition in [
+ "CREATE TABLE t (v int GENERATED ALWAYS AS (1) STORED)",
+ "CREATE TABLE t (v int, CHECK (v > 0))",
+ "CREATE TABLE t (v int, FOREIGN KEY(v) REFERENCES p(v))",
+ "CREATE TABLE t (v varchar(10), KEY i (v(2)))",
+ "CREATE TABLE t (v varchar(10), FULLTEXT KEY i (v))",
+ "CREATE TABLE t (v int) /*!50100 PARTITION BY HASH(v) PARTITIONS 2 */",
+ "CREATE TABLE t (v int); DROP TABLE p",
+ "CREATE TEMPORARY TABLE t (v int)",
+ "CREATE TABLE t (v datetime ON UPDATE CURRENT_TIMESTAMP)",
+ ] {
+ assert!(
+ prepare(
+ definition,
+ &[col(
+ "v",
+ if definition.contains("varchar") {
+ "varchar(10)"
+ } else if definition.contains("datetime") {
+ "datetime"
+ } else {
+ "int"
+ }
+ )],
+ "D",
+ "t"
+ )
+ .is_err(),
+ "{definition}"
+ );
+ }
+ assert!(prepare("CREATE TABLE t(v int, w int)", &[col("v", "int")], "D", "t").is_err());
+ assert!(prepare("CREATE TABLE t(v bigint unsigned AUTO_INCREMENT)", &[col("v", "bigint unsigned")], "D", "t")
+ .is_err());
+ }
+
+ #[test]
+ fn defaults_reject_sql_expressions_and_zero_dates() {
+ for definition in [
+ "CREATE TABLE t(v date DEFAULT '0000-00-00')",
+ "CREATE TABLE t(v date DEFAULT '2024-00-01')",
+ "CREATE TABLE t(v date DEFAULT (uuid()))",
+ ] {
+ assert!(prepare(definition, &[col("v", "date")], "D", "t").is_err());
+ }
+ let ddl =
+ prepare("CREATE TABLE t(v datetime(6) DEFAULT CURRENT_TIMESTAMP(6))", &[col("v", "datetime(6)")], "D", "t")
+ .unwrap()
+ .ddl;
+ assert!(ddl.contains("DATETIME(6) DEFAULT CURRENT_TIMESTAMP"));
+ assert!(!ddl.contains("CURRENT_TIMESTAMP("));
+ }
+
+ #[test]
+ fn projection_preserves_exact_wire_values_and_nulls() {
+ let columns = vec![
+ col("bits", "bit(64)"),
+ col("blob", "blob"),
+ col("n", "decimal(38,18)"),
+ col("dur", "time(6)"),
+ col("j", "json"),
+ ];
+ let sql = project_page(
+ "SELECT `bits`, `blob`, `n`, `dur`, `j` FROM `db`.`t` WHERE `bits` > 0 ORDER BY `bits` LIMIT 2",
+ &columns,
+ )
+ .unwrap();
+ assert!(sql.contains("LPAD(BIN(`bits` + 0), 64, '0') AS `bits`"));
+ assert!(sql.contains("CASE WHEN `blob` IS NULL THEN NULL ELSE CONCAT('0x', HEX(`blob`)) END"));
+ assert!(sql.contains("CAST(`n` AS CHAR)"));
+ assert!(sql.ends_with("FROM `db`.`t` WHERE `bits` > 0 ORDER BY `bits` LIMIT 2"));
+ for (value, kind, expected) in [
+ (json!("00000001"), "BIT(8)", "B'00000001'"),
+ (json!("0x00ff"), "BLOB", "X'00ff'"),
+ (json!("0x"), "BLOB", "X''"),
+ (json!(null), "JSON", "NULL"),
+ ] {
+ assert_eq!(escape_value_typed(&value, &DatabaseType::Xugu, Some(kind)), expected);
+ }
+ }
+
+ #[test]
+ fn row_validation_keeps_null_empty_and_zero_dates_distinct() {
+ let columns = [col("v", "datetime(6)")];
+ assert!(validate_rows(&columns, &[vec![json!(null)], vec![json!("2026-10-24 12:34:56.123456")]]).is_ok());
+ assert!(validate_rows(&columns, &[vec![json!("0000-00-00 00:00:00")]]).is_err());
+ assert!(validate_rows(&columns, &[vec![]]).is_err());
+ assert!(validate_rows(&[col("v", "varchar(20)")], &[vec![json!("中文😀")]]).is_err());
+ assert!(validate_rows(&[col("v", "varchar(20)")], &[vec![json!("中文")]]).is_ok());
+ }
+
+ #[test]
+ fn temporal_target_validation_never_assumes_missing_precision_is_lossless() {
+ let source = [col("v", "datetime(6)")];
+ assert!(validate_target(&source, &[col("v", "DATETIME(6)")]).is_ok());
+ assert!(validate_target(&source, &[col("v", "DATETIME(3)")]).is_err());
+ assert!(validate_target(&source, &[col("v", "DATETIME")]).is_err());
+ assert!(validate_target(&source, &[db::ColumnInfo { numeric_scale: Some(6), ..col("v", "DATETIME") }]).is_ok());
+ let source = [col("v", "time(6)")];
+ assert!(validate_target(&source, &[col("v", "INTERVAL HOUR(3) TO SECOND(6)")]).is_ok());
+ assert!(validate_target(&source, &[col("v", "INTERVAL HOUR(2) TO SECOND(6)")]).is_err());
+ assert!(validate_target(&source, &[col("v", "INTERVAL HOUR(3) TO SECOND(3)")]).is_err());
+ assert!(validate_target(&source, &[col("v", "INTERVAL HOUR TO SECOND")]).is_err());
+ let enriched = db::ColumnInfo {
+ numeric_precision: Some(3),
+ numeric_scale: Some(6),
+ ..col("v", "INTERVAL HOUR TO SECOND")
+ };
+ assert!(validate_target(&source, &[enriched]).is_ok());
+ }
+
+ #[test]
+ fn supplementary_unicode_defaults_and_identifiers_fail_closed() {
+ assert!(prepare("CREATE TABLE t(v VARCHAR(20) DEFAULT '😀')", &[col("v", "varchar(20)")], "D", "t").is_err());
+ assert!(prepare("CREATE TABLE `t😀`(v INT)", &[col("v", "int")], "D", "t").is_err());
+ }
+
+ #[test]
+ fn nul_text_is_rejected_without_rejecting_binary_zero_bytes() {
+ for kind in ["varchar(20)", "text", "enum('a','b')"] {
+ let column = col("v", kind);
+ assert!(validate_rows(&[column], &[vec![json!("a\0b")]]).unwrap_err().contains("NUL"));
+ }
+ assert!(validate_rows(&[col("v", "blob")], &[vec![json!("0x0000FF")], vec![json!("0x")]]).is_ok());
+ assert!(prepare("CREATE TABLE t(v VARCHAR(20) DEFAULT 'a\0b')", &[col("v", "varchar(20)")], "D", "t")
+ .unwrap_err()
+ .contains("NUL"));
+ assert!(default_sql(
+ &Expr::Value(sqlparser::ast::Value::SingleQuotedString("a\0b".into()).into()),
+ &col("v", "varchar(20)"),
+ "VARCHAR(20)"
+ )
+ .unwrap_err()
+ .contains("NUL"));
+ let predicate = unsafe_text_predicate(&col("a`b", "text"));
+ assert!(predicate.contains("LOCATE(0x00, CAST(`a``b` AS BINARY)) > 0"));
+ assert!(predicate.contains("HEX(`a``b`) REGEXP '^(..)*F"));
+ }
+
+ #[test]
+ fn target_validation_rejects_loss_before_overwrite() {
+ let source = col("v", "decimal(20,6)");
+ assert!(validate_target(std::slice::from_ref(&source), &[col("v", "numeric(20,6)")]).is_ok());
+ assert!(validate_target(std::slice::from_ref(&source), &[col("v", "numeric(20,3)")]).is_err());
+ assert!(validate_target(std::slice::from_ref(&source), &[col("v", "numeric(18,6)")]).is_err());
+ assert!(validate_target(&[col("v", "bigint unsigned")], &[col("v", "bigint")]).is_err());
+ assert!(validate_target(&[col("v", "varchar(20)")], &[col("v", "varchar(10)")]).is_err());
+ assert!(validate_target(&[col("v", "varchar(20)")], &[col("v", "varchar(30)")]).is_ok());
+ let target = db::ColumnInfo { is_nullable: false, ..col("v", "numeric(20,6)") };
+ assert!(validate_target(&[source], &[target]).is_err());
+ assert!(validate_target(
+ &[col("v", "int")],
+ &[col("v", "int"), db::ColumnInfo { is_nullable: false, ..col("required", "int") }]
+ )
+ .is_err());
+ }
+
+ #[test]
+ fn defaults_preserve_bit_binary_negative_and_unicode_literals() {
+ for (source_type, definition, expected) in [
+ ("bit(8)", "CREATE TABLE t(v BIT(8) DEFAULT b'00000001')", "DEFAULT B'00000001'"),
+ ("bit(8)", "CREATE TABLE t(v BIT(8) DEFAULT b'101')", "DEFAULT B'00000101'"),
+ ("binary(2)", "CREATE TABLE t(v BINARY(2) DEFAULT X'00FF')", "DEFAULT X'00FF'"),
+ ("int", "CREATE TABLE t(v INT DEFAULT -7)", "DEFAULT -7"),
+ ("varchar(20)", "CREATE TABLE t(v VARCHAR(20) DEFAULT '中文''值')", "DEFAULT '中文''值'"),
+ ] {
+ let ddl = prepare(definition, &[col("v", source_type)], "D", "t").unwrap().ddl;
+ assert!(ddl.contains(expected), "{ddl}");
+ }
+ }
+
+ #[test]
+ fn current_timestamp_defaults_use_xugu_syntax_with_column_precision() {
+ for precision in 0..=6 {
+ let kind = format!("datetime({precision})");
+ let definition = format!("CREATE TABLE t(v DATETIME({precision}) DEFAULT CURRENT_TIMESTAMP({precision}))");
+ let ddl = prepare(&definition, &[col("v", &kind)], "D", "t").unwrap().ddl;
+ assert!(ddl.contains(&format!("DATETIME({precision}) DEFAULT CURRENT_TIMESTAMP")), "{ddl}");
+ assert!(!ddl.contains("CURRENT_TIMESTAMP("), "{ddl}");
+ }
+ }
+
+ #[test]
+ fn pair_local_text_literals_keep_backslashes_and_json_escapes() {
+ let text = json!("C:\\path\\o'clock\nnext");
+ let document = json!(r#"{"path":"C:\\tmp","line":"a\nb"}"#);
+ assert_eq!(value_literal(&text, Some("VARCHAR(80)")), "'C:\\path\\o''clock\nnext'");
+ assert_eq!(value_literal(&document, Some("JSON")), format!("'{}'", document.as_str().unwrap()));
+ let columns = vec!["v".into(), "j".into(), "b".into()];
+ let kinds = vec![Some("VARCHAR(80)".into()), Some("JSON".into()), Some("BLOB".into())];
+ let rows = vec![vec![text, document, json!("0x00ff")], vec![json!(null), json!(null), json!("0x")]];
+ let limits = SqlBatchLimits::for_database(&DatabaseType::Xugu, 1);
+ let batches = write_batches(&columns, &kinds, &rows, "t", "S", None, limits, true).unwrap();
+ assert_eq!(batches.len(), 2);
+ assert!(batches[0].contains("'C:\\path\\o''clock\nnext'"), "{}", batches[0]);
+ assert!(batches[0].contains("X'00ff'"));
+ assert!(batches[1].contains("(NULL, NULL, X'')"));
+ assert!(write_batches(&columns, &kinds, &rows, "t", "S", None, limits.with_hard_sql_bytes(Some(10)), true,)
+ .is_err());
+ // Do not change any other pair's shared formatter as a side effect.
+ assert_eq!(escape_value_typed(&json!(r"a\b"), &DatabaseType::Mysql, Some("VARCHAR(20)")), r"'a\\b'");
+ }
+
+ #[test]
+ fn trailing_spaces_are_rejected_only_for_trimming_target_types() {
+ for kind in ["char(20)", "varchar(20)", "enum('a ','b')"] {
+ let column = col("v", kind);
+ assert!(validate_rows(std::slice::from_ref(&column), &[vec![json!("a ")]]).is_err());
+ assert!(unsafe_text_predicate(&column).contains("RIGHT(CAST(`v` AS BINARY), 1) = 0x20"));
+ }
+ for kind in ["text", "longtext", "json"] {
+ let column = col("v", kind);
+ assert!(validate_rows(std::slice::from_ref(&column), &[vec![json!("a ")]]).is_ok());
+ assert!(!unsafe_text_predicate(&column).contains("RIGHT("));
+ }
+ assert!(prepare("CREATE TABLE t(v varchar(20) DEFAULT 'a ')", &[col("v", "varchar(20)")], "S", "t").is_err());
+ assert!(validate_rows(&[col("v", "varchar(20)")], &[vec![json!(" a b")], vec![json!("")], vec![json!(null)]])
+ .is_ok());
+ }
+
+ #[test]
+ fn enum_members_are_not_replaced_with_arbitrary_length_or_dropped() {
+ let column = db::ColumnInfo {
+ character_maximum_length: Some(10),
+ enum_values: Some(vec!["ready".into(), "it's done".into(), "".into()]),
+ ..col("v", "enum('ready','it''s done','')")
+ };
+ let ddl = prepare("CREATE TABLE t(v enum('ready','it''s done',''))", std::slice::from_ref(&column), "D", "t")
+ .unwrap()
+ .ddl;
+ assert!(ddl.contains("VARCHAR(10) CHECK (`v` IN ('ready', 'it''s done', ''))"), "{ddl}");
+ assert!(prepare(
+ "CREATE TABLE t(v enum('ready','it''s done',''))",
+ &[db::ColumnInfo { enum_values: None, ..column }],
+ "D",
+ "t"
+ )
+ .is_err());
+ }
+
+ #[test]
+ fn projection_handles_keywords_inside_quoted_column_names() {
+ let columns = [col("x FROM y", "blob"), col("tick`name", "int")];
+ let sql = project_page("SELECT `x FROM y`, `tick``name` FROM `db`.`t` LIMIT 1", &columns).unwrap();
+ assert!(sql.ends_with("FROM `db`.`t` LIMIT 1"));
+ assert!(sql.contains("HEX(`x FROM y`)"));
+ assert!(project_page("SELECT * FROM `db`.`t`", &columns).is_err());
+ }
+
+ #[test]
+ fn ordinary_indexes_use_xugu_simple_names_on_qualified_tables() {
+ let ddl = prepare("CREATE TABLE t(v INT, KEY idx_v(v))", &[col("v", "int")], "Dst", "MixedTable").unwrap().ddl;
+ assert!(ddl.contains("CREATE INDEX `MixedTable_idx_v` ON `Dst`.`MixedTable` (`v`)"), "{ddl}");
+ assert!(!ddl.contains("CREATE INDEX `Dst`."), "{ddl}");
+ }
+}
diff --git a/crates/dbx-core/src/data/transfer_runner.rs b/crates/dbx-core/src/data/transfer_runner.rs
index 0e484ac570..a863da65f5 100644
--- a/crates/dbx-core/src/data/transfer_runner.rs
+++ b/crates/dbx-core/src/data/transfer_runner.rs
@@ -66,6 +66,22 @@ async fn emit_terminal_progress<F>(
emitter.emit_terminal(progress);
}
+fn cancelled_table_counts(
+ source: &DatabaseType,
+ target: &DatabaseType,
+ moved_rows: Option<u64>,
+ source_rows: Option<u64>,
+) -> (u64, Option<u64>) {
+ // Cancellation does not roll back batches already committed to Xugu. Keep
+ // the current table's acknowledged writes, never a previous table's count.
+ // Preserve the existing terminal-event contract for every other DB pair.
+ if super::transfer::mysql_xugu::is_pair(source, target) {
+ (moved_rows.unwrap_or(0), source_rows)
+ } else {
+ (0, None)
+ }
+}
+
#[allow(clippy::too_many_arguments)]
pub async fn run_transfer<F>(
state: Arc<AppState>,
@@ -87,6 +103,9 @@ where
let (routine_context, package_transfer) = match async {
super::transfer::validate_transfer_database_pair(&request, &source_db_type, &target_db_type)?;
+ if super::transfer::mysql_xugu::is_pair(&source_db_type, &target_db_type) {
+ super::transfer::mysql_xugu::preflight(&state, &request, &source_pool_key, &target_pool_key).await?;
+ }
let routine = crate::schema::oracle_routines::capture_routine_transfer_context(
&state,
&request.source_connection_id,
@@ -506,6 +525,8 @@ where
}
Err(e) => {
if e == "Cancelled" {
+ let (rows_transferred, total_rows) =
+ cancelled_table_counts(&source_db_type, &target_db_type, moved_row_count, source_row_count);
if let Some(journal) = history.as_ref() {
journal
.finish_table(
@@ -527,8 +548,8 @@ where
table: table.clone(),
table_index: i,
total_tables,
- rows_transferred: 0,
- total_rows: None,
+ rows_transferred,
+ total_rows,
status: TransferStatus::Cancelled,
error: None,
terminal: true,
@@ -799,6 +820,26 @@ mod tests {
use crate::models::connection::ConnectionConfig;
use serde_json::json;
+ #[test]
+ fn mysql_xugu_cancellation_preserves_only_current_table_acknowledged_rows() {
+ assert_eq!(
+ cancelled_table_counts(&DatabaseType::Mysql, &DatabaseType::Xugu, Some(413), Some(10_000)),
+ (413, Some(10_000))
+ );
+ assert_eq!(
+ cancelled_table_counts(&DatabaseType::Mysql, &DatabaseType::Xugu, None, Some(10_000)),
+ (0, Some(10_000))
+ );
+ assert_eq!(cancelled_table_counts(&DatabaseType::Mysql, &DatabaseType::Xugu, None, None), (0, None));
+ for (source, target) in [
+ (DatabaseType::Mysql, DatabaseType::Mysql),
+ (DatabaseType::Xugu, DatabaseType::Xugu),
+ (DatabaseType::Postgres, DatabaseType::Xugu),
+ ] {
+ assert_eq!(cancelled_table_counts(&source, &target, Some(413), Some(10_000)), (0, None));
+ }
+ }
+
#[tokio::test]
async fn unsupported_package_requests_stop_before_connection_capture_or_any_table_work() {
let directory = tempfile::tempdir().unwrap();
diff --git a/docs/public/screenshots/mysql-xugu-transfer.png b/docs/public/screenshots/mysql-xugu-transfer.png
new file mode 100644
index 0000000000..86bca33a02
Binary files /dev/null and b/docs/public/screenshots/mysql-xugu-transfer.png differ