-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
596 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@datatruck/cli": minor | ||
--- | ||
|
||
Add MongoDB restore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { fetchData } from "./fs"; | ||
|
||
export type MongoUriObject<Resolved = false> = { | ||
host: string; | ||
username?: string; | ||
password?: [Resolved] extends [true] ? string : string | { path: string }; | ||
port?: number; | ||
database: string; | ||
}; | ||
|
||
export function toMongoUri(object: MongoUriObject<true>) { | ||
const url = new URL(`mongodb://${object.host}`); | ||
if (typeof object.username === "string") url.username = object.username; | ||
if (typeof object.password === "string") url.password = object.password; | ||
if (typeof object.port === "number") url.port = object.port.toString(); | ||
url.pathname = `/${object.database}`; | ||
return url.href; | ||
} | ||
|
||
export async function resolveMongoUri( | ||
input: string | MongoUriObject, | ||
): Promise<MongoUriObject<true>> { | ||
let object: MongoUriObject; | ||
if (typeof input === "string") { | ||
const url = new URL(input); | ||
object = { | ||
host: url.hostname, | ||
password: url.password, | ||
port: url.port ? Number(url.port) : undefined, | ||
username: url.username, | ||
database: url.pathname.slice(1), | ||
}; | ||
} else { | ||
object = input; | ||
} | ||
return { | ||
...object, | ||
password: | ||
object.password !== undefined | ||
? (await fetchData(object.password, (p) => p.path)) ?? "" | ||
: "", | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import { createCommands } from "../src/utils/datatruck/command"; | ||
import { parseStringList } from "../src/utils/string"; | ||
import { makeConfig, makeRepositoryConfig, testRepositoryTypes } from "./util"; | ||
import { MongoClient } from "mongodb"; | ||
import { MongoMemoryServer } from "mongodb-memory-server"; | ||
import { describe, expect, it } from "vitest"; | ||
|
||
const repositoryTypes = parseStringList( | ||
process.env.DTT_REPO, | ||
testRepositoryTypes, | ||
true, | ||
); | ||
|
||
const dataFormats = parseStringList( | ||
process.env.DTT_DATA_FORMAT, | ||
["defaults" as const], | ||
process.env.CI ? ["defaults"] : true, | ||
); | ||
|
||
describe( | ||
"mongo-dump-task", | ||
{ | ||
timeout: 300_000, | ||
}, | ||
() => { | ||
it.each( | ||
repositoryTypes.flatMap((repositoryType) => | ||
dataFormats.map((dataFormat) => ({ | ||
repositoryType, | ||
dataFormat, | ||
})), | ||
), | ||
)("backup and restore $repositoryType", async ({ repositoryType }) => { | ||
const verbose = 1; | ||
const dbName = `tmp_dtt_db`; | ||
const mongoServer = await MongoMemoryServer.create({ | ||
auth: { | ||
enable: true, | ||
extraUsers: [ | ||
{ | ||
createUser: "test", | ||
pwd: "test", | ||
roles: [{ role: "root", db: "admin" }], | ||
}, | ||
], | ||
}, | ||
}); | ||
const port = Number(new URL(mongoServer.getUri()).port); | ||
try { | ||
const client = new MongoClient(`mongodb://test:[email protected]:${port}`); | ||
|
||
const db = client.db(dbName); | ||
const sourceData: Record<string, Record<string, any>[]> = { | ||
col1: [ | ||
{ _id: 1, value: null }, | ||
{ _id: 2, value: "a" }, | ||
{ _id: 3, value: '"with\' quotes"' }, | ||
{ _id: 4, value: '"with\nline\r\nsalts"' }, | ||
{ _id: 5, value: '"\ttext' }, | ||
{ _id: 6, value: null }, | ||
{ _id: 7, value: "a\nb" }, | ||
{ _id: 8, value: "»finish" }, | ||
], | ||
col2: [{ _id: 3, value: "b" }], | ||
}; | ||
|
||
for (const collection of Object.keys(sourceData)) { | ||
for (const row of sourceData[collection]) { | ||
db.collection(collection).insertOne(row); | ||
} | ||
} | ||
|
||
const config = await makeConfig({ | ||
repositories: [await makeRepositoryConfig(repositoryType)], | ||
packages: [ | ||
{ | ||
name: "main/mongo-dump", | ||
repositoryNames: [repositoryType], | ||
task: { | ||
name: "mongo-dump", | ||
config: { | ||
uri: { | ||
database: dbName, | ||
host: "127.0.0.1", | ||
port, | ||
username: "test", | ||
password: "test", | ||
}, | ||
targetDatabase: { | ||
name: `${dbName}_{snapshotId}`, | ||
}, | ||
}, | ||
}, | ||
}, | ||
], | ||
}); | ||
|
||
const dtt = createCommands({ config, verbose }); | ||
await dtt.init({}); | ||
await dtt.backup({}); | ||
const snapshots = await dtt.snapshots({}); | ||
expect(snapshots).toHaveLength(1); | ||
const [snapshot] = snapshots; | ||
|
||
await dtt.restore({ id: snapshot.id }); | ||
|
||
const restoredDbName = `${dbName}_${snapshot.id}`; | ||
const collections = (await client.db(restoredDbName).collections()) | ||
.map((col) => col.collectionName) | ||
.sort(); | ||
|
||
expect(collections.join()).toBe(Object.keys(sourceData).sort().join()); | ||
|
||
for (const collection in sourceData) { | ||
const rows = await client | ||
.db(restoredDbName) | ||
.collection(collection) | ||
.aggregate([{ $sort: { _id: 1 } }]) | ||
.toArray(); | ||
|
||
expect(JSON.stringify(rows)).toBe( | ||
JSON.stringify(sourceData[collection]), | ||
); | ||
} | ||
} finally { | ||
await mongoServer.stop(); | ||
} | ||
}); | ||
}, | ||
); |
Oops, something went wrong.