knex-migrator 5.4.0 → 5.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/database.js +15 -1
  2. package/package.json +1 -1
package/lib/database.js CHANGED
@@ -180,7 +180,16 @@ exports.drop = function drop(options) {
180
180
  } else if (DatabaseInfo.isSQLite(connection)) {
181
181
  // @NOTE: sqlite3 does not support "DROP DATABASE". We have to drop each table instead.
182
182
  // @NOTE: We cannot just remove the sqlite3 file, because any database connection will get invalid.
183
- return connection.raw(`SELECT name FROM sqlite_master WHERE type='table';`)
183
+ // @NOTE: better-sqlite3 enforces foreign key constraints, so dropping a parent table before
184
+ // its referencing children trips an FK error. Disable foreign keys for the duration of the
185
+ // drop and restore afterwards. SQLite uses a single connection here, so the PRAGMA persists
186
+ // across the drops. The legacy sqlite3 driver does not enforce these, so we leave it untouched.
187
+ const isBetterSqlite = connection.client.config.client === 'better-sqlite3';
188
+
189
+ return (isBetterSqlite ? connection.raw('PRAGMA foreign_keys = OFF;') : Promise.resolve())
190
+ .then(function () {
191
+ return connection.raw(`SELECT name FROM sqlite_master WHERE type='table';`);
192
+ })
184
193
  .then(function (tables) {
185
194
  return sequence(tables.map(table => () => {
186
195
  if (table.name === 'sqlite_sequence') {
@@ -201,6 +210,11 @@ exports.drop = function drop(options) {
201
210
  return Promise.reject(new errors.KnexMigrateError({
202
211
  err: err
203
212
  }));
213
+ })
214
+ .finally(function () {
215
+ if (isBetterSqlite) {
216
+ return connection.raw('PRAGMA foreign_keys = ON;');
217
+ }
204
218
  });
205
219
  } else {
206
220
  return Promise.reject(new errors.KnexMigrateError({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "knex-migrator",
3
- "version": "5.4.0",
3
+ "version": "5.4.1",
4
4
  "description": "Database migrations with knex.",
5
5
  "keywords": [
6
6
  "ghost",