From a701933f48ad5f4f141cd85d18f9f86938cbe410 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Mon, 23 Oct 2023 17:57:25 +0100 Subject: [PATCH 01/20] Frontend changes for the user column migration work. --- packages/frontend-core/src/api/tables.js | 9 +++++++ .../components/grid/cells/HeaderCell.svelte | 24 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/packages/frontend-core/src/api/tables.js b/packages/frontend-core/src/api/tables.js index a08e35d3d8..34d2371e1a 100644 --- a/packages/frontend-core/src/api/tables.js +++ b/packages/frontend-core/src/api/tables.js @@ -140,4 +140,13 @@ export const buildTableEndpoints = API => ({ }, }) }, + migrateColumn: async ({ tableId, oldColumn, newColumn }) => { + return await API.post({ + url: `/api/tables/${tableId}/migrate`, + body: { + oldColumn, + newColumn, + }, + }) + }, }) diff --git a/packages/frontend-core/src/components/grid/cells/HeaderCell.svelte b/packages/frontend-core/src/components/grid/cells/HeaderCell.svelte index d6cbcb582d..01c759a15c 100644 --- a/packages/frontend-core/src/components/grid/cells/HeaderCell.svelte +++ b/packages/frontend-core/src/components/grid/cells/HeaderCell.svelte @@ -10,6 +10,7 @@ export let orderable = true const { + API, reorder, isReordering, isResizing, @@ -114,6 +115,24 @@ open = false } + const migrateUserColumn = async () => { + let subtype = "users" + if (column.schema.relationshipType === "one-to-many") { + subtype = "user" + } + + await API.migrateColumn({ + tableId: $definition._id, + oldColumn: column.schema, + newColumn: { + name: `${column.schema.name} migrated`, + type: "bb_reference", + subtype, + }, + }) + open = false + } + const duplicateColumn = async () => { open = false @@ -262,6 +281,11 @@ > Hide column + {#if column.schema.type === "link" && column.schema.tableId === "ta_users"} + + Migrate to user column + + {/if} {/if} From 2f0a40e9bb8c8e8893517d6dfcca1433ff088173 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Tue, 24 Oct 2023 15:18:46 +0100 Subject: [PATCH 02/20] Introduce modal to show warning to users, and toast to show success. --- .../backend/DataTable/TableDataTable.svelte | 6 +++ .../components/grid/cells/HeaderCell.svelte | 42 +++++++++---------- .../grid/controls/MigrationModal.svelte | 42 +++++++++++++++++++ 3 files changed, 69 insertions(+), 21 deletions(-) create mode 100644 packages/frontend-core/src/components/grid/controls/MigrationModal.svelte diff --git a/packages/builder/src/components/backend/DataTable/TableDataTable.svelte b/packages/builder/src/components/backend/DataTable/TableDataTable.svelte index 5fee849afb..22deacbe03 100644 --- a/packages/builder/src/components/backend/DataTable/TableDataTable.svelte +++ b/packages/builder/src/components/backend/DataTable/TableDataTable.svelte @@ -53,6 +53,11 @@ await datasources.fetch() } } + + const refreshDefinitions = async () => { + await tables.fetch() + await datasources.fetch() + }
@@ -66,6 +71,7 @@ schemaOverrides={isUsersTable ? userSchemaOverrides : null} showAvatars={false} on:updatedatasource={handleGridTableUpdate} + on:refreshdefinitions={refreshDefinitions} > {#if isUsersTable && $store.features.disableUserMetadata} diff --git a/packages/frontend-core/src/components/grid/cells/HeaderCell.svelte b/packages/frontend-core/src/components/grid/cells/HeaderCell.svelte index 01c759a15c..9b05f9ef79 100644 --- a/packages/frontend-core/src/components/grid/cells/HeaderCell.svelte +++ b/packages/frontend-core/src/components/grid/cells/HeaderCell.svelte @@ -1,9 +1,17 @@ + + + +
Hide column - {#if column.schema.type === "link" && column.schema.tableId === "ta_users"} - + {#if $config.canEditColumns && column.schema.type === "link" && column.schema.tableId === "ta_users"} + Migrate to user column {/if} diff --git a/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte b/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte new file mode 100644 index 0000000000..b262f93797 --- /dev/null +++ b/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte @@ -0,0 +1,42 @@ + + + + TODO: copy here + From d3670ddf21a43552aee52602c3a40acd7eea7f38 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Tue, 24 Oct 2023 17:22:49 +0100 Subject: [PATCH 03/20] Add an input to allow the user to choose the new column name. --- .../backend/DataTable/TableDataTable.svelte | 1 + .../grid/controls/MigrationModal.svelte | 22 ++++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/packages/builder/src/components/backend/DataTable/TableDataTable.svelte b/packages/builder/src/components/backend/DataTable/TableDataTable.svelte index 22deacbe03..40bcf2b74e 100644 --- a/packages/builder/src/components/backend/DataTable/TableDataTable.svelte +++ b/packages/builder/src/components/backend/DataTable/TableDataTable.svelte @@ -55,6 +55,7 @@ } const refreshDefinitions = async () => { + console.log("woot") await tables.fetch() await datasources.fetch() } diff --git a/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte b/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte index b262f93797..e329038217 100644 --- a/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte +++ b/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte @@ -1,11 +1,18 @@
@@ -71,7 +66,6 @@ schemaOverrides={isUsersTable ? userSchemaOverrides : null} showAvatars={false} on:updatedatasource={handleGridTableUpdate} - on:refreshdefinitions={refreshDefinitions} > {#if isUsersTable && $store.features.disableUserMetadata} diff --git a/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte b/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte index ee7fd7bfbe..111ff86170 100644 --- a/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte +++ b/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte @@ -45,7 +45,6 @@ notifications.error(`Failed to migrate: ${e.message}`) } await rows.actions.refreshData() - dispatch("refreshdefintions") } From 7acce7b7c0b0a8ca4363263a8d728007c0eecfb8 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 25 Oct 2023 16:01:08 +0100 Subject: [PATCH 14/20] Remove unused dispatch import. --- .../src/components/grid/controls/MigrationModal.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte b/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte index 111ff86170..ec2972a03e 100644 --- a/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte +++ b/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte @@ -8,7 +8,7 @@ import { getContext } from "svelte" import { ValidColumnNameRegex } from "@budibase/shared-core" - const { API, dispatch, definition, rows } = getContext("grid") + const { API, definition, rows } = getContext("grid") export let column From 2c5dd99da202a45dfb97a5cb87351862ecc3627e Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 25 Oct 2023 16:37:15 +0100 Subject: [PATCH 15/20] Use FieldSubtype enum instead of raw strings. --- .../src/components/grid/controls/MigrationModal.svelte | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte b/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte index ec2972a03e..0f173da8b9 100644 --- a/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte +++ b/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte @@ -7,6 +7,7 @@ } from "@budibase/bbui" import { getContext } from "svelte" import { ValidColumnNameRegex } from "@budibase/shared-core" + import { FieldSubtype } from "@budibase/types" const { API, definition, rows } = getContext("grid") @@ -25,9 +26,9 @@ } const migrateUserColumn = async () => { - let subtype = "users" + let subtype = FieldSubtype.USERS if (column.schema.relationshipType === "one-to-many") { - subtype = "user" + subtype = FieldSubtype.USER } try { From ef84e96f98c7a38a6145fb47beee4bdbf5237a73 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 25 Oct 2023 16:38:14 +0100 Subject: [PATCH 16/20] Use RelationshipType enum instead of raw string. --- .../src/components/grid/controls/MigrationModal.svelte | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte b/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte index 0f173da8b9..c6a49c6a48 100644 --- a/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte +++ b/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte @@ -7,7 +7,7 @@ } from "@budibase/bbui" import { getContext } from "svelte" import { ValidColumnNameRegex } from "@budibase/shared-core" - import { FieldSubtype } from "@budibase/types" + import { FieldSubtype, RelationshipType } from "@budibase/types" const { API, definition, rows } = getContext("grid") @@ -27,7 +27,7 @@ const migrateUserColumn = async () => { let subtype = FieldSubtype.USERS - if (column.schema.relationshipType === "one-to-many") { + if (column.schema.relationshipType === RelationshipType.ONE_TO_MANY) { subtype = FieldSubtype.USER } From c5097487e22438a0e4fbc961f2cb422c07329a00 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 25 Oct 2023 16:38:55 +0100 Subject: [PATCH 17/20] Use FieldType constant instead of raw string. --- .../src/components/grid/controls/MigrationModal.svelte | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte b/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte index c6a49c6a48..73ea32408b 100644 --- a/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte +++ b/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte @@ -7,7 +7,7 @@ } from "@budibase/bbui" import { getContext } from "svelte" import { ValidColumnNameRegex } from "@budibase/shared-core" - import { FieldSubtype, RelationshipType } from "@budibase/types" + import { FieldSubtype, FieldType, RelationshipType } from "@budibase/types" const { API, definition, rows } = getContext("grid") @@ -37,7 +37,7 @@ oldColumn: column.schema, newColumn: { name: newColumnName, - type: "bb_reference", + type: FieldType.BB_REFERENCE, subtype, }, }) From e03b1be9d1b2c7c3e8c3ded8f0b3e304a23446bc Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 25 Oct 2023 16:41:37 +0100 Subject: [PATCH 18/20] Make sure new column name cannot be the same as an existing column name. --- .../src/components/grid/controls/MigrationModal.svelte | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte b/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte index 73ea32408b..1957c3259f 100644 --- a/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte +++ b/packages/frontend-core/src/components/grid/controls/MigrationModal.svelte @@ -17,8 +17,8 @@ $: error = checkNewColumnName(newColumnName) const checkNewColumnName = newColumnName => { - if (column.schema.name === newColumnName) { - return "New column name can't be the same as the existing column name." + if (newColumnName in $definition.schema) { + return "New column name can't be the same as an existing column name." } if (newColumnName.match(ValidColumnNameRegex) === null) { return "Illegal character; must be alpha-numeric." From 4a00649f7f7c01d1280a08ff35cedd4963779021 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 25 Oct 2023 16:46:14 +0100 Subject: [PATCH 19/20] Simplify the function signature of processInternalTables --- packages/server/src/sdk/app/tables/getters.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/server/src/sdk/app/tables/getters.ts b/packages/server/src/sdk/app/tables/getters.ts index 34cddc8dc7..47da0beb40 100644 --- a/packages/server/src/sdk/app/tables/getters.ts +++ b/packages/server/src/sdk/app/tables/getters.ts @@ -19,8 +19,8 @@ import { import datasources from "../datasources" import sdk from "../../../sdk" -function processInternalTables(docs: AllDocsResponse): Table[] { - return docs.rows.map(tableDoc => processInternalTable(tableDoc.doc)) +function processInternalTables(tables: Table[]): Table[] { + return tables.map(processInternalTable) } export function processInternalTable(table: Table): Table { @@ -40,7 +40,7 @@ export async function getAllInternalTables(db?: Database): Promise { include_docs: true, }) ) - return processInternalTables(internalTables) + return processInternalTables(internalTables.rows.map(row => row.doc!)) } async function getAllExternalTables(): Promise { @@ -110,7 +110,9 @@ export async function getTables(tableIds: string[]): Promise { const internalTableDocs = await db.allDocs( getMultiIDParams(internalTableIds) ) - tables = tables.concat(processInternalTables(internalTableDocs)) + tables = tables.concat( + processInternalTables(internalTableDocs.rows.map(row => row.doc!)) + ) } return tables } From 6c3b535863878c521bb312606218b89fa2f0f787 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 25 Oct 2023 16:49:29 +0100 Subject: [PATCH 20/20] Simplify try-catch in the migrate function. --- packages/server/src/sdk/app/tables/migration.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/server/src/sdk/app/tables/migration.ts b/packages/server/src/sdk/app/tables/migration.ts index b678b64318..293f9184d6 100644 --- a/packages/server/src/sdk/app/tables/migration.ts +++ b/packages/server/src/sdk/app/tables/migration.ts @@ -34,10 +34,8 @@ export async function migrate( table = await sdk.tables.saveTable(table) let migrator = getColumnMigrator(table, oldColumn, newColumn) - let result: MigrationResult - try { - result = await migrator.doMigration() + return await migrator.doMigration() } catch (e) { // If the migration fails then we need to roll back the table schema // change. @@ -45,8 +43,6 @@ export async function migrate( await sdk.tables.saveTable(table) throw e } - - return result } interface ColumnMigrator {