knex-migrator 4.0.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.
- package/CHANGELOG.md +171 -0
- package/LICENSE +19 -0
- package/README.md +347 -0
- package/bin/knex-migrator +13 -0
- package/bin/knex-migrator-health +35 -0
- package/bin/knex-migrator-init +34 -0
- package/bin/knex-migrator-migrate +38 -0
- package/bin/knex-migrator-reset +31 -0
- package/bin/knex-migrator-rollback +37 -0
- package/lib/database.js +204 -0
- package/lib/errors.js +84 -0
- package/lib/index.js +1243 -0
- package/lib/locking.js +113 -0
- package/lib/utils.js +213 -0
- package/logging.js +8 -0
- package/migrations/add-primary-key-to-lock-table.js +58 -0
- package/migrations/field-length.js +22 -0
- package/migrations/index.js +17 -0
- package/migrations/lock-table.js +41 -0
- package/migrations/use-index.js +24 -0
- package/package.json +80 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
# knex-migrator Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
|
+
|
|
5
|
+
<a name="3.2.6"></a>
|
|
6
|
+
# [3.2.6](https://github.com/TryGhost/Ghost-CLI/compare/3.2.5...v3.2.6) (2019-03-20)
|
|
7
|
+
|
|
8
|
+
### Bug fixes
|
|
9
|
+
|
|
10
|
+
- 🐛 Fixed `knex-migrator migrate --init`
|
|
11
|
+
|
|
12
|
+
<a name="3.2.1"></a>
|
|
13
|
+
# [3.2.1](https://github.com/TryGhost/Ghost-CLI/compare/3.2.0...v3.2.1) (2019-03-20)
|
|
14
|
+
|
|
15
|
+
### Bug fixes
|
|
16
|
+
|
|
17
|
+
- 🐛 Fixed option collision with commander (`--version` vs `--v`)
|
|
18
|
+
|
|
19
|
+
<a name="3.2.0"></a>
|
|
20
|
+
# [3.2.0](https://github.com/TryGhost/Ghost-CLI/compare/3.1.8...v3.2.0) (2019-03-20)
|
|
21
|
+
|
|
22
|
+
### Features
|
|
23
|
+
|
|
24
|
+
✨ Added ability to rollback to a specific version
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
<a name="3.1.6"></a>
|
|
28
|
+
# [3.1.6](https://github.com/TryGhost/Ghost-CLI/compare/3.1.5...v3.1.6) (2018-04-30)
|
|
29
|
+
|
|
30
|
+
### Node
|
|
31
|
+
* 🔥Drop Node v4 support
|
|
32
|
+
|
|
33
|
+
### Bug fixes
|
|
34
|
+
|
|
35
|
+
* 🐛Fixed incorrect version number comparison
|
|
36
|
+
* 🐛Fixed wrong folders order on db initialisation
|
|
37
|
+
* 🐛Fixed comparing expected and actual migration tasks
|
|
38
|
+
|
|
39
|
+
<a name="3.1.3"></a>
|
|
40
|
+
# [3.1.3](https://github.com/TryGhost/Ghost-CLI/compare/3.1.2...v3.1.3) (2018-01-02)
|
|
41
|
+
|
|
42
|
+
### Improvements
|
|
43
|
+
|
|
44
|
+
* Optimised error message for lock
|
|
45
|
+
* Added support for specifying the config via options
|
|
46
|
+
|
|
47
|
+
<a name="3.1.2"></a>
|
|
48
|
+
# [3.1.2](https://github.com/TryGhost/Ghost-CLI/compare/3.1.1...v3.1.2) (2017-12-05)
|
|
49
|
+
|
|
50
|
+
### Improvements
|
|
51
|
+
|
|
52
|
+
* Make database upgrades more relaxed
|
|
53
|
+
|
|
54
|
+
<a name="3.1.1"></a>
|
|
55
|
+
# [3.1.1](https://github.com/TryGhost/Ghost-CLI/compare/3.1.0...v3.1.1) (2017-12-05)
|
|
56
|
+
|
|
57
|
+
### Improvements
|
|
58
|
+
|
|
59
|
+
* Added protection against database migrations
|
|
60
|
+
|
|
61
|
+
<a name="3.1.0"></a>
|
|
62
|
+
# [3.1.0](https://github.com/TryGhost/Ghost-CLI/compare/3.0.10...v3.1.0) (2017-12-05)
|
|
63
|
+
|
|
64
|
+
### Improvements
|
|
65
|
+
|
|
66
|
+
* Added more database upgrades
|
|
67
|
+
* Added unique constaint to the migrations table
|
|
68
|
+
* Define field lengths for migrations table
|
|
69
|
+
|
|
70
|
+
<a name="3.0.10"></a>
|
|
71
|
+
# [3.0.10](https://github.com/TryGhost/Ghost-CLI/compare/3.0.9...v3.0.10) (2017-12-04)
|
|
72
|
+
|
|
73
|
+
### Fixes
|
|
74
|
+
|
|
75
|
+
* Fixed `acquired_at` and `released_at` with correct date format
|
|
76
|
+
|
|
77
|
+
<a name="3.0.9"></a>
|
|
78
|
+
# [3.0.9](https://github.com/TryGhost/Ghost-CLI/compare/3.0.8...v3.0.9) (2017-12-04)
|
|
79
|
+
|
|
80
|
+
### Fixes
|
|
81
|
+
|
|
82
|
+
* Do not unlock if lock was never acquired
|
|
83
|
+
|
|
84
|
+
<a name="3.0.8"></a>
|
|
85
|
+
# [3.0.8](https://github.com/TryGhost/Ghost-CLI/compare/3.0.7...v3.0.8) (2017-12-04)
|
|
86
|
+
|
|
87
|
+
### Fixes
|
|
88
|
+
|
|
89
|
+
* Added more npm keywords
|
|
90
|
+
* Rollback should not happen if lock was never acquired
|
|
91
|
+
|
|
92
|
+
<a name="3.0.7"></a>
|
|
93
|
+
# [3.0.7](https://github.com/TryGhost/Ghost-CLI/compare/3.0.6...v3.0.7) (2017-11-15)
|
|
94
|
+
|
|
95
|
+
### Fixes
|
|
96
|
+
|
|
97
|
+
* Added missing `--force` flag for `knex-migrator reset`
|
|
98
|
+
|
|
99
|
+
<a name="3.0.6"></a>
|
|
100
|
+
# [3.0.6](https://github.com/TryGhost/Ghost-CLI/compare/3.0.5...v3.0.6) (2017-11-04)
|
|
101
|
+
|
|
102
|
+
### Fixes
|
|
103
|
+
|
|
104
|
+
* Do not throw error on reset, otherwise it's harder to handle this via the JS API
|
|
105
|
+
* There is no good reason to throw an error, so revert to success response
|
|
106
|
+
|
|
107
|
+
<a name="3.0.5"></a>
|
|
108
|
+
# [3.0.5](https://github.com/TryGhost/Ghost-CLI/compare/3.0.4...v3.0.5) (2017-11-04)
|
|
109
|
+
|
|
110
|
+
### Fixes
|
|
111
|
+
|
|
112
|
+
* Fixed order of rollback
|
|
113
|
+
|
|
114
|
+
<a name="3.0.4"></a>
|
|
115
|
+
# [3.0.4](https://github.com/TryGhost/Ghost-CLI/compare/3.0.3...v3.0.4) (2017-11-04)
|
|
116
|
+
|
|
117
|
+
### Fixes
|
|
118
|
+
|
|
119
|
+
* Fixed rollback for init scripts
|
|
120
|
+
|
|
121
|
+
<a name="3.0.3"></a>
|
|
122
|
+
# [3.0.3](https://github.com/TryGhost/Ghost-CLI/compare/3.0.2...v3.0.3) (2017-11-04)
|
|
123
|
+
|
|
124
|
+
### Fixes
|
|
125
|
+
|
|
126
|
+
* Fixed `--force` flag for `knex-migrator rollback`
|
|
127
|
+
|
|
128
|
+
<a name="3.0.2"></a>
|
|
129
|
+
# [3.0.2](https://github.com/TryGhost/Ghost-CLI/compare/3.0.1...v3.0.2) (2017-11-03)
|
|
130
|
+
|
|
131
|
+
### Fixes
|
|
132
|
+
|
|
133
|
+
* Fixed missing throw/catch behaviour,
|
|
134
|
+
|
|
135
|
+
<a name="3.0.1"></a>
|
|
136
|
+
# [3.0.1](https://github.com/TryGhost/Ghost-CLI/compare/3.0.0...v3.0.1) (2017-11-03)
|
|
137
|
+
|
|
138
|
+
### Fixes
|
|
139
|
+
|
|
140
|
+
* Fixed binary for `knex-migrator rollback`
|
|
141
|
+
|
|
142
|
+
<a name="3.0.0"></a>
|
|
143
|
+
# [3.0.0](https://github.com/TryGhost/Ghost-CLI/compare/2.1.9...v3.0.0) (2017-11-03)
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
### Breaking Changes
|
|
147
|
+
* You have to export `up` and `down` in your migration scripts. `down` is optional, but highly recommended, otherwise on failure your changes won't rollback completely.
|
|
148
|
+
* The `options` object which is passed into the migration script contains now by default a connection object. (`options.connection`). You have to explicit enable transactions, see README.
|
|
149
|
+
* A new migrations lock table get's added as soon as you run **any** command. You don't have to worry about that.
|
|
150
|
+
* Expect bug fixes in the upcoming days.
|
|
151
|
+
|
|
152
|
+
### Features
|
|
153
|
+
|
|
154
|
+
* Support full rollback (auto rollback on error)
|
|
155
|
+
* Shutdown hook
|
|
156
|
+
* Concurrency and locking
|
|
157
|
+
* A new shiny `rollback` command for manual rollback if needed.
|
|
158
|
+
|
|
159
|
+
<a name="2.1.9"></a>
|
|
160
|
+
# [2.1.9](https://github.com/TryGhost/Ghost-CLI/compare/2.1.8...v2.1.9) (2017-10-26)
|
|
161
|
+
|
|
162
|
+
### Features
|
|
163
|
+
|
|
164
|
+
* Enabled Node 8 Support
|
|
165
|
+
|
|
166
|
+
<a name="2.1.8"></a>
|
|
167
|
+
# [2.1.9](https://github.com/TryGhost/Ghost-CLI/compare/2.1.7...v2.1.8) (2017-10-24)
|
|
168
|
+
|
|
169
|
+
### Fixes
|
|
170
|
+
|
|
171
|
+
* Fixed migration order on db initialisation
|
package/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2013-2021 Ghost Foundation
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
# knex-migrator
|
|
2
|
+
|
|
3
|
+
A database migration tool for [knex.js](https://github.com/tgriesser/knex), which supports MySQL and SQlite3.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- [x] JS API
|
|
8
|
+
- [x] CLI Tool
|
|
9
|
+
- [x] Differentiation between database initialization and migration (Support for a database schema, [like we use in Ghost](https://github.com/TryGhost/Ghost/blob/1.16.2/core/server/data/schema/schema.js))
|
|
10
|
+
- [x] Support for database creation
|
|
11
|
+
- [x] Hooks
|
|
12
|
+
- [x] Rollback to latest version
|
|
13
|
+
- [x] Auto-Rollback on error
|
|
14
|
+
- [x] Database health check
|
|
15
|
+
- [x] Supports transactions
|
|
16
|
+
- [x] Full atomic, support for separate DML/DDL scripts (no autocommit)
|
|
17
|
+
- [x] Migration lock
|
|
18
|
+
- [x] Full debug & pretty log support
|
|
19
|
+
- [x] Custom migration folder structure
|
|
20
|
+
- [x] Stable (Used in [Ghost](https://github.com/TryGhost/Ghost) for many years in thousands of blogs in production mode)
|
|
21
|
+
|
|
22
|
+
# Install
|
|
23
|
+
|
|
24
|
+
`npm install knex-migrator --save`
|
|
25
|
+
|
|
26
|
+
or
|
|
27
|
+
|
|
28
|
+
`yarn add knex-migrator`
|
|
29
|
+
|
|
30
|
+
Add me to your globals:
|
|
31
|
+
- `npm install --global knex-migrator`
|
|
32
|
+
|
|
33
|
+
# Usage
|
|
34
|
+
|
|
35
|
+
## Pre-word
|
|
36
|
+
|
|
37
|
+
- Replicas are unsupported, because Knex.js [doesn't support them](https://github.com/tgriesser/knex/issues/2253).
|
|
38
|
+
- Sqlite does **not** support read locks by default. Read [here](https://github.com/TryGhost/knex-migrator/issues/87) why.
|
|
39
|
+
- [Comparison](https://github.com/TryGhost/knex-migrator/issues/119) with other available migration tools.
|
|
40
|
+
- Don't mix DDL/DML statements in a migration script. In MySQL DDL statements use implicit commits.
|
|
41
|
+
- It's highly recommended to write both the `up` and the `down` function to ensure a full rollback.
|
|
42
|
+
- If your process dies while migrations are running, knex-migrator won't be able to release the migration lock.
|
|
43
|
+
To release to lock you can run `knex-migrator rollback`. **But** it's recommended to check your database first to see in which state it is.
|
|
44
|
+
You can check the tables `migrations` and `migrations_lock`. The rollback will rollback any migrations which were executed based on your current version.
|
|
45
|
+
|
|
46
|
+
## Configure knex-migrator
|
|
47
|
+
|
|
48
|
+
The tool requires a config file in your project root.
|
|
49
|
+
Please add a file named `MigratorConfig.js`. Knex-migrator will load the config file.
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
```
|
|
53
|
+
module.exports = {
|
|
54
|
+
database: {
|
|
55
|
+
client: String (Required) ['mysql', 'sqlite3']
|
|
56
|
+
connection: {
|
|
57
|
+
host: String, (Required) [e.g. '127.0.0.1']
|
|
58
|
+
user: String, (Required)
|
|
59
|
+
password: String, (Required)
|
|
60
|
+
charset: String, (Optional) [Default: 'utf8mb4']
|
|
61
|
+
database: String (Required)
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
migrationPath: String, (Required) [e.g. '/var/www/project/migrations']
|
|
65
|
+
currentVersion: String, (Required) [e.g. '2.0']
|
|
66
|
+
subfolder: String (Optional) [Default: 'versions']
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Please take a look at [this real example](https://github.com/TryGhost/Ghost/blob/2.19.3/MigratorConfig.js).
|
|
71
|
+
|
|
72
|
+
## Folder Structure
|
|
73
|
+
|
|
74
|
+
```
|
|
75
|
+
project/
|
|
76
|
+
migrations/
|
|
77
|
+
hooks/
|
|
78
|
+
init/
|
|
79
|
+
index.js
|
|
80
|
+
before.js
|
|
81
|
+
shutdown.js
|
|
82
|
+
migrate/
|
|
83
|
+
index.js
|
|
84
|
+
after.js
|
|
85
|
+
shutdown.js
|
|
86
|
+
init/
|
|
87
|
+
1-add-tables.js
|
|
88
|
+
versions/
|
|
89
|
+
1.0/
|
|
90
|
+
1-add-events-table.js
|
|
91
|
+
2-normalise-settings.js
|
|
92
|
+
2.0/
|
|
93
|
+
1-add-timestamps-columns.js
|
|
94
|
+
2.1/
|
|
95
|
+
1-remove-empty-strings.js
|
|
96
|
+
2-add-webhooks-table.js
|
|
97
|
+
3-add-permissions.js
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Please take a look at [this real example](https://github.com/TryGhost/Ghost/tree/2.19.3/core/server/data/migrations).
|
|
101
|
+
|
|
102
|
+
## Hooks
|
|
103
|
+
|
|
104
|
+
Knex-migrator offers a couple of hooks, which makes it possible to hook into the migration process. You can create a hook per type: 'init' or 'migrate'. The folder name must be `hooks` and is not configurable. Please create an index.js file to export your functions, see [example](https://github.com/TryGhost/Ghost/blob/2.19.3/core/server/data/migrations/hooks/init/index.js).
|
|
105
|
+
|
|
106
|
+
|hook|description|
|
|
107
|
+
|---|---|
|
|
108
|
+
|before|is called before anything happens|
|
|
109
|
+
|beforeEach| is called before each migration script|
|
|
110
|
+
|after|is called after everything happened|
|
|
111
|
+
|afterEach|is called after each migration script|
|
|
112
|
+
|shutdown|is called before the migrator shuts down|
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
## Migration Files
|
|
116
|
+
|
|
117
|
+
### Config
|
|
118
|
+
You can configure each migration script.
|
|
119
|
+
|
|
120
|
+
```
|
|
121
|
+
module.exports.config = {
|
|
122
|
+
transaction: Boolean
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
### Examples
|
|
128
|
+
```
|
|
129
|
+
const Promise = require('bluebird');
|
|
130
|
+
|
|
131
|
+
module.exports.up = function(options) {
|
|
132
|
+
const connection = options.connection;
|
|
133
|
+
|
|
134
|
+
...
|
|
135
|
+
|
|
136
|
+
return Promise.resolve();
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
module.exports.down = function(options) {
|
|
140
|
+
const connection = options.connection;
|
|
141
|
+
|
|
142
|
+
...
|
|
143
|
+
|
|
144
|
+
return Promise.resolve();
|
|
145
|
+
}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
```
|
|
149
|
+
const Promise = require('bluebird');
|
|
150
|
+
|
|
151
|
+
module.exports.config = {
|
|
152
|
+
transaction: true
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
module.exports.up = function(options) {
|
|
156
|
+
const connection = options.transacting;
|
|
157
|
+
|
|
158
|
+
...
|
|
159
|
+
|
|
160
|
+
return Promise.resolve();
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
module.exports.down = function(options) {
|
|
164
|
+
const connection = options.transacting;
|
|
165
|
+
|
|
166
|
+
...
|
|
167
|
+
|
|
168
|
+
return Promise.resolve();
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## CLI
|
|
173
|
+
|
|
174
|
+
### Commands
|
|
175
|
+
|
|
176
|
+
#### knex-migrator help
|
|
177
|
+
|
|
178
|
+
```
|
|
179
|
+
$ knex-migrator help
|
|
180
|
+
Usage: knex-migrator [options] [command]
|
|
181
|
+
|
|
182
|
+
Options:
|
|
183
|
+
-v, --version output the version number
|
|
184
|
+
-h, --help output usage information
|
|
185
|
+
|
|
186
|
+
Commands:
|
|
187
|
+
init|i [config] init db
|
|
188
|
+
migrate|m [config] migrate db
|
|
189
|
+
reset|r reset db
|
|
190
|
+
health|h health of db
|
|
191
|
+
rollback|ro rollbacks your db
|
|
192
|
+
help [cmd] display help for [cmd]
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
#### knex-migrator health
|
|
196
|
+
|
|
197
|
+
- Returns the database health/state
|
|
198
|
+
- Based on your current version and your migration scripts
|
|
199
|
+
|
|
200
|
+
#### knex-migrator init
|
|
201
|
+
|
|
202
|
+
- Initializes your database based on your init scripts
|
|
203
|
+
- Creates the database if it was not created yet
|
|
204
|
+
|
|
205
|
+
##### Options
|
|
206
|
+
|
|
207
|
+
```bash
|
|
208
|
+
# Skips a specific migration script
|
|
209
|
+
--skip
|
|
210
|
+
|
|
211
|
+
# Runs only a specific migration script
|
|
212
|
+
--only
|
|
213
|
+
|
|
214
|
+
# Path to MigratorConfig.js
|
|
215
|
+
--mgpath
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
#### knex-migrator migrate
|
|
219
|
+
|
|
220
|
+
- Migrates your database to latest version
|
|
221
|
+
- Automatic rollback if an error occurs
|
|
222
|
+
|
|
223
|
+
##### Options
|
|
224
|
+
|
|
225
|
+
```bash
|
|
226
|
+
# The version you would like to migrate to
|
|
227
|
+
--v
|
|
228
|
+
|
|
229
|
+
# Combo Feature to check whether the database was already initialized
|
|
230
|
+
--init
|
|
231
|
+
|
|
232
|
+
# Force the execution no matter which current version you are on
|
|
233
|
+
--force
|
|
234
|
+
|
|
235
|
+
# Path to MigratorConfig.js
|
|
236
|
+
--mgpath
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
#### knex-migrator rollback
|
|
240
|
+
|
|
241
|
+
- Rolls back your database
|
|
242
|
+
- By default, you can only rollback if the database is locked
|
|
243
|
+
|
|
244
|
+
##### Options
|
|
245
|
+
|
|
246
|
+
```bash
|
|
247
|
+
# Ignores the migration lock
|
|
248
|
+
--force
|
|
249
|
+
|
|
250
|
+
# Version you would like to rollback to
|
|
251
|
+
--v
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
#### knex-migrator reset
|
|
255
|
+
|
|
256
|
+
- Resets your database
|
|
257
|
+
- Removes the database
|
|
258
|
+
|
|
259
|
+
##### Options
|
|
260
|
+
|
|
261
|
+
```bash
|
|
262
|
+
# Ignores the migration lock
|
|
263
|
+
--force
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
### Advanced
|
|
267
|
+
|
|
268
|
+
`DEBUG=knex-migrator:* knex-migrator migrate`
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
## JS API
|
|
272
|
+
|
|
273
|
+
### Instantiation
|
|
274
|
+
|
|
275
|
+
```js
|
|
276
|
+
const KnexMigrator = require('knex-migrator');
|
|
277
|
+
|
|
278
|
+
# Option 1: Pass path to MigratorConfig.js
|
|
279
|
+
const knexMigrator = new KnexMigrator({
|
|
280
|
+
knexMigratorFilePath: process.cwd()
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
# Option 2: Pass object with config
|
|
284
|
+
const knexMigrator = new KnexMigrator({
|
|
285
|
+
knexMigratorConfig: { ... }
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
### Commands
|
|
291
|
+
|
|
292
|
+
```js
|
|
293
|
+
# Health
|
|
294
|
+
knexMigrator.isDatabaseOK
|
|
295
|
+
|
|
296
|
+
# Initialise database
|
|
297
|
+
knexMigrator.init
|
|
298
|
+
|
|
299
|
+
# Migrate database
|
|
300
|
+
knexMigrator.migrate
|
|
301
|
+
|
|
302
|
+
# Rollback database
|
|
303
|
+
knexMigrator.rollback
|
|
304
|
+
|
|
305
|
+
# Reset database
|
|
306
|
+
knexMigrator.reset
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
### Examples
|
|
310
|
+
|
|
311
|
+
```js
|
|
312
|
+
knexMigrator.isDatabaseOK()
|
|
313
|
+
.then(function() {
|
|
314
|
+
// database is OK
|
|
315
|
+
// initialization & migrations are not missing
|
|
316
|
+
})
|
|
317
|
+
.catch(function(err) {
|
|
318
|
+
if (err.code === 'DB_NOT_INITIALISED') {
|
|
319
|
+
return knexMigrator.init();
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
if (err.code === 'DB_NEEDS_MIGRATION') {
|
|
323
|
+
return knexMigrator.migrate();
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
# Test
|
|
330
|
+
|
|
331
|
+
- `yarn lint` run just eslint
|
|
332
|
+
- `yarn test` run eslint && then tests
|
|
333
|
+
- `NODE_ENV=testing-mysql yarn test` to test with MySQL
|
|
334
|
+
|
|
335
|
+
# Publish
|
|
336
|
+
|
|
337
|
+
- `yarn ship`
|
|
338
|
+
|
|
339
|
+
# Copyright & License
|
|
340
|
+
|
|
341
|
+
Copyright (c) 2013-2021 Ghost Foundation - Released under the [MIT license](LICENSE).
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
var program = require('commander'),
|
|
4
|
+
pkg = require('../package.json');
|
|
5
|
+
|
|
6
|
+
program
|
|
7
|
+
.version(pkg.version, '-v, --version')
|
|
8
|
+
.command('init [config]', 'init db').alias('i')
|
|
9
|
+
.command('migrate [config]', 'migrate db').alias('m')
|
|
10
|
+
.command('reset', 'reset db').alias('r')
|
|
11
|
+
.command('health', 'health of db').alias('h')
|
|
12
|
+
.command('rollback', 'rollbacks your db').alias('ro')
|
|
13
|
+
.parse(process.argv);
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
var program = require('commander');
|
|
4
|
+
var utils = require('../lib/utils');
|
|
5
|
+
|
|
6
|
+
var logging = require('../logging');
|
|
7
|
+
var knexMigrator;
|
|
8
|
+
|
|
9
|
+
utils.getKnexMigrator({path: process.cwd()})
|
|
10
|
+
.then(function (KnexMigrator) {
|
|
11
|
+
program
|
|
12
|
+
.option('--mgpath <path>')
|
|
13
|
+
.parse(process.argv);
|
|
14
|
+
|
|
15
|
+
try {
|
|
16
|
+
knexMigrator = new KnexMigrator({knexMigratorFilePath: program.mgpath, executedFromShell: true});
|
|
17
|
+
} catch (err) {
|
|
18
|
+
logging.error(err);
|
|
19
|
+
process.exit(1);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return knexMigrator.isDatabaseOK()
|
|
23
|
+
.then(function () {
|
|
24
|
+
logging.info('Woohoo, Database is healthy');
|
|
25
|
+
});
|
|
26
|
+
})
|
|
27
|
+
.catch(function (err) {
|
|
28
|
+
logging.error(err.message);
|
|
29
|
+
|
|
30
|
+
if (err.help) {
|
|
31
|
+
logging.info(err.help);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
process.exit(1);
|
|
35
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
var program = require('commander');
|
|
4
|
+
var utils = require('../lib/utils');
|
|
5
|
+
|
|
6
|
+
var logging = require('../logging');
|
|
7
|
+
var knexMigrator;
|
|
8
|
+
|
|
9
|
+
utils.getKnexMigrator({path: process.cwd()})
|
|
10
|
+
.then(function (KnexMigrator) {
|
|
11
|
+
program
|
|
12
|
+
.option('--skip <item>')
|
|
13
|
+
.option('--only <item>')
|
|
14
|
+
.option('--mgpath <path>')
|
|
15
|
+
.parse(process.argv);
|
|
16
|
+
|
|
17
|
+
try {
|
|
18
|
+
knexMigrator = new KnexMigrator({knexMigratorFilePath: program.mgpath, executedFromShell: true});
|
|
19
|
+
} catch (err) {
|
|
20
|
+
logging.error(err);
|
|
21
|
+
process.exit(1);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return knexMigrator.init({
|
|
25
|
+
skip: program.skip,
|
|
26
|
+
only: program.only
|
|
27
|
+
}).then(function () {
|
|
28
|
+
logging.info('Finished database init!');
|
|
29
|
+
});
|
|
30
|
+
})
|
|
31
|
+
.catch(function (err) {
|
|
32
|
+
logging.error(err);
|
|
33
|
+
process.exit(1);
|
|
34
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
var program = require('commander');
|
|
4
|
+
var utils = require('../lib/utils');
|
|
5
|
+
|
|
6
|
+
var logging = require('../logging');
|
|
7
|
+
var knexMigrator;
|
|
8
|
+
|
|
9
|
+
utils.getKnexMigrator({path: process.cwd()})
|
|
10
|
+
.then(function (KnexMigrator) {
|
|
11
|
+
program
|
|
12
|
+
.option('--v <item>')
|
|
13
|
+
.option('--only <item>')
|
|
14
|
+
.option('--mgpath <path>')
|
|
15
|
+
.option('--force')
|
|
16
|
+
.option('--init')
|
|
17
|
+
.parse(process.argv);
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
knexMigrator = new KnexMigrator({knexMigratorFilePath: program.mgpath, executedFromShell: true});
|
|
21
|
+
} catch (err) {
|
|
22
|
+
logging.error(err);
|
|
23
|
+
process.exit(1);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return knexMigrator.migrate({
|
|
27
|
+
version: program.v,
|
|
28
|
+
only: program.only,
|
|
29
|
+
force: program.force,
|
|
30
|
+
init: program.init
|
|
31
|
+
}).then(function () {
|
|
32
|
+
logging.info('Finished database migration!');
|
|
33
|
+
});
|
|
34
|
+
})
|
|
35
|
+
.catch(function (err) {
|
|
36
|
+
logging.error(err);
|
|
37
|
+
process.exit(1);
|
|
38
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
var program = require('commander');
|
|
4
|
+
var utils = require('../lib/utils');
|
|
5
|
+
|
|
6
|
+
var logging = require('../logging');
|
|
7
|
+
var knexMigrator;
|
|
8
|
+
|
|
9
|
+
utils.getKnexMigrator({path: process.cwd()})
|
|
10
|
+
.then(function (KnexMigrator) {
|
|
11
|
+
program
|
|
12
|
+
.option('--mgpath <path>')
|
|
13
|
+
.option('--force')
|
|
14
|
+
.parse(process.argv);
|
|
15
|
+
|
|
16
|
+
try {
|
|
17
|
+
knexMigrator = new KnexMigrator({knexMigratorFilePath: program.mgpath, executedFromShell: true});
|
|
18
|
+
} catch (err) {
|
|
19
|
+
logging.error(err);
|
|
20
|
+
process.exit(1);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return knexMigrator.reset({force: program.force})
|
|
24
|
+
.then(function () {
|
|
25
|
+
logging.info('Finished database reset!');
|
|
26
|
+
});
|
|
27
|
+
})
|
|
28
|
+
.catch(function (err) {
|
|
29
|
+
logging.error(err);
|
|
30
|
+
process.exit(1);
|
|
31
|
+
});
|