Removing tables and their related table definitions.

This commit is contained in:
mike12345567 2024-07-09 16:33:10 +01:00
parent 6e699a163d
commit 4cb23759a3
1 changed files with 18 additions and 3 deletions

View File

@ -176,9 +176,24 @@ export async function addTable(table: Table) {
export async function removeTable(table: Table) {
const db = context.getAppDB()
try {
const definition = await db.get<SQLiteDefinition>(SQLITE_DESIGN_DOC_ID)
if (definition.sql?.tables?.[table._id!]) {
delete definition.sql.tables[table._id!]
let response = await Promise.all([
tablesSdk.getAllInternalTables(),
db.get<SQLiteDefinition>(SQLITE_DESIGN_DOC_ID),
])
const tables: Table[] = response[0],
definition: SQLiteDefinition = response[1]
const tableIds = tables
.map(tbl => tbl._id!)
.filter(id => !id.includes(table._id!))
let cleanup = false
for (let tableKey of Object.keys(definition.sql?.tables || {})) {
// there are no tables matching anymore
if (!tableIds.find(id => tableKey.includes(id))) {
delete definition.sql.tables[tableKey]
cleanup = true
}
}
if (cleanup) {
await db.put(definition)
// make sure SQS is cleaned up, tables removed
await db.sqlDiskCleanup()