Fixing issue #4811 - when renaming SQL columns use an actual rename alteration rather than dropping the column.

This commit is contained in:
mike12345567 2022-03-07 16:31:35 +00:00
parent e159e4a15e
commit 1357c53ff8
3 changed files with 46 additions and 8 deletions

View File

@ -25,7 +25,8 @@ async function makeTableRequest(
operation,
table,
tables,
oldTable = null
oldTable = null,
renamed = null
) {
const json = {
endpoint: {
@ -41,6 +42,9 @@ async function makeTableRequest(
if (oldTable) {
json.meta.table = oldTable
}
if (renamed) {
json.meta.renamed = renamed
}
return makeExternalQuery(datasource, json)
}
@ -160,6 +164,7 @@ function isRelationshipSetup(column) {
exports.save = async function (ctx) {
const table = ctx.request.body
const { _rename: renamed } = table
// can't do this right now
delete table.dataImport
const datasourceId = getDatasourceId(ctx.request.body)
@ -241,7 +246,14 @@ exports.save = async function (ctx) {
const operation = oldTable
? DataSourceOperation.UPDATE_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)
for (let extraTable of extraTablesToUpdate) {
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
datasource.entities[tableToSave.name] = tableToSave
await db.put(datasource)

View File

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

View File

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