mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-20 23:46:14 -04:00
ImportEtherpad: Don't make any changes if data is bad
This commit is contained in:
parent
a2e77a7128
commit
23f8a12922
2 changed files with 22 additions and 2 deletions
|
@ -47,6 +47,11 @@ exports.setPadRaw = async (padId, r) => {
|
||||||
if (originalPadId !== padId) throw new Error('unexpected pad ID in record');
|
if (originalPadId !== padId) throw new Error('unexpected pad ID in record');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// First validate and transform values. Do not commit any records to the database yet in case
|
||||||
|
// there is a problem with the data.
|
||||||
|
|
||||||
|
const dbRecords = new Map();
|
||||||
|
const existingAuthors = new Set();
|
||||||
await Promise.all(Object.entries(records).map(async ([key, value]) => {
|
await Promise.all(Object.entries(records).map(async ([key, value]) => {
|
||||||
if (!value) {
|
if (!value) {
|
||||||
return;
|
return;
|
||||||
|
@ -62,7 +67,7 @@ exports.setPadRaw = async (padId, r) => {
|
||||||
}
|
}
|
||||||
checkOriginalPadId(value.padIDs);
|
checkOriginalPadId(value.padIDs);
|
||||||
if (await authorManager.doesAuthorExist(id)) {
|
if (await authorManager.doesAuthorExist(id)) {
|
||||||
await authorManager.addPad(id, padId);
|
existingAuthors.add(id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
value.padIDs = {[padId]: 1};
|
value.padIDs = {[padId]: 1};
|
||||||
|
@ -79,11 +84,16 @@ exports.setPadRaw = async (padId, r) => {
|
||||||
logger.warn(`(pad ${padId}) Ignoring record with unsupported key: ${key}`);
|
logger.warn(`(pad ${padId}) Ignoring record with unsupported key: ${key}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await db.set(key, value);
|
dbRecords.set(key, value);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
if (unsupportedElements.size) {
|
if (unsupportedElements.size) {
|
||||||
logger.warn('Ignoring unsupported elements (you might want to install a plugin): ' +
|
logger.warn('Ignoring unsupported elements (you might want to install a plugin): ' +
|
||||||
`${[...unsupportedElements].join(', ')}`);
|
`${[...unsupportedElements].join(', ')}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
|
...[...dbRecords].map(async ([k, v]) => await db.set(k, v)),
|
||||||
|
...[...existingAuthors].map(async (authorId) => await authorManager.addPad(authorId, padId)),
|
||||||
|
]);
|
||||||
};
|
};
|
||||||
|
|
|
@ -62,6 +62,16 @@ describe(__filename, function () {
|
||||||
assert(await db.get(badKey) == null);
|
assert(await db.get(badKey) == null);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('changes are all or nothing', async function () {
|
||||||
|
const authorId = makeAuthorId();
|
||||||
|
const data = makeExport(authorId);
|
||||||
|
data['pad:differentPadId:revs:0'] = data['pad:testing:revs:0'];
|
||||||
|
delete data['pad:testing:revs:0'];
|
||||||
|
assert.rejects(importEtherpad.setPadRaw(padId, JSON.stringify(data)), /unexpected pad ID/);
|
||||||
|
assert(!await authorManager.doesAuthorExist(authorId));
|
||||||
|
assert(!await padManager.doesPadExist(padId));
|
||||||
|
});
|
||||||
|
|
||||||
describe('author pad IDs', function () {
|
describe('author pad IDs', function () {
|
||||||
let existingAuthorId;
|
let existingAuthorId;
|
||||||
let newAuthorId;
|
let newAuthorId;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue