knex-migrator 4.2.3 → 4.2.4

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.
package/lib/database.js CHANGED
@@ -3,6 +3,7 @@ const knex = require('knex'),
3
3
  omit = require('lodash/omit'),
4
4
  debug = require('debug')('knex-migrator:database'),
5
5
  errors = require('./errors');
6
+ const DatabaseInfo = require('@tryghost/database-info');
6
7
 
7
8
  /**
8
9
  * @NOTE: Knex-migrator only supports knex query builder.
@@ -21,6 +22,12 @@ exports.connect = function connect(options) {
21
22
  const client = options.client;
22
23
 
23
24
  if (client === 'sqlite3') {
25
+ // TODO: remove this hack
26
+ // I need to monkey-patch the sqlite3 dialect to use `sqlite3` to make
27
+ // the jumps smaller as I'm updating `knex`
28
+ const Dialect = require(`knex/lib/dialects/sqlite3/index.js`);
29
+ Dialect.prototype._driver = () => require('sqlite3');
30
+
24
31
  options.useNullAsDefault = options.useNullAsDefault || false;
25
32
  }
26
33
 
@@ -77,23 +84,21 @@ module.exports.createTransaction = function (connection, callback) {
77
84
  * @TODO: https://github.com/TryGhost/knex-migrator/issues/91
78
85
  * @returns {Bluebird<R> | Bluebird<any> | * | Promise<T>}
79
86
  */
80
- exports.createMigrationsTable = function createMigrationsTable(connection) {
81
- return connection('migrations')
82
- .catch(function (err) {
83
- // CASE: table does not exist
84
- if (err.errno === 1 || err.errno === 1146) {
85
- debug('Creating table: migrations');
86
-
87
- return connection.schema.createTable('migrations', function (table) {
88
- table.increments().primary();
89
- table.string('name');
90
- table.string('version');
91
- table.string('currentVersion');
92
- });
93
- }
87
+ exports.createMigrationsTable = async function createMigrationsTable(connection) {
88
+ const hasTable = await connection.schema.hasTable('migrations');
89
+ if (hasTable) {
90
+ return;
91
+ }
94
92
 
95
- throw err;
96
- });
93
+ // CASE: table does not exist
94
+ debug('Creating table: migrations');
95
+
96
+ await connection.schema.createTable('migrations', function (table) {
97
+ table.increments().primary();
98
+ table.string('name');
99
+ table.string('version');
100
+ table.string('currentVersion');
101
+ });
97
102
  };
98
103
 
99
104
  /**
@@ -108,9 +113,9 @@ exports.createDatabaseIfNotExist = function createDatabaseIfNotExist(dbConfig) {
108
113
  collation = dbConfig.connection.collation || 'utf8mb4_general_ci';
109
114
 
110
115
  // @NOTE: Skip, because sqlite3 is a file based database.
111
- if (dbConfig.client === 'sqlite3') {
116
+ if (DatabaseInfo.isSQLiteConfig(dbConfig)) {
112
117
  return Promise.resolve();
113
- } else if (!['mysql', 'mysql2'].includes(dbConfig.client)) {
118
+ } else if (!DatabaseInfo.isMySQLConfig(dbConfig)) {
114
119
  return Promise.reject(new errors.KnexMigrateError({
115
120
  message: 'Database is not supported.'
116
121
  }));
@@ -165,7 +170,7 @@ exports.drop = function drop(options) {
165
170
  const connection = options.connection,
166
171
  dbConfig = options.dbConfig;
167
172
 
168
- if (['mysql', 'mysql2'].includes(dbConfig.client)) {
173
+ if (DatabaseInfo.isMySQL(connection)) {
169
174
  debug('Drop database: ' + dbConfig.connection.database);
170
175
 
171
176
  return connection.raw('DROP DATABASE `' + dbConfig.connection.database + '`;')
@@ -179,7 +184,7 @@ exports.drop = function drop(options) {
179
184
  err: err
180
185
  }));
181
186
  });
182
- } else if (dbConfig.client === 'sqlite3') {
187
+ } else if (DatabaseInfo.isSQLite(connection)) {
183
188
  // @NOTE: sqlite3 does not support "DROP DATABASE". We have to drop each table instead.
184
189
  // @NOTE: We cannot just remove the sqlite3 file, because any database connection will get invalid.
185
190
  return connection.raw('SELECT name FROM sqlite_master WHERE type="table";')
@@ -1,12 +1,11 @@
1
1
  const debug = require('debug')('knex-migrator:lock-table');
2
+ const DatabaseInfo = require('@tryghost/database-info');
2
3
 
3
4
  /**
4
5
  * Checks if primary key index exists in a table over the given columns.
5
6
  */
6
7
  function hasPrimaryKeySQLite(tableName, knex) {
7
- const client = knex.client.config.client;
8
-
9
- if (client !== 'sqlite3') {
8
+ if (!DatabaseInfo.isSQLite(knex)) {
10
9
  throw new Error('Must use hasPrimaryKeySQLite on an SQLite3 database');
11
10
  }
12
11
 
@@ -21,8 +20,7 @@ function hasPrimaryKeySQLite(tableName, knex) {
21
20
  * Adds an primary key index to a table over the given columns.
22
21
  */
23
22
  function addPrimaryKey(tableName, columns, knex) {
24
- const isSQLite = knex.client.config.client === 'sqlite3';
25
- if (isSQLite) {
23
+ if (DatabaseInfo.isSQLite(knex)) {
26
24
  return hasPrimaryKeySQLite(tableName, knex)
27
25
  .then((primaryKeyExists) => {
28
26
  if (primaryKeyExists) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "knex-migrator",
3
- "version": "4.2.3",
3
+ "version": "4.2.4",
4
4
  "description": "Database migrations with knex.",
5
5
  "keywords": [
6
6
  "ghost",
@@ -47,15 +47,17 @@
47
47
  "node": "^12.22.1 || ^14.17.0 || ^16.13.0"
48
48
  },
49
49
  "dependencies": {
50
- "@tryghost/logging": "2.0.4",
50
+ "@tryghost/database-info": "0.3.0",
51
+ "@tryghost/logging": "2.1.1",
51
52
  "bluebird": "3.7.2",
52
53
  "commander": "5.1.0",
53
54
  "compare-ver": "2.0.2",
54
55
  "debug": "4.3.4",
55
56
  "ghost-ignition": "4.6.3",
56
- "knex": "0.21.21",
57
+ "knex": "1.0.4",
57
58
  "lodash": "4.17.21",
58
59
  "moment": "2.24.0",
60
+ "mysql2": "2.3.3",
59
61
  "nconf": "0.11.3",
60
62
  "resolve": "1.22.0"
61
63
  },
@@ -75,7 +77,6 @@
75
77
  "sinon": "9.2.4"
76
78
  },
77
79
  "optionalDependencies": {
78
- "mysql2": "2.3.3",
79
80
  "sqlite3": "5.0.2"
80
81
  }
81
82
  }