2020-04-18 20:41:23 +03:00
|
|
|
const filter = require('lodash/filter');
|
|
|
|
const values = require('lodash/values');
|
2021-02-26 21:36:46 +01:00
|
|
|
const identity = require('lodash/identity');
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2021-01-01 18:46:16 +02:00
|
|
|
const TableCompiler = require('../../../schema/tablecompiler');
|
2021-02-26 21:36:46 +01:00
|
|
|
const { formatDefault } = require('../../../formatter/formatterUtils');
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2021-01-01 18:46:16 +02:00
|
|
|
class TableCompiler_SQLite3 extends TableCompiler {
|
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new table.
|
|
|
|
createQuery(columns, ifNot) {
|
|
|
|
const createStatement = ifNot
|
|
|
|
? 'create table if not exists '
|
|
|
|
: 'create table ';
|
|
|
|
let sql =
|
|
|
|
createStatement + this.tableName() + ' (' + columns.sql.join(', ');
|
|
|
|
|
|
|
|
// SQLite forces primary keys to be added when the table is initially created
|
|
|
|
// so we will need to check for a primary key commands and add the columns
|
|
|
|
// to the table's declaration here so they can be created on the tables.
|
|
|
|
sql += this.foreignKeys() || '';
|
|
|
|
sql += this.primaryKeys() || '';
|
|
|
|
sql += ')';
|
|
|
|
|
|
|
|
this.pushQuery(sql);
|
|
|
|
}
|
|
|
|
|
2021-02-26 21:36:46 +01:00
|
|
|
addColumns(columns, prefix, colCompilers) {
|
|
|
|
if (prefix === this.alterColumnsPrefix) {
|
|
|
|
const compiler = this;
|
|
|
|
|
|
|
|
const columnsInfo = colCompilers.map((col) => {
|
|
|
|
const name = this.client.customWrapIdentifier(
|
|
|
|
col.getColumnName(),
|
|
|
|
identity,
|
|
|
|
col.columnBuilder.queryContext()
|
|
|
|
);
|
|
|
|
|
|
|
|
const type = col.getColumnType();
|
|
|
|
|
|
|
|
const defaultTo = col.modified['defaultTo']
|
|
|
|
? formatDefault(col.modified['defaultTo'][0], type, this.client)
|
|
|
|
: null;
|
|
|
|
|
|
|
|
const notNull =
|
|
|
|
col.modified['nullable'] && col.modified['nullable'][0] === false;
|
|
|
|
|
|
|
|
return { name, type, defaultTo, notNull };
|
|
|
|
});
|
|
|
|
|
2021-01-01 18:46:16 +02:00
|
|
|
this.pushQuery({
|
2021-02-26 21:36:46 +01:00
|
|
|
sql: `PRAGMA table_info(${this.tableName()})`,
|
2021-03-03 19:50:45 +01:00
|
|
|
output(pragma) {
|
2021-02-26 21:36:46 +01:00
|
|
|
return compiler.client
|
2021-03-03 19:50:45 +01:00
|
|
|
.ddl(compiler, pragma, this.connection)
|
2021-02-26 21:36:46 +01:00
|
|
|
.alterColumn(columnsInfo);
|
|
|
|
},
|
2021-01-01 18:46:16 +02:00
|
|
|
});
|
2021-02-26 21:36:46 +01:00
|
|
|
} else {
|
|
|
|
for (let i = 0, l = columns.sql.length; i < l; i++) {
|
|
|
|
this.pushQuery({
|
|
|
|
sql: `alter table ${this.tableName()} add column ${columns.sql[i]}`,
|
|
|
|
bindings: columns.bindings[i],
|
|
|
|
});
|
|
|
|
}
|
2021-01-01 18:46:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compile a drop unique key command.
|
|
|
|
dropUnique(columns, indexName) {
|
|
|
|
indexName = indexName
|
|
|
|
? this.formatter.wrap(indexName)
|
|
|
|
: this._indexCommand('unique', this.tableNameRaw, columns);
|
|
|
|
this.pushQuery(`drop index ${indexName}`);
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
2021-01-01 18:46:16 +02:00
|
|
|
|
|
|
|
// Compile a drop foreign key command.
|
|
|
|
dropForeign(columns, indexName) {
|
|
|
|
const compiler = this;
|
|
|
|
|
2020-12-26 12:10:40 -05:00
|
|
|
this.pushQuery({
|
|
|
|
sql: `PRAGMA table_info(${this.tableName()})`,
|
|
|
|
output(pragma) {
|
|
|
|
return compiler.client
|
|
|
|
.ddl(compiler, pragma, this.connection)
|
2021-01-01 18:46:16 +02:00
|
|
|
.dropForeign(columns, indexName);
|
2020-12-26 12:10:40 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2021-01-01 18:46:16 +02:00
|
|
|
|
|
|
|
// Compile a drop primary key command.
|
|
|
|
dropPrimary(constraintName) {
|
|
|
|
const compiler = this;
|
2020-12-26 12:10:40 -05:00
|
|
|
|
|
|
|
this.pushQuery({
|
|
|
|
sql: `PRAGMA table_info(${this.tableName()})`,
|
|
|
|
output(pragma) {
|
|
|
|
return compiler.client
|
|
|
|
.ddl(compiler, pragma, this.connection)
|
2021-01-01 18:46:16 +02:00
|
|
|
.dropPrimary(constraintName);
|
2020-12-26 12:10:40 -05:00
|
|
|
},
|
|
|
|
});
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
|
2021-01-01 18:46:16 +02:00
|
|
|
dropIndex(columns, indexName) {
|
|
|
|
indexName = indexName
|
|
|
|
? this.formatter.wrap(indexName)
|
|
|
|
: this._indexCommand('index', this.tableNameRaw, columns);
|
|
|
|
this.pushQuery(`drop index ${indexName}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compile a unique key command.
|
|
|
|
unique(columns, indexName) {
|
|
|
|
indexName = indexName
|
|
|
|
? this.formatter.wrap(indexName)
|
|
|
|
: this._indexCommand('unique', this.tableNameRaw, columns);
|
|
|
|
columns = this.formatter.columnize(columns);
|
|
|
|
this.pushQuery(
|
|
|
|
`create unique index ${indexName} on ${this.tableName()} (${columns})`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compile a plain index key command.
|
|
|
|
index(columns, indexName) {
|
|
|
|
indexName = indexName
|
|
|
|
? this.formatter.wrap(indexName)
|
|
|
|
: this._indexCommand('index', this.tableNameRaw, columns);
|
|
|
|
columns = this.formatter.columnize(columns);
|
|
|
|
this.pushQuery(
|
|
|
|
`create index ${indexName} on ${this.tableName()} (${columns})`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a primary key to an existing table.
|
|
|
|
*
|
|
|
|
* @NOTE The `createQuery` method above handles table creation. Don't do anything regarding table
|
|
|
|
* creation in this method
|
|
|
|
*
|
|
|
|
* @param {string | string[]} columns - Column name(s) to assign as primary keys
|
|
|
|
* @param {string} [constraintName] - Custom name for the PK constraint
|
|
|
|
*/
|
|
|
|
primary(columns, constraintName) {
|
|
|
|
const compiler = this;
|
|
|
|
|
|
|
|
columns = this.formatter.columnize(columns);
|
|
|
|
columns = Array.isArray(columns) ? columns : [columns];
|
|
|
|
|
|
|
|
if (this.method !== 'create' && this.method !== 'createIfNot') {
|
|
|
|
this.pushQuery({
|
|
|
|
sql: `PRAGMA table_info(${this.tableName()})`,
|
|
|
|
output(pragma) {
|
|
|
|
return compiler.client
|
|
|
|
.ddl(compiler, pragma, this.connection)
|
|
|
|
.primary(columns, constraintName);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a foreign key constraint to an existing table
|
|
|
|
*
|
|
|
|
* @NOTE The `createQuery` method above handles foreign key constraints on table creation. Don't do
|
|
|
|
* anything regarding table creation in this method
|
|
|
|
*
|
|
|
|
* @param {object} foreignInfo - Information about the current column foreign setup
|
|
|
|
* @param {string | string[]} [foreignInfo.column] - Column in the current constraint
|
|
|
|
* @param {string | undefined} foreignInfo.keyName - Name of the foreign key constraint
|
|
|
|
* @param {string} foreignInfo.references - What column it references in the other table
|
|
|
|
* @param {string} foreignInfo.inTable - What table is referenced in this constraint
|
|
|
|
* @param {string} [foreignInfo.onUpdate] - What to do on updates
|
|
|
|
* @param {string} [foreignInfo.onDelete] - What to do on deletions
|
|
|
|
*/
|
|
|
|
foreign(foreignInfo) {
|
|
|
|
const compiler = this;
|
|
|
|
|
|
|
|
if (this.method !== 'create' && this.method !== 'createIfNot') {
|
|
|
|
foreignInfo.column = this.formatter.columnize(foreignInfo.column);
|
|
|
|
foreignInfo.column = Array.isArray(foreignInfo.column)
|
|
|
|
? foreignInfo.column
|
|
|
|
: [foreignInfo.column];
|
|
|
|
foreignInfo.inTable = this.formatter.columnize(foreignInfo.inTable);
|
|
|
|
foreignInfo.references = this.formatter.columnize(foreignInfo.references);
|
|
|
|
|
|
|
|
this.pushQuery({
|
|
|
|
sql: `PRAGMA table_info(${this.tableName()})`,
|
2021-01-03 04:10:26 +02:00
|
|
|
statementsProducer(pragma, connection) {
|
2021-01-01 18:46:16 +02:00
|
|
|
return compiler.client
|
2021-01-03 04:10:26 +02:00
|
|
|
.ddl(compiler, pragma, connection)
|
2021-01-01 18:46:16 +02:00
|
|
|
.foreign(foreignInfo);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
primaryKeys() {
|
|
|
|
const pks = filter(this.grouped.alterTable || [], { method: 'primary' });
|
|
|
|
if (pks.length > 0 && pks[0].args.length > 0) {
|
|
|
|
const columns = pks[0].args[0];
|
|
|
|
let constraintName = pks[0].args[1] || '';
|
|
|
|
if (constraintName) {
|
|
|
|
constraintName = ' constraint ' + this.formatter.wrap(constraintName);
|
|
|
|
}
|
|
|
|
return `,${constraintName} primary key (${this.formatter.columnize(
|
|
|
|
columns
|
|
|
|
)})`;
|
2018-10-11 18:14:26 +02:00
|
|
|
}
|
2021-01-01 18:46:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
foreignKeys() {
|
|
|
|
let sql = '';
|
|
|
|
const foreignKeys = filter(this.grouped.alterTable || [], {
|
|
|
|
method: 'foreign',
|
|
|
|
});
|
|
|
|
for (let i = 0, l = foreignKeys.length; i < l; i++) {
|
|
|
|
const foreign = foreignKeys[i].args[0];
|
|
|
|
const column = this.formatter.columnize(foreign.column);
|
|
|
|
const references = this.formatter.columnize(foreign.references);
|
|
|
|
const foreignTable = this.formatter.wrap(foreign.inTable);
|
|
|
|
let constraintName = foreign.keyName || '';
|
|
|
|
if (constraintName) {
|
|
|
|
constraintName = ' constraint ' + this.formatter.wrap(constraintName);
|
|
|
|
}
|
|
|
|
sql += `,${constraintName} foreign key(${column}) references ${foreignTable}(${references})`;
|
|
|
|
if (foreign.onDelete) sql += ` on delete ${foreign.onDelete}`;
|
|
|
|
if (foreign.onUpdate) sql += ` on update ${foreign.onUpdate}`;
|
2018-10-11 18:14:26 +02:00
|
|
|
}
|
2021-01-01 18:46:16 +02:00
|
|
|
return sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
createTableBlock() {
|
|
|
|
return this.getColumns().concat().join(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
renameColumn(from, to) {
|
|
|
|
this.pushQuery({
|
2021-01-03 22:49:12 +01:00
|
|
|
sql: `alter table ${this.tableName()} rename ${this.formatter.wrap(
|
|
|
|
from
|
|
|
|
)} to ${this.formatter.wrap(to)}`,
|
2021-01-01 18:46:16 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
dropColumn() {
|
|
|
|
const compiler = this;
|
|
|
|
const columns = values(arguments);
|
|
|
|
this.pushQuery({
|
|
|
|
sql: `PRAGMA table_info(${this.tableName()})`,
|
|
|
|
output(pragma) {
|
|
|
|
return compiler.client
|
|
|
|
.ddl(compiler, pragma, this.connection)
|
|
|
|
.dropColumn(columns);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2019-06-04 00:37:17 +02:00
|
|
|
module.exports = TableCompiler_SQLite3;
|