feat(export): support database setup and batch SQL scripts

This commit is contained in:
二丫讲梵
2026-07-24 23:52:10 +08:00
committed by GitHub
parent 3d1601c807
commit 9cfee7f497
21 changed files with 622 additions and 235 deletions
@@ -59,6 +59,7 @@ const tableError = ref<string | null>(null);
const includeStructure = ref(true);
const includeData = ref(true);
const includeObjects = ref(true);
const includeCreateDatabase = ref(false);
const dropTableIfExists = ref(false);
const omitAutoIncrement = ref(false);
// `AUTO_INCREMENT` stripping is a MySQL-only DDL transform (backend gates on
@@ -77,6 +78,7 @@ const pendingPrefillTables = ref<string[]>([]);
const exportAllDatabases = ref(false);
const batchDatabaseIndex = ref(0);
const batchDatabaseTotal = ref(0);
const batchRowsExported = ref(0);
const activeDatabaseExportId = ref("");
const sqlConnections = computed(() => store.connections.filter((c) => !["redis", "mongodb", "elasticsearch", "qdrant", "milvus", "weaviate", "chromadb", "etcd", "zookeeper", "mq", "nacos"].includes(c.db_type)));
@@ -110,11 +112,6 @@ function joinExportPath(directory: string, fileName: string): string {
return `${directory.replace(/[\\/]+$/, "")}${separator}${fileName}`;
}
function currentRowsExported(): number {
const progress: ExportProgress | null = exportProgress.value;
return progress?.rowsExported ?? 0;
}
async function loadDatabases(connId: string) {
if (!connId) return;
loadingMeta.value = true;
@@ -276,6 +273,7 @@ async function startExport() {
includeStructure: includeStructure.value,
includeData: includeData.value,
includeObjects: includeObjects.value,
includeCreateDatabase: includeCreateDatabase.value,
dropTableIfExists: dropTableIfExists.value,
omitAutoIncrement: omitAutoIncrement.value,
batchSize: 1000,
@@ -343,6 +341,7 @@ async function startAllDatabasesExport() {
exportCancelled.value = false;
exportProgress.value = null;
batchDatabaseIndex.value = 0;
batchRowsExported.value = 0;
const dbs = [...selectedDatabases.value];
const batchId = generateDatabaseExportId();
@@ -370,19 +369,30 @@ async function startAllDatabasesExport() {
includeStructure: includeStructure.value,
includeData: includeData.value,
includeObjects: includeObjects.value,
includeCreateDatabase: includeCreateDatabase.value,
dropTableIfExists: dropTableIfExists.value,
omitAutoIncrement: omitAutoIncrement.value,
batchSize: 1000,
};
let currentDatabaseRowsExported = 0;
await runDatabaseExportUntilTerminal(request, (progress) => {
exportProgress.value = { ...progress, exportId: batchId, currentObject: `${item.displayName}: ${progress.currentObject || item.displayName}` };
const nextRowsExported = Math.max(0, progress.rowsExported);
batchRowsExported.value += Math.max(0, nextRowsExported - currentDatabaseRowsExported);
currentDatabaseRowsExported = nextRowsExported;
exportProgress.value = {
...progress,
exportId: batchId,
currentObject: `${item.displayName}: ${progress.currentObject || item.displayName}`,
rowsExported: batchRowsExported.value,
};
updateDatabaseExportTask(batchId, {
...progress,
exportId: batchId,
currentObject: item.displayName,
objectIndex: index,
totalObjects: exportPlan.length,
rowsExported: batchRowsExported.value,
});
if (progress.status === "Error") {
exportError.value = progress.error;
@@ -400,16 +410,18 @@ async function startAllDatabasesExport() {
if (!exportError.value && !exportCancelled.value) {
exportDone.value = true;
isExporting.value = false;
updateDatabaseExportTask(batchId, {
const finalProgress: api.ExportProgress = {
exportId: batchId,
currentObject: t("databaseExport.allDatabasesTask", { count: dbs.length }),
objectIndex: exportPlan.length,
totalObjects: exportPlan.length,
rowsExported: currentRowsExported(),
rowsExported: batchRowsExported.value,
totalRows: null,
status: "Done",
error: null,
});
};
exportProgress.value = finalProgress;
updateDatabaseExportTask(batchId, finalProgress);
toast(t("databaseExport.exportAllSuccess", { count: dbs.length }), 3000);
}
} catch (e: any) {
@@ -419,7 +431,7 @@ async function startAllDatabasesExport() {
currentObject: t("databaseExport.allDatabasesTask", { count: dbs.length }),
objectIndex: Math.max(0, batchDatabaseIndex.value - 1),
totalObjects: batchDatabaseTotal.value || dbs.length,
rowsExported: currentRowsExported(),
rowsExported: batchRowsExported.value,
totalRows: null,
status: "Error",
error: exportError.value,
@@ -459,6 +471,7 @@ function resetState() {
includeStructure.value = true;
includeData.value = true;
includeObjects.value = true;
includeCreateDatabase.value = false;
dropTableIfExists.value = false;
omitAutoIncrement.value = false;
isExporting.value = false;
@@ -469,10 +482,17 @@ function resetState() {
exportId.value = "";
batchDatabaseIndex.value = 0;
batchDatabaseTotal.value = 0;
batchRowsExported.value = 0;
activeDatabaseExportId.value = "";
}
const progressPercent = computed(() => {
if (exportAllDatabases.value && batchDatabaseTotal.value > 0) {
if (exportDone.value) return 100;
const current = exportProgress.value;
const currentDatabaseProgress = current && current.totalObjects > 0 ? current.objectIndex / current.totalObjects : 0;
return Math.round(Math.min(1, (Math.max(0, batchDatabaseIndex.value - 1) + currentDatabaseProgress) / batchDatabaseTotal.value) * 100);
}
const p = exportProgress.value;
if (!p || p.totalObjects === 0) return 0;
return Math.round((p.objectIndex / p.totalObjects) * 100);
@@ -679,6 +699,11 @@ watch(
<Square v-else class="w-3.5 h-3.5 text-muted-foreground/40 shrink-0" />
{{ t("databaseExport.includeStructure") }}
</div>
<div v-if="isMysqlFamily" class="flex items-center gap-2 cursor-pointer text-xs" @click="includeCreateDatabase = !includeCreateDatabase">
<CheckSquare v-if="includeCreateDatabase" class="w-3.5 h-3.5 text-primary shrink-0" />
<Square v-else class="w-3.5 h-3.5 text-muted-foreground/40 shrink-0" />
{{ t("databaseExport.includeCreateDatabase") }}
</div>
<div class="flex items-center gap-2 text-xs" :class="includeStructure ? 'cursor-pointer' : 'cursor-not-allowed text-muted-foreground/50'" @click="includeStructure && (dropTableIfExists = !dropTableIfExists)">
<CheckSquare v-if="dropTableIfExists && includeStructure" class="w-3.5 h-3.5 text-primary shrink-0" />
<Square v-else class="w-3.5 h-3.5 text-muted-foreground/40 shrink-0" />
@@ -708,7 +733,7 @@ watch(
{{ t("databaseExport.currentDatabase", { current: batchDatabaseIndex, total: batchDatabaseTotal }) }}
</div>
<div v-if="exportProgress" class="space-y-2">
<div class="text-xs text-muted-foreground">
<div v-if="!exportAllDatabases || !exportDone" class="text-xs text-muted-foreground">
{{
t("databaseExport.currentTable", {
table: exportProgress.currentObject,
@@ -723,7 +748,7 @@ watch(
</div>
<div class="text-xs text-muted-foreground">
{{ t("databaseExport.rowsExported", { count: exportProgress.rowsExported.toLocaleString() }) }}
{{ exportAllDatabases ? t("databaseExport.allRowsExported", { count: exportProgress.rowsExported.toLocaleString() }) : t("databaseExport.rowsExported", { current: exportProgress.objectIndex, total: exportProgress.totalObjects, count: exportProgress.rowsExported.toLocaleString() }) }}
</div>
</div>
@@ -17,7 +17,7 @@ import { useProductionSafetyStore } from "@/stores/productionSafetyStore";
import { productionContextForDatabase } from "@/lib/database/productionSafety";
import { fetchSqlFileTargetOptions } from "@/composables/useDatabaseOptions";
import { requiresSqlFileTargetDatabaseSelection } from "@/lib/connection/connectionLevelDatabaseBootstrap";
import { cancelSqlFileExecution, executeSqlFile, listenSqlFileProgress, previewSqlFile, type SqlFilePreview, type SqlFileProgress, type SqlFileStatus } from "@/lib/backend/api";
import { cancelSqlFileExecution, executeSqlFiles, listenSqlFileProgress, previewSqlFile, type SqlFilePreview, type SqlFileProgress, type SqlFileStatus } from "@/lib/backend/api";
import { useExportTracker } from "@/composables/useExportTracker";
import { Check, CheckSquare, FileCode, FolderOpen, Loader2, Play, Square, X } from "@lucide/vue";
@@ -38,7 +38,7 @@ const productionSafetyStore = useProductionSafetyStore();
const fileInput = ref<HTMLInputElement | null>(null);
const filePath = ref("");
const preview = ref<SqlFilePreview | null>(null);
const previews = ref<SqlFilePreview[]>([]);
const selectingFile = ref(false);
const loadingPreview = ref(false);
const connectionId = ref("");
@@ -56,6 +56,7 @@ const progress = ref<SqlFileProgress | null>(null);
const terminalStatus = ref<SqlFileStatus | "idle">("idle");
const terminalError = ref("");
const refreshedTarget = ref(false);
const MAX_WEB_SQL_FILE_BYTES = 200 * 1024 * 1024;
const sqlConnections = computed(() => store.connections.filter((c) => !["redis", "mongodb", "elasticsearch", "qdrant", "milvus", "weaviate", "chromadb", "etcd", "zookeeper", "mq", "nacos"].includes(c.db_type)));
@@ -63,8 +64,14 @@ const selectedConnection = computed(() => sqlConnections.value.find((c) => c.id
const canStart = computed(() => {
const connection = selectedConnection.value;
if (!preview.value || !connection || running.value || loadingPreview.value || loadingDatabases.value) return false;
return !!database.value.trim() || !requiresSqlFileTargetDatabaseSelection(connection, preview.value.canExecuteWithoutSelectedDatabase);
if (previews.value.length === 0 || !connection || running.value || loadingPreview.value || loadingDatabases.value) return false;
let hasDatabaseContext = false;
const canExecuteWithoutSelectedDatabase = previews.value.every((item) => {
if (!hasDatabaseContext && !item.canExecuteWithoutSelectedDatabase) return false;
hasDatabaseContext ||= item.establishesDatabaseContext === true;
return true;
});
return !!database.value.trim() || !requiresSqlFileTargetDatabaseSelection(connection, canExecuteWithoutSelectedDatabase);
});
const statusTone = computed(() => {
@@ -90,6 +97,7 @@ const progressPercent = computed(() => {
if (current <= 0) return running.value ? 8 : 0;
return Math.min(95, Math.max(8, Math.round((attempted / current) * 100)));
});
const preview = computed(() => previews.value[0] ?? null);
const previewLineCount = computed(() => preview.value?.preview.split(/\r\n|\r|\n/).length ?? 0);
const previewLineNumbers = computed(() => Array.from({ length: previewLineCount.value }, (_, index) => index + 1));
const previewIsTruncated = computed(() => {
@@ -127,10 +135,6 @@ function statusLabel(status: SqlFileStatus | "idle") {
return t(`sqlFile.status.${status}`);
}
function isTerminalStatus(status: SqlFileStatus | "idle") {
return status === "done" || status === "error" || status === "cancelled";
}
function resolveInitialConnectionId() {
if (props.prefillConnectionId && sqlConnections.value.some((c) => c.id === props.prefillConnectionId)) {
return props.prefillConnectionId;
@@ -162,7 +166,7 @@ function resetExecution() {
function resetState() {
filePath.value = "";
preview.value = null;
previews.value = [];
selectingFile.value = false;
loadingPreview.value = false;
connectionId.value = resolveInitialConnectionId();
@@ -209,17 +213,26 @@ async function previewSelectedSqlFile(fileOrPath: string | File) {
if (isTauriRuntime()) {
return previewSqlFile(fileOrPath as string);
}
const file = fileOrPath as File;
if (file.size > MAX_WEB_SQL_FILE_BYTES) {
throw new Error(`File too large: ${file.size} bytes (max ${MAX_WEB_SQL_FILE_BYTES} bytes)`);
}
const { previewSqlFile: previewWebSqlFile } = await import("@/lib/backend/http");
return previewWebSqlFile(fileOrPath as File);
return previewWebSqlFile(file);
}
async function loadPreview(fileOrPath: string | File) {
async function loadPreviews(filesOrPaths: Array<string | File>) {
loadingPreview.value = true;
preview.value = null;
previews.value = [];
filePath.value = "";
resetExecution();
try {
preview.value = await previewSelectedSqlFile(fileOrPath);
filePath.value = preview.value.filePath;
resetExecution();
const nextPreviews: SqlFilePreview[] = [];
for (const fileOrPath of filesOrPaths) {
nextPreviews.push(await previewSelectedSqlFile(fileOrPath));
}
previews.value = nextPreviews;
filePath.value = nextPreviews.map((item) => item.filePath).join("; ");
} catch (e: any) {
toast(e?.message || String(e), 5000);
} finally {
@@ -237,11 +250,12 @@ async function selectFile() {
try {
const { open } = await import("@tauri-apps/plugin-dialog");
const selected = await open({
multiple: false,
multiple: true,
filters: [{ name: "SQL", extensions: ["sql"] }],
});
if (typeof selected === "string") {
await loadPreview(selected);
const paths = Array.isArray(selected) ? selected : selected ? [selected] : [];
if (paths.length > 0) {
await loadPreviews(paths);
}
} catch (e: any) {
toast(e?.message || String(e), 5000);
@@ -252,23 +266,27 @@ async function selectFile() {
async function handleFileInputChange(event: Event) {
const input = event.target as HTMLInputElement;
const file = input.files?.[0];
const files = Array.from(input.files ?? []);
input.value = "";
if (!file || running.value) return;
if (files.length === 0 || running.value) return;
selectingFile.value = true;
try {
await loadPreview(file);
await loadPreviews(files);
} finally {
selectingFile.value = false;
}
}
async function listenProgress(id: string, handler: (next: SqlFileProgress) => void): Promise<() => void> {
async function listenProgress(id: string, handler: (next: SqlFileProgress) => void, onError?: (error: Error) => void): Promise<() => void> {
if (isTauriRuntime()) {
return listenSqlFileProgress(handler);
}
const { listenSqlFileProgressById } = await import("@/lib/sql/httpSqlFileProgress");
return listenSqlFileProgressById(id, handler);
return listenSqlFileProgressById(id, handler, onError);
}
function isTerminalProgress(status: SqlFileStatus): boolean {
return status === "done" || status === "error" || status === "cancelled";
}
async function refreshTargetAfterImport() {
@@ -282,12 +300,15 @@ async function refreshTargetAfterImport() {
}
async function startExecution() {
if (!canStart.value || !preview.value) return;
if (!canStart.value || previews.value.length === 0) return;
const productionContext = productionContextForDatabase(selectedConnection.value, database.value);
if (productionContext.active) {
// File previews are truncated, so production file execution is always reviewed instead of inferring safety from a partial preview.
const confirmed = await productionSafetyStore.requestConfirmation({
sql: preview.value.preview,
sql: previews.value
.map((item) => item.preview)
.join("\n\n")
.slice(0, 20_000),
connectionName: selectedConnection.value?.name,
database: database.value,
productionDatabases: productionContext.databases,
@@ -296,8 +317,8 @@ async function startExecution() {
if (!confirmed) return;
}
const id = uuid();
executionId.value = id;
const batchId = uuid();
executionId.value = batchId;
running.value = true;
cancelling.value = false;
cancelRequested.value = false;
@@ -305,9 +326,9 @@ async function startExecution() {
terminalStatus.value = "running";
terminalError.value = "";
progress.value = null;
addSqlFileTask(id, preview.value.fileName, preview.value.filePath);
const taskLabel = previews.value.length === 1 ? previews.value[0]!.fileName : `${previews.value[0]!.fileName} (+${previews.value.length - 1})`;
addSqlFileTask(batchId, taskLabel, filePath.value);
let unlisten: (() => void) | undefined;
try {
await store.ensureConnected(connectionId.value);
if (cancelRequested.value) {
@@ -315,58 +336,60 @@ async function startExecution() {
return;
}
unlisten = await listenProgress(id, (next) => {
if (next.executionId !== id) return;
progress.value = next;
terminalStatus.value = next.status;
terminalError.value = next.error ?? terminalError.value;
updateSqlFileTask(id, next);
if (isTerminalStatus(next.status)) {
running.value = false;
cancelling.value = false;
}
if (next.status === "done") {
void refreshTargetAfterImport();
}
let resolveTerminalProgress: (progress: SqlFileProgress) => void = () => {};
let rejectTerminalProgress: (error: Error) => void = () => {};
const terminalProgress = new Promise<SqlFileProgress>((resolve, reject) => {
resolveTerminalProgress = resolve;
rejectTerminalProgress = reject;
});
let completedSuccessfully = false;
const unlisten = await listenProgress(
batchId,
(next) => {
if (next.executionId !== batchId) return;
progress.value = next;
terminalStatus.value = next.status;
terminalError.value = next.error ?? terminalError.value;
updateSqlFileTask(batchId, next);
if (isTerminalProgress(next.status)) {
resolveTerminalProgress(next);
}
},
rejectTerminalProgress,
);
if (cancelRequested.value) {
terminalStatus.value = "cancelled";
return;
try {
executionStarted.value = true;
await executeSqlFiles(
{
executionId: batchId,
connectionId: connectionId.value,
database: database.value.trim(),
filePath: previews.value[0]!.filePath,
continueOnError: continueOnError.value,
},
previews.value.map((item) => item.filePath),
);
const terminal = await terminalProgress;
if (terminal.status === "error") {
throw new Error(terminal.error || "SQL file execution failed");
}
if (terminal.status === "cancelled") {
cancelRequested.value = true;
}
completedSuccessfully = terminal.status === "done";
} finally {
executionStarted.value = false;
unlisten();
}
executionStarted.value = true;
await executeSqlFile({
executionId: id,
connectionId: connectionId.value,
database: database.value.trim(),
filePath: preview.value.filePath,
continueOnError: continueOnError.value,
});
if (!isTerminalStatus(terminalStatus.value)) {
terminalStatus.value = cancelRequested.value ? "cancelled" : "done";
const lastProgress = progress.value as SqlFileProgress | null;
updateSqlFileTask(id, {
executionId: id,
status: terminalStatus.value,
statementIndex: lastProgress?.statementIndex ?? 0,
successCount: lastProgress?.successCount ?? 0,
failureCount: lastProgress?.failureCount ?? 0,
affectedRows: lastProgress?.affectedRows ?? 0,
elapsedMs: lastProgress?.elapsedMs ?? 0,
statementSummary: lastProgress?.statementSummary ?? "",
error: lastProgress?.error ?? null,
});
if (terminalStatus.value === "done") {
await refreshTargetAfterImport();
}
}
if (completedSuccessfully) await refreshTargetAfterImport();
} catch (e: any) {
terminalStatus.value = cancelRequested.value ? "cancelled" : "error";
terminalError.value = e?.message || String(e);
const lastProgress = progress.value as SqlFileProgress | null;
updateSqlFileTask(id, {
executionId: id,
updateSqlFileTask(batchId, {
executionId: batchId,
status: terminalStatus.value,
statementIndex: lastProgress?.statementIndex ?? 0,
successCount: lastProgress?.successCount ?? 0,
@@ -380,7 +403,6 @@ async function startExecution() {
toast(terminalError.value, 5000);
}
} finally {
unlisten?.();
running.value = false;
cancelling.value = false;
executionStarted.value = false;
@@ -429,7 +451,7 @@ watch(
// When opened from the SQL Files panel with a pre-selected file, load its
// preview automatically so the user can review statements before running.
if (props.prefillFilePath) {
void loadPreview(props.prefillFilePath);
void loadPreviews([props.prefillFilePath]);
}
},
{ immediate: true },
@@ -454,7 +476,7 @@ watch(
</div>
<div class="flex items-center gap-2">
<input ref="fileInput" type="file" accept=".sql,text/sql" class="hidden" @change="handleFileInputChange" />
<input ref="fileInput" type="file" accept=".sql,text/sql" multiple class="hidden" @change="handleFileInputChange" />
<Input :model-value="filePath" readonly class="h-8 text-xs font-mono" :placeholder="t('sqlFile.selectSqlFile')" />
<Button variant="outline" size="sm" class="h-8 shrink-0" :disabled="running || selectingFile" @click="selectFile">
<Loader2 v-if="selectingFile || loadingPreview" class="w-3.5 h-3.5 mr-1.5 animate-spin" />
@@ -468,6 +490,7 @@ watch(
<div class="min-w-0 flex items-center gap-2">
<FileCode class="w-3.5 h-3.5 text-muted-foreground shrink-0" />
<span class="font-medium truncate">{{ preview.fileName }}</span>
<span v-if="previews.length > 1" class="shrink-0 text-muted-foreground">(+{{ previews.length - 1 }})</span>
</div>
<div class="flex shrink-0 items-center gap-2 text-muted-foreground">
<span>{{ previewLineSummary }}</span>
+3 -1
View File
@@ -4411,6 +4411,7 @@ export default {
title: "Export Database",
options: "Options",
includeStructure: "Table structure (DDL)",
includeCreateDatabase: "Include database setup (CREATE DATABASE / USE)",
dropTableIfExists: "Add DROP TABLE IF EXISTS before DDL",
omitAutoIncrement: "Omit AUTO_INCREMENT (for fresh-install scripts)",
includeData: "Table data (INSERT)",
@@ -4434,7 +4435,8 @@ export default {
selectExportDirectory: "Select export directory",
currentDatabase: "Database {current}/{total}",
currentTable: "Current: {table} ({current}/{total})",
rowsExported: "{count} rows exported",
rowsExported: "{current}/{total} objects processed, {count} rows exported",
allRowsExported: "Total rows exported: {count}",
exportSuccess: "Database exported successfully",
exportAllSuccess: "{count} databases exported successfully",
allDatabasesTask: "All databases ({count})",
+3 -1
View File
@@ -4176,6 +4176,7 @@ export default withEnglishFallback({
title: "Exportar base de datos",
options: "Opciones",
includeStructure: "Estructura de tablas (DDL)",
includeCreateDatabase: "Incluir creación de base de datos (CREATE DATABASE / USE)",
dropTableIfExists: "Agregar DROP TABLE IF EXISTS antes del DDL",
omitAutoIncrement: "Omitir AUTO_INCREMENT (para scripts de instalación nueva)",
includeData: "Datos de tablas (INSERT)",
@@ -4198,7 +4199,8 @@ export default withEnglishFallback({
selectExportDirectory: "Seleccionar directorio de exportación",
currentDatabase: "Base de datos {current}/{total}",
currentTable: "Actual: {table} ({current}/{total})",
rowsExported: "{count} filas exportadas",
rowsExported: "{current}/{total} objetos procesados, {count} filas exportadas",
allRowsExported: "Total de filas exportadas: {count}",
exportSuccess: "Base de datos exportada exitosamente",
exportAllSuccess: "{count} bases de datos exportadas exitosamente",
allDatabasesTask: "Todas las bases de datos ({count})",
+3 -1
View File
@@ -4174,6 +4174,7 @@ export default withEnglishFallback({
title: "Esporta Database",
options: "Opzioni",
includeStructure: "Struttura tabella (DDL)",
includeCreateDatabase: "Includi creazione database (CREATE DATABASE / USE)",
dropTableIfExists: "Aggiungi DROP TABLE IF EXISTS prima del DDL",
omitAutoIncrement: "Ometti AUTO_INCREMENT (per script di installazione pulita)",
includeData: "Dati tabella (INSERT)",
@@ -4197,7 +4198,8 @@ export default withEnglishFallback({
selectExportDirectory: "Seleziona directory di esportazione",
currentDatabase: "Database {current}/{total}",
currentTable: "Corrente: {table} ({current}/{total})",
rowsExported: "{count} righe esportate",
rowsExported: "{current}/{total} oggetti elaborati, {count} righe esportate",
allRowsExported: "Totale righe esportate: {count}",
exportSuccess: "Database esportato con successo",
exportAllSuccess: "{count} database esportati con successo",
allDatabasesTask: "Tutti i database ({count})",
+3 -1
View File
@@ -4175,6 +4175,7 @@ export default withEnglishFallback({
title: "データベースをエクスポート",
options: "オプション",
includeStructure: "テーブル構造 (DDL)",
includeCreateDatabase: "データベース作成文を含める(CREATE DATABASE / USE)",
dropTableIfExists: "DDLの前にDROP TABLE IF EXISTSを追加",
omitAutoIncrement: "AUTO_INCREMENT を省略(新規インストール用スクリプト向け)",
includeData: "テーブルデータ (INSERT)",
@@ -4198,7 +4199,8 @@ export default withEnglishFallback({
selectExportDirectory: "エクスポート先ディレクトリを選択",
currentDatabase: "データベース {current}/{total}",
currentTable: "現在: {table} ({current}/{total})",
rowsExported: "{count}行をエクスポートしました",
rowsExported: "{current}/{total} オブジェクトを処理、{count}行をエクスポートしました",
allRowsExported: "累計エクスポート行数: {count}",
exportSuccess: "データベースを正常にエクスポートしました",
exportAllSuccess: "{count}個のデータベースを正常にエクスポートしました",
allDatabasesTask: "すべてのデータベース ({count})",
+3 -1
View File
@@ -4176,6 +4176,7 @@ export default withEnglishFallback({
title: "Exportar banco de dados",
options: "Opções",
includeStructure: "Estrutura da tabela (DDL)",
includeCreateDatabase: "Incluir criação do banco (CREATE DATABASE / USE)",
dropTableIfExists: "Adicionar DROP TABLE IF EXISTS antes do DDL",
omitAutoIncrement: "Omitir AUTO_INCREMENT (para scripts de instalação do zero)",
includeData: "Dados da tabela (INSERT)",
@@ -4200,7 +4201,8 @@ export default withEnglishFallback({
selectExportDirectory: "Selecionar diretório de exportação",
currentDatabase: "Banco de dados {current}/{total}",
currentTable: "Atual: {table} ({current}/{total})",
rowsExported: "{count} linhas exportadas",
rowsExported: "{current}/{total} objetos processados, {count} linhas exportadas",
allRowsExported: "Total de linhas exportadas: {count}",
exportSuccess: "Banco de dados exportado com sucesso",
exportAllSuccess: "{count} bancos de dados exportados com sucesso",
allDatabasesTask: "Todos os bancos de dados ({count})",
+3 -1
View File
@@ -4398,6 +4398,7 @@ export default withEnglishFallback({
title: "导出数据库",
options: "选项",
includeStructure: "表结构 (DDL)",
includeCreateDatabase: "导出建库语句(CREATE DATABASE / USE)",
dropTableIfExists: "导出前添加 DROP TABLE IF EXISTS",
omitAutoIncrement: "省略 AUTO_INCREMENT(用于全新安装的初始化脚本)",
includeData: "表数据 (INSERT)",
@@ -4421,7 +4422,8 @@ export default withEnglishFallback({
selectExportDirectory: "选择导出目录",
currentDatabase: "数据库 {current}/{total}",
currentTable: "当前: {table} ({current}/{total})",
rowsExported: "已导出 {count} 行",
rowsExported: "已处理 {current}/{total} 个对象,已导出 {count} 行",
allRowsExported: "累计已导出 {count} 行",
exportSuccess: "数据库导出成功",
exportAllSuccess: "已成功导出 {count} 个数据库",
allDatabasesTask: "全部数据库({count})",
+3 -1
View File
@@ -3989,6 +3989,7 @@ export default withEnglishFallback({
title: "匯出資料庫",
options: "選項",
includeStructure: "資料表結構 (DDL)",
includeCreateDatabase: "匯出建庫語句(CREATE DATABASE / USE)",
dropTableIfExists: "匯出前新增 DROP TABLE IF EXISTS",
omitAutoIncrement: "省略 AUTO_INCREMENT(用於全新安裝的初始化腳本)",
includeData: "資料表資料 (INSERT)",
@@ -4012,7 +4013,8 @@ export default withEnglishFallback({
selectExportDirectory: "選擇匯出目錄",
currentDatabase: "資料庫 {current}/{total}",
currentTable: "目前: {table} ({current}/{total})",
rowsExported: "已匯出 {count} 列",
rowsExported: "已處理 {current}/{total} 個物件,已匯出 {count} 列",
allRowsExported: "累計已匯出 {count} 列",
exportSuccess: "資料庫匯出成功",
exportAllSuccess: "已成功匯出 {count} 個資料庫",
allDatabasesTask: "全部資料庫({count})",
+1
View File
@@ -310,6 +310,7 @@ export const listSshConfigHosts = forward("listSshConfigHosts");
// SQL File Execution
export const previewSqlFile = forward("previewSqlFile");
export const executeSqlFile = forward("executeSqlFile");
export const executeSqlFiles = forward("executeSqlFiles");
export const cancelSqlFileExecution = forward("cancelSqlFileExecution");
export const listenSqlFileProgress = forward("listenSqlFileProgress");
export const pendingOpenSqlFiles = forward("pendingOpenSqlFiles");
+4
View File
@@ -1495,6 +1495,10 @@ export async function executeSqlFile(request: SqlFileRequest): Promise<void> {
return post("/api/sql-file/execute", { request });
}
export async function executeSqlFiles(request: SqlFileRequest, filePaths: string[]): Promise<void> {
return post("/api/sql-file/execute", { request, filePaths });
}
export async function cancelSqlFileExecution(executionId: string): Promise<boolean> {
return post("/api/sql-file/cancel", { executionId });
}
+6
View File
@@ -2204,6 +2204,7 @@ export interface SqlFilePreview {
sizeBytes: number;
preview: string;
canExecuteWithoutSelectedDatabase: boolean;
establishesDatabaseContext?: boolean;
}
export interface SqlFileProgress {
@@ -2226,6 +2227,10 @@ export async function executeSqlFile(request: SqlFileRequest): Promise<void> {
return invoke("execute_sql_file", { request });
}
export async function executeSqlFiles(request: SqlFileRequest, filePaths: string[]): Promise<void> {
return invoke("execute_sql_files", { request, filePaths });
}
export async function cancelSqlFileExecution(executionId: string): Promise<boolean> {
return invoke("cancel_sql_file_execution", { executionId });
}
@@ -2467,6 +2472,7 @@ export interface DatabaseExportRequest {
includeStructure: boolean;
includeData: boolean;
includeObjects: boolean;
includeCreateDatabase?: boolean;
dropTableIfExists?: boolean;
omitAutoIncrement?: boolean;
failOnError?: boolean;
+27 -13
View File
@@ -1,17 +1,31 @@
import type { SqlFileProgress } from "@/lib/backend/tauri";
import { apiUrl } from "@/lib/common/webPath";
export function listenSqlFileProgressById(executionId: string, handler: (progress: SqlFileProgress) => void): () => void {
const es = new EventSource(apiUrl(`/api/sql-file/progress/${executionId}`));
es.onmessage = (e) => {
const progress: SqlFileProgress = JSON.parse(e.data);
handler(progress);
if (progress.status === "done" || progress.status === "error" || progress.status === "cancelled") {
es.close();
}
};
es.onerror = () => {
es.close();
};
return () => es.close();
export function listenSqlFileProgressById(executionId: string, handler: (progress: SqlFileProgress) => void, onError?: (error: Error) => void): Promise<() => void> {
return new Promise((resolve, reject) => {
const es = new EventSource(apiUrl(`/api/sql-file/progress/${executionId}`));
let opened = false;
const close = () => es.close();
es.onopen = () => {
opened = true;
resolve(close);
};
es.onmessage = (e) => {
const progress: SqlFileProgress = JSON.parse(e.data);
handler(progress);
if (progress.status === "done" || progress.status === "error" || progress.status === "cancelled") {
close();
}
};
es.onerror = () => {
close();
const error = new Error("SQL file progress connection failed");
if (opened) {
onError?.(error);
} else {
reject(error);
}
};
});
}
+102 -20
View File
@@ -38,6 +38,11 @@ pub struct DatabaseExportRequest {
pub include_structure: bool,
pub include_data: bool,
pub include_objects: bool,
/// Include a MySQL `CREATE DATABASE IF NOT EXISTS` and `USE` preamble.
/// The option is intentionally opt-in because it needs CREATE privileges
/// when importing into a new server.
#[serde(default)]
pub include_create_database: bool,
#[serde(default)]
pub drop_table_if_exists: bool,
/// Drop the table-level `AUTO_INCREMENT=N` clause from exported MySQL DDL,
@@ -89,6 +94,59 @@ fn database_export_total_objects(request: &DatabaseExportRequest, counts: &Datab
total
}
fn mysql_sql_string_literal(value: &str) -> String {
format!("'{}'", value.replace('\\', "\\\\").replace('\'', "''"))
}
fn mysql_database_export_preamble(database: &str, charset: Option<&str>, collation: Option<&str>) -> String {
let database = quote_identifier(database, &DatabaseType::Mysql);
let charset = charset.map(str::trim).filter(|value| !value.is_empty());
let collation = collation.map(str::trim).filter(|value| !value.is_empty());
let options = match charset {
Some(charset) => match collation {
Some(collation) => format!(" CHARACTER SET {charset} COLLATE {collation}"),
None => format!(" CHARACTER SET {charset}"),
},
None => String::new(),
};
format!("CREATE DATABASE IF NOT EXISTS {database}{options};\nUSE {database};\n")
}
async fn mysql_database_export_preamble_for_request(
state: &crate::connection::AppState,
request: &DatabaseExportRequest,
) -> String {
let metadata_sql = format!(
"SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = {}",
mysql_sql_string_literal(&request.database)
);
let metadata = crate::query::execute_sql_statement(
state,
&request.connection_id,
&request.database,
&metadata_sql,
None,
None,
)
.await
.ok()
.and_then(|result| {
result.rows.first().map(|row| {
(
row.first().and_then(Value::as_str).map(str::to_string),
row.get(1).and_then(Value::as_str).map(str::to_string),
)
})
});
match metadata {
Some((charset, collation)) => {
mysql_database_export_preamble(&request.database, charset.as_deref(), collation.as_deref())
}
None => mysql_database_export_preamble(&request.database, None, None),
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DatabaseBackupSnapshot {
@@ -1274,6 +1332,12 @@ pub async fn export_database_sql_core(
// 4. Create file
let mut file = std::fs::File::create(&request.file_path).map_err(|e| format!("Failed to write file: {e}"))?;
let create_database_preamble = if request.include_create_database && matches!(db_type, DatabaseType::Mysql) {
Some(mysql_database_export_preamble_for_request(state, request).await)
} else {
None
};
// 5. Write header
let timestamp = chrono::Local::now().format("%Y-%m-%d %H:%M:%S");
writeln!(file, "-- Database export: {}", request.database).map_err(|e| format!("Failed to write file: {e}"))?;
@@ -1281,6 +1345,11 @@ pub async fn export_database_sql_core(
writeln!(file, "-- Generated by DBX").map_err(|e| format!("Failed to write file: {e}"))?;
writeln!(file).map_err(|e| format!("Failed to write file: {e}"))?;
if let Some(preamble) = create_database_preamble {
writeln!(file, "-- Database setup").map_err(|e| format!("Failed to write file: {e}"))?;
writeln!(file, "{preamble}").map_err(|e| format!("Failed to write file: {e}"))?;
}
// 6. For MySQL: disable foreign key checks
if matches!(db_type, DatabaseType::Mysql) {
writeln!(file, "SET FOREIGN_KEY_CHECKS = 0;\n").map_err(|e| format!("Failed to write file: {e}"))?;
@@ -1425,6 +1494,7 @@ pub async fn export_database_sql_core(
);
let mut object_index: usize = 0;
let mut total_rows_exported = 0_u64;
// Export tables
let batch_size = if request.batch_size == 0 { 1000 } else { request.batch_size };
@@ -1438,7 +1508,7 @@ pub async fn export_database_sql_core(
current_object: extension.name.clone(),
object_index,
total_objects,
rows_exported: 0,
rows_exported: total_rows_exported,
total_rows: None,
status: ExportStatus::Running,
error: None,
@@ -1458,7 +1528,7 @@ pub async fn export_database_sql_core(
current_object: sequence.name.clone(),
object_index,
total_objects,
rows_exported: 0,
rows_exported: total_rows_exported,
total_rows: None,
status: ExportStatus::Running,
error: None,
@@ -1559,7 +1629,7 @@ pub async fn export_database_sql_core(
current_object: table_info.name.clone(),
object_index,
total_objects,
rows_exported: 0,
rows_exported: total_rows_exported,
total_rows: None,
status: ExportStatus::Cancelled,
error: None,
@@ -1575,7 +1645,7 @@ pub async fn export_database_sql_core(
current_object: table_name.clone(),
object_index,
total_objects,
rows_exported: 0,
rows_exported: total_rows_exported,
total_rows: None,
status: ExportStatus::Running,
error: None,
@@ -1596,7 +1666,7 @@ pub async fn export_database_sql_core(
current_object: sequence.name.clone(),
object_index,
total_objects,
rows_exported: 0,
rows_exported: total_rows_exported,
total_rows: None,
status: ExportStatus::Running,
error: None,
@@ -1682,7 +1752,6 @@ pub async fn export_database_sql_core(
if !col_names.is_empty() {
if let Some(snapshot_session_id) = request.snapshot_session_id.as_deref() {
let sql = database_export_select_sql(&col_names, table_name, &request.schema, &db_type);
let mut rows_exported = 0_u64;
crate::query::stream_rows_in_manual_transaction(
state,
snapshot_session_id,
@@ -1702,13 +1771,13 @@ pub async fn export_database_sql_core(
&request.schema,
&db_type,
)?;
rows_exported += rows.len() as u64;
total_rows_exported += rows.len() as u64;
on_progress(ExportProgress {
export_id: request.export_id.clone(),
current_object: table_name.clone(),
object_index,
total_objects,
rows_exported,
rows_exported: total_rows_exported,
total_rows: None,
status: ExportStatus::Running,
error: None,
@@ -1737,7 +1806,6 @@ pub async fn export_database_sql_core(
Err(_) => None,
};
let mut offset = 0_u64;
let mut rows_exported = 0_u64;
loop {
if is_export_cancelled(&request.export_id).await {
@@ -1746,7 +1814,7 @@ pub async fn export_database_sql_core(
current_object: table_name.clone(),
object_index,
total_objects,
rows_exported,
rows_exported: total_rows_exported,
total_rows,
status: ExportStatus::Cancelled,
error: None,
@@ -1787,14 +1855,14 @@ pub async fn export_database_sql_core(
&request.schema,
&db_type,
)?;
rows_exported += row_count as u64;
total_rows_exported += row_count as u64;
offset += row_count as u64;
on_progress(ExportProgress {
export_id: request.export_id.clone(),
current_object: table_name.clone(),
object_index,
total_objects,
rows_exported,
rows_exported: total_rows_exported,
total_rows,
status: ExportStatus::Running,
error: None,
@@ -1837,7 +1905,7 @@ pub async fn export_database_sql_core(
current_object: view_name.clone(),
object_index,
total_objects,
rows_exported: 0,
rows_exported: total_rows_exported,
total_rows: None,
status: ExportStatus::Running,
error: None,
@@ -1888,7 +1956,7 @@ pub async fn export_database_sql_core(
current_object: proc_name.clone(),
object_index,
total_objects,
rows_exported: 0,
rows_exported: total_rows_exported,
total_rows: None,
status: ExportStatus::Running,
error: None,
@@ -1943,7 +2011,7 @@ pub async fn export_database_sql_core(
current_object: func_name.clone(),
object_index,
total_objects,
rows_exported: 0,
rows_exported: total_rows_exported,
total_rows: None,
status: ExportStatus::Running,
error: None,
@@ -1997,7 +2065,7 @@ pub async fn export_database_sql_core(
current_object: String::new(),
object_index,
total_objects,
rows_exported: 0,
rows_exported: total_rows_exported,
total_rows: None,
status: ExportStatus::Done,
error: None,
@@ -2054,10 +2122,11 @@ mod tests {
database_export_total_objects, drop_table_if_exists_sql, filter_export_table_infos, format_export_sql_literal,
format_export_table_ddl, generate_postgres_extension_ddl, generate_postgres_sequence_create_ddl,
generate_postgres_sequence_owner_ddl, generate_postgres_sequence_setval_sql,
is_postgres_extension_member_routine, normalize_export_table_ddl, record_export_error,
BuildDatabaseSqlExportOptions, BuildExportInsertStatementsOptions, DatabaseExportObjectCounts,
DatabaseExportRequest, DdlNormalizeOptions, ExportedTableSql, PostgresExportExtension, PostgresExportSequence,
PostgresExtensionMembers, DATABASE_EXPORT_INSERT_BATCH_SIZE, DATABASE_EXPORT_ROW_LIMIT,
is_postgres_extension_member_routine, mysql_database_export_preamble, normalize_export_table_ddl,
record_export_error, BuildDatabaseSqlExportOptions, BuildExportInsertStatementsOptions,
DatabaseExportObjectCounts, DatabaseExportRequest, DdlNormalizeOptions, ExportedTableSql,
PostgresExportExtension, PostgresExportSequence, PostgresExtensionMembers, DATABASE_EXPORT_INSERT_BATCH_SIZE,
DATABASE_EXPORT_ROW_LIMIT,
};
use crate::models::connection::DatabaseType;
use crate::types::{ObjectInfo, ObjectSourceKind, TableInfo};
@@ -2105,6 +2174,7 @@ mod tests {
include_structure,
include_data,
include_objects,
include_create_database: false,
drop_table_if_exists: false,
omit_auto_increment: false,
fail_on_error: false,
@@ -2137,6 +2207,18 @@ mod tests {
}
}
#[test]
fn mysql_database_export_preamble_preserves_database_settings() {
assert_eq!(
mysql_database_export_preamble("app`db", Some("utf8mb4"), Some("utf8mb4_unicode_ci")),
"CREATE DATABASE IF NOT EXISTS `app``db` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;\nUSE `app``db`;\n"
);
assert_eq!(
mysql_database_export_preamble("app", None, None),
"CREATE DATABASE IF NOT EXISTS `app`;\nUSE `app`;\n"
);
}
#[test]
fn export_progress_total_excludes_schema_routines_for_selected_tables() {
// Counts are already filtered to the selected table/view set before
+2
View File
@@ -69,6 +69,8 @@ pub struct SqlFilePreview {
pub size_bytes: u64,
pub preview: String,
pub can_execute_without_selected_database: bool,
#[serde(default)]
pub establishes_database_context: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
+157 -48
View File
@@ -355,50 +355,81 @@ pub async fn execute_sql_file_path(
file_path: &Path,
token: CancellationToken,
started_at: Instant,
emit: impl FnMut(SqlFileProgress),
) -> Result<(), String> {
execute_sql_file_paths(state, request, &[file_path], token, started_at, emit).await
}
/// Executes multiple SQL files as one import operation. MySQL-family imports
/// deliberately reuse one pinned connection across every file, so session
/// state such as `USE`, temporary tables, variables, and transactions remains
/// available to the next file in the batch.
pub async fn execute_sql_file_paths(
state: &AppState,
request: &SqlFileRequest,
file_paths: &[&Path],
token: CancellationToken,
started_at: Instant,
mut emit: impl FnMut(SqlFileProgress),
) -> Result<(), String> {
if file_paths.is_empty() {
let error = "No SQL files selected".to_string();
emit(sql_file_error_progress(&request.execution_id, started_at, error.clone()));
return Err(error);
}
let import_target = sql_file_import_target(state, &request.connection_id).await;
let options =
import_target.as_ref().map(|target| SqlParsingOptions::for_database_type(target.db_type)).unwrap_or_default();
let mut splitter = StreamingSqlFileSplitter::new(import_target.as_ref().map(|target| target.db_type), options);
let mut mysql_executor = MySqlSqlFileExecutor::build(state, request, import_target.as_ref()).await?;
let database_type = import_target.as_ref().map(|target| target.db_type);
let mut progress = SqlFileExecutionProgress::new();
let mut pending_statements = Vec::with_capacity(SQL_FILE_STATEMENT_BATCH_SIZE);
let mut decoder = match SqlFileStreamDecoder::open(file_path).await {
Ok(decoder) => decoder,
let mut mysql_executor = match MySqlSqlFileExecutor::build(state, request, import_target.as_ref()).await {
Ok(executor) => executor,
Err(error) => {
emit(sql_file_error_progress(&request.execution_id, started_at, error.clone()));
emit(sql_file_execution_error_progress(&request.execution_id, started_at, &progress, error.clone()));
return Err(error);
}
};
loop {
let chunk = match decoder.next_chunk().await {
Ok(chunk) => chunk,
for file_path in file_paths {
let mut splitter = StreamingSqlFileSplitter::new(database_type, options);
let mut pending_statements = Vec::with_capacity(SQL_FILE_STATEMENT_BATCH_SIZE);
let mut decoder = match SqlFileStreamDecoder::open(file_path).await {
Ok(decoder) => decoder,
Err(error) => {
emit(sql_file_progress(
&request.execution_id,
SqlFileStatus::Error,
progress.statement_index,
progress.success_count,
progress.failure_count,
progress.affected_rows,
started_at,
"",
Some(error.clone()),
));
emit(sql_file_execution_error_progress(&request.execution_id, started_at, &progress, error.clone()));
return Err(error);
}
};
let Some(chunk) = chunk else {
break;
};
if token.is_cancelled() {
emit_sql_file_terminal_progress(request, &token, started_at, &progress, &mut emit);
return Ok(());
}
pending_statements.extend(splitter.push_chunk(&chunk));
if pending_statements.len() >= SQL_FILE_STATEMENT_BATCH_SIZE {
loop {
let chunk = match decoder.next_chunk().await {
Ok(chunk) => chunk,
Err(error) => {
emit(sql_file_progress(
&request.execution_id,
SqlFileStatus::Error,
progress.statement_index,
progress.success_count,
progress.failure_count,
progress.affected_rows,
started_at,
"",
Some(error.clone()),
));
return Err(error);
}
};
let Some(chunk) = chunk else {
break;
};
if token.is_cancelled() {
emit_sql_file_terminal_progress(request, &token, started_at, &progress, &mut emit);
return Ok(());
}
pending_statements.extend(splitter.push_chunk(&chunk));
if pending_statements.len() < SQL_FILE_STATEMENT_BATCH_SIZE {
continue;
}
execute_sql_file_statement_batch(
state,
request,
@@ -412,21 +443,21 @@ pub async fn execute_sql_file_path(
)
.await?;
}
}
pending_statements.extend(splitter.finish());
execute_sql_file_statement_batch(
state,
request,
&token,
started_at,
&mut pending_statements,
import_target.as_ref(),
mysql_executor.as_mut(),
&mut progress,
&mut emit,
)
.await?;
pending_statements.extend(splitter.finish());
execute_sql_file_statement_batch(
state,
request,
&token,
started_at,
&mut pending_statements,
import_target.as_ref(),
mysql_executor.as_mut(),
&mut progress,
&mut emit,
)
.await?;
}
emit_sql_file_terminal_progress(request, &token, started_at, &progress, &mut emit);
Ok(())
}
@@ -729,6 +760,25 @@ pub fn sql_file_error_progress(execution_id: &str, started_at: Instant, error: S
sql_file_progress(execution_id, SqlFileStatus::Error, 0, 0, 0, 0, started_at, "", Some(error))
}
fn sql_file_execution_error_progress(
execution_id: &str,
started_at: Instant,
progress: &SqlFileExecutionProgress,
error: String,
) -> SqlFileProgress {
sql_file_progress(
execution_id,
SqlFileStatus::Error,
progress.statement_index,
progress.success_count,
progress.failure_count,
progress.affected_rows,
started_at,
"",
Some(error),
)
}
async fn sql_file_import_target(state: &AppState, connection_id: &str) -> Option<SqlFileImportTarget> {
let configs = state.configs.read().await;
configs
@@ -736,7 +786,13 @@ async fn sql_file_import_target(state: &AppState, connection_id: &str) -> Option
.map(|config| SqlFileImportTarget { db_type: config.db_type, driver_profile: config.driver_profile.clone() })
}
pub fn mysql_like_sql_file_can_execute_without_selected_database(file_content: &str) -> bool {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MysqlLikeSqlFileBootstrapAnalysis {
pub can_execute_without_selected_database: bool,
pub establishes_database_context: bool,
}
pub fn mysql_like_sql_file_bootstrap_analysis(file_content: &str) -> MysqlLikeSqlFileBootstrapAnalysis {
let options = SqlParsingOptions::mysql_compatible();
let mut splitter = SqlStatementSplitter::with_options(options);
let mut statements = splitter.push_chunk(file_content);
@@ -759,7 +815,10 @@ pub fn mysql_like_sql_file_can_execute_without_selected_database(file_content: &
// run before the script establishes its own database context.
saw_statement = true;
let Some((keyword, remainder)) = leading_sql_keyword(statement) else {
return false;
return MysqlLikeSqlFileBootstrapAnalysis {
can_execute_without_selected_database: false,
establishes_database_context: has_database_context,
};
};
if keyword.eq_ignore_ascii_case("SET") {
@@ -785,11 +844,21 @@ pub fn mysql_like_sql_file_can_execute_without_selected_database(file_content: &
}
if !has_database_context {
return false;
return MysqlLikeSqlFileBootstrapAnalysis {
can_execute_without_selected_database: false,
establishes_database_context: false,
};
}
}
saw_statement
MysqlLikeSqlFileBootstrapAnalysis {
can_execute_without_selected_database: saw_statement,
establishes_database_context: has_database_context,
}
}
pub fn mysql_like_sql_file_can_execute_without_selected_database(file_content: &str) -> bool {
mysql_like_sql_file_bootstrap_analysis(file_content).can_execute_without_selected_database
}
fn mysql_use_database_target(sql: &str) -> Option<String> {
@@ -1635,6 +1704,46 @@ mod tests {
));
}
#[test]
fn mysql_like_sql_file_bootstrap_analysis_tracks_context_for_following_files() {
assert_eq!(
mysql_like_sql_file_bootstrap_analysis("SHOW DATABASES;"),
MysqlLikeSqlFileBootstrapAnalysis {
can_execute_without_selected_database: true,
establishes_database_context: false,
}
);
assert_eq!(
mysql_like_sql_file_bootstrap_analysis("CREATE DATABASE app_db;\nUSE app_db;\nCREATE TABLE users(id INT);"),
MysqlLikeSqlFileBootstrapAnalysis {
can_execute_without_selected_database: true,
establishes_database_context: true,
}
);
assert_eq!(
mysql_like_sql_file_bootstrap_analysis("CREATE TABLE users(id INT);"),
MysqlLikeSqlFileBootstrapAnalysis {
can_execute_without_selected_database: false,
establishes_database_context: false,
}
);
}
#[test]
fn execution_error_progress_preserves_cumulative_counters() {
let progress =
SqlFileExecutionProgress { statement_index: 4, success_count: 3, failure_count: 1, affected_rows: 9 };
let terminal =
sql_file_execution_error_progress("exec-1", Instant::now(), &progress, "file missing".to_string());
assert_eq!(terminal.status, SqlFileStatus::Error);
assert_eq!(terminal.statement_index, 4);
assert_eq!(terminal.success_count, 3);
assert_eq!(terminal.failure_count, 1);
assert_eq!(terminal.affected_rows, 9);
}
#[test]
fn parses_mysql_use_database_targets() {
assert_eq!(mysql_use_database_target("USE app_db"), Some("app_db".to_string()));
@@ -176,6 +176,7 @@ async fn database_export_writes_structure_and_data_for_all_tables() {
include_structure: true,
include_data: true,
include_objects: false,
include_create_database: false,
drop_table_if_exists: true,
omit_auto_increment: false,
fail_on_error: true,
+5 -1
View File
@@ -586,7 +586,11 @@ async fn main() {
)
.route("/export/query-result/cancel", post(routes::query_result_export::cancel_query_result_export))
// SQL file
.route("/sql-file/preview", post(routes::sql_file::preview_sql_file))
.route(
"/sql-file/preview",
post(routes::sql_file::preview_sql_file)
.layer(DefaultBodyLimit::max(routes::sql_file::SQL_FILE_UPLOAD_MAX_BYTES.saturating_add(1024 * 1024))),
)
.route("/sql-file/execute", post(routes::sql_file::execute_sql_file))
.route("/sql-file/progress/{executionId}", get(routes::sql_file::sql_file_progress))
.route("/sql-file/cancel", post(routes::sql_file::cancel_sql_file))
+133 -42
View File
@@ -1,5 +1,6 @@
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;
use axum::extract::{Multipart, Path as AxumPath, State};
use axum::response::sse::{Event, Sse};
@@ -7,21 +8,28 @@ use axum::Json;
use dbx_core::sql;
use dbx_core::sql::{SqlFileProgress, SqlFileRequest, SqlFileStatus};
use dbx_core::sql_file_import::{
execute_sql_file_content, sql_file_error_progress, sql_file_progress as build_sql_file_progress,
execute_sql_file_paths, sql_file_error_progress, sql_file_progress as build_sql_file_progress,
SqlFileProgressEmitter,
};
use futures::stream::Stream;
use serde::Deserialize;
use tokio::sync::broadcast;
use tokio_util::sync::CancellationToken;
use uuid::Uuid;
use crate::error::AppError;
use crate::state::WebState;
const PENDING_SQL_FILE_PROGRESS_CHANNEL_TTL: Duration = Duration::from_secs(30);
const SQL_FILE_UPLOAD_MAX_AGE: Duration = Duration::from_secs(24 * 60 * 60);
pub const SQL_FILE_UPLOAD_MAX_BYTES: usize = 200 * 1024 * 1024;
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SqlFileExecuteWrapper {
pub request: SqlFileRequest,
#[serde(default)]
pub file_paths: Vec<String>,
}
#[derive(Deserialize)]
@@ -34,12 +42,20 @@ pub async fn preview_sql_file(
State(state): State<Arc<WebState>>,
mut multipart: Multipart,
) -> Result<Json<serde_json::Value>, AppError> {
let tmp_dir = state.data_dir.join("tmp");
let tmp_dir = state.data_dir.join("tmp").join("sql_file");
std::fs::create_dir_all(&tmp_dir).map_err(|e| AppError::from(e.to_string()))?;
cleanup_expired_sql_file_uploads(&tmp_dir);
if let Some(field) = multipart.next_field().await.map_err(|e| AppError::from(e.to_string()))? {
let file_name = field.file_name().unwrap_or("upload.sql").to_string();
let data = field.bytes().await.map_err(|e| AppError::from(e.to_string()))?;
if data.len() > SQL_FILE_UPLOAD_MAX_BYTES {
return Err(AppError::from(format!(
"File too large: {} bytes (max {} bytes)",
data.len(),
SQL_FILE_UPLOAD_MAX_BYTES
)));
}
let file_path = safe_uploaded_sql_path(&tmp_dir, &file_name)?;
std::fs::write(&file_path, &data).map_err(|e| AppError::from(e.to_string()))?;
@@ -47,13 +63,15 @@ pub async fn preview_sql_file(
let size_bytes = data.len() as u64;
let content = sql::decode_sql_file_bytes(&data).map_err(AppError::from)?;
let preview: String = content.chars().take(20_000).collect();
let bootstrap_analysis = dbx_core::sql_file_import::mysql_like_sql_file_bootstrap_analysis(&content);
return Ok(Json(serde_json::json!({
"fileName": file_name,
"filePath": file_path.to_string_lossy(),
"sizeBytes": size_bytes,
"preview": preview,
"canExecuteWithoutSelectedDatabase": dbx_core::sql_file_import::mysql_like_sql_file_can_execute_without_selected_database(&content),
"canExecuteWithoutSelectedDatabase": bootstrap_analysis.can_execute_without_selected_database,
"establishesDatabaseContext": bootstrap_analysis.establishes_database_context,
})));
}
@@ -75,7 +93,14 @@ pub async fn execute_sql_file(
}
let execution_id = req.execution_id.clone();
let file_path = validated_uploaded_sql_path(&state.data_dir, &req.file_path)?;
let requested_paths = if body.file_paths.is_empty() { vec![req.file_path.clone()] } else { body.file_paths };
if requested_paths.is_empty() {
return Err(AppError::from("No SQL files selected".to_string()));
}
let file_paths = requested_paths
.iter()
.map(|file_path| validated_uploaded_sql_path(&state.data_dir, file_path))
.collect::<Result<Vec<_>, _>>()?;
let token = CancellationToken::new();
{
@@ -85,8 +110,10 @@ pub async fn execute_sql_file(
}
executions.insert(execution_id.clone(), token.clone());
}
let (tx, _) = tokio::sync::broadcast::channel::<String>(256);
state.sse_channels.write().await.insert(execution_id.clone(), tx.clone());
let tx = {
let mut channels = state.sse_channels.write().await;
channels.entry(execution_id.clone()).or_insert_with(|| tokio::sync::broadcast::channel::<String>(256).0).clone()
};
let app = state.app.clone();
let state_clone = state.clone();
@@ -107,37 +134,30 @@ pub async fn execute_sql_file(
"",
None,
));
match std::fs::metadata(&file_path) {
Ok(meta) if meta.len() > 200 * 1024 * 1024 => {
progress_emitter.emit(sql_file_error_progress(
&req.execution_id,
started_at,
format!("File too large: {} bytes (max {} bytes)", meta.len(), 200 * 1024 * 1024),
));
cleanup_sql_file_execution(&state_clone, &req.execution_id).await;
return;
for file_path in &file_paths {
match std::fs::metadata(file_path) {
Ok(meta) if meta.len() > SQL_FILE_UPLOAD_MAX_BYTES as u64 => {
progress_emitter.emit(sql_file_error_progress(
&req.execution_id,
started_at,
format!("File too large: {} bytes (max {} bytes)", meta.len(), SQL_FILE_UPLOAD_MAX_BYTES),
));
cleanup_sql_file_execution(&state_clone, &req.execution_id).await;
return;
}
Err(e) => {
progress_emitter.emit(sql_file_error_progress(&req.execution_id, started_at, e.to_string()));
cleanup_sql_file_execution(&state_clone, &req.execution_id).await;
return;
}
_ => {}
}
Err(e) => {
progress_emitter.emit(sql_file_error_progress(&req.execution_id, started_at, e.to_string()));
cleanup_sql_file_execution(&state_clone, &req.execution_id).await;
return;
}
_ => {}
}
let file_content = match std::fs::read(&file_path).and_then(|bytes| {
sql::decode_sql_file_bytes(&bytes)
.map_err(|message| std::io::Error::new(std::io::ErrorKind::InvalidData, message))
}) {
Ok(content) => content,
Err(e) => {
progress_emitter.emit(sql_file_error_progress(&req.execution_id, started_at, e.to_string()));
cleanup_sql_file_execution(&state_clone, &req.execution_id).await;
return;
}
};
let _ = execute_sql_file_content(&app, &req, &file_content, token, started_at, |progress| {
let file_path_refs: Vec<&Path> = file_paths.iter().map(PathBuf::as_path).collect();
// The core executor emits exactly one terminal Error with the latest
// cumulative counters before returning Err.
let _ = execute_sql_file_paths(&app, &req, &file_path_refs, token, started_at, |progress| {
progress_emitter.emit(progress);
})
.await;
@@ -164,7 +184,38 @@ fn safe_uploaded_sql_path(tmp_dir: &Path, file_name: &str) -> Result<PathBuf, Ap
if base_name.is_empty() || base_name == "." || base_name == ".." {
return Err(AppError::from("Invalid SQL file name".to_string()));
}
Ok(tmp_dir.join(base_name))
let file_name = Path::new(base_name);
let stem = file_name.file_stem().and_then(|stem| stem.to_str()).filter(|stem| !stem.is_empty()).unwrap_or("upload");
let extension =
file_name.extension().and_then(|extension| extension.to_str()).filter(|extension| !extension.is_empty());
let unique_name = match extension {
Some(extension) => format!("{stem}-{}.{}", Uuid::new_v4(), extension),
None => format!("{stem}-{}", Uuid::new_v4()),
};
Ok(tmp_dir.join(unique_name))
}
fn cleanup_expired_sql_file_uploads(tmp_dir: &Path) {
cleanup_sql_file_uploads_older_than(tmp_dir, SQL_FILE_UPLOAD_MAX_AGE);
}
fn cleanup_sql_file_uploads_older_than(tmp_dir: &Path, max_age: Duration) {
let Ok(entries) = std::fs::read_dir(tmp_dir) else {
return;
};
for entry in entries.flatten() {
let path = entry.path();
let expired = entry
.metadata()
.ok()
.filter(|metadata| metadata.is_file())
.and_then(|metadata| metadata.modified().ok())
.and_then(|modified| modified.elapsed().ok())
.is_some_and(|age| age >= max_age);
if expired {
let _ = std::fs::remove_file(path);
}
}
}
fn validated_uploaded_sql_path(data_dir: &Path, file_path: &str) -> Result<PathBuf, AppError> {
@@ -185,10 +236,28 @@ pub async fn sql_file_progress(
State(state): State<Arc<WebState>>,
AxumPath(execution_id): AxumPath<String>,
) -> Result<Sse<impl Stream<Item = Result<Event, std::convert::Infallible>>>, AppError> {
let channels = state.sse_channels.read().await;
let tx = channels.get(&execution_id).ok_or_else(|| AppError::from("Execution not found".to_string()))?;
// The client subscribes before it sends the execution request. Creating the
// channel on demand prevents that SSE connection from racing the POST and
// guarantees it is subscribed before the background task emits `Started`.
let (tx, created) = {
let mut channels = state.sse_channels.write().await;
let created = !channels.contains_key(&execution_id);
let tx = channels
.entry(execution_id.clone())
.or_insert_with(|| tokio::sync::broadcast::channel::<String>(256).0)
.clone();
(tx, created)
};
if created {
let state = state.clone();
tokio::spawn(async move {
tokio::time::sleep(PENDING_SQL_FILE_PROGRESS_CHANNEL_TTL).await;
if !state.sql_file_executions.read().await.contains_key(&execution_id) {
state.remove_sse_channel(&execution_id).await;
}
});
}
let rx = tx.subscribe();
drop(channels);
Ok(crate::sse::sse_from_lossy_channel(rx))
}
@@ -207,19 +276,28 @@ pub async fn cancel_sql_file(
#[cfg(test)]
mod tests {
use super::{safe_uploaded_sql_path, validated_uploaded_sql_path};
use std::time::Duration;
use super::{cleanup_sql_file_uploads_older_than, safe_uploaded_sql_path, validated_uploaded_sql_path};
#[test]
fn uploaded_sql_path_uses_only_the_file_name() {
fn uploaded_sql_paths_are_unique_and_keep_the_extension() {
let data_dir = std::env::temp_dir().join(format!("dbx-web-sql-file-test-{}", uuid::Uuid::new_v4()));
let tmp_dir = data_dir.join("tmp");
let path = match safe_uploaded_sql_path(&tmp_dir, "../outside.sql") {
let first = match safe_uploaded_sql_path(&tmp_dir, "../outside.sql") {
Ok(path) => path,
Err(error) => panic!("{}", error.message),
};
let second = match safe_uploaded_sql_path(&tmp_dir, "nested/outside.sql") {
Ok(path) => path,
Err(error) => panic!("{}", error.message),
};
assert_eq!(path, tmp_dir.join("outside.sql"));
assert!(first.starts_with(&tmp_dir));
assert!(second.starts_with(&tmp_dir));
assert_ne!(first, second);
assert_eq!(first.extension().and_then(|extension| extension.to_str()), Some("sql"));
let _ = std::fs::remove_dir_all(data_dir);
}
@@ -236,4 +314,17 @@ mod tests {
assert!(result.is_err());
let _ = std::fs::remove_dir_all(data_dir);
}
#[test]
fn expired_sql_file_uploads_are_removed() {
let tmp_dir = std::env::temp_dir().join(format!("dbx-web-sql-file-test-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&tmp_dir).unwrap();
let upload = tmp_dir.join("upload.sql");
std::fs::write(&upload, "select 1;").unwrap();
cleanup_sql_file_uploads_older_than(&tmp_dir, Duration::ZERO);
assert!(!upload.exists());
let _ = std::fs::remove_dir_all(tmp_dir);
}
}
+26 -16
View File
@@ -9,8 +9,8 @@ use tokio_util::sync::CancellationToken;
use crate::commands::connection::{ensure_connection_writable, AppState};
use dbx_core::sql_file_import::{
execute_sql_file_path, mysql_like_sql_file_can_execute_without_selected_database, read_sql_file_preview,
sql_file_progress, SqlFileProgressEmitter,
execute_sql_file_paths, mysql_like_sql_file_bootstrap_analysis, read_sql_file_preview, sql_file_progress,
SqlFileProgressEmitter,
};
pub use dbx_core::sql::{SqlFilePreview, SqlFileRequest, SqlFileStatus};
@@ -35,7 +35,7 @@ pub async fn preview_sql_file(file_path: String) -> Result<SqlFilePreview, Strin
let path = PathBuf::from(&file_path);
let metadata = tokio::fs::metadata(&path).await.map_err(|e| e.to_string())?;
let prefix = read_sql_file_preview(&path, 1_000_000).await?;
let can_execute_without_selected_database = mysql_like_sql_file_can_execute_without_selected_database(&prefix);
let bootstrap_analysis = mysql_like_sql_file_bootstrap_analysis(&prefix);
let preview = prefix.chars().take(20_000).collect();
Ok(SqlFilePreview {
@@ -43,7 +43,8 @@ pub async fn preview_sql_file(file_path: String) -> Result<SqlFilePreview, Strin
file_path,
size_bytes: metadata.len(),
preview,
can_execute_without_selected_database,
can_execute_without_selected_database: bootstrap_analysis.can_execute_without_selected_database,
establishes_database_context: bootstrap_analysis.establishes_database_context,
})
}
@@ -52,9 +53,22 @@ pub async fn execute_sql_file(
app: AppHandle,
state: State<'_, Arc<AppState>>,
request: SqlFileRequest,
) -> Result<(), String> {
execute_sql_files(app, state, request.clone(), vec![request.file_path.clone()]).await
}
#[tauri::command]
pub async fn execute_sql_files(
app: AppHandle,
state: State<'_, Arc<AppState>>,
request: SqlFileRequest,
file_paths: Vec<String>,
) -> Result<(), String> {
// Fast-fail: reject early if the connection is read-only (individual statements are also checked in do_execute)
ensure_connection_writable(&state, &request.connection_id, "SQL file execution").await?;
if file_paths.is_empty() {
return Err("No SQL files selected".to_string());
}
let token = CancellationToken::new();
{
let mut executions = sql_file_executions().write().await;
@@ -62,7 +76,7 @@ pub async fn execute_sql_file(
}
let started_at = Instant::now();
let result = execute_sql_file_inner(&app, &state, &request, token, started_at).await;
let result = execute_sql_files_inner(&app, &state, &request, &file_paths, token, started_at).await;
{
let mut executions = sql_file_executions().write().await;
remove_sql_file_execution(&mut executions, &request.execution_id);
@@ -81,10 +95,11 @@ pub async fn cancel_sql_file_execution(execution_id: String) -> Result<bool, Str
}
}
async fn execute_sql_file_inner(
async fn execute_sql_files_inner(
app: &AppHandle,
state: &State<'_, Arc<AppState>>,
request: &SqlFileRequest,
file_paths: &[String],
token: CancellationToken,
started_at: Instant,
) -> Result<(), String> {
@@ -102,16 +117,11 @@ async fn execute_sql_file_inner(
"",
None,
));
execute_sql_file_path(
state.inner().as_ref(),
request,
PathBuf::from(&request.file_path).as_path(),
token,
started_at,
|progress| {
progress_emitter.emit(progress);
},
)
let paths: Vec<PathBuf> = file_paths.iter().map(PathBuf::from).collect();
let path_refs: Vec<&std::path::Path> = paths.iter().map(PathBuf::as_path).collect();
execute_sql_file_paths(state.inner().as_ref(), request, &path_refs, token, started_at, |progress| {
progress_emitter.emit(progress);
})
.await
}
+1
View File
@@ -1498,6 +1498,7 @@ pub fn run() {
commands::data_compare::build_data_compare_sync_plan,
commands::sql_file::preview_sql_file,
commands::sql_file::execute_sql_file,
commands::sql_file::execute_sql_files,
commands::sql_file::cancel_sql_file_execution,
commands::external_sql::pending_open_sql_files,
commands::external_sql::read_external_sql_file,