Browse Source

refactor: store local copy of updated ncSchema

Signed-off-by: Raju Udava <86527202+dstala@users.noreply.github.com>
sync
Raju Udava 3 years ago
parent
commit
7c8c93b731
  1. 60
      packages/nocodb/tests/sync/sync.js

60
packages/nocodb/tests/sync/sync.js

@ -329,6 +329,7 @@ async function nocoCreateBaseSchema(aTblSchema) {
ncCreatedProjectSchema.id, ncCreatedProjectSchema.id,
tables[idx] tables[idx]
); );
updateNcTblSchema(table)
// update mapping table // update mapping table
await sMap.addToMappingTbl(aTblSchema[idx].id, table.id, table.title) await sMap.addToMappingTbl(aTblSchema[idx].id, table.id, table.title)
@ -410,6 +411,7 @@ async function nocoCreateLinkToAnotherRecord(aTblSchema) {
// ? 'mm' // ? 'mm'
// : 'hm' // : 'hm'
}); });
updateNcTblSchema(ncTbl)
syncLog(`NC API: dbTableColumn.create LinkToAnotherRecord`) syncLog(`NC API: dbTableColumn.create LinkToAnotherRecord`)
let ncId = ncTbl.columns.find(x => x.title === aTblLinkColumns[i].name + suffix)?.id let ncId = ncTbl.columns.find(x => x.title === aTblLinkColumns[i].name + suffix)?.id
@ -506,7 +508,7 @@ async function nocoCreateLinkToAnotherRecord(aTblSchema) {
...childLinkColumn, ...childLinkColumn,
title: aTblLinkColumns[i].name + suffix, title: aTblLinkColumns[i].name + suffix,
}); });
updateNcTblSchema(ncTbl)
let ncId = ncTbl.columns.find(x => x.title === aTblLinkColumns[i].name + suffix)?.id let ncId = ncTbl.columns.find(x => x.title === aTblLinkColumns[i].name + suffix)?.id
await sMap.addToMappingTbl(aTblLinkColumns[i].id, ncId, aTblLinkColumns[i].name + suffix) await sMap.addToMappingTbl(aTblLinkColumns[i].id, ncId, aTblLinkColumns[i].name + suffix)
@ -547,14 +549,15 @@ async function nocoCreateLookups(aTblSchema) {
continue; continue;
} }
let lookupColumn = await api.dbTableColumn.create(srcTableId, { let ncTbl = await api.dbTableColumn.create(srcTableId, {
uidt: UITypes.Lookup, uidt: UITypes.Lookup,
title: aTblColumns[i].name, title: aTblColumns[i].name,
fk_relation_column_id: ncRelationColumnId, fk_relation_column_id: ncRelationColumnId,
fk_lookup_column_id: ncLookupColumnId fk_lookup_column_id: ncLookupColumnId
}); });
updateNcTblSchema(ncTbl)
let ncId = lookupColumn.columns.find(x => x.title === aTblColumns[i].name)?.id let ncId = ncTbl.columns.find(x => x.title === aTblColumns[i].name)?.id
await sMap.addToMappingTbl(aTblColumns[i].id, ncId, aTblColumns[i].name) await sMap.addToMappingTbl(aTblColumns[i].id, ncId, aTblColumns[i].name)
syncLog(`NC API: dbTableColumn.create LOOKUP`) syncLog(`NC API: dbTableColumn.create LOOKUP`)
@ -587,14 +590,15 @@ async function nocoCreateLookups(aTblSchema) {
continue; continue;
} }
let lookupColumn = await api.dbTableColumn.create(srcTableId, { let ncTbl = await api.dbTableColumn.create(srcTableId, {
uidt: UITypes.Lookup, uidt: UITypes.Lookup,
title: nestedLookupTbl[i].name, title: nestedLookupTbl[i].name,
fk_relation_column_id: ncRelationColumnId, fk_relation_column_id: ncRelationColumnId,
fk_lookup_column_id: ncLookupColumnId fk_lookup_column_id: ncLookupColumnId
}); });
updateNcTblSchema(ncTbl)
let ncId = lookupColumn.columns.find(x => x.title === nestedLookupTbl[i].name)?.id let ncId = ncTbl.columns.find(x => x.title === nestedLookupTbl[i].name)?.id
await sMap.addToMappingTbl(nestedLookupTbl[i].id, ncId, nestedLookupTbl[i].name) await sMap.addToMappingTbl(nestedLookupTbl[i].id, ncId, nestedLookupTbl[i].name)
// remove entry // remove entry
@ -633,16 +637,17 @@ async function nocoCreateRollups(aTblSchema) {
continue; continue;
} }
let rollupColumn = await api.dbTableColumn.create(srcTableId, { let ncTbl = await api.dbTableColumn.create(srcTableId, {
uidt: UITypes.Rollup, uidt: UITypes.Rollup,
title: aTblColumns[i].name, title: aTblColumns[i].name,
fk_relation_column_id: ncRelationColumnId, fk_relation_column_id: ncRelationColumnId,
fk_rollup_column_id: ncRollupColumnId, fk_rollup_column_id: ncRollupColumnId,
rollup_function: 'sum' // fix me: hardwired rollup_function: 'sum' // fix me: hardwired
}); });
updateNcTblSchema(ncTbl)
syncLog(`NC API: dbTableColumn.create ROLLUP`) syncLog(`NC API: dbTableColumn.create ROLLUP`)
let ncId = rollupColumn.columns.find(x => x.title === aTblColumns[i].name)?.id let ncId = ncTbl.columns.find(x => x.title === aTblColumns[i].name)?.id
await sMap.addToMappingTbl(aTblColumns[i].id, ncId, aTblColumns[i].name) await sMap.addToMappingTbl(aTblColumns[i].id, ncId, aTblColumns[i].name)
} }
@ -665,18 +670,19 @@ async function nocoLookupForRollups() {
continue; continue;
} }
let lookupColumn = await api.dbTableColumn.create(srcTableId, { let ncTbl = await api.dbTableColumn.create(srcTableId, {
uidt: UITypes.Lookup, uidt: UITypes.Lookup,
title: nestedLookupTbl[i].name, title: nestedLookupTbl[i].name,
fk_relation_column_id: ncRelationColumnId, fk_relation_column_id: ncRelationColumnId,
fk_lookup_column_id: ncLookupColumnId fk_lookup_column_id: ncLookupColumnId
}); });
updateNcTblSchema(ncTbl)
// remove entry // remove entry
nestedLookupTbl.splice(i, 1) nestedLookupTbl.splice(i, 1)
syncLog(`NC API: dbTableColumn.create LOOKUP`) syncLog(`NC API: dbTableColumn.create LOOKUP`)
let ncId = lookupColumn.columns.find(x => x.title === nestedLookupTbl[i].name)?.id let ncId = ncTbl.columns.find(x => x.title === nestedLookupTbl[i].name)?.id
await sMap.addToMappingTbl(nestedLookupTbl[i].id, ncId, nestedLookupTbl[i].name) await sMap.addToMappingTbl(nestedLookupTbl[i].id, ncId, nestedLookupTbl[i].name)
} }
} }
@ -690,8 +696,14 @@ async function nocoSetPrimary(aTblSchema) {
// skip primary column configuration if we field not migrated // skip primary column configuration if we field not migrated
syncLog(`NC API: dbTableColumn.primaryColumnSet`) syncLog(`NC API: dbTableColumn.primaryColumnSet`)
if(ncCol) if(ncCol) {
await api.dbTableColumn.primaryColumnSet(ncCol.id); await api.dbTableColumn.primaryColumnSet(ncCol.id);
// update schema
let ncTblId = sMap.getNcIdFromAtId(aTblSchema[idx].id)
let ncTbl = await api.dbTable.read(ncTblId)
updateNcTblSchema(ncTbl)
}
} }
} }
@ -1099,6 +1111,31 @@ async function nocoAddUsers(aTblSchema) {
} }
} }
let ncSchema = {
tables: [],
tablesById: {}
}
function updateNcTblSchema(tblSchema) {
let tblId = tblSchema.id;
// replace entry from array if already exists
let idx = ncSchema.tables.findIndex(x => x.id === tblId)
if(idx !== -1) ncSchema.tables.splice(idx, 1)
ncSchema.tables.push(tblSchema)
// overwrite object if it exists
ncSchema.tablesById[tblId] = tblSchema
}
async function nocoReadNcSchema() {
let tableList = await api.dbTable.list(ncCreatedProjectSchema.id)
for(let tblCnt = 0; tblCnt < tableList.list.length; tblCnt++) {
let tblSchema = await api.dbTable.read(tableList.list[tblCnt].id)
updateNcTblSchema(tblSchema)
}
}
// start function // start function
module.exports = async function nc_migrateATbl(syncDB) { module.exports = async function nc_migrateATbl(syncDB) {
@ -1137,6 +1174,9 @@ module.exports = async function nc_migrateATbl(syncDB) {
// configure primary values // configure primary values
await nocoSetPrimary(aTblSchema); await nocoSetPrimary(aTblSchema);
// build configured nc schema for usage in rest of the migration
// await nocoReadNcSchema()
// add users // add users
await nocoAddUsers(schema) await nocoAddUsers(schema)

Loading…
Cancel
Save