Merge pull request #9815 from Budibase/bug/budi-6076-cannot-delete-columns-in-google-sheet

Bug - budi-6076 cannot delete columns in google sheet
This commit is contained in:
Michael Drury 2023-02-28 13:56:32 +00:00 committed by GitHub
commit fcb9f3e116
4 changed files with 145 additions and 13 deletions

View File

@ -192,13 +192,13 @@
editableColumn.name = originalName
}
function deleteColumn() {
async function deleteColumn() {
try {
editableColumn.name = deleteColName
if (editableColumn.name === $tables.selected.primaryDisplay) {
notifications.error("You cannot delete the display column")
} else {
tables.deleteField(editableColumn)
await tables.deleteField(editableColumn)
notifications.success(`Column ${editableColumn.name} deleted.`)
confirmDeleteDialog.hide()
hide()

View File

@ -172,6 +172,9 @@ module FetchMock {
),
ok: true,
})
} else if (url === "https://www.googleapis.com/oauth2/v4/token") {
// any valid response
return json({})
} else if (url.includes("failonce.com")) {
failCount++
if (failCount === 1) {

View File

@ -11,8 +11,8 @@ import { OAuth2Client } from "google-auth-library"
import { buildExternalTableId } from "./utils"
import { DataSourceOperation, FieldTypes } from "../constants"
import { GoogleSpreadsheet } from "google-spreadsheet"
import fetch from "node-fetch"
import { configs, HTTPError } from "@budibase/backend-core"
const fetch = require("node-fetch")
interface GoogleSheetsConfig {
spreadsheetId: string
@ -111,7 +111,7 @@ const SCHEMA: Integration = {
class GoogleSheetsIntegration implements DatasourcePlus {
private readonly config: GoogleSheetsConfig
private client: any
private client: GoogleSpreadsheet
public tables: Record<string, Table> = {}
public schemaErrors: Record<string, string> = {}
@ -203,7 +203,7 @@ class GoogleSheetsIntegration implements DatasourcePlus {
async buildSchema(datasourceId: string) {
await this.connect()
const sheets = await this.client.sheetsByIndex
const sheets = this.client.sheetsByIndex
const tables: Record<string, Table> = {}
for (let sheet of sheets) {
// must fetch rows to determine schema
@ -286,7 +286,7 @@ class GoogleSheetsIntegration implements DatasourcePlus {
async updateTable(table?: any) {
try {
await this.connect()
const sheet = await this.client.sheetsByTitle[table.name]
const sheet = this.client.sheetsByTitle[table.name]
await sheet.loadHeaderRow()
if (table._rename) {
@ -300,10 +300,17 @@ class GoogleSheetsIntegration implements DatasourcePlus {
}
await sheet.setHeaderRow(headers)
} else {
let newField = Object.keys(table.schema).find(
const updatedHeaderValues = [...sheet.headerValues]
const newField = Object.keys(table.schema).find(
key => !sheet.headerValues.includes(key)
)
await sheet.setHeaderRow([...sheet.headerValues, newField])
if (newField) {
updatedHeaderValues.push(newField)
}
await sheet.setHeaderRow(updatedHeaderValues)
}
} catch (err) {
console.error("Error updating table in google sheets", err)
@ -314,7 +321,7 @@ class GoogleSheetsIntegration implements DatasourcePlus {
async deleteTable(sheet: any) {
try {
await this.connect()
const sheetToDelete = await this.client.sheetsByTitle[sheet]
const sheetToDelete = this.client.sheetsByTitle[sheet]
return await sheetToDelete.delete()
} catch (err) {
console.error("Error deleting table in google sheets", err)
@ -325,7 +332,7 @@ class GoogleSheetsIntegration implements DatasourcePlus {
async create(query: { sheet: string; row: any }) {
try {
await this.connect()
const sheet = await this.client.sheetsByTitle[query.sheet]
const sheet = this.client.sheetsByTitle[query.sheet]
const rowToInsert =
typeof query.row === "string" ? JSON.parse(query.row) : query.row
const row = await sheet.addRow(rowToInsert)
@ -341,7 +348,7 @@ class GoogleSheetsIntegration implements DatasourcePlus {
async read(query: { sheet: string }) {
try {
await this.connect()
const sheet = await this.client.sheetsByTitle[query.sheet]
const sheet = this.client.sheetsByTitle[query.sheet]
const rows = await sheet.getRows()
const headerValues = sheet.headerValues
const response = []
@ -360,7 +367,7 @@ class GoogleSheetsIntegration implements DatasourcePlus {
async update(query: { sheet: string; rowIndex: number; row: any }) {
try {
await this.connect()
const sheet = await this.client.sheetsByTitle[query.sheet]
const sheet = this.client.sheetsByTitle[query.sheet]
const rows = await sheet.getRows()
const row = rows[query.rowIndex]
if (row) {
@ -384,7 +391,7 @@ class GoogleSheetsIntegration implements DatasourcePlus {
async delete(query: { sheet: string; rowIndex: number }) {
await this.connect()
const sheet = await this.client.sheetsByTitle[query.sheet]
const sheet = this.client.sheetsByTitle[query.sheet]
const rows = await sheet.getRows()
const row = rows[query.rowIndex]
if (row) {

View File

@ -0,0 +1,122 @@
import type { GoogleSpreadsheetWorksheet } from "google-spreadsheet"
jest.mock("google-auth-library")
const { OAuth2Client } = require("google-auth-library")
const setCredentialsMock = jest.fn()
const getAccessTokenMock = jest.fn()
OAuth2Client.mockImplementation(() => {
return {
setCredentials: setCredentialsMock,
getAccessToken: getAccessTokenMock,
}
})
jest.mock("google-spreadsheet")
const { GoogleSpreadsheet } = require("google-spreadsheet")
const sheetsByTitle: { [title: string]: GoogleSpreadsheetWorksheet } = {}
GoogleSpreadsheet.mockImplementation(() => {
return {
useOAuth2Client: jest.fn(),
loadInfo: jest.fn(),
sheetsByTitle,
}
})
import { structures } from "@budibase/backend-core/tests"
import TestConfiguration from "../../tests/utilities/TestConfiguration"
import GoogleSheetsIntegration from "../googlesheets"
import { FieldType, Table, TableSchema } from "../../../../types/src/documents"
describe("Google Sheets Integration", () => {
let integration: any,
config = new TestConfiguration()
beforeEach(async () => {
integration = new GoogleSheetsIntegration.integration({
spreadsheetId: "randomId",
auth: {
appId: "appId",
accessToken: "accessToken",
refreshToken: "refreshToken",
},
})
await config.init()
})
function createBasicTable(name: string, columns: string[]): Table {
return {
name,
schema: {
...columns.reduce((p, c) => {
p[c] = {
name: c,
type: FieldType.STRING,
constraints: {
type: "string",
},
}
return p
}, {} as TableSchema),
},
}
}
function createSheet({
headerValues,
}: {
headerValues: string[]
}): GoogleSpreadsheetWorksheet {
return {
// to ignore the unmapped fields
...({} as any),
loadHeaderRow: jest.fn(),
headerValues,
setHeaderRow: jest.fn(),
}
}
describe("update table", () => {
test("adding a new field will be adding a new header row", async () => {
await config.doInContext(structures.uuid(), async () => {
const tableColumns = ["name", "description", "new field"]
const table = createBasicTable(structures.uuid(), tableColumns)
const sheet = createSheet({ headerValues: ["name", "description"] })
sheetsByTitle[table.name] = sheet
await integration.updateTable(table)
expect(sheet.loadHeaderRow).toBeCalledTimes(1)
expect(sheet.setHeaderRow).toBeCalledTimes(1)
expect(sheet.setHeaderRow).toBeCalledWith(tableColumns)
})
})
test("removing an existing field will not remove the data from the spreadsheet", async () => {
await config.doInContext(structures.uuid(), async () => {
const tableColumns = ["name"]
const table = createBasicTable(structures.uuid(), tableColumns)
const sheet = createSheet({
headerValues: ["name", "description", "location"],
})
sheetsByTitle[table.name] = sheet
await integration.updateTable(table)
expect(sheet.loadHeaderRow).toBeCalledTimes(1)
expect(sheet.setHeaderRow).toBeCalledTimes(1)
expect(sheet.setHeaderRow).toBeCalledWith([
"name",
"description",
"location",
])
// No undefineds are sent
expect((sheet.setHeaderRow as any).mock.calls[0][0]).toHaveLength(3)
})
})
})
})