knex-migrator 4.2.2 → 4.2.5

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.
@@ -3,7 +3,7 @@
3
3
  var program = require('commander');
4
4
  var utils = require('../lib/utils');
5
5
 
6
- var logging = require('../logging');
6
+ var logging = require('@tryghost/logging');
7
7
  var knexMigrator;
8
8
 
9
9
  utils.getKnexMigrator({path: process.cwd()})
@@ -3,7 +3,7 @@
3
3
  var program = require('commander');
4
4
  var utils = require('../lib/utils');
5
5
 
6
- var logging = require('../logging');
6
+ var logging = require('@tryghost/logging');
7
7
  var knexMigrator;
8
8
 
9
9
  utils.getKnexMigrator({path: process.cwd()})
@@ -3,7 +3,7 @@
3
3
  var program = require('commander');
4
4
  var utils = require('../lib/utils');
5
5
 
6
- var logging = require('../logging');
6
+ var logging = require('@tryghost/logging');
7
7
  var knexMigrator;
8
8
 
9
9
  utils.getKnexMigrator({path: process.cwd()})
@@ -3,7 +3,7 @@
3
3
  var program = require('commander');
4
4
  var utils = require('../lib/utils');
5
5
 
6
- var logging = require('../logging');
6
+ var logging = require('@tryghost/logging');
7
7
  var knexMigrator;
8
8
 
9
9
  utils.getKnexMigrator({path: process.cwd()})
@@ -3,7 +3,7 @@
3
3
  const program = require('commander');
4
4
  const utils = require('../lib/utils');
5
5
 
6
- const logging = require('../logging');
6
+ const logging = require('@tryghost/logging');
7
7
  let knexMigrator;
8
8
 
9
9
  utils.getKnexMigrator({path: process.cwd()})
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.
@@ -77,23 +78,21 @@ module.exports.createTransaction = function (connection, callback) {
77
78
  * @TODO: https://github.com/TryGhost/knex-migrator/issues/91
78
79
  * @returns {Bluebird<R> | Bluebird<any> | * | Promise<T>}
79
80
  */
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
- }
81
+ exports.createMigrationsTable = async function createMigrationsTable(connection) {
82
+ const hasTable = await connection.schema.hasTable('migrations');
83
+ if (hasTable) {
84
+ return;
85
+ }
94
86
 
95
- throw err;
96
- });
87
+ // CASE: table does not exist
88
+ debug('Creating table: migrations');
89
+
90
+ await connection.schema.createTable('migrations', function (table) {
91
+ table.increments().primary();
92
+ table.string('name');
93
+ table.string('version');
94
+ table.string('currentVersion');
95
+ });
97
96
  };
98
97
 
99
98
  /**
@@ -108,9 +107,9 @@ exports.createDatabaseIfNotExist = function createDatabaseIfNotExist(dbConfig) {
108
107
  collation = dbConfig.connection.collation || 'utf8mb4_general_ci';
109
108
 
110
109
  // @NOTE: Skip, because sqlite3 is a file based database.
111
- if (dbConfig.client === 'sqlite3') {
110
+ if (DatabaseInfo.isSQLiteConfig(dbConfig)) {
112
111
  return Promise.resolve();
113
- } else if (!['mysql', 'mysql2'].includes(dbConfig.client)) {
112
+ } else if (!DatabaseInfo.isMySQLConfig(dbConfig)) {
114
113
  return Promise.reject(new errors.KnexMigrateError({
115
114
  message: 'Database is not supported.'
116
115
  }));
@@ -165,7 +164,7 @@ exports.drop = function drop(options) {
165
164
  const connection = options.connection,
166
165
  dbConfig = options.dbConfig;
167
166
 
168
- if (['mysql', 'mysql2'].includes(dbConfig.client)) {
167
+ if (DatabaseInfo.isMySQL(connection)) {
169
168
  debug('Drop database: ' + dbConfig.connection.database);
170
169
 
171
170
  return connection.raw('DROP DATABASE `' + dbConfig.connection.database + '`;')
@@ -179,7 +178,7 @@ exports.drop = function drop(options) {
179
178
  err: err
180
179
  }));
181
180
  });
182
- } else if (dbConfig.client === 'sqlite3') {
181
+ } else if (DatabaseInfo.isSQLite(connection)) {
183
182
  // @NOTE: sqlite3 does not support "DROP DATABASE". We have to drop each table instead.
184
183
  // @NOTE: We cannot just remove the sqlite3 file, because any database connection will get invalid.
185
184
  return connection.raw('SELECT name FROM sqlite_master WHERE type="table";')
package/lib/index.js CHANGED
@@ -5,7 +5,7 @@ const debug = require('debug')('knex-migrator:index');
5
5
  const database = require('./database');
6
6
  const utils = require('./utils');
7
7
  const errors = require('./errors');
8
- const logging = require('../logging');
8
+ const logging = require('@tryghost/logging');
9
9
  const migrations = require('../migrations');
10
10
  const locking = require('./locking');
11
11
 
@@ -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.2",
3
+ "version": "4.2.5",
4
4
  "description": "Database migrations with knex.",
5
5
  "keywords": [
6
6
  "ghost",
@@ -47,14 +47,17 @@
47
47
  "node": "^12.22.1 || ^14.17.0 || ^16.13.0"
48
48
  },
49
49
  "dependencies": {
50
+ "@tryghost/database-info": "0.3.0",
51
+ "@tryghost/logging": "2.1.1",
50
52
  "bluebird": "3.7.2",
51
53
  "commander": "5.1.0",
52
54
  "compare-ver": "2.0.2",
53
- "debug": "4.3.3",
55
+ "debug": "4.3.4",
54
56
  "ghost-ignition": "4.6.3",
55
- "knex": "0.21.19",
57
+ "knex": "1.0.4",
56
58
  "lodash": "4.17.21",
57
59
  "moment": "2.24.0",
60
+ "mysql2": "2.3.3",
58
61
  "nconf": "0.11.3",
59
62
  "resolve": "1.22.0"
60
63
  },
@@ -74,7 +77,6 @@
74
77
  "sinon": "9.2.4"
75
78
  },
76
79
  "optionalDependencies": {
77
- "mysql2": "2.3.3",
78
- "sqlite3": "5.0.2"
80
+ "@vscode/sqlite3": "5.0.8"
79
81
  }
80
82
  }
package/logging.js DELETED
@@ -1,8 +0,0 @@
1
- var logging = require('ghost-ignition').logging;
2
-
3
- module.exports = logging({
4
- env: process.env.NODE_ENV,
5
- mode: 'long',
6
- level: 'info',
7
- transports: ['stdout', 'stderr']
8
- });