Skip to content

Migrate using decorators #4966

@antoine-pous

Description

@antoine-pous

Issue type:

[ ] question
[ ] bug report
[x] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo
[x] any

TypeORM version:

[ ] latest
[x] @next
[ ] 0.x.x (or put your version here)

Request:
Actually when we migrate we mostly use queryRunner.query which run raw SQL queries. But it would be awesome to be able to use decorators instead. Then a migration can be even more readable and it's code will become compliant with it's model.

Here's an example:
We add an index to a table, then just do

Actual way to migrate:

class addIndexToTable123456789 {
  up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`CREATE UNIQUE INDEX "IDX_1234" ON "schema"."table" ("col1", "col2", "col3") `)
  }

  down(): Promise<void> {
    await queryRunner.query(`DROP INDEX "schema"."IDX_1234"`)
  }
}

Requested changes:

@migrate
class addIndexToTable123456789 {
  @up()
  @addIndex('indexName', 'tableName', ['col1', 'col2', 'col3'], { unique: true })

  @down()
  @dropIndex('indexName', 'tableName')
}

The main goal is avoiding raw strings and use a minimalist code to perform migrations when it's possible, when we can't we can still use queryRunner.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions