knex-migrator 4.2.1 → 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/bin/knex-migrator-health +1 -1
- package/bin/knex-migrator-init +1 -1
- package/bin/knex-migrator-migrate +1 -1
- package/bin/knex-migrator-reset +1 -1
- package/bin/knex-migrator-rollback +1 -1
- package/lib/database.js +27 -20
- package/lib/index.js +1 -1
- package/migrations/add-primary-key-to-lock-table.js +3 -5
- package/package.json +6 -4
- package/logging.js +0 -8
package/bin/knex-migrator-health
CHANGED
package/bin/knex-migrator-init
CHANGED
package/bin/knex-migrator-reset
CHANGED
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
|
|
|
@@ -28,6 +35,8 @@ exports.connect = function connect(options) {
|
|
|
28
35
|
options.connection.timezone = options.connection.timezone || 'Z';
|
|
29
36
|
options.connection.charset = options.connection.charset || 'utf8mb4';
|
|
30
37
|
options.connection.decimalNumbers = true;
|
|
38
|
+
|
|
39
|
+
delete options.connection.filename;
|
|
31
40
|
}
|
|
32
41
|
|
|
33
42
|
return knex(options);
|
|
@@ -75,23 +84,21 @@ module.exports.createTransaction = function (connection, callback) {
|
|
|
75
84
|
* @TODO: https://github.com/TryGhost/knex-migrator/issues/91
|
|
76
85
|
* @returns {Bluebird<R> | Bluebird<any> | * | Promise<T>}
|
|
77
86
|
*/
|
|
78
|
-
exports.createMigrationsTable = function createMigrationsTable(connection) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
debug('Creating table: migrations');
|
|
84
|
-
|
|
85
|
-
return connection.schema.createTable('migrations', function (table) {
|
|
86
|
-
table.increments().primary();
|
|
87
|
-
table.string('name');
|
|
88
|
-
table.string('version');
|
|
89
|
-
table.string('currentVersion');
|
|
90
|
-
});
|
|
91
|
-
}
|
|
87
|
+
exports.createMigrationsTable = async function createMigrationsTable(connection) {
|
|
88
|
+
const hasTable = await connection.schema.hasTable('migrations');
|
|
89
|
+
if (hasTable) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
92
|
|
|
93
|
-
|
|
94
|
-
|
|
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
|
+
});
|
|
95
102
|
};
|
|
96
103
|
|
|
97
104
|
/**
|
|
@@ -106,9 +113,9 @@ exports.createDatabaseIfNotExist = function createDatabaseIfNotExist(dbConfig) {
|
|
|
106
113
|
collation = dbConfig.connection.collation || 'utf8mb4_general_ci';
|
|
107
114
|
|
|
108
115
|
// @NOTE: Skip, because sqlite3 is a file based database.
|
|
109
|
-
if (dbConfig
|
|
116
|
+
if (DatabaseInfo.isSQLiteConfig(dbConfig)) {
|
|
110
117
|
return Promise.resolve();
|
|
111
|
-
} else if (!
|
|
118
|
+
} else if (!DatabaseInfo.isMySQLConfig(dbConfig)) {
|
|
112
119
|
return Promise.reject(new errors.KnexMigrateError({
|
|
113
120
|
message: 'Database is not supported.'
|
|
114
121
|
}));
|
|
@@ -163,7 +170,7 @@ exports.drop = function drop(options) {
|
|
|
163
170
|
const connection = options.connection,
|
|
164
171
|
dbConfig = options.dbConfig;
|
|
165
172
|
|
|
166
|
-
if (
|
|
173
|
+
if (DatabaseInfo.isMySQL(connection)) {
|
|
167
174
|
debug('Drop database: ' + dbConfig.connection.database);
|
|
168
175
|
|
|
169
176
|
return connection.raw('DROP DATABASE `' + dbConfig.connection.database + '`;')
|
|
@@ -177,7 +184,7 @@ exports.drop = function drop(options) {
|
|
|
177
184
|
err: err
|
|
178
185
|
}));
|
|
179
186
|
});
|
|
180
|
-
} else if (
|
|
187
|
+
} else if (DatabaseInfo.isSQLite(connection)) {
|
|
181
188
|
// @NOTE: sqlite3 does not support "DROP DATABASE". We have to drop each table instead.
|
|
182
189
|
// @NOTE: We cannot just remove the sqlite3 file, because any database connection will get invalid.
|
|
183
190
|
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('
|
|
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
|
-
|
|
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
|
-
|
|
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
|
+
"version": "4.2.4",
|
|
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.
|
|
55
|
+
"debug": "4.3.4",
|
|
54
56
|
"ghost-ignition": "4.6.3",
|
|
55
|
-
"knex": "0.
|
|
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
80
|
"sqlite3": "5.0.2"
|
|
79
81
|
}
|
|
80
82
|
}
|