feat(sql-files): create, rename, and delete SQL files in tree
This commit is contained in:
22 files changed
+590
-8
No files matched your search
@@ -1,7 +1,7 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import { beforeEach, describe, expect, it } from "vitest";
|
||||
import { EXTERNAL_SQL_FILE_TARGETS_STORAGE_KEY, MAX_EXTERNAL_SQL_FILE_TARGETS, rememberExternalSqlFileTarget, resolveExternalSqlFileTarget, unassociatedExternalSqlFileTarget } from "@/lib/sql/externalSqlFileTarget";
|
||||
import { EXTERNAL_SQL_FILE_TARGETS_STORAGE_KEY, MAX_EXTERNAL_SQL_FILE_TARGETS, forgetExternalSqlFileTarget, moveExternalSqlFileTarget, rememberExternalSqlFileTarget, resolveExternalSqlFileTarget, unassociatedExternalSqlFileTarget } from "@/lib/sql/externalSqlFileTarget";
|
||||
|
||||
describe("external SQL file targets", () => {
|
||||
beforeEach(() => {
|
||||
@@ -54,6 +54,20 @@ describe("external SQL file targets", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("moves and removes saved targets when a managed file is renamed or deleted", () => {
|
||||
rememberExternalSqlFileTarget("C:\\work\\draft.sql", { connectionId: "saved-connection", database: "sales" });
|
||||
|
||||
moveExternalSqlFileTarget("C:/work/draft.sql", "C:/work/report.sql");
|
||||
expect(resolveExternalSqlFileTarget("C:/work/draft.sql", () => true, unassociatedExternalSqlFileTarget())).toEqual(unassociatedExternalSqlFileTarget());
|
||||
expect(resolveExternalSqlFileTarget("C:/work/report.sql", () => true, unassociatedExternalSqlFileTarget())).toMatchObject({
|
||||
connectionId: "saved-connection",
|
||||
database: "sales",
|
||||
});
|
||||
|
||||
forgetExternalSqlFileTarget("C:/work/report.sql");
|
||||
expect(resolveExternalSqlFileTarget("C:/work/report.sql", () => true, unassociatedExternalSqlFileTarget())).toEqual(unassociatedExternalSqlFileTarget());
|
||||
});
|
||||
|
||||
it("ignores malformed persisted state", () => {
|
||||
localStorage.setItem(EXTERNAL_SQL_FILE_TARGETS_STORAGE_KEY, "not-json");
|
||||
|
||||
|
||||
@@ -380,6 +380,9 @@ export const inspectExternalSqlFile = forward("inspectExternalSqlFile");
|
||||
export const writeExternalSqlFile = forward("writeExternalSqlFile");
|
||||
export const saveExternalSqlFile = forward("saveExternalSqlFile");
|
||||
export const listSqlFilesInFolder = forward("listSqlFilesInFolder");
|
||||
export const createSqlFileInFolder = forward("createSqlFileInFolder");
|
||||
export const renameSqlFileInFolder = forward("renameSqlFileInFolder");
|
||||
export const deleteSqlFileInFolder = forward("deleteSqlFileInFolder");
|
||||
|
||||
// Nacos
|
||||
export const nacosTestConnection = forward("nacosTestConnection");
|
||||
|
||||
@@ -2221,6 +2221,18 @@ export async function listSqlFilesInFolder(_folderPath: string): Promise<SqlFile
|
||||
throw new Error("Listing SQL files in a folder is only available in the desktop app");
|
||||
}
|
||||
|
||||
export async function createSqlFileInFolder(_rootPath: string, _directoryPath: string, _fileName: string): Promise<string> {
|
||||
throw new Error("Managing SQL files in folders is only available in the desktop app");
|
||||
}
|
||||
|
||||
export async function renameSqlFileInFolder(_rootPath: string, _filePath: string, _fileName: string): Promise<string> {
|
||||
throw new Error("Managing SQL files in folders is only available in the desktop app");
|
||||
}
|
||||
|
||||
export async function deleteSqlFileInFolder(_rootPath: string, _filePath: string): Promise<void> {
|
||||
throw new Error("Managing SQL files in folders is only available in the desktop app");
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Data Transfer
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@@ -891,6 +891,18 @@ export async function listSqlFilesInFolder(folderPath: string): Promise<SqlFileE
|
||||
return invoke("list_sql_files_in_folder", { folderPath });
|
||||
}
|
||||
|
||||
export async function createSqlFileInFolder(rootPath: string, directoryPath: string, fileName: string): Promise<string> {
|
||||
return invoke("create_sql_file_in_folder", { rootPath, directoryPath, fileName });
|
||||
}
|
||||
|
||||
export async function renameSqlFileInFolder(rootPath: string, filePath: string, fileName: string): Promise<string> {
|
||||
return invoke("rename_sql_file_in_folder", { rootPath, filePath, fileName });
|
||||
}
|
||||
|
||||
export async function deleteSqlFileInFolder(rootPath: string, filePath: string): Promise<void> {
|
||||
return invoke("delete_sql_file_in_folder", { rootPath, filePath });
|
||||
}
|
||||
|
||||
// --- AI Conversations ---
|
||||
|
||||
export interface AiChatMessage {
|
||||
|
||||
@@ -47,6 +47,26 @@ export function rememberExternalSqlFileTarget(path: string, target: ExternalSqlF
|
||||
saveExternalSqlFileTargets([{ path: normalizedPath, ...target, updatedAt: Date.now() }, ...remaining].slice(0, MAX_EXTERNAL_SQL_FILE_TARGETS));
|
||||
}
|
||||
|
||||
export function moveExternalSqlFileTarget(previousPath: string, nextPath: string) {
|
||||
const previous = normalizeExternalSqlPath(previousPath);
|
||||
const next = normalizeExternalSqlPath(nextPath);
|
||||
if (!previous || !next || previous === next) return;
|
||||
const targets = loadExternalSqlFileTargets();
|
||||
const target = targets.find((item) => item.path === previous);
|
||||
const remaining = targets.filter((item) => item.path !== previous && item.path !== next);
|
||||
if (!target) {
|
||||
saveExternalSqlFileTargets(remaining);
|
||||
return;
|
||||
}
|
||||
saveExternalSqlFileTargets([{ ...target, path: next, updatedAt: Date.now() }, ...remaining].slice(0, MAX_EXTERNAL_SQL_FILE_TARGETS));
|
||||
}
|
||||
|
||||
export function forgetExternalSqlFileTarget(path: string) {
|
||||
const normalizedPath = normalizeExternalSqlPath(path);
|
||||
if (!normalizedPath) return;
|
||||
saveExternalSqlFileTargets(loadExternalSqlFileTargets().filter((item) => item.path !== normalizedPath));
|
||||
}
|
||||
|
||||
export function resolveExternalSqlFileTarget(path: string, connectionExists: (connectionId: string) => boolean, fallback: ExternalSqlFileTarget): ExternalSqlFileTarget {
|
||||
const normalizedPath = normalizeExternalSqlPath(path);
|
||||
const saved = loadExternalSqlFileTargets().find((item) => item.path === normalizedPath);
|
||||
|
||||
Reference in new issue
Block a user