Fix for googlesheets issue - when table initially created doesn't have the correct schema, adding a function to handle static schemas like Gsheets.

This commit is contained in:
mike12345567 2023-03-14 12:11:01 +00:00
parent 3d84409a58
commit da9e1bed64
4 changed files with 40 additions and 15 deletions

View File

@ -7,6 +7,7 @@ import {
generateJunctionTableName, generateJunctionTableName,
foreignKeyStructure, foreignKeyStructure,
hasTypeChanged, hasTypeChanged,
setStaticSchemas,
} from "./utils" } from "./utils"
import { FieldTypes } from "../../../constants" import { FieldTypes } from "../../../constants"
import { makeExternalQuery } from "../../../integrations/base/query" import { makeExternalQuery } from "../../../integrations/base/query"
@ -195,19 +196,19 @@ function isRelationshipSetup(column: FieldSchema) {
} }
export async function save(ctx: UserCtx) { export async function save(ctx: UserCtx) {
const table: TableRequest = ctx.request.body const inputs: TableRequest = ctx.request.body
const renamed = table?._rename const renamed = inputs?._rename
// can't do this right now // can't do this right now
delete table.rows delete inputs.rows
const datasourceId = getDatasourceId(ctx.request.body)! const datasourceId = getDatasourceId(ctx.request.body)!
// table doesn't exist already, note that it is created // table doesn't exist already, note that it is created
if (!table._id) { if (!inputs._id) {
table.created = true inputs.created = true
} }
let tableToSave: TableRequest = { let tableToSave: TableRequest = {
type: "table", type: "table",
_id: buildExternalTableId(datasourceId, table.name), _id: buildExternalTableId(datasourceId, inputs.name),
...table, ...inputs,
} }
let oldTable let oldTable
@ -224,6 +225,10 @@ export async function save(ctx: UserCtx) {
if (!datasource.entities) { if (!datasource.entities) {
datasource.entities = {} datasource.entities = {}
} }
// GSheets is a specific case - only ever has a static primary key
tableToSave = setStaticSchemas(datasource, tableToSave)
const oldTables = cloneDeep(datasource.entities) const oldTables = cloneDeep(datasource.entities)
const tables: Record<string, Table> = datasource.entities const tables: Record<string, Table> = datasource.entities
@ -246,7 +251,7 @@ export async function save(ctx: UserCtx) {
const junctionTable = generateManyLinkSchema( const junctionTable = generateManyLinkSchema(
datasource, datasource,
schema, schema,
table, tableToSave,
relatedTable relatedTable
) )
if (tables[junctionTable.name]) { if (tables[junctionTable.name]) {
@ -256,10 +261,12 @@ export async function save(ctx: UserCtx) {
extraTablesToUpdate.push(junctionTable) extraTablesToUpdate.push(junctionTable)
} else { } else {
const fkTable = const fkTable =
relationType === RelationshipTypes.ONE_TO_MANY ? table : relatedTable relationType === RelationshipTypes.ONE_TO_MANY
? tableToSave
: relatedTable
const foreignKey = generateLinkSchema( const foreignKey = generateLinkSchema(
schema, schema,
table, tableToSave,
relatedTable, relatedTable,
relationType relationType
) )
@ -271,11 +278,11 @@ export async function save(ctx: UserCtx) {
fkTable.constrained.push(foreignKey) fkTable.constrained.push(foreignKey)
} }
// foreign key is in other table, need to save it to external // foreign key is in other table, need to save it to external
if (fkTable._id !== table._id) { if (fkTable._id !== tableToSave._id) {
extraTablesToUpdate.push(fkTable) extraTablesToUpdate.push(fkTable)
} }
} }
generateRelatedSchema(schema, relatedTable, table, relatedColumnName) generateRelatedSchema(schema, relatedTable, tableToSave, relatedColumnName)
schema.main = true schema.main = true
} }

View File

@ -1,7 +1,11 @@
import { parse, isSchema, isRows } from "../../../utilities/schema" import { parse, isSchema, isRows } from "../../../utilities/schema"
import { getRowParams, generateRowID, InternalTables } from "../../../db/utils" import { getRowParams, generateRowID, InternalTables } from "../../../db/utils"
import { isEqual } from "lodash" import { isEqual } from "lodash"
import { AutoFieldSubTypes, FieldTypes } from "../../../constants" import {
AutoFieldSubTypes,
FieldTypes,
GOOGLE_SHEETS_PRIMARY_KEY,
} from "../../../constants"
import { import {
inputProcessing, inputProcessing,
cleanupAttachments, cleanupAttachments,
@ -16,7 +20,7 @@ import viewTemplate from "../view/viewBuilder"
import { cloneDeep } from "lodash/fp" import { cloneDeep } from "lodash/fp"
import { quotas } from "@budibase/pro" import { quotas } from "@budibase/pro"
import { events, context } from "@budibase/backend-core" import { events, context } from "@budibase/backend-core"
import { Database } from "@budibase/types" import { Database, Datasource, SourceName, Table } from "@budibase/types"
export async function clearColumns(table: any, columnNames: any) { export async function clearColumns(table: any, columnNames: any) {
const db: Database = context.getAppDB() const db: Database = context.getAppDB()
@ -392,5 +396,17 @@ export function hasTypeChanged(table: any, oldTable: any) {
return false return false
} }
// used for external tables, some of them will have static schemas that need
// to be hard set
export function setStaticSchemas(datasource: Datasource, table: Table) {
// GSheets is a specific case - only ever has a static primary key
if (table && datasource.source === SourceName.GOOGLE_SHEETS) {
table.primary = [GOOGLE_SHEETS_PRIMARY_KEY]
// if there is an id column, remove it, should never exist in GSheets
delete table.schema?.id
}
return table
}
const _TableSaveFunctions = TableSaveFunctions const _TableSaveFunctions = TableSaveFunctions
export { _TableSaveFunctions as TableSaveFunctions } export { _TableSaveFunctions as TableSaveFunctions }

View File

@ -180,3 +180,4 @@ export enum AutomationErrors {
// pass through the list from the auth/core lib // pass through the list from the auth/core lib
export const ObjectStoreBuckets = objectStore.ObjectStoreBuckets export const ObjectStoreBuckets = objectStore.ObjectStoreBuckets
export const MAX_AUTOMATION_RECURRING_ERRORS = 5 export const MAX_AUTOMATION_RECURRING_ERRORS = 5
export const GOOGLE_SHEETS_PRIMARY_KEY = "rowNumber"

View File

@ -19,6 +19,7 @@ import { GoogleSpreadsheet } from "google-spreadsheet"
import fetch from "node-fetch" import fetch from "node-fetch"
import { configs, HTTPError } from "@budibase/backend-core" import { configs, HTTPError } from "@budibase/backend-core"
import { dataFilters } from "@budibase/shared-core" import { dataFilters } from "@budibase/shared-core"
import { GOOGLE_SHEETS_PRIMARY_KEY } from "../constants"
interface GoogleSheetsConfig { interface GoogleSheetsConfig {
spreadsheetId: string spreadsheetId: string
@ -227,7 +228,7 @@ class GoogleSheetsIntegration implements DatasourcePlus {
// base table // base table
const table: Table = { const table: Table = {
name: title, name: title,
primary: ["rowNumber"], primary: [GOOGLE_SHEETS_PRIMARY_KEY],
schema: {}, schema: {},
} }
if (id) { if (id) {