2016-03-02 17:07:05 +01:00
|
|
|
// SQLite3_DDL
|
|
|
|
//
|
|
|
|
// All of the SQLite3 specific DDL helpers for renaming/dropping
|
|
|
|
// columns and changing datatypes.
|
|
|
|
// -------
|
|
|
|
|
2020-04-18 20:41:23 +03:00
|
|
|
const find = require('lodash/find');
|
|
|
|
const fromPairs = require('lodash/fromPairs');
|
|
|
|
const invert = require('lodash/invert');
|
|
|
|
const isEmpty = require('lodash/isEmpty');
|
|
|
|
const negate = require('lodash/negate');
|
|
|
|
const omit = require('lodash/omit');
|
|
|
|
const uniqueId = require('lodash/uniqueId');
|
2020-12-26 12:10:40 -05:00
|
|
|
const { COMMA_NO_PAREN_REGEX } = require('../../../constants');
|
2021-01-01 19:42:19 +02:00
|
|
|
const {
|
|
|
|
createTempTable,
|
|
|
|
copyData,
|
|
|
|
dropTempTable,
|
|
|
|
dropOriginal,
|
|
|
|
reinsertData,
|
|
|
|
renameTable,
|
|
|
|
} = require('./internal/sqlite-ddl-operations');
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// So altering the schema in SQLite3 is a major pain.
|
|
|
|
// We have our own object to deal with the renaming and altering the types
|
|
|
|
// for sqlite3 things.
|
2021-01-01 19:42:19 +02:00
|
|
|
class SQLite3_DDL {
|
|
|
|
constructor(client, tableCompiler, pragma, connection) {
|
|
|
|
this.client = client;
|
|
|
|
this.tableCompiler = tableCompiler;
|
|
|
|
this.pragma = pragma;
|
|
|
|
this.tableNameRaw = this.tableCompiler.tableNameRaw;
|
|
|
|
this.alteredName = uniqueId('_knex_temp_alter');
|
|
|
|
this.connection = connection;
|
|
|
|
this.formatter =
|
|
|
|
client && client.config && client.config.wrapIdentifier
|
|
|
|
? client.config.wrapIdentifier
|
|
|
|
: (value) => value;
|
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2019-03-13 22:58:59 +01:00
|
|
|
tableName() {
|
|
|
|
return this.formatter(this.tableNameRaw, (value) => value);
|
2021-01-01 19:42:19 +02:00
|
|
|
}
|
2019-03-13 22:58:59 +01:00
|
|
|
|
2021-01-01 19:42:19 +02:00
|
|
|
async getColumn(column) {
|
2019-03-13 22:58:59 +01:00
|
|
|
const currentCol = find(this.pragma, (col) => {
|
|
|
|
return (
|
2019-10-12 02:08:01 +05:30
|
|
|
this.client.wrapIdentifier(col.name).toLowerCase() ===
|
|
|
|
this.client.wrapIdentifier(column).toLowerCase()
|
2019-03-13 22:58:59 +01:00
|
|
|
);
|
|
|
|
});
|
2018-07-09 08:10:34 -04:00
|
|
|
if (!currentCol)
|
|
|
|
throw new Error(
|
2019-03-13 22:58:59 +01:00
|
|
|
`The column ${column} is not in the ${this.tableName()} table`
|
2018-07-09 08:10:34 -04:00
|
|
|
);
|
2016-03-02 17:07:05 +01:00
|
|
|
return currentCol;
|
2021-01-01 19:42:19 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
getTableSql() {
|
2019-03-13 22:58:59 +01:00
|
|
|
this.trx.disableProcessing();
|
|
|
|
return this.trx
|
|
|
|
.raw(
|
|
|
|
`SELECT name, sql FROM sqlite_master WHERE type="table" AND name="${this.tableName()}"`
|
|
|
|
)
|
|
|
|
.then((result) => {
|
|
|
|
this.trx.enableProcessing();
|
|
|
|
return result;
|
|
|
|
});
|
2021-01-01 19:42:19 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2021-01-01 19:42:19 +02:00
|
|
|
async renameTable() {
|
|
|
|
return this.trx.raw(renameTable(this.tableName(), this.alteredName));
|
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
dropOriginal() {
|
2021-01-01 19:42:19 +02:00
|
|
|
return this.trx.raw(dropOriginal(this.tableName()));
|
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
dropTempTable() {
|
2021-01-01 19:42:19 +02:00
|
|
|
return this.trx.raw(dropTempTable(this.alteredName));
|
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
copyData() {
|
2021-01-01 19:42:19 +02:00
|
|
|
return copyData(this.trx, this.tableName(), this.alteredName);
|
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
reinsertData(iterator) {
|
2021-01-01 19:42:19 +02:00
|
|
|
return reinsertData(this.trx, iterator, this.tableName(), this.alteredName);
|
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
createTempTable(createTable) {
|
2019-10-16 00:03:35 +03:00
|
|
|
return this.trx.raw(
|
2021-01-01 19:42:19 +02:00
|
|
|
createTempTable(createTable, this.tableName(), this.alteredName)
|
2019-10-16 00:03:35 +03:00
|
|
|
);
|
2021-01-01 19:42:19 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
_doReplace(sql, from, to) {
|
2019-10-06 18:27:52 +02:00
|
|
|
const oneLineSql = sql.replace(/\s+/g, ' ');
|
2019-10-12 02:08:01 +05:30
|
|
|
const matched = oneLineSql.match(/^CREATE TABLE\s+(\S+)\s*\((.*)\)/);
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
const tableName = matched[1];
|
|
|
|
const defs = matched[2];
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
if (!defs) {
|
|
|
|
throw new Error('No column definitions in this statement!');
|
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
let parens = 0,
|
|
|
|
args = [],
|
|
|
|
ptr = 0;
|
2016-05-17 01:01:34 +10:00
|
|
|
let i = 0;
|
|
|
|
const x = defs.length;
|
|
|
|
for (i = 0; i < x; i++) {
|
2016-03-02 17:07:05 +01:00
|
|
|
switch (defs[i]) {
|
|
|
|
case '(':
|
|
|
|
parens++;
|
2016-05-17 01:01:34 +10:00
|
|
|
break;
|
2016-03-02 17:07:05 +01:00
|
|
|
case ')':
|
|
|
|
parens--;
|
2016-05-17 01:01:34 +10:00
|
|
|
break;
|
2016-03-02 17:07:05 +01:00
|
|
|
case ',':
|
|
|
|
if (parens === 0) {
|
|
|
|
args.push(defs.slice(ptr, i));
|
|
|
|
ptr = i + 1;
|
|
|
|
}
|
2016-05-17 01:01:34 +10:00
|
|
|
break;
|
2016-03-02 17:07:05 +01:00
|
|
|
case ' ':
|
|
|
|
if (ptr === i) {
|
|
|
|
ptr = i + 1;
|
|
|
|
}
|
2016-05-17 01:01:34 +10:00
|
|
|
break;
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
args.push(defs.slice(ptr, i));
|
|
|
|
|
2019-10-12 02:08:01 +05:30
|
|
|
const fromIdentifier = from.replace(/[`"'[\]]/g, '');
|
2018-10-03 05:02:37 +02:00
|
|
|
|
2019-10-16 00:03:35 +03:00
|
|
|
args = args.map((item) => {
|
2019-10-06 18:27:52 +02:00
|
|
|
let split = item.trim().split(' ');
|
|
|
|
|
2019-10-12 02:08:01 +05:30
|
|
|
// SQLite supports all quoting mechanisms prevalent in all major dialects of SQL
|
|
|
|
// and preserves the original quoting in sqlite_master.
|
|
|
|
//
|
|
|
|
// Also, identifiers are never case sensitive, not even when quoted.
|
2019-10-06 18:27:52 +02:00
|
|
|
//
|
2019-10-12 02:08:01 +05:30
|
|
|
// Ref: https://www.sqlite.org/lang_keywords.html
|
|
|
|
const fromMatchCandidates = [
|
|
|
|
new RegExp(`\`${fromIdentifier}\``, 'i'),
|
|
|
|
new RegExp(`"${fromIdentifier}"`, 'i'),
|
|
|
|
new RegExp(`'${fromIdentifier}'`, 'i'),
|
2019-10-16 00:03:35 +03:00
|
|
|
new RegExp(`\\[${fromIdentifier}\\]`, 'i'),
|
2019-10-12 02:08:01 +05:30
|
|
|
];
|
|
|
|
if (fromIdentifier.match(/^\S+$/)) {
|
|
|
|
fromMatchCandidates.push(new RegExp(`\\b${fromIdentifier}\\b`, 'i'));
|
2019-10-06 18:27:52 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2019-10-12 02:08:01 +05:30
|
|
|
const doesMatchFromIdentifier = (target) =>
|
2020-04-18 20:41:23 +03:00
|
|
|
fromMatchCandidates.some((c) => target.match(c));
|
2019-10-12 02:08:01 +05:30
|
|
|
|
|
|
|
const replaceFromIdentifier = (target) =>
|
2019-10-16 00:03:35 +03:00
|
|
|
fromMatchCandidates.reduce(
|
|
|
|
(result, candidate) => result.replace(candidate, to),
|
|
|
|
target
|
|
|
|
);
|
2019-10-12 02:08:01 +05:30
|
|
|
|
|
|
|
if (doesMatchFromIdentifier(split[0])) {
|
2016-03-02 17:07:05 +01:00
|
|
|
// column definition
|
|
|
|
if (to) {
|
|
|
|
split[0] = to;
|
|
|
|
return split.join(' ');
|
|
|
|
}
|
|
|
|
return ''; // for deletions
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip constraint name
|
2018-07-09 08:10:34 -04:00
|
|
|
const idx = /constraint/i.test(split[0]) ? 2 : 0;
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// primary key and unique constraints have one or more
|
|
|
|
// columns from this table listed between (); replace
|
|
|
|
// one if it matches
|
|
|
|
if (/primary|unique/i.test(split[idx])) {
|
2019-10-16 00:03:35 +03:00
|
|
|
const ret = item.replace(/\(.*\)/, replaceFromIdentifier);
|
2019-10-12 02:08:01 +05:30
|
|
|
// If any member columns are dropped then uniqueness/pk constraint
|
|
|
|
// can not be retained
|
|
|
|
if (ret !== item && isEmpty(to)) return '';
|
|
|
|
return ret;
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// foreign keys have one or more columns from this table
|
|
|
|
// listed between (); replace one if it matches
|
|
|
|
// foreign keys also have a 'references' clause
|
|
|
|
// which may reference THIS table; if it does, replace
|
|
|
|
// column references in that too!
|
|
|
|
if (/foreign/.test(split[idx])) {
|
|
|
|
split = item.split(/ references /i);
|
|
|
|
// the quoted column names save us from having to do anything
|
|
|
|
// other than a straight replace here
|
2019-10-12 02:08:01 +05:30
|
|
|
const replacedKeySpec = replaceFromIdentifier(split[0]);
|
|
|
|
|
|
|
|
if (split[0] !== replacedKeySpec) {
|
|
|
|
// If we are removing one or more columns of a foreign
|
|
|
|
// key, then we should not retain the key at all
|
|
|
|
if (isEmpty(to)) return '';
|
|
|
|
else split[0] = replacedKeySpec;
|
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
if (split[1].slice(0, tableName.length) === tableName) {
|
2019-10-12 02:08:01 +05:30
|
|
|
// self-referential foreign key
|
2019-10-16 00:03:35 +03:00
|
|
|
const replacedKeyTargetSpec = split[1].replace(
|
|
|
|
/\(.*\)/,
|
|
|
|
replaceFromIdentifier
|
|
|
|
);
|
2019-10-12 02:08:01 +05:30
|
|
|
if (split[1] !== replacedKeyTargetSpec) {
|
|
|
|
// If we are removing one or more columns of a foreign
|
|
|
|
// key, then we should not retain the key at all
|
|
|
|
if (isEmpty(to)) return '';
|
|
|
|
else split[1] = replacedKeyTargetSpec;
|
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
return split.join(' references ');
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
});
|
2019-10-12 02:08:01 +05:30
|
|
|
|
|
|
|
args = args.filter(negate(isEmpty));
|
|
|
|
|
|
|
|
if (args.length === 0) {
|
|
|
|
throw new Error('Unable to drop last column from table');
|
|
|
|
}
|
|
|
|
|
2019-10-06 18:27:52 +02:00
|
|
|
return oneLineSql
|
2018-07-09 08:10:34 -04:00
|
|
|
.replace(/\(.*\)/, () => `(${args.join(', ')})`)
|
|
|
|
.replace(/,\s*([,)])/, '$1');
|
2021-01-01 19:42:19 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// Boy, this is quite a method.
|
2021-01-01 19:42:19 +02:00
|
|
|
async renameColumn(from, to) {
|
2018-07-09 08:10:34 -04:00
|
|
|
return this.client.transaction(
|
2019-06-18 00:07:24 +02:00
|
|
|
async (trx) => {
|
2018-07-09 08:10:34 -04:00
|
|
|
this.trx = trx;
|
2019-06-18 00:07:24 +02:00
|
|
|
const column = await this.getColumn(from);
|
|
|
|
const sql = await this.getTableSql(column);
|
|
|
|
const a = this.client.wrapIdentifier(from);
|
|
|
|
const b = this.client.wrapIdentifier(to);
|
|
|
|
const createTable = sql[0];
|
|
|
|
const newSql = this._doReplace(createTable.sql, a, b);
|
|
|
|
if (sql === newSql) {
|
|
|
|
throw new Error('Unable to find the column to change');
|
|
|
|
}
|
2019-10-12 02:08:01 +05:30
|
|
|
|
2019-06-18 00:07:24 +02:00
|
|
|
const { from: mappedFrom, to: mappedTo } = invert(
|
|
|
|
this.client.postProcessResponse(
|
|
|
|
invert({
|
|
|
|
from,
|
|
|
|
to,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2019-10-16 00:03:35 +03:00
|
|
|
return this.reinsertMapped(createTable, newSql, (row) => {
|
|
|
|
row[mappedTo] = row[mappedFrom];
|
|
|
|
return omit(row, mappedFrom);
|
|
|
|
});
|
2018-07-09 08:10:34 -04:00
|
|
|
},
|
|
|
|
{ connection: this.connection }
|
|
|
|
);
|
2021-01-01 19:42:19 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2021-01-01 19:42:19 +02:00
|
|
|
async dropColumn(columns) {
|
2018-07-09 08:10:34 -04:00
|
|
|
return this.client.transaction(
|
|
|
|
(trx) => {
|
|
|
|
this.trx = trx;
|
2019-10-16 00:03:35 +03:00
|
|
|
return Promise.all(columns.map((column) => this.getColumn(column)))
|
|
|
|
.then(() => this.getTableSql())
|
|
|
|
.then((sql) => {
|
2018-07-09 08:10:34 -04:00
|
|
|
const createTable = sql[0];
|
|
|
|
let newSql = createTable.sql;
|
|
|
|
columns.forEach((column) => {
|
|
|
|
const a = this.client.wrapIdentifier(column);
|
|
|
|
newSql = this._doReplace(newSql, a, '');
|
|
|
|
});
|
|
|
|
if (sql === newSql) {
|
|
|
|
throw new Error('Unable to find the column to change');
|
|
|
|
}
|
2019-03-13 22:58:59 +01:00
|
|
|
const mappedColumns = Object.keys(
|
|
|
|
this.client.postProcessResponse(
|
|
|
|
fromPairs(columns.map((column) => [column, column]))
|
|
|
|
)
|
|
|
|
);
|
2019-10-16 00:03:35 +03:00
|
|
|
return this.reinsertMapped(createTable, newSql, (row) =>
|
|
|
|
omit(row, ...mappedColumns)
|
|
|
|
);
|
2018-07-09 08:10:34 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
{ connection: this.connection }
|
|
|
|
);
|
2021-01-01 19:42:19 +02:00
|
|
|
}
|
2019-10-16 00:03:35 +03:00
|
|
|
|
2021-01-01 19:42:19 +02:00
|
|
|
async dropForeign(columns, indexName) {
|
2020-12-08 07:49:41 -05:00
|
|
|
return this.client.transaction(
|
|
|
|
async (trx) => {
|
|
|
|
this.trx = trx;
|
|
|
|
|
|
|
|
const sql = await this.getTableSql();
|
|
|
|
|
|
|
|
const createTable = sql[0];
|
|
|
|
|
|
|
|
const oneLineSql = createTable.sql.replace(/\s+/g, ' ');
|
|
|
|
const matched = oneLineSql.match(/^CREATE TABLE\s+(\S+)\s*\((.*)\)/);
|
|
|
|
|
|
|
|
const defs = matched[2];
|
|
|
|
|
|
|
|
if (!defs) {
|
|
|
|
throw new Error('No column definitions in this statement!');
|
|
|
|
}
|
|
|
|
|
|
|
|
const updatedDefs = defs
|
2020-12-26 12:10:40 -05:00
|
|
|
.split(COMMA_NO_PAREN_REGEX)
|
2020-12-08 07:49:41 -05:00
|
|
|
.map((line) => line.trim())
|
|
|
|
.filter((defLine) => {
|
|
|
|
if (
|
|
|
|
defLine.startsWith('constraint') === false &&
|
|
|
|
defLine.includes('foreign key') === false
|
|
|
|
)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (indexName) {
|
|
|
|
if (defLine.includes(indexName)) return false;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
const matched = defLine.match(/\(`(\S+)`\)/);
|
|
|
|
const columnName = matched[1];
|
|
|
|
|
|
|
|
return columns.includes(columnName) === false;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.join(', ');
|
|
|
|
|
|
|
|
const newSql = oneLineSql.replace(defs, updatedDefs);
|
|
|
|
|
|
|
|
return this.reinsertMapped(createTable, newSql, (row) => {
|
|
|
|
return row;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
{ connection: this.connection }
|
|
|
|
);
|
2021-01-01 19:42:19 +02:00
|
|
|
}
|
2020-12-08 07:49:41 -05:00
|
|
|
|
2021-01-01 19:42:19 +02:00
|
|
|
async dropPrimary(constraintName) {
|
2020-12-26 12:10:40 -05:00
|
|
|
return this.client.transaction(
|
|
|
|
async (trx) => {
|
|
|
|
this.trx = trx;
|
|
|
|
|
|
|
|
const sql = await this.getTableSql();
|
|
|
|
|
|
|
|
const createTable = sql[0];
|
|
|
|
|
|
|
|
const oneLineSql = createTable.sql.replace(/\s+/g, ' ');
|
|
|
|
const matched = oneLineSql.match(/^CREATE TABLE\s+(\S+)\s*\((.*)\)/);
|
|
|
|
|
|
|
|
const defs = matched[2];
|
|
|
|
|
|
|
|
if (!defs) {
|
|
|
|
throw new Error('No column definitions in this statement!');
|
|
|
|
}
|
|
|
|
|
|
|
|
const updatedDefs = defs
|
|
|
|
.split(COMMA_NO_PAREN_REGEX)
|
|
|
|
.map((line) => line.trim())
|
|
|
|
.filter((defLine) => {
|
|
|
|
if (
|
|
|
|
defLine.startsWith('constraint') === false &&
|
|
|
|
defLine.includes('primary key') === false
|
|
|
|
)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (constraintName) {
|
|
|
|
if (defLine.includes(constraintName)) return false;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.join(', ');
|
|
|
|
|
|
|
|
const newSql = oneLineSql.replace(defs, updatedDefs);
|
|
|
|
|
|
|
|
return this.reinsertMapped(createTable, newSql, (row) => {
|
|
|
|
return row;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
{ connection: this.connection }
|
|
|
|
);
|
2021-01-01 19:42:19 +02:00
|
|
|
}
|
2020-12-26 12:10:40 -05:00
|
|
|
|
2021-01-01 19:42:19 +02:00
|
|
|
async primary(columns, constraintName) {
|
2020-12-26 12:10:40 -05:00
|
|
|
return this.client.transaction(
|
|
|
|
async (trx) => {
|
|
|
|
this.trx = trx;
|
|
|
|
|
|
|
|
const tableInfo = (await this.getTableSql())[0];
|
|
|
|
const currentSQL = tableInfo.sql;
|
|
|
|
|
|
|
|
const oneLineSQL = currentSQL.replace(/\s+/g, ' ');
|
|
|
|
const matched = oneLineSQL.match(/^CREATE TABLE\s+(\S+)\s*\((.*)\)/);
|
|
|
|
|
|
|
|
const columnDefinitions = matched[2];
|
|
|
|
|
|
|
|
if (!columnDefinitions) {
|
|
|
|
throw new Error('No column definitions in this statement!');
|
|
|
|
}
|
|
|
|
|
|
|
|
const primaryKeyDef = `primary key(${columns.join(',')})`;
|
|
|
|
const constraintDef = constraintName
|
|
|
|
? `constraint ${constraintName} ${primaryKeyDef}`
|
|
|
|
: primaryKeyDef;
|
|
|
|
|
|
|
|
const newColumnDefinitions = [
|
|
|
|
...columnDefinitions
|
|
|
|
.split(COMMA_NO_PAREN_REGEX)
|
|
|
|
.map((line) => line.trim())
|
|
|
|
.filter((line) => line.startsWith('primary') === false)
|
|
|
|
.map((line) => line.replace(/primary key/i, '')),
|
|
|
|
constraintDef,
|
|
|
|
].join(', ');
|
|
|
|
|
|
|
|
const newSQL = oneLineSQL.replace(
|
|
|
|
columnDefinitions,
|
|
|
|
newColumnDefinitions
|
|
|
|
);
|
|
|
|
|
|
|
|
return this.reinsertMapped(tableInfo, newSQL, (row) => {
|
|
|
|
return row;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
{ connection: this.connection }
|
|
|
|
);
|
2021-01-01 19:42:19 +02:00
|
|
|
}
|
2020-12-26 12:10:40 -05:00
|
|
|
|
2021-01-01 19:42:19 +02:00
|
|
|
async foreign(foreignInfo) {
|
2020-12-26 12:10:40 -05:00
|
|
|
return this.client.transaction(
|
|
|
|
async (trx) => {
|
|
|
|
this.trx = trx;
|
|
|
|
|
|
|
|
const tableInfo = (await this.getTableSql())[0];
|
|
|
|
const currentSQL = tableInfo.sql;
|
|
|
|
|
|
|
|
const oneLineSQL = currentSQL.replace(/\s+/g, ' ');
|
|
|
|
const matched = oneLineSQL.match(/^CREATE TABLE\s+(\S+)\s*\((.*)\)/);
|
|
|
|
|
|
|
|
const columnDefinitions = matched[2];
|
|
|
|
|
|
|
|
if (!columnDefinitions) {
|
|
|
|
throw new Error('No column definitions in this statement!');
|
|
|
|
}
|
|
|
|
|
|
|
|
const newColumnDefinitions = columnDefinitions
|
|
|
|
.split(COMMA_NO_PAREN_REGEX)
|
|
|
|
.map((line) => line.trim());
|
|
|
|
|
|
|
|
let newForeignSQL = '';
|
|
|
|
|
|
|
|
if (foreignInfo.keyName) {
|
|
|
|
newForeignSQL += `CONSTRAINT ${foreignInfo.keyName}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
newForeignSQL += ` FOREIGN KEY (${foreignInfo.column.join(', ')}) `;
|
|
|
|
newForeignSQL += ` REFERENCES ${foreignInfo.inTable} (${foreignInfo.references})`;
|
|
|
|
|
|
|
|
if (foreignInfo.onUpdate) {
|
|
|
|
newForeignSQL += ` ON UPDATE ${foreignInfo.onUpdate}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (foreignInfo.onDelete) {
|
|
|
|
newForeignSQL += ` ON DELETE ${foreignInfo.onUpdate}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
newColumnDefinitions.push(newForeignSQL);
|
|
|
|
|
|
|
|
const newSQL = oneLineSQL.replace(
|
|
|
|
columnDefinitions,
|
|
|
|
newColumnDefinitions.join(', ')
|
|
|
|
);
|
|
|
|
|
|
|
|
return this.reinsertMapped(tableInfo, newSQL, (row) => {
|
|
|
|
return row;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
{ connection: this.connection }
|
|
|
|
);
|
2021-01-01 19:42:19 +02:00
|
|
|
}
|
2020-12-26 12:10:40 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @fixme
|
|
|
|
*
|
|
|
|
* There's a bunch of overlap between renameColumn/dropColumn/dropForeign/primary/foreign.
|
|
|
|
* It'll be helpful to refactor this file heavily to combine/optimize some of these calls
|
|
|
|
*/
|
|
|
|
|
2019-10-16 00:03:35 +03:00
|
|
|
reinsertMapped(createTable, newSql, mapRow) {
|
|
|
|
return Promise.resolve()
|
|
|
|
.then(() => this.createTempTable(createTable))
|
|
|
|
.then(() => this.copyData())
|
|
|
|
.then(() => this.dropOriginal())
|
|
|
|
.then(() => this.trx.raw(newSql))
|
|
|
|
.then(() => this.reinsertData(mapRow))
|
|
|
|
.then(() => this.dropTempTable());
|
2021-01-01 19:42:19 +02:00
|
|
|
}
|
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2019-06-04 00:37:17 +02:00
|
|
|
module.exports = SQLite3_DDL;
|