Skip to content

Commit

Permalink
Merge pull request #12255 from Budibase/fix/budi-7720-user-migration
Browse files Browse the repository at this point in the history
Fix user migration from user table
  • Loading branch information
mike12345567 authored Nov 2, 2023
2 parents e78fdf2 + 00f1d2c commit 87b8ef4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
39 changes: 29 additions & 10 deletions packages/server/src/sdk/app/tables/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
isBBReferenceField,
isRelationshipField,
LinkDocument,
LinkInfo,
RelationshipFieldMetadata,
RelationshipType,
Row,
Expand Down Expand Up @@ -125,7 +126,23 @@ abstract class UserColumnMigrator implements ColumnMigrator {
protected newColumn: BBReferenceFieldMetadata
) {}

abstract updateRow(row: Row, link: LinkDocument): void
abstract updateRow(row: Row, linkInfo: LinkInfo): void

pickUserTableLinkSide(link: LinkDocument): LinkInfo {
if (link.doc1.tableId === InternalTable.USER_METADATA) {
return link.doc1
} else {
return link.doc2
}
}

pickOtherTableLinkSide(link: LinkDocument): LinkInfo {
if (link.doc1.tableId === InternalTable.USER_METADATA) {
return link.doc2
} else {
return link.doc1
}
}

async doMigration(): Promise<MigrationResult> {
let oldTable = cloneDeep(this.table)
Expand All @@ -137,23 +154,25 @@ abstract class UserColumnMigrator implements ColumnMigrator {

let links = await sdk.links.fetchWithDocument(this.table._id!)
for (let link of links) {
const userSide = this.pickUserTableLinkSide(link)
const otherSide = this.pickOtherTableLinkSide(link)
if (
link.doc1.tableId !== this.table._id ||
link.doc1.fieldName !== this.oldColumn.name ||
link.doc2.tableId !== InternalTable.USER_METADATA
otherSide.tableId !== this.table._id ||
otherSide.fieldName !== this.oldColumn.name ||
userSide.tableId !== InternalTable.USER_METADATA
) {
continue
}

let row = rowsById[link.doc1.rowId]
let row = rowsById[otherSide.rowId]
if (!row) {
// This can happen if the row has been deleted but the link hasn't,
// which was a state that was found during the initial testing of this
// feature. Not sure exactly what can cause it, but best to be safe.
continue
}

this.updateRow(row, link)
this.updateRow(row, userSide)
}

let db = context.getAppDB()
Expand All @@ -175,20 +194,20 @@ abstract class UserColumnMigrator implements ColumnMigrator {
}

class SingleUserColumnMigrator extends UserColumnMigrator {
updateRow(row: Row, link: LinkDocument): void {
updateRow(row: Row, linkInfo: LinkInfo): void {
row[this.newColumn.name] = dbCore.getGlobalIDFromUserMetadataID(
link.doc2.rowId
linkInfo.rowId
)
}
}

class MultiUserColumnMigrator extends UserColumnMigrator {
updateRow(row: Row, link: LinkDocument): void {
updateRow(row: Row, linkInfo: LinkInfo): void {
if (!row[this.newColumn.name]) {
row[this.newColumn.name] = []
}
row[this.newColumn.name].push(
dbCore.getGlobalIDFromUserMetadataID(link.doc2.rowId)
dbCore.getGlobalIDFromUserMetadataID(linkInfo.rowId)
)
}
}
18 changes: 8 additions & 10 deletions packages/types/src/documents/app/links.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { Document } from "../document"

export interface LinkInfo {
rowId: string
fieldName: string
tableId: string
}

export interface LinkDocument extends Document {
type: string
doc1: {
rowId: string
fieldName: string
tableId: string
}
doc2: {
rowId: string
fieldName: string
tableId: string
}
doc1: LinkInfo
doc2: LinkInfo
}

export interface LinkDocumentValue {
Expand Down

0 comments on commit 87b8ef4

Please sign in to comment.