Merge pull request #4834 from Budibase/fix/4811

Renaming SQL columns - don't drop old column
This commit is contained in:
Michael Drury 2022-03-08 09:55:30 +00:00 committed by GitHub
commit cfb8040848
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 8 deletions

View File

@ -25,7 +25,8 @@ async function makeTableRequest(
operation, operation,
table, table,
tables, tables,
oldTable = null oldTable = null,
renamed = null
) { ) {
const json = { const json = {
endpoint: { endpoint: {
@ -41,6 +42,9 @@ async function makeTableRequest(
if (oldTable) { if (oldTable) {
json.meta.table = oldTable json.meta.table = oldTable
} }
if (renamed) {
json.meta.renamed = renamed
}
return makeExternalQuery(datasource, json) return makeExternalQuery(datasource, json)
} }
@ -160,6 +164,7 @@ function isRelationshipSetup(column) {
exports.save = async function (ctx) { exports.save = async function (ctx) {
const table = ctx.request.body const table = ctx.request.body
const { _rename: renamed } = table
// can't do this right now // can't do this right now
delete table.dataImport delete table.dataImport
const datasourceId = getDatasourceId(ctx.request.body) const datasourceId = getDatasourceId(ctx.request.body)
@ -241,7 +246,14 @@ exports.save = async function (ctx) {
const operation = oldTable const operation = oldTable
? DataSourceOperation.UPDATE_TABLE ? DataSourceOperation.UPDATE_TABLE
: DataSourceOperation.CREATE_TABLE : DataSourceOperation.CREATE_TABLE
await makeTableRequest(datasource, operation, tableToSave, tables, oldTable) await makeTableRequest(
datasource,
operation,
tableToSave,
tables,
oldTable,
renamed
)
// update any extra tables (like foreign keys in other tables) // update any extra tables (like foreign keys in other tables)
for (let extraTable of extraTablesToUpdate) { for (let extraTable of extraTablesToUpdate) {
const oldExtraTable = oldTables[extraTable.name] const oldExtraTable = oldTables[extraTable.name]
@ -258,6 +270,8 @@ exports.save = async function (ctx) {
) )
} }
// remove the rename prop
delete tableToSave._rename
// store it into couch now for budibase reference // store it into couch now for budibase reference
datasource.entities[tableToSave.name] = tableToSave datasource.entities[tableToSave.name] = tableToSave
await db.put(datasource) await db.put(datasource)

View File

@ -138,6 +138,11 @@ export interface PaginationJson {
page?: string | number page?: string | number
} }
export interface RenameColumn {
old: string
updated: string
}
export interface RelationshipsJson { export interface RelationshipsJson {
through?: string through?: string
from?: string from?: string
@ -166,6 +171,7 @@ export interface QueryJson {
meta?: { meta?: {
table?: Table table?: Table
tables?: Record<string, Table> tables?: Record<string, Table>
renamed: RenameColumn
} }
extra?: { extra?: {
idFilter?: SearchFilters idFilter?: SearchFilters

View File

@ -1,6 +1,10 @@
import { Knex, knex } from "knex" import { Knex, knex } from "knex"
import { Table } from "../../definitions/common" import { Table } from "../../definitions/common"
import { Operation, QueryJson } from "../../definitions/datasource" import {
Operation,
QueryJson,
RenameColumn,
} from "../../definitions/datasource"
import { breakExternalTableId } from "../utils" import { breakExternalTableId } from "../utils"
import SchemaBuilder = Knex.SchemaBuilder import SchemaBuilder = Knex.SchemaBuilder
import CreateTableBuilder = Knex.CreateTableBuilder import CreateTableBuilder = Knex.CreateTableBuilder
@ -10,7 +14,8 @@ function generateSchema(
schema: CreateTableBuilder, schema: CreateTableBuilder,
table: Table, table: Table,
tables: Record<string, Table>, tables: Record<string, Table>,
oldTable: null | Table = null oldTable: null | Table = null,
renamed?: RenameColumn
) { ) {
let primaryKey = table && table.primary ? table.primary[0] : null let primaryKey = table && table.primary ? table.primary[0] : null
const columns = Object.values(table.schema) const columns = Object.values(table.schema)
@ -29,7 +34,11 @@ function generateSchema(
for (let [key, column] of Object.entries(table.schema)) { for (let [key, column] of Object.entries(table.schema)) {
// skip things that are already correct // skip things that are already correct
const oldColumn = oldTable ? oldTable.schema[key] : null const oldColumn = oldTable ? oldTable.schema[key] : null
if ((oldColumn && oldColumn.type) || (primaryKey === key && !isJunction)) { if (
(oldColumn && oldColumn.type) ||
(primaryKey === key && !isJunction) ||
renamed?.updated === key
) {
continue continue
} }
switch (column.type) { switch (column.type) {
@ -81,6 +90,10 @@ function generateSchema(
} }
} }
if (renamed) {
schema.renameColumn(renamed.old, renamed.updated)
}
// need to check if any columns have been deleted // need to check if any columns have been deleted
if (oldTable) { if (oldTable) {
const deletedColumns = Object.entries(oldTable.schema) const deletedColumns = Object.entries(oldTable.schema)
@ -90,6 +103,9 @@ function generateSchema(
) )
.map(([key]) => key) .map(([key]) => key)
deletedColumns.forEach(key => { deletedColumns.forEach(key => {
if (renamed?.old === key) {
return
}
if (oldTable.constrained && oldTable.constrained.indexOf(key) !== -1) { if (oldTable.constrained && oldTable.constrained.indexOf(key) !== -1) {
schema.dropForeign(key) schema.dropForeign(key)
} }
@ -114,10 +130,11 @@ function buildUpdateTable(
knex: SchemaBuilder, knex: SchemaBuilder,
table: Table, table: Table,
tables: Record<string, Table>, tables: Record<string, Table>,
oldTable: Table oldTable: Table,
renamed: RenameColumn
): SchemaBuilder { ): SchemaBuilder {
return knex.alterTable(table.name, schema => { return knex.alterTable(table.name, schema => {
generateSchema(schema, table, tables, oldTable) generateSchema(schema, table, tables, oldTable, renamed)
}) })
} }
@ -167,7 +184,8 @@ class SqlTableQueryBuilder {
client, client,
json.table, json.table,
json.meta.tables, json.meta.tables,
json.meta.table json.meta.table,
json.meta.renamed
) )
break break
case Operation.DELETE_TABLE: case Operation.DELETE_TABLE: