Fixing an edge case, create table with auto ID, create some rows, then update a column and try to create another row, ID generation would have reset, this makes sure internal changes to the table are kept.

This commit is contained in:
mike12345567 2021-02-17 10:36:36 +00:00
parent 91926d17d9
commit 28e244fa16
1 changed files with 24 additions and 4 deletions

View File

@ -8,7 +8,7 @@ const {
generateRowID, generateRowID,
} = require("../../db/utils") } = require("../../db/utils")
const { isEqual } = require("lodash/fp") const { isEqual } = require("lodash/fp")
const { FieldTypes } = require("../../constants") const { FieldTypes, AutoFieldSubTypes } = require("../../constants")
async function checkForColumnUpdates(db, oldTable, updatedTable) { async function checkForColumnUpdates(db, oldTable, updatedTable) {
let updatedRows let updatedRows
@ -40,6 +40,27 @@ async function checkForColumnUpdates(db, oldTable, updatedTable) {
return updatedRows return updatedRows
} }
// makes sure the passed in table isn't going to reset the auto ID
function makeSureTableUpToDate(table, tableToSave) {
if (!table) {
return tableToSave
}
// sure sure rev is up to date
tableToSave._rev = table._rev
// make sure auto IDs are always updated - these are internal
// so the client may not know they have changed
for (let [field, column] of Object.entries(table.schema)) {
if (
column.autocolumn &&
column.subtype === AutoFieldSubTypes.AUTO_ID &&
tableToSave.schema[field]
) {
tableToSave.schema[field].lastID = column.lastID
}
}
return tableToSave
}
exports.fetch = async function(ctx) { exports.fetch = async function(ctx) {
const db = new CouchDB(ctx.user.appId) const db = new CouchDB(ctx.user.appId)
const body = await db.allDocs( const body = await db.allDocs(
@ -59,7 +80,7 @@ exports.save = async function(ctx) {
const appId = ctx.user.appId const appId = ctx.user.appId
const db = new CouchDB(appId) const db = new CouchDB(appId)
const { dataImport, ...rest } = ctx.request.body const { dataImport, ...rest } = ctx.request.body
const tableToSave = { let tableToSave = {
type: "table", type: "table",
_id: generateTableID(), _id: generateTableID(),
views: {}, views: {},
@ -70,8 +91,7 @@ exports.save = async function(ctx) {
let oldTable let oldTable
if (ctx.request.body && ctx.request.body._id) { if (ctx.request.body && ctx.request.body._id) {
oldTable = await db.get(ctx.request.body._id) oldTable = await db.get(ctx.request.body._id)
// update _rev just to make sure its always accurate tableToSave = makeSureTableUpToDate(oldTable, tableToSave)
tableToSave._rev = oldTable._rev
} }
// make sure that types don't change of a column, have to remove // make sure that types don't change of a column, have to remove