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

Conversation

tomasciccola
Copy link
Contributor

@tomasciccola tomasciccola commented Aug 26, 2024

should close #761
The approach I took is basically set a bunch of promises that only get resolved after a successfull configImport. I think this approach is right

@tomasciccola tomasciccola requested a review from EvanHahn August 26, 2024 13:43
src/mapeo-project.js Outdated Show resolved Hide resolved
@tomasciccola tomasciccola requested a review from EvanHahn August 26, 2024 17:36
Copy link
Contributor

@EvanHahn EvanHahn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we test that (1) old data is properly deleted (2) nothing is deleted when config import fails?

We might already have these tests somewhere.

src/mapeo-project.js Outdated Show resolved Hide resolved
src/mapeo-project.js Outdated Show resolved Hide resolved
@EvanHahn EvanHahn changed the title feat: delete old config docs only after successfull new config import feat: delete old config docs only after successful new config import Aug 26, 2024
@tomasciccola tomasciccola requested a review from EvanHahn August 27, 2024 17:12
@tomasciccola
Copy link
Contributor Author

Could we test that (1) old data is properly deleted (2) nothing is deleted when config import fails?

We might already have these tests somewhere.

missed this comments. Gonna add this checks first thing tomorrow and re-ask for review!

await project.importConfig({
configPath: defaultConfigPath,
})
assert.equal(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a failing test that I'm not being able to solve. I basically:

  • load default config and take note of the number of presets
  • load another config
  • load the default config again
  • Expect the number of presets to be the same as whe the first default config was loaded
    The actual number of presets is nPresetsDefaultConfig + nPresetsValidConfig.
    If I run it in a loop, I get the same number every time (so presets are being deleted...)

Copy link
Contributor Author

@tomasciccola tomasciccola Aug 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was just me misunderstanding Promises. Before I did:
Promise.all([[presetsToDelete], [fieldsToDelete], [translationsToDelete]].flat())
(basically flatting the list of stuff to delete and passing it to Promise.all)
Now I do:
Promise.all([Promise.all(presetsToDelete), Promise.all(fieldsToDelete), Promise.all(translationsToDelete)])

I think I'm maybe not doing deletions of presets, fields, translations in parallel now (only within themselves, so preset deletion is in parallel, field deletion too, etc). I'm doubting this because I'm not awaiting the inner Promise.alls

@EvanHahn any thoughts?

Copy link
Contributor

@EvanHahn EvanHahn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM other than my small comments.

src/mapeo-project.js Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, why did this file change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two updates to that file:

  1. key -> tagKey
  2. added color
    Since I'm now counting loaded presets on that config, I wanted to have it correctly loaded

const nFields = (await project.field.getMany()).length
const nTranslations = (await project.$translation.dataType.getMany()).length

// load wrong config
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this isn't just a wrong config, it's a bogus path.

Suggested change
// load wrong config
// load a non-existent config

Comment on lines 922 to 934
translations.forEach(async ({ docRefType, docRef, docId }) => {
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) {
toDelete.push(docId)
}
}
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for being unclear earlier. We need to await all of these.

Suggested change
translations.forEach(async ({ docRefType, docRef, docId }) => {
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) {
toDelete.push(docId)
}
}
})
await Promise.all(
translations.map(async ({ docRefType, docRef, docId }) => {
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) {
toDelete.push(docId)
}
}
})
)

Also, should we make toDelete a Set? It's possible for multiple translations to refer to the same field/preset, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, right. Changed on the last commit

@tomasciccola tomasciccola merged commit 952d298 into main Aug 29, 2024
7 checks passed
@tomasciccola tomasciccola deleted the fix/deleteConfigOnlyIfSuccessfullImport branch August 29, 2024 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

don't delete presets, fields and translations if the config-import fails
2 participants