Browse Source

fix: handle foreign key and view restrictions

Signed-off-by: mertmit <mertmit99@gmail.com>
pull/3668/head
mertmit 2 years ago
parent
commit
9b32963439
  1. 21
      packages/nocodb/src/lib/db/sql-client/lib/sqlite/SqliteClient.ts

21
packages/nocodb/src/lib/db/sql-client/lib/sqlite/SqliteClient.ts

@ -7,6 +7,9 @@ import Result from '../../../util/Result';
import queries from './sqlite.queries'; import queries from './sqlite.queries';
import lodash from 'lodash'; import lodash from 'lodash';
import _ from 'lodash'; import _ from 'lodash';
import { customAlphabet } from 'nanoid';
const nanoid = customAlphabet('1234567890abcdefghijklmnopqrstuvwxyz_', 6);
const log = new Debug('SqliteClient'); const log = new Debug('SqliteClient');
@ -1546,6 +1549,9 @@ class SqliteClient extends KnexClient {
const pkQuery = this.alterTablePK(args.columns, args.originalColumns, upQuery); const pkQuery = this.alterTablePK(args.columns, args.originalColumns, upQuery);
await this.sqlClient.raw('PRAGMA foreign_keys = OFF;');
await this.sqlClient.raw('PRAGMA legacy_alter_table = ON;');
const trx = await this.sqlClient.transaction(); const trx = await this.sqlClient.transaction();
try { try {
@ -1577,6 +1583,9 @@ class SqliteClient extends KnexClient {
await trx.rollback(); await trx.rollback();
log.ppe(e, _func); log.ppe(e, _func);
throw e; throw e;
} finally {
await this.sqlClient.raw('PRAGMA foreign_keys = ON;');
await this.sqlClient.raw('PRAGMA legacy_alter_table = OFF;');
} }
console.log(upQuery); console.log(upQuery);
@ -1986,11 +1995,11 @@ class SqliteClient extends KnexClient {
const defaultValue = getDefaultValue(n); const defaultValue = getDefaultValue(n);
let shouldSanitize = true; let shouldSanitize = true;
if (change === 2) { if (change === 2) {
let dropFkRestrictionQuery = this.genQuery('PRAGMA foreign_keys = OFF;'); const suffix = nanoid();
let backupOldColumnQuery = this.genQuery( let backupOldColumnQuery = this.genQuery(
`ALTER TABLE ?? RENAME COLUMN ?? TO ??;`, `ALTER TABLE ?? RENAME COLUMN ?? TO ??;`,
[t, o.cn, `${o.cno}_nc_backup`], [t, o.cn, `${o.cno}_nc_${suffix}`],
shouldSanitize shouldSanitize
); );
@ -2001,13 +2010,11 @@ class SqliteClient extends KnexClient {
addNewColumnQuery += n.rqd ? ` NOT NULL` : ' '; addNewColumnQuery += n.rqd ? ` NOT NULL` : ' ';
addNewColumnQuery = this.genQuery(`ALTER TABLE ?? ${addNewColumnQuery};`, [t], shouldSanitize); addNewColumnQuery = this.genQuery(`ALTER TABLE ?? ${addNewColumnQuery};`, [t], shouldSanitize);
let updateNewColumnQuery = this.genQuery(`UPDATE ?? SET ?? = ??;`, [t, n.cn, `${o.cno}_nc_backup`], shouldSanitize); let updateNewColumnQuery = this.genQuery(`UPDATE ?? SET ?? = ??;`, [t, n.cn, `${o.cno}_nc_${suffix}`], shouldSanitize);
let dropOldColumnQuery = this.genQuery(`ALTER TABLE ?? DROP COLUMN ??;`, [t, `${o.cno}_nc_backup`], shouldSanitize);
let restoreFkRestrictionQuery = this.genQuery('PRAGMA foreign_keys = ON;'); let dropOldColumnQuery = this.genQuery(`ALTER TABLE ?? DROP COLUMN ??;`, [t, `${o.cno}_nc_${suffix}`], shouldSanitize);
query = `${dropFkRestrictionQuery}${backupOldColumnQuery}${addNewColumnQuery}${updateNewColumnQuery}${dropOldColumnQuery}${restoreFkRestrictionQuery}`; query = `${backupOldColumnQuery}${addNewColumnQuery}${updateNewColumnQuery}${dropOldColumnQuery}`;
} else if (change === 0) { } else if (change === 0) {
query = existingQuery ? ',' : ''; query = existingQuery ? ',' : '';
query += this.genQuery(`?? ${n.dt}`, [n.cn], shouldSanitize); query += this.genQuery(`?? ${n.dt}`, [n.cn], shouldSanitize);

Loading…
Cancel
Save