Merge pull request #10917 from Budibase/fix/datasource-409

Fix datasource conflicts
This commit is contained in:
Andrew Kingston 2023-06-23 11:54:15 +01:00 committed by GitHub
commit 007b6d605b
7 changed files with 34 additions and 7 deletions

View File

@ -1,5 +1,5 @@
<script> <script>
import { tables } from "stores/backend" import { datasources, tables } from "stores/backend"
import EditRolesButton from "./buttons/EditRolesButton.svelte" import EditRolesButton from "./buttons/EditRolesButton.svelte"
import { TableNames } from "constants" import { TableNames } from "constants"
import { Grid } from "@budibase/frontend-core" import { Grid } from "@budibase/frontend-core"
@ -26,6 +26,18 @@
$: id = $tables.selected?._id $: id = $tables.selected?._id
$: isUsersTable = id === TableNames.USERS $: isUsersTable = id === TableNames.USERS
$: isInternal = $tables.selected?.type !== "external" $: isInternal = $tables.selected?.type !== "external"
const handleGridTableUpdate = async e => {
tables.replaceTable(id, e.detail)
// We need to refresh datasources when an external table changes.
// Type "external" may exist - sometimes type is "table" and sometimes it
// is "external" - it has different meanings in different endpoints.
// If we check both these then we hopefully catch all external tables.
if (e.detail?.type === "external" || e.detail?.sql) {
await datasources.fetch()
}
}
</script> </script>
<div class="wrapper"> <div class="wrapper">
@ -37,7 +49,7 @@
allowDeleteRows={!isUsersTable} allowDeleteRows={!isUsersTable}
schemaOverrides={isUsersTable ? userSchemaOverrides : null} schemaOverrides={isUsersTable ? userSchemaOverrides : null}
showAvatars={false} showAvatars={false}
on:updatetable={e => tables.replaceTable(id, e.detail)} on:updatetable={handleGridTableUpdate}
> >
<svelte:fragment slot="controls"> <svelte:fragment slot="controls">
{#if isInternal} {#if isInternal}

View File

@ -93,6 +93,7 @@
try { try {
await beforeSave() await beforeSave()
table = await tables.save(newTable) table = await tables.save(newTable)
await datasources.fetch()
await afterSave(table) await afterSave(table)
} catch (e) { } catch (e) {
notifications.error(e) notifications.error(e)

View File

@ -65,6 +65,7 @@
const updatedTable = cloneDeep(table) const updatedTable = cloneDeep(table)
updatedTable.name = updatedName updatedTable.name = updatedName
await tables.save(updatedTable) await tables.save(updatedTable)
await datasources.fetch()
notifications.success("Table renamed successfully") notifications.success("Table renamed successfully")
} }

View File

@ -113,6 +113,10 @@ export function createDatasourcesStore() {
...state, ...state,
list: [...state.list, datasource], list: [...state.list, datasource],
})) }))
// If this is a new datasource then we should refresh the tables list,
// because otherwise we'll never see the new tables
tables.fetch()
} }
// Update existing datasource // Update existing datasource

View File

@ -1,5 +1,4 @@
import { get, writable, derived } from "svelte/store" import { get, writable, derived } from "svelte/store"
import { datasources } from "./"
import { cloneDeep } from "lodash/fp" import { cloneDeep } from "lodash/fp"
import { API } from "api" import { API } from "api"
import { SWITCHABLE_TYPES } from "constants/backend" import { SWITCHABLE_TYPES } from "constants/backend"
@ -63,7 +62,6 @@ export function createTablesStore() {
const savedTable = await API.saveTable(updatedTable) const savedTable = await API.saveTable(updatedTable)
replaceTable(savedTable._id, savedTable) replaceTable(savedTable._id, savedTable)
await datasources.fetch()
select(savedTable._id) select(savedTable._id)
return savedTable return savedTable
} }

View File

@ -90,12 +90,12 @@ export const deriveStores = context => {
// Update local state // Update local state
table.set(newTable) table.set(newTable)
// Update server
await API.saveTable(newTable)
// Broadcast change to external state can be updated, as this change // Broadcast change to external state can be updated, as this change
// will not be received by the builder websocket because we caused it ourselves // will not be received by the builder websocket because we caused it ourselves
dispatch("updatetable", newTable) dispatch("updatetable", newTable)
// Update server
await API.saveTable(newTable)
} }
return { return {

View File

@ -26,6 +26,7 @@ import {
RelationshipTypes, RelationshipTypes,
} from "@budibase/types" } from "@budibase/types"
import sdk from "../../../sdk" import sdk from "../../../sdk"
import { builderSocket } from "../../../websockets"
const { cloneDeep } = require("lodash/fp") const { cloneDeep } = require("lodash/fp")
async function makeTableRequest( async function makeTableRequest(
@ -318,6 +319,11 @@ export async function save(ctx: UserCtx) {
datasource.entities[tableToSave.name] = tableToSave datasource.entities[tableToSave.name] = tableToSave
await db.put(datasource) await db.put(datasource)
// Since tables are stored inside datasources, we need to notify clients
// that the datasource definition changed
const updatedDatasource = await db.get(datasource._id)
builderSocket?.emitDatasourceUpdate(ctx, updatedDatasource)
return tableToSave return tableToSave
} }
@ -344,6 +350,11 @@ export async function destroy(ctx: UserCtx) {
await db.put(datasource) await db.put(datasource)
// Since tables are stored inside datasources, we need to notify clients
// that the datasource definition changed
const updatedDatasource = await db.get(datasource._id)
builderSocket?.emitDatasourceUpdate(ctx, updatedDatasource)
return tableToDelete return tableToDelete
} }