Browse Source

feat: sqlite alter table change column

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

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

@ -1552,7 +1552,22 @@ class SqliteClient extends KnexClient {
); );
//downQuery += alterTablePK(args.originalColumns, args.columns, downQuery); //downQuery += alterTablePK(args.originalColumns, args.columns, downQuery);
await this.sqlClient.raw(upQuery); const trx = await this.sqlClient.transaction();
try {
const queries = upQuery.split(';');
for (let i = 0; i < queries.length; i++) {
if (queries[i].trim() !== '') {
await trx.raw(queries[i]);
}
}
await trx.commit();
} catch (e) {
await trx.rollback();
log.ppe(e, _func);
throw e;
}
console.log(upQuery); console.log(upQuery);
@ -1950,30 +1965,43 @@ class SqliteClient extends KnexClient {
const defaultValue = getDefaultValue(n); const defaultValue = getDefaultValue(n);
let shouldSanitize = true; let shouldSanitize = true;
if (change === 2) { if (change === 2) {
query += this.genQuery( let dropFkRestrictionQuery = this.genQuery('PRAGMA foreign_keys = OFF;');
`ALTER TABLE ?? RENAME COLUMN ?? TO ??`,
[t, o.cn, n.cn], let backupOldColumnQuery = this.genQuery(
`ALTER TABLE ?? RENAME COLUMN ?? TO ??;`,
[t, o.cn, `${o.cno}_nc_backup`],
shouldSanitize shouldSanitize
); );
let addNewColumnQuery = ''
addNewColumnQuery += this.genQuery(` ADD ?? ${n.dt}`, [n.cn], shouldSanitize);
addNewColumnQuery += n.dtxp && n.dt !== 'text' ? `(${n.dtxp})` : '';
addNewColumnQuery += n.cdf ? ` DEFAULT ${n.cdf}` : !n.rqd ? ' ' : ` DEFAULT ''`;
addNewColumnQuery += n.rqd ? ` NOT NULL` : ' ';
addNewColumnQuery = this.genQuery(`ALTER TABLE ?? ${addNewColumnQuery};`, [t], shouldSanitize);
let updateNewColumnQuery = this.genQuery(`UPDATE ?? SET ?? = ??;`, [t, n.cn, `${o.cno}_nc_backup`], shouldSanitize);
let dropOldColumnQuery = this.genQuery(`ALTER TABLE ?? DROP COLUMN ??;`, [t, `${o.cno}_nc_backup`], shouldSanitize);
let restoreFkRestrictionQuery = this.genQuery('PRAGMA foreign_keys = ON;');
query = `${dropFkRestrictionQuery}${backupOldColumnQuery}${addNewColumnQuery}${updateNewColumnQuery}${dropOldColumnQuery}${restoreFkRestrictionQuery}`;
} 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);
query += n.dtxp && n.dt !== 'text' ? `(${n.dtxp})` : ''; query += n.dtxp && n.dt !== 'text' ? `(${n.dtxp})` : '';
query += n.cdf query += n.cdf
? n.cdf.includes(',') ? ` DEFAULT ${n.cdf}`
? ` DEFAULT ('${n.cdf}')` : !n.rqd ? ' ' : ` DEFAULT ''`;
: ` DEFAULT ${n.cdf}`
: ' ';
query += n.rqd ? ` NOT NULL` : ' '; query += n.rqd ? ` NOT NULL` : ' ';
} else if (change === 1) { } else if (change === 1) {
shouldSanitize = true; shouldSanitize = true;
query += this.genQuery(` ADD ?? ${n.dt}`, [n.cn], shouldSanitize); query += this.genQuery(` ADD ?? ${n.dt}`, [n.cn], shouldSanitize);
query += n.dtxp && n.dt !== 'text' ? `(${n.dtxp})` : ''; query += n.dtxp && n.dt !== 'text' ? `(${n.dtxp})` : '';
query += n.cdf query += n.cdf
? n.cdf.includes(',') ? ` DEFAULT ${n.cdf}`
? ` DEFAULT ('${n.cdf}')` : !n.rqd ? ' ' : ` DEFAULT ''`;
: ` DEFAULT ${n.cdf}`
: ' ';
query += n.rqd ? ` NOT NULL` : ' '; query += n.rqd ? ` NOT NULL` : ' ';
query = this.genQuery(`ALTER TABLE ?? ${query};`, [t], shouldSanitize); query = this.genQuery(`ALTER TABLE ?? ${query};`, [t], shouldSanitize);
} else { } else {

Loading…
Cancel
Save