feat(mongo): add collection clone
This commit is contained in:
1 parent
e80942de17
commit
5fe3b5b435
34 files changed
+1217
-21
No files matched your search
@@ -570,6 +570,7 @@ export const mongoCreateDatabase = forward("mongoCreateDatabase");
|
||||
export const mongoDropDatabase = forward("mongoDropDatabase");
|
||||
export const mongoDropCollection = forward("mongoDropCollection");
|
||||
export const mongoRenameCollection = forward("mongoRenameCollection");
|
||||
export const mongoCloneCollection = forward("mongoCloneCollection");
|
||||
export const documentFindDocuments = forward("documentFindDocuments");
|
||||
export const elasticsearchCountDocuments = forward("elasticsearchCountDocuments");
|
||||
export const mongoFindDocuments = forward("mongoFindDocuments");
|
||||
|
||||
@@ -99,6 +99,7 @@ import type {
|
||||
DocumentQueryResult,
|
||||
MongoDocumentResult,
|
||||
MongoCollectionStatsResult,
|
||||
MongoCloneCollectionResult,
|
||||
MongoDropIndexesResult,
|
||||
MongoGridFsBucketInfo,
|
||||
HistoryEntry,
|
||||
@@ -3005,6 +3006,15 @@ export async function mongoRenameCollection(connectionId: string, database: stri
|
||||
});
|
||||
}
|
||||
|
||||
export async function mongoCloneCollection(connectionId: string, database: string, sourceCollection: string, targetCollection: string): Promise<MongoCloneCollectionResult> {
|
||||
return post("/api/mongo/clone-collection", {
|
||||
connectionId,
|
||||
database,
|
||||
sourceCollection,
|
||||
targetCollection,
|
||||
});
|
||||
}
|
||||
|
||||
export async function elasticsearchListIndices(connectionId: string): Promise<string[]> {
|
||||
const collections = await documentListCollections(connectionId, "default");
|
||||
return collections.map((c) => c.name);
|
||||
|
||||
@@ -2888,6 +2888,11 @@ export interface MongoDropIndexesResult {
|
||||
failures?: MongoDropIndexFailure[];
|
||||
}
|
||||
|
||||
export interface MongoCloneCollectionResult {
|
||||
documents_copied: number;
|
||||
indexes_copied: number;
|
||||
}
|
||||
|
||||
export interface MongoGridFsFileInfo {
|
||||
id: string;
|
||||
filename?: string;
|
||||
@@ -2955,6 +2960,15 @@ export async function mongoRenameCollection(connectionId: string, database: stri
|
||||
});
|
||||
}
|
||||
|
||||
export async function mongoCloneCollection(connectionId: string, database: string, sourceCollection: string, targetCollection: string): Promise<MongoCloneCollectionResult> {
|
||||
return invoke("mongo_clone_collection", {
|
||||
connectionId,
|
||||
database,
|
||||
sourceCollection,
|
||||
targetCollection,
|
||||
});
|
||||
}
|
||||
|
||||
export async function elasticsearchListIndices(connectionId: string): Promise<string[]> {
|
||||
const collections = await documentListCollections(connectionId, "default");
|
||||
return collections.map((c) => c.name);
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
buildMongoCreateIndexRequest,
|
||||
isCloneableMongoCollection,
|
||||
isProtectedMongoIndex,
|
||||
isRenamableMongoCollection,
|
||||
mongoCollectionKindFromNode,
|
||||
mongoCollectionTableTypeFromNode,
|
||||
mongoCloneCollectionPreview,
|
||||
mongoCreateIndexPreview,
|
||||
mongoDropCollectionPreview,
|
||||
mongoDropAllIndexesPreview,
|
||||
@@ -32,6 +34,15 @@ describe("isRenamableMongoCollection", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("isCloneableMongoCollection", () => {
|
||||
it("allows ordinary collections only", () => {
|
||||
expect(isCloneableMongoCollection("users")).toBe(true);
|
||||
expect(isCloneableMongoCollection("report_view", "view")).toBe(false);
|
||||
expect(isCloneableMongoCollection("metrics", "timeseries")).toBe(false);
|
||||
expect(isCloneableMongoCollection("system.users")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("mongoCollectionKindFromNode", () => {
|
||||
it("reads collectionKind from node meta without using SQL tableType", () => {
|
||||
expect(mongoCollectionKindFromNode({ meta: { collectionKind: "view" } })).toBe("view");
|
||||
@@ -69,6 +80,15 @@ describe("mongo shell previews", () => {
|
||||
expect(mongoRenameCollectionPreview("app", " users ", " renamed ")).toBe('db.getSiblingDB("app").getCollection(" users ").renameCollection(" renamed ")');
|
||||
});
|
||||
|
||||
it("describes the version-compatible clone primitives", () => {
|
||||
expect(mongoCloneCollectionPreview("app", " users ", " users_backup ")).toBe(
|
||||
"// DBX copies collection options, documents, and non-_id indexes.\n" +
|
||||
'db.getSiblingDB("app").createCollection(" users_backup ", /* source options */);\n' +
|
||||
'db.getSiblingDB("app").getCollection(" users ").find({}).forEach(function (document) { db.getSiblingDB("app").getCollection(" users_backup ").insertOne(document); });\n' +
|
||||
"// Recreate source indexes except the target's automatic _id index.",
|
||||
);
|
||||
});
|
||||
|
||||
it("builds drop previews with database scope", () => {
|
||||
expect(mongoDropCollectionPreview("app", "users")).toBe('db.getSiblingDB("app").getCollection("users").drop()');
|
||||
expect(mongoDropIndexPreview("app", "users", "idx_name")).toBe('db.getSiblingDB("app").getCollection("users").dropIndex("idx_name")');
|
||||
|
||||
@@ -38,6 +38,11 @@ export function isRenamableMongoCollection(name: string, kind: MongoCollectionKi
|
||||
return kind === "collection" && !name.startsWith("system.");
|
||||
}
|
||||
|
||||
/** Only ordinary collections have transferable options, documents, and indexes. */
|
||||
export function isCloneableMongoCollection(name: string, kind: MongoCollectionKind = "collection"): boolean {
|
||||
return kind === "collection" && !name.startsWith("system.");
|
||||
}
|
||||
|
||||
export function mongoCollectionKindFromNode(node: Pick<TreeNode, "meta">): MongoCollectionKind {
|
||||
const meta = node.meta;
|
||||
if (meta && "collectionKind" in meta && meta.collectionKind) {
|
||||
@@ -62,6 +67,22 @@ export function mongoRenameCollectionPreview(database: string, oldName: string,
|
||||
return `db.getSiblingDB(${JSON.stringify(database)}).getCollection(${JSON.stringify(oldName)}).renameCollection(${JSON.stringify(newName)})`;
|
||||
}
|
||||
|
||||
/**
|
||||
* DBX executes these stable primitives in the backend instead of MongoDB's
|
||||
* deprecated clone commands, which vary across server generations.
|
||||
*/
|
||||
export function mongoCloneCollectionPreview(database: string, sourceName: string, targetName: string): string {
|
||||
const db = `db.getSiblingDB(${JSON.stringify(database)})`;
|
||||
const source = `${db}.getCollection(${JSON.stringify(sourceName)})`;
|
||||
const target = `${db}.getCollection(${JSON.stringify(targetName)})`;
|
||||
return [
|
||||
`// DBX copies collection options, documents, and non-_id indexes.`,
|
||||
`${db}.createCollection(${JSON.stringify(targetName)}, /* source options */);`,
|
||||
`${source}.find({}).forEach(function (document) { ${target}.insertOne(document); });`,
|
||||
`// Recreate source indexes except the target's automatic _id index.`,
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
export function mongoDropCollectionPreview(database: string, collection: string): string {
|
||||
return `db.getSiblingDB(${JSON.stringify(database)}).getCollection(${JSON.stringify(collection)}).drop()`;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user