query-interface.js
6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'use strict';
const sequelizeErrors = require('../../errors');
const QueryTypes = require('../../query-types');
const { QueryInterface } = require('../abstract/query-interface');
const { cloneDeep } = require('../../utils');
/**
* The interface that Sequelize uses to talk with SQLite database
*/
class SQLiteQueryInterface extends QueryInterface {
/**
* A wrapper that fixes SQLite's inability to remove columns from existing tables.
* It will create a backup of the table, drop the table afterwards and create a
* new table with the same name but without the obsolete column.
*
* @override
*/
async removeColumn(tableName, attributeName, options) {
options = options || {};
const fields = await this.describeTable(tableName, options);
delete fields[attributeName];
const sql = this.queryGenerator.removeColumnQuery(tableName, fields);
const subQueries = sql.split(';').filter(q => q !== '');
for (const subQuery of subQueries) await this.sequelize.query(`${subQuery};`, { raw: true, ...options });
}
/**
* A wrapper that fixes SQLite's inability to change columns from existing tables.
* It will create a backup of the table, drop the table afterwards and create a
* new table with the same name but with a modified version of the respective column.
*
* @override
*/
async changeColumn(tableName, attributeName, dataTypeOrOptions, options) {
options = options || {};
const fields = await this.describeTable(tableName, options);
fields[attributeName] = this.normalizeAttribute(dataTypeOrOptions);
const sql = this.queryGenerator.removeColumnQuery(tableName, fields);
const subQueries = sql.split(';').filter(q => q !== '');
for (const subQuery of subQueries) await this.sequelize.query(`${subQuery};`, { raw: true, ...options });
}
/**
* A wrapper that fixes SQLite's inability to rename columns from existing tables.
* It will create a backup of the table, drop the table afterwards and create a
* new table with the same name but with a renamed version of the respective column.
*
* @override
*/
async renameColumn(tableName, attrNameBefore, attrNameAfter, options) {
options = options || {};
const fields = await this.assertTableHasColumn(tableName, attrNameBefore, options);
fields[attrNameAfter] = { ...fields[attrNameBefore] };
delete fields[attrNameBefore];
const sql = this.queryGenerator.renameColumnQuery(tableName, attrNameBefore, attrNameAfter, fields);
const subQueries = sql.split(';').filter(q => q !== '');
for (const subQuery of subQueries) await this.sequelize.query(`${subQuery};`, { raw: true, ...options });
}
/**
* @override
*/
async removeConstraint(tableName, constraintName, options) {
let createTableSql;
const constraints = await this.showConstraint(tableName, constraintName);
// sqlite can't show only one constraint, so we find here the one to remove
const constraint = constraints.find(constaint => constaint.constraintName === constraintName);
if (!constraint) {
throw new sequelizeErrors.UnknownConstraintError({
message: `Constraint ${constraintName} on table ${tableName} does not exist`,
constraint: constraintName,
table: tableName
});
}
createTableSql = constraint.sql;
constraint.constraintName = this.queryGenerator.quoteIdentifier(constraint.constraintName);
let constraintSnippet = `, CONSTRAINT ${constraint.constraintName} ${constraint.constraintType} ${constraint.constraintCondition}`;
if (constraint.constraintType === 'FOREIGN KEY') {
const referenceTableName = this.queryGenerator.quoteTable(constraint.referenceTableName);
constraint.referenceTableKeys = constraint.referenceTableKeys.map(columnName => this.queryGenerator.quoteIdentifier(columnName));
const referenceTableKeys = constraint.referenceTableKeys.join(', ');
constraintSnippet += ` REFERENCES ${referenceTableName} (${referenceTableKeys})`;
constraintSnippet += ` ON UPDATE ${constraint.updateAction}`;
constraintSnippet += ` ON DELETE ${constraint.deleteAction}`;
}
createTableSql = createTableSql.replace(constraintSnippet, '');
createTableSql += ';';
const fields = await this.describeTable(tableName, options);
const sql = this.queryGenerator._alterConstraintQuery(tableName, fields, createTableSql);
const subQueries = sql.split(';').filter(q => q !== '');
for (const subQuery of subQueries) await this.sequelize.query(`${subQuery};`, { raw: true, ...options });
}
/**
* @override
*/
async addConstraint(tableName, options) {
if (!options.fields) {
throw new Error('Fields must be specified through options.fields');
}
if (!options.type) {
throw new Error('Constraint type must be specified through options.type');
}
options = cloneDeep(options);
const constraintSnippet = this.queryGenerator.getConstraintSnippet(tableName, options);
const describeCreateTableSql = this.queryGenerator.describeCreateTableQuery(tableName);
const constraints = await this.sequelize.query(describeCreateTableSql, { ...options, type: QueryTypes.SELECT, raw: true });
let sql = constraints[0].sql;
const index = sql.length - 1;
//Replace ending ')' with constraint snippet - Simulates String.replaceAt
//http://stackoverflow.com/questions/1431094
const createTableSql = `${sql.substr(0, index)}, ${constraintSnippet})${sql.substr(index + 1)};`;
const fields = await this.describeTable(tableName, options);
sql = this.queryGenerator._alterConstraintQuery(tableName, fields, createTableSql);
const subQueries = sql.split(';').filter(q => q !== '');
for (const subQuery of subQueries) await this.sequelize.query(`${subQuery};`, { raw: true, ...options });
}
/**
* @override
*/
async getForeignKeyReferencesForTable(tableName, options) {
const database = this.sequelize.config.database;
const query = this.queryGenerator.getForeignKeysQuery(tableName, database);
const result = await this.sequelize.query(query, options);
return result.map(row => ({
tableName,
columnName: row.from,
referencedTableName: row.table,
referencedColumnName: row.to,
tableCatalog: database,
referencedTableCatalog: database
}));
}
/**
* @override
*/
async dropAllTables(options) {
options = options || {};
const skip = options.skip || [];
const tableNames = await this.showAllTables(options);
await this.sequelize.query('PRAGMA foreign_keys = OFF', options);
await this._dropAllTables(tableNames, skip, options);
await this.sequelize.query('PRAGMA foreign_keys = ON', options);
}
}
exports.SQLiteQueryInterface = SQLiteQueryInterface;