Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: delete old config docs only after successful new config import #765

Merged
merged 11 commits into from
Aug 29, 2024
44 changes: 25 additions & 19 deletions src/mapeo-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -727,10 +727,10 @@ export class MapeoProject extends TypedEmitter {

try {
// check for already present fields and presets and delete them if exist
await deleteAll(this.preset)
await deleteAll(this.field)
const deletePresetsPromise = deleteAll(this.preset)
const deleteFieldsPromise = deleteAll(this.field)
// delete only translations that refer to deleted fields and presets
await deleteTranslations({
const deleteTranslationsPromise = deleteTranslations({
tomasciccola marked this conversation as resolved.
Show resolved Hide resolved
logger: this.#l,
translation: this.$translation.dataType,
preset: this.preset,
Expand Down Expand Up @@ -821,7 +821,7 @@ export class MapeoProject extends TypedEmitter {
)
} else {
throw new Error(
`docRef for preset or field with name ${name} not found`
`docRef for ${value.docRefType} with name ${name} not found`
tomasciccola marked this conversation as resolved.
Show resolved Hide resolved
)
}
}
Expand All @@ -842,6 +842,12 @@ export class MapeoProject extends TypedEmitter {
configMetadata: config.metadata,
})
this.#loadingConfig = false
await Promise.all([
deletePresetsPromise,
deleteFieldsPromise,
deleteTranslationsPromise,
])

return config.warnings
} catch (e) {
this.#l.log('error loading config', e)
Expand Down Expand Up @@ -879,22 +885,22 @@ async function deleteAll(dataType) {
*/
async function deleteTranslations(opts) {
const translations = await opts.translation.getMany()
await Promise.all(
translations.map(async ({ docId, docRef, docRefType }) => {
if (docRefType === 'preset' || docRefType === 'field') {
let shouldDelete = false
try {
const toDelete = await opts[docRefType].getByDocId(docRef.docId)
shouldDelete = toDelete.deleted
} catch (e) {
opts.logger.log(`referred ${docRef.docId} is not found`)
}
if (shouldDelete) {
await opts.translation.delete(docId)
}
/** @type any[] should actually be a list of what `.delete()` returns */
const deletions = []
translations.forEach(async ({ docRefType, docRef, docId }) => {
EvanHahn marked this conversation as resolved.
Show resolved Hide resolved
if (docRefType === 'field' || docRefType === 'preset') {
let doc
try {
doc = await opts[docRefType].getByVersionId(docRef.versionId)
} catch (e) {
opts.logger.log(`referred ${docRef.versionId} is not found`)
}
})
)
if (doc) {
deletions.push(opts.translation.delete(docId))
}
}
})
return Promise.all(deletions)
}

/**
Expand Down
Loading