knex-migrator 4.1.1 → 4.2.0
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/LICENSE +1 -1
- package/README.md +2 -2
- package/lib/database.js +10 -5
- package/lib/index.js +23 -20
- package/package.json +4 -4
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -52,7 +52,7 @@ Please add a file named `MigratorConfig.js`. Knex-migrator will load the config
|
|
|
52
52
|
```
|
|
53
53
|
module.exports = {
|
|
54
54
|
database: {
|
|
55
|
-
client: String (Required) ['mysql', 'sqlite3']
|
|
55
|
+
client: String (Required) ['mysql', 'mysql2', 'sqlite3']
|
|
56
56
|
connection: {
|
|
57
57
|
host: String, (Required) [e.g. '127.0.0.1']
|
|
58
58
|
user: String, (Required)
|
|
@@ -338,7 +338,7 @@ knexMigrator.isDatabaseOK()
|
|
|
338
338
|
|
|
339
339
|
# Copyright & License
|
|
340
340
|
|
|
341
|
-
Copyright (c) 2013-
|
|
341
|
+
Copyright (c) 2013-2022 Ghost Foundation - Released under the [MIT license](LICENSE).
|
|
342
342
|
|
|
343
343
|
|
|
344
344
|
|
package/lib/database.js
CHANGED
|
@@ -12,16 +12,21 @@ const knex = require('knex'),
|
|
|
12
12
|
*/
|
|
13
13
|
exports.connect = function connect(options) {
|
|
14
14
|
options = options || {};
|
|
15
|
+
|
|
16
|
+
// Alias `mysql` to `mysql2` so we can maintain backwards compatibility
|
|
17
|
+
if (options.client === 'mysql') {
|
|
18
|
+
options.client = 'mysql2';
|
|
19
|
+
}
|
|
20
|
+
|
|
15
21
|
const client = options.client;
|
|
16
22
|
|
|
17
23
|
if (client === 'sqlite3') {
|
|
18
24
|
options.useNullAsDefault = options.useNullAsDefault || false;
|
|
19
25
|
}
|
|
20
26
|
|
|
21
|
-
if (client === '
|
|
22
|
-
options.connection.timezone = options.connection.timezone || '
|
|
27
|
+
if (client === 'mysql2') {
|
|
28
|
+
options.connection.timezone = options.connection.timezone || 'Z';
|
|
23
29
|
options.connection.charset = options.connection.charset || 'utf8mb4';
|
|
24
|
-
options.connection.collation = options.connection.collation || 'utf8mb4_general_ci';
|
|
25
30
|
}
|
|
26
31
|
|
|
27
32
|
return knex(options);
|
|
@@ -102,7 +107,7 @@ exports.createDatabaseIfNotExist = function createDatabaseIfNotExist(dbConfig) {
|
|
|
102
107
|
// @NOTE: Skip, because sqlite3 is a file based database.
|
|
103
108
|
if (dbConfig.client === 'sqlite3') {
|
|
104
109
|
return Promise.resolve();
|
|
105
|
-
} else if (dbConfig.client
|
|
110
|
+
} else if (!['mysql', 'mysql2'].includes(dbConfig.client)) {
|
|
106
111
|
return Promise.reject(new errors.KnexMigrateError({
|
|
107
112
|
message: 'Database is not supported.'
|
|
108
113
|
}));
|
|
@@ -157,7 +162,7 @@ exports.drop = function drop(options) {
|
|
|
157
162
|
const connection = options.connection,
|
|
158
163
|
dbConfig = options.dbConfig;
|
|
159
164
|
|
|
160
|
-
if (dbConfig.client
|
|
165
|
+
if (['mysql', 'mysql2'].includes(dbConfig.client)) {
|
|
161
166
|
debug('Drop database: ' + dbConfig.connection.database);
|
|
162
167
|
|
|
163
168
|
return connection.raw('DROP DATABASE `' + dbConfig.connection.database + '`;')
|
package/lib/index.js
CHANGED
|
@@ -155,30 +155,33 @@ KnexMigrator.prototype.init = function init(options) {
|
|
|
155
155
|
throw err;
|
|
156
156
|
}
|
|
157
157
|
|
|
158
|
-
return database.createTransaction(self.connection, function (transacting) {
|
|
158
|
+
return database.createTransaction(self.connection, async function (transacting) {
|
|
159
|
+
const existingMigrations = await transacting('migrations').select('name');
|
|
160
|
+
const existingMigrationsNames = existingMigrations.map(m => m.name);
|
|
161
|
+
const migrationsToAdd = [];
|
|
162
|
+
|
|
159
163
|
// CASE: Run over all migration scripts and add the file name to the database.
|
|
160
|
-
|
|
164
|
+
for (const versionToMigrateTo of versionsToMigrateTo) {
|
|
161
165
|
let versionPath = path.join(self.migrationPath, self.subfolder, versionToMigrateTo);
|
|
162
166
|
let filesToMigrateTo = utils.listFiles(versionPath) || [];
|
|
163
167
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
});
|
|
168
|
+
// CASE: check if migration exists, do not insert twice
|
|
169
|
+
for (const name of filesToMigrateTo) {
|
|
170
|
+
if (existingMigrationsNames.includes(name)) {
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
migrationsToAdd.push({
|
|
175
|
+
name,
|
|
176
|
+
version: versionToMigrateTo,
|
|
177
|
+
currentVersion: self.currentVersion
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
await self.connection
|
|
183
|
+
.batchInsert('migrations', migrationsToAdd)
|
|
184
|
+
.transacting(transacting);
|
|
182
185
|
});
|
|
183
186
|
})
|
|
184
187
|
.then(function () {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "knex-migrator",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.2.0",
|
|
4
4
|
"description": "Database migrations with knex.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ghost",
|
|
@@ -50,13 +50,13 @@
|
|
|
50
50
|
"bluebird": "3.7.2",
|
|
51
51
|
"commander": "5.1.0",
|
|
52
52
|
"compare-ver": "2.0.2",
|
|
53
|
-
"debug": "4.3.
|
|
53
|
+
"debug": "4.3.3",
|
|
54
54
|
"ghost-ignition": "4.6.3",
|
|
55
55
|
"knex": "0.21.19",
|
|
56
56
|
"lodash": "4.17.21",
|
|
57
57
|
"moment": "2.24.0",
|
|
58
58
|
"nconf": "0.11.3",
|
|
59
|
-
"resolve": "1.
|
|
59
|
+
"resolve": "1.22.0"
|
|
60
60
|
},
|
|
61
61
|
"files": [
|
|
62
62
|
"bin",
|
|
@@ -74,7 +74,7 @@
|
|
|
74
74
|
"sinon": "9.2.4"
|
|
75
75
|
},
|
|
76
76
|
"optionalDependencies": {
|
|
77
|
-
"
|
|
77
|
+
"mysql2": "2.3.3",
|
|
78
78
|
"sqlite3": "5.0.2"
|
|
79
79
|
}
|
|
80
80
|
}
|