Typeorm Migrate Sql - Generate Sql Files and Migrate
Typeorm Migrate Sql is a package that can generate new migrations in sql format and runs those generated migrations.
- MySQL
- MariaDB
| Engine/Package | Version | Optional |
|---|---|---|
| Node.js | ^12.20.0 or >=14 | |
| TypeORM | ^0.3.0 | |
| Node MySQL 2 | ^2.2.5 | |
| ts-node | ^10.7.0 | true |
You can install Typeorm Migrate Sql using npm, yarn, or pnpm.
# npm npm i typeorm-migrate-sql # yarn yarn add typeorm-migrate-sql # pnpm pnpm i typeorm-migrate-sqlRunning our command via npx.
npx typeorm-migrate-sql helpWe recommend that you add the following to package.json for convenience.
"scripts": { "migrate": "typeorm-migrate-sql" }Then you may run the command like following.
npm run migrate -- helpnpx typeorm-migrate-sql create add_userThe command creates two empty files called <timestamp>_add_user.up.sql and <timestamp>_add_user.down.sql in directory db/migrations. You can specify the directory via cli options.
npx typeorm-migrate-sql generate my_schema add_userAfter modifying typeorm entities, you can run this command to generate two sql files called <timestamp>_add_user.up.sql and <timestamp>_add_user.down.sql in directory db/migrations. You can set connection options, location of entities, and the output directory via cli options.
npx typeorm-migrate-sql version my_schemaThe command gets current migration version recorded in the database. Connection and some other options can be set via cli options.
# run either one npx typeorm-migrate-sql up my_schema npx typeorm-migrate-sql down my_schema npx typeorm-migrate-sql goto my_schema 1637690400000These commands run migrations to a version. up applies an up migration, down applies a down migration, while goto migrates to the specific version. Similarly, options can be set via cli. Note that if the last migration failed, these commands will also fail.
The package is inspired by golang-migrate.
Contributing is welcome!
MIT