|
1 | 1 | # Typeorm Migrate Sql |
2 | 2 |
|
3 | 3 | Typeorm Migrate Sql - Generate Sql Files and Migrate |
| 4 | + |
| 5 | +Typeorm Migrate Sql is a package that can generate new migrations in sql format |
| 6 | +and runs those generated migrations. |
| 7 | + |
| 8 | +## Supported Databases |
| 9 | + |
| 10 | +- MySQL |
| 11 | +- MariaDB |
| 12 | + |
| 13 | +## Requirements |
| 14 | + |
| 15 | +| Engine/Package | Version | Optional | |
| 16 | +|------------------------|----------|----------| |
| 17 | +| Node.js | >=12.9.0 | | |
| 18 | +| [TypeORM][typeorm] | ^0.3.0 | | |
| 19 | +| [Node MySQL 2][mysql2] | ^2.2.5 | | |
| 20 | +| [ts-node][ts-node] | ^10.7.0 | true | |
| 21 | + |
| 22 | +## Installation |
| 23 | + |
| 24 | +You can install Typeorm Migrate Sql using `npm`, `yarn`, or `pnpm`. |
| 25 | + |
| 26 | +```sh |
| 27 | +# npm |
| 28 | +npm i typeorm-migrate-sql |
| 29 | +# yarn |
| 30 | +yarn add typeorm-migrate-sql |
| 31 | +# pnpm |
| 32 | +pnpm i typeorm-migrate-sql |
| 33 | +``` |
| 34 | + |
| 35 | +## Usage |
| 36 | + |
| 37 | +### CLI |
| 38 | + |
| 39 | +Running our command via `npx`. |
| 40 | + |
| 41 | +```sh |
| 42 | +npx typeorm-migrate-sql help |
| 43 | +``` |
| 44 | + |
| 45 | +We recommend that you add the following to `package.json` for convenience. |
| 46 | + |
| 47 | +```json |
| 48 | +"scripts": { |
| 49 | + "migrate": "typeorm-migrate-sql" |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +Then you may run the command like following. |
| 54 | + |
| 55 | +```sh |
| 56 | +npm run migrate -- help |
| 57 | +``` |
| 58 | + |
| 59 | +#### Create a Set of Up/Down Migrations |
| 60 | + |
| 61 | +```sh |
| 62 | +npx typeorm-migrate-sql create add_user |
| 63 | +``` |
| 64 | + |
| 65 | +The command creates two empty files called `<timestamp>_add_user.up.sql` and |
| 66 | +`<timestamp>_add_user.down.sql` in directory `db/migrations`. You can specify |
| 67 | +the directory via cli options. |
| 68 | + |
| 69 | +#### Generate a Set of Up/Down Migrations |
| 70 | + |
| 71 | +```sh |
| 72 | +npx typeorm-migrate-sql generate my_schema add_user |
| 73 | +``` |
| 74 | + |
| 75 | +After modifying typeorm entities, you can run this command to generate two sql |
| 76 | +files called `<timestamp>_add_user.up.sql` and `<timestamp>_add_user.down.sql` |
| 77 | +in directory `db/migrations`. You can set connection options, location of |
| 78 | +entities, and the output directory via cli options. |
| 79 | + |
| 80 | +#### Get Current Migration Version |
| 81 | + |
| 82 | +```sh |
| 83 | +npx typeorm-migrate-sql version my_schema |
| 84 | +``` |
| 85 | + |
| 86 | +The command gets current migration version recorded in the database. Connection |
| 87 | +and some other options can be set via cli options. |
| 88 | + |
| 89 | +#### Run Migrations |
| 90 | + |
| 91 | +```sh |
| 92 | +# run either one |
| 93 | +npx typeorm-migrate-sql up my_schema |
| 94 | +npx typeorm-migrate-sql down my_schema |
| 95 | +npx typeorm-migrate-sql goto my_schema 1637690400000 |
| 96 | +``` |
| 97 | + |
| 98 | +These commands run migrations to a version. `up` applies an up migration, `down` |
| 99 | +applies a down migration, while `goto` migrates to the specific version. |
| 100 | +Similarly, options can be set via cli. |
| 101 | + |
| 102 | +## Inspiration |
| 103 | + |
| 104 | +The package is inspired by [golang-migrate][golang-migrate]. |
| 105 | + |
| 106 | +## Contributing |
| 107 | + |
| 108 | +Contributing is welcome! |
| 109 | + |
| 110 | +## License |
| 111 | + |
| 112 | +MIT |
| 113 | + |
| 114 | +## Links |
| 115 | + |
| 116 | +[GitHub](https://github.com/2020leon/typeorm-migrate-sql), |
| 117 | +[npm](https://www.npmjs.com/package/typeorm-migrate-sql), |
| 118 | +[yarn](https://yarnpkg.com/package/typeorm-migrate-sql) |
| 119 | + |
| 120 | +[typeorm]: https://github.com/typeorm/typeorm |
| 121 | +[mysql2]: https://github.com/sidorares/node-mysql2 |
| 122 | +[ts-node]: https://github.com/TypeStrong/ts-node |
| 123 | +[golang-migrate]: https://github.com/golang-migrate/migrate |
0 commit comments