Skip to content

Commit e111965

Browse files
authored
Merge pull request #23 from tinemlakar/feat/timestamp-support
Expanded logic to support non sequential tags (timestamps)
2 parents e76bd58 + 2886ff3 commit e111965

File tree

18 files changed

+6633
-4440
lines changed

18 files changed

+6633
-4440
lines changed

.example.env

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Here are exmaples of enviroment variables that is used in the code.
2+
# If yo want to use .env files in your project you might need to manually install dotenv package (it's dev only dependency on this package).
3+
4+
5+
# set to true, if you want to ensure that new migratiosn scripts are followong index pattern/timeline
6+
# Default "true" in "NODE_ENV='production", "false" otherwise
7+
MIGRATIONS_STRICT_ORDER=true
8+
9+
# For scripts ordered by numbers. Migration will fail if number will missmach the migration version.
10+
# To enable migrations scripts ordered by timestamp, this must be voided or set to false.
11+
# Default = false
12+
MIGRATIONS_NUMERIC_ORDER=false
13+
14+
# Default path to the migrations folder for CLI script generator
15+
MIGRATION_FOLDER=
16+
17+
# For running tests in this package you have to set database connection in env
18+
DB_HOST=
19+
DB_PORT=
20+
DB_USER=
21+
DB_PASSWORD=
22+
DB_DATABASE=

.github/workflows/nodejs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99

1010
strategy:
1111
matrix:
12-
node-version: [8.x, 10.x, 12.x]
12+
node-version: [16.x, 18.x, 20.x]
1313

1414
steps:
1515
- uses: actions/checkout@v2

README.md

Lines changed: 76 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,36 @@
22

33
MySQL migration tool for typescript projects. Supports mysqljs and node-mysql2 driver.
44

5-
## IMPORTANT NOTICE
6-
7-
This code is work in progress. Please test your upgrade and downgrade scripts well before use it on production database!
8-
9-
Please also note the following paragraph from License:
10-
11-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17-
SOFTWARE.
18-
195
## Installation
206

217
```ssh
228
npm i ts-mysql-migrate
239
```
2410

11+
## Changelog
12+
13+
### v1.1.0
14+
15+
Now it is possible to [generate migration scripts](#cli--support-for-timestamp) with timestamps in order not to create problems when merging from different branches. That was an annoying issue in larger teams working on same project.
16+
17+
You can control behavior of strict order with environment variable `MIGRATIONS_STRICT_ORDER`. See [environment variables](#environment-variables) for details.
18+
19+
## Environment variables
20+
21+
This project utilizes environment variables to configure various aspects of the migration process. You can define these variables in a `.env` file at the root of your project. To use `.env` files, ensure the `dotenv` package is installed, as it is required for loading these variables into your environment.
22+
23+
Here are some key environment variables used in this project:
24+
25+
* **`MIGRATIONS_STRICT_ORDER`**: Ensures migration scripts follow a strict index/timeline order. Possible to override with passing `strictOrder` parameter to constructor. Default is `true` in production (`NODE_ENV='production'`), otherwise `false`.
26+
* **`MIGRATIONS_NUMERIC_ORDER`**: Enforces strict number order for migration scripts ordered by numbers. Should be disabled if scripts are prefixed with timestamps. Possible to override with passing `numOrder` parameter to constructor. Set to `false` by default.
27+
* **`MIGRATION_FOLDER`**: Specifies the default path to the migrations folder for the CLI script generator.
28+
* **Database Configuration for running tests**:
29+
* `DB_HOST`: Database host
30+
* `DB_PORT`: Database port.
31+
* `DB_USER`: Database user.
32+
* `DB_PASSWORD`: Database password.
33+
* `DB_DATABASE`: Database name.
34+
2535
## API
2636

2737
### initialize()
@@ -48,7 +58,9 @@ Closes database connection and releases handles.
4858

4959
### Create migration scripts
5060

51-
Create migration scripts and name them with number prefix to set the order of execution (versions). Each migration script should have ```upgrade``` and ```downgrade``` functions exported. These functions must have ```queryFn``` as parameter - see examples below.
61+
Create migration scripts and name them with number prefix to set the order of execution (versions). In v2 you can also generate it with [CLI](#cli--support-for-timestamp) to have a timestamp as identifier.
62+
63+
Each migration script should have ```upgrade``` and ```downgrade``` functions exported. These functions must have ```queryFn``` as parameter - see examples below.
5264

5365
Example: ```/src/migrations/1-init_db.ts```
5466

@@ -178,3 +190,52 @@ You can put them in ```package.json``` and run it from npm. Example:
178190
"downgrade-production": "node -r ts-node/register ./src/scripts/downgrade-prod.ts",
179191
},
180192
```
193+
194+
## CLI & support for timestamp
195+
196+
> New in v2.
197+
198+
You can now save script names with any number as a prefix - this is meant to be used with unix timestamp. To generate script with current timestamp, you can run CLI command from local installation by running
199+
200+
```sh
201+
npx generate-migration
202+
```
203+
204+
Alternatively you can also install package globally
205+
206+
```sh
207+
npm i ts-mysql-migrate -g
208+
```
209+
210+
Then you can call the command in the root of your project.
211+
212+
```sh
213+
generate-migration
214+
```
215+
216+
You can set the env variable `MIGRATION_FOLDER` to customize default or even set script in your `package.json` for example:
217+
218+
```json
219+
{
220+
"scripts":{
221+
"new-migration": "cross-env MIGRATION_FOLDER=my-migration-folder generate-migration"
222+
}
223+
}
224+
225+
```
226+
227+
and then just call `npm run new-migration` when you'd like to create new migration.
228+
229+
## IMPORTANT NOTICE
230+
231+
Please test your upgrade and downgrade scripts well before use it on production database!
232+
233+
Please also note the following paragraph from License:
234+
235+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
236+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
237+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
238+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
239+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
240+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
241+
SOFTWARE.

jestconfig.json

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
{
2+
"testTimeout": 1800000,
3+
"moduleFileExtensions": [
4+
"ts",
5+
"tsx",
6+
"js",
7+
"json"
8+
],
9+
"testPathIgnorePatterns": [
10+
"dist/*"
11+
],
12+
"transformIgnorePatterns": [
13+
"node_modules/*"
14+
],
15+
"testRegex": ".*\\.test\\.ts$",
216
"transform": {
3-
"^.+\\.(t|j)sx?$": "ts-jest"
17+
"^.+\\.(t|j)s$": "ts-jest"
418
},
5-
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
6-
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"],
7-
"verbose": true,
19+
"collectCoverage": false,
20+
"collectCoverageFrom": [
21+
"**/*.(t|j)s"
22+
],
23+
"coverageDirectory": "../coverage",
824
"testEnvironment": "node",
9-
"collectCoverage": false
25+
"preset": "ts-jest",
26+
"verbose": true
1027
}

0 commit comments

Comments
 (0)