Adding default tables to sync.

This commit is contained in:
mike12345567 2024-07-05 16:03:38 +01:00
parent 5066da2630
commit 34d073bcb7
2 changed files with 17 additions and 5 deletions

View File

@ -41,6 +41,7 @@ import {
getTableIDList,
} from "./filters"
import { dataFilters } from "@budibase/shared-core"
import { DEFAULT_TABLE_IDS } from "../../../../constants"
const builder = new sql.Sql(SqlClient.SQL_LITE)
const MISSING_COLUMN_REGEX = new RegExp(`no such column: .+`)
@ -211,6 +212,18 @@ async function runSqlQuery(
return response
}
function resyncDefinitionsRequired(status: number, message: string) {
// pre data_ prefix on column names, need to resync
return (
(status === 400 && message?.match(USER_COLUMN_PREFIX_REGEX)) ||
// default tables aren't included in definition
(status === 400 &&
DEFAULT_TABLE_IDS.find(tableId => message?.includes(tableId))) ||
// no design document found, needs a full sync
(status === 404 && message?.includes(SQLITE_DESIGN_DOC_ID))
)
}
export async function search(
options: RowSearchParams,
table: Table
@ -338,10 +351,7 @@ export async function search(
return response
} catch (err: any) {
const msg = typeof err === "string" ? err : err.message
const syncAndRepeat =
(err.status === 400 && msg?.match(USER_COLUMN_PREFIX_REGEX)) ||
(err.status === 404 && msg?.includes(SQLITE_DESIGN_DOC_ID))
if (syncAndRepeat) {
if (resyncDefinitionsRequired(err.status, msg)) {
await sdk.tables.sqs.syncDefinition()
return search(options, table)
}

View File

@ -15,6 +15,7 @@ import {
generateJunctionTableID,
} from "../../../../db/utils"
import { isEqual } from "lodash"
import { DEFAULT_TABLES } from "../../../../db/defaultData/datasource_bb_default"
const FieldTypeMap: Record<FieldType, SQLiteType> = {
[FieldType.BOOLEAN]: SQLiteType.NUMERIC,
@ -126,8 +127,9 @@ function mapTable(table: Table): SQLiteTables {
// nothing exists, need to iterate though existing tables
async function buildBaseDefinition(): Promise<PreSaveSQLiteDefinition> {
const tables = await tablesSdk.getAllInternalTables()
const defaultTables = DEFAULT_TABLES
const definition = sql.designDoc.base("tableId")
for (let table of tables) {
for (let table of tables.concat(defaultTables)) {
definition.sql.tables = {
...definition.sql.tables,
...mapTable(table),