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:
commit
fcb9f3e116
|
@ -192,13 +192,13 @@
|
||||||
editableColumn.name = originalName
|
editableColumn.name = originalName
|
||||||
}
|
}
|
||||||
|
|
||||||
function deleteColumn() {
|
async function deleteColumn() {
|
||||||
try {
|
try {
|
||||||
editableColumn.name = deleteColName
|
editableColumn.name = deleteColName
|
||||||
if (editableColumn.name === $tables.selected.primaryDisplay) {
|
if (editableColumn.name === $tables.selected.primaryDisplay) {
|
||||||
notifications.error("You cannot delete the display column")
|
notifications.error("You cannot delete the display column")
|
||||||
} else {
|
} else {
|
||||||
tables.deleteField(editableColumn)
|
await tables.deleteField(editableColumn)
|
||||||
notifications.success(`Column ${editableColumn.name} deleted.`)
|
notifications.success(`Column ${editableColumn.name} deleted.`)
|
||||||
confirmDeleteDialog.hide()
|
confirmDeleteDialog.hide()
|
||||||
hide()
|
hide()
|
||||||
|
|
|
@ -172,6 +172,9 @@ module FetchMock {
|
||||||
),
|
),
|
||||||
ok: true,
|
ok: true,
|
||||||
})
|
})
|
||||||
|
} else if (url === "https://www.googleapis.com/oauth2/v4/token") {
|
||||||
|
// any valid response
|
||||||
|
return json({})
|
||||||
} else if (url.includes("failonce.com")) {
|
} else if (url.includes("failonce.com")) {
|
||||||
failCount++
|
failCount++
|
||||||
if (failCount === 1) {
|
if (failCount === 1) {
|
||||||
|
|
|
@ -11,8 +11,8 @@ import { OAuth2Client } from "google-auth-library"
|
||||||
import { buildExternalTableId } from "./utils"
|
import { buildExternalTableId } from "./utils"
|
||||||
import { DataSourceOperation, FieldTypes } from "../constants"
|
import { DataSourceOperation, FieldTypes } from "../constants"
|
||||||
import { GoogleSpreadsheet } from "google-spreadsheet"
|
import { GoogleSpreadsheet } from "google-spreadsheet"
|
||||||
|
import fetch from "node-fetch"
|
||||||
import { configs, HTTPError } from "@budibase/backend-core"
|
import { configs, HTTPError } from "@budibase/backend-core"
|
||||||
const fetch = require("node-fetch")
|
|
||||||
|
|
||||||
interface GoogleSheetsConfig {
|
interface GoogleSheetsConfig {
|
||||||
spreadsheetId: string
|
spreadsheetId: string
|
||||||
|
@ -111,7 +111,7 @@ const SCHEMA: Integration = {
|
||||||
|
|
||||||
class GoogleSheetsIntegration implements DatasourcePlus {
|
class GoogleSheetsIntegration implements DatasourcePlus {
|
||||||
private readonly config: GoogleSheetsConfig
|
private readonly config: GoogleSheetsConfig
|
||||||
private client: any
|
private client: GoogleSpreadsheet
|
||||||
public tables: Record<string, Table> = {}
|
public tables: Record<string, Table> = {}
|
||||||
public schemaErrors: Record<string, string> = {}
|
public schemaErrors: Record<string, string> = {}
|
||||||
|
|
||||||
|
@ -203,7 +203,7 @@ class GoogleSheetsIntegration implements DatasourcePlus {
|
||||||
|
|
||||||
async buildSchema(datasourceId: string) {
|
async buildSchema(datasourceId: string) {
|
||||||
await this.connect()
|
await this.connect()
|
||||||
const sheets = await this.client.sheetsByIndex
|
const sheets = this.client.sheetsByIndex
|
||||||
const tables: Record<string, Table> = {}
|
const tables: Record<string, Table> = {}
|
||||||
for (let sheet of sheets) {
|
for (let sheet of sheets) {
|
||||||
// must fetch rows to determine schema
|
// must fetch rows to determine schema
|
||||||
|
@ -286,7 +286,7 @@ class GoogleSheetsIntegration implements DatasourcePlus {
|
||||||
async updateTable(table?: any) {
|
async updateTable(table?: any) {
|
||||||
try {
|
try {
|
||||||
await this.connect()
|
await this.connect()
|
||||||
const sheet = await this.client.sheetsByTitle[table.name]
|
const sheet = this.client.sheetsByTitle[table.name]
|
||||||
await sheet.loadHeaderRow()
|
await sheet.loadHeaderRow()
|
||||||
|
|
||||||
if (table._rename) {
|
if (table._rename) {
|
||||||
|
@ -300,10 +300,17 @@ class GoogleSheetsIntegration implements DatasourcePlus {
|
||||||
}
|
}
|
||||||
await sheet.setHeaderRow(headers)
|
await sheet.setHeaderRow(headers)
|
||||||
} else {
|
} else {
|
||||||
let newField = Object.keys(table.schema).find(
|
const updatedHeaderValues = [...sheet.headerValues]
|
||||||
|
|
||||||
|
const newField = Object.keys(table.schema).find(
|
||||||
key => !sheet.headerValues.includes(key)
|
key => !sheet.headerValues.includes(key)
|
||||||
)
|
)
|
||||||
await sheet.setHeaderRow([...sheet.headerValues, newField])
|
|
||||||
|
if (newField) {
|
||||||
|
updatedHeaderValues.push(newField)
|
||||||
|
}
|
||||||
|
|
||||||
|
await sheet.setHeaderRow(updatedHeaderValues)
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("Error updating table in google sheets", err)
|
console.error("Error updating table in google sheets", err)
|
||||||
|
@ -314,7 +321,7 @@ class GoogleSheetsIntegration implements DatasourcePlus {
|
||||||
async deleteTable(sheet: any) {
|
async deleteTable(sheet: any) {
|
||||||
try {
|
try {
|
||||||
await this.connect()
|
await this.connect()
|
||||||
const sheetToDelete = await this.client.sheetsByTitle[sheet]
|
const sheetToDelete = this.client.sheetsByTitle[sheet]
|
||||||
return await sheetToDelete.delete()
|
return await sheetToDelete.delete()
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("Error deleting table in google sheets", 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 }) {
|
async create(query: { sheet: string; row: any }) {
|
||||||
try {
|
try {
|
||||||
await this.connect()
|
await this.connect()
|
||||||
const sheet = await this.client.sheetsByTitle[query.sheet]
|
const sheet = this.client.sheetsByTitle[query.sheet]
|
||||||
const rowToInsert =
|
const rowToInsert =
|
||||||
typeof query.row === "string" ? JSON.parse(query.row) : query.row
|
typeof query.row === "string" ? JSON.parse(query.row) : query.row
|
||||||
const row = await sheet.addRow(rowToInsert)
|
const row = await sheet.addRow(rowToInsert)
|
||||||
|
@ -341,7 +348,7 @@ class GoogleSheetsIntegration implements DatasourcePlus {
|
||||||
async read(query: { sheet: string }) {
|
async read(query: { sheet: string }) {
|
||||||
try {
|
try {
|
||||||
await this.connect()
|
await this.connect()
|
||||||
const sheet = await this.client.sheetsByTitle[query.sheet]
|
const sheet = this.client.sheetsByTitle[query.sheet]
|
||||||
const rows = await sheet.getRows()
|
const rows = await sheet.getRows()
|
||||||
const headerValues = sheet.headerValues
|
const headerValues = sheet.headerValues
|
||||||
const response = []
|
const response = []
|
||||||
|
@ -360,7 +367,7 @@ class GoogleSheetsIntegration implements DatasourcePlus {
|
||||||
async update(query: { sheet: string; rowIndex: number; row: any }) {
|
async update(query: { sheet: string; rowIndex: number; row: any }) {
|
||||||
try {
|
try {
|
||||||
await this.connect()
|
await this.connect()
|
||||||
const sheet = await this.client.sheetsByTitle[query.sheet]
|
const sheet = this.client.sheetsByTitle[query.sheet]
|
||||||
const rows = await sheet.getRows()
|
const rows = await sheet.getRows()
|
||||||
const row = rows[query.rowIndex]
|
const row = rows[query.rowIndex]
|
||||||
if (row) {
|
if (row) {
|
||||||
|
@ -384,7 +391,7 @@ class GoogleSheetsIntegration implements DatasourcePlus {
|
||||||
|
|
||||||
async delete(query: { sheet: string; rowIndex: number }) {
|
async delete(query: { sheet: string; rowIndex: number }) {
|
||||||
await this.connect()
|
await this.connect()
|
||||||
const sheet = await this.client.sheetsByTitle[query.sheet]
|
const sheet = this.client.sheetsByTitle[query.sheet]
|
||||||
const rows = await sheet.getRows()
|
const rows = await sheet.getRows()
|
||||||
const row = rows[query.rowIndex]
|
const row = rows[query.rowIndex]
|
||||||
if (row) {
|
if (row) {
|
||||||
|
|
|
@ -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)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
Loading…
Reference in New Issue