bugfix: database upgrade broken when field added

This commit is contained in:
Michael Shanks 2020-04-02 06:21:52 +01:00
parent 609875ac3e
commit 84d145026b
2 changed files with 46 additions and 2 deletions

View File

@ -44,18 +44,22 @@ import {
} from "../templateApi/hierarchy" } from "../templateApi/hierarchy"
import { getRecordInfo } from "../recordApi/recordInfo" import { getRecordInfo } from "../recordApi/recordInfo"
import { getIndexDir } from "../indexApi/getIndexDir" import { getIndexDir } from "../indexApi/getIndexDir"
import { _deleteIndex } from "../indexApi/delete"
import { initialiseIndex } from "../indexing/initialiseIndex" import { initialiseIndex } from "../indexing/initialiseIndex"
export const executeTransactions = app => async transactions => { export const executeTransactions = app => async transactions => {
const recordsByShard = mappedRecordsByIndexShard(app.hierarchy, transactions) const recordsByShard = mappedRecordsByIndexShard(app.hierarchy, transactions)
for (const shard of keys(recordsByShard)) { for (const shard of keys(recordsByShard)) {
if (recordsByShard[shard].isRebuild) if (recordsByShard[shard].isRebuild) {
if (await app.datastore.exists(shard))
await app.datastore.deleteFile(shard)
await initialiseIndex( await initialiseIndex(
app.datastore, app.datastore,
getParentKey(recordsByShard[shard].indexDir), getParentKey(recordsByShard[shard].indexDir),
recordsByShard[shard].indexNode recordsByShard[shard].indexNode
) )
}
await applyToShard( await applyToShard(
app.hierarchy, app.hierarchy,
app.datastore, app.datastore,
@ -76,7 +80,11 @@ const mappedRecordsByIndexShard = (hierarchy, transactions) => {
const indexBuild = getBuildIndexTransactionsByShard(hierarchy, transactions) const indexBuild = getBuildIndexTransactionsByShard(hierarchy, transactions)
const toRemove = [...deletes, ...updates.toRemove, ...indexBuild.toRemove] const toRemove = [
...deletes,
...updates.toRemove,
...indexBuild.toRemove,
]
const toWrite = [...created, ...updates.toWrite, ...indexBuild.toWrite] const toWrite = [...created, ...updates.toWrite, ...indexBuild.toWrite]

View File

@ -240,6 +240,40 @@ describe("upgradeData", () => {
}) })
it("should rebuild affected index when field is removed", async () => {
const { oldSetup, newSetup, records } = await configure()
newSetup.contact.fields = newSetup.contact.fields.filter(f => f.name !== "status")
let itemsInIndex = await _listItems(oldSetup.app, "/contact_index")
expect(itemsInIndex.length).toBe(2)
expect(itemsInIndex[0].status).toBeDefined()
expect(itemsInIndex[0].status).toBe(records.contact1.status)
await upgradeData(oldSetup.app)(newSetup.root)
itemsInIndex = await _listItems(newSetup.app, "/contact_index")
expect(itemsInIndex.length).toBe(2)
expect(itemsInIndex[0].status).toBeUndefined()
})
it("should rebuild affected index when field is added", async () => {
const { oldSetup, newSetup, records } = await configure()
const aliveField = newSetup.templateApi.getNewField("string")
aliveField.name = "isalive"
newSetup.templateApi.addField(newSetup.contact, aliveField)
let itemsInIndex = await _listItems(oldSetup.app, "/contact_index")
expect(itemsInIndex.length).toBe(2)
expect(itemsInIndex[0].isalive).toBeUndefined()
await upgradeData(oldSetup.app)(newSetup.root)
itemsInIndex = await _listItems(newSetup.app, "/contact_index")
expect(itemsInIndex.length).toBe(2)
expect(itemsInIndex[0].isalive).toBe(null)
})
const configure = async () => { const configure = async () => {
const oldSetup = await setup() const oldSetup = await setup()
@ -256,8 +290,10 @@ const configure = async () => {
const createSomeRecords = async recordApi => { const createSomeRecords = async recordApi => {
const contact1 = recordApi.getNew("/contacts", "contact") const contact1 = recordApi.getNew("/contacts", "contact")
contact1.name = "bobby" contact1.name = "bobby"
contact1.status = "New"
const contact2 = recordApi.getNew("/contacts", "contact") const contact2 = recordApi.getNew("/contacts", "contact")
contact2.name = "poppy" contact2.name = "poppy"
contact2.status = "Complete"
await recordApi.save(contact1) await recordApi.save(contact1)
await recordApi.save(contact2) await recordApi.save(contact2)