What goes up…

How about we expect to be that subsequent to conveying everything to creation, the business group concludes that they need to empower clients to alternatively add their genuine name to their profiles. Taking everything into account, we should simply make another one:

$ ./node_modules/.receptacle/hub pg-relocate make add-client name ac

Furthermore, execute the change:

trade async work up(pgm: MigrationBuilder): Promise<void> {

pgm.addColumn(‘users’, {

name: { type: ‘string’ },



In the wake of applying the new movement – similarly as beforehand – , we presently have an extra name segment that we can utilize. Incredible achievement!

…should descend

When testing the new component on the organizing climate, the business group understood that it would be vastly improved to store given and family name independently. As it turns, this is really not inconsequential so the whole element was rejected for the present. Fortunately, moving this back in the data set is simple as pie:

$ ./node_modules/.container/hub pg-move down

> Migrating documents:

> – 20210312130143080_add-client name

### MIGRATION 20210312130143080_add-client name (DOWN) ###

Modify TABLE “clients”

DROP “name”;

Erase FROM “public”.”pgmigrations” WHERE name=’20210312130143080_add-client name’;

Relocations complete!

Note that while up runs every single forthcoming relocation, down just returns the latest one.

However, Moji Developers stand by a moment – we never carried out the down() work! What’s going on? Incidentally, numerous strategies like pgm.createTable() or pgm.addColumn() “know” how to fix their own progressions which empowers pg-move to advantageously produce the converse relocation for us. Tragically, a considerable lot of the more oftentimes utilized capacities, most noticeably pgm.alterColumn() and pgm.alterTable(), don’t fall under that class. Endeavoring to relocate down will come up short, it is in these conditions that one needs to physically carry out the down() work:

trade async work up(pgm: MigrationBuilder): Promise<void> {

pgm.alterColumn(‘users’, ‘name’, { type:’varchar(255)’ })


trade async work down(pgm: MigrationBuilder): Promise<void> {

pgm.alterColumn(‘users’, ‘name’, { type: ‘text’ })


An extraordinary case here is pgm.addConstraint() which doesn’t uphold surmising the down relocation in the event that we depend on the auto-created imperative name, however will turn out great on the off chance that we don’t. There are additionally a few highlights that are not yet upheld by the MigrationBuilder (like allowing consents or embeddings, erasing or refreshing lines) in which case we need to depend on crude SQL articulations with pgm.sql(). Executing self-assertive SQL clearly implies that it is inconceivable for the library to naturally induce the down movement, so it will again toss a blunder and advise us so much in the event that we request that it do as such. As a side note, pg-relocate doesn’t accompany support for getting away so utilizing an extra library, for example, pg-escape is suggested.

Now and then it is plain difficult to fix a relocation, for example in the event that you drop a section or a table and it is absolutely impossible to reproduce the information whenever it is no more. For this situation we can illuminate the library like so:

trade const down = bogus;


In case PostgreSQL is the data set crate you put your eggs in and you don’t utilize an ORM that accompanies a relocation system, pg-move is an unshakable, lightweight and easy to utilize decision. There are as yet a couple of issues to address practically speaking, like how and when to really run the relocations. While this not unimportant to reply in everyday terms, tracking down a commonsense arrangement in a substantial task with a realized engineering is generally lovely straight forward.

Leave a Comment

Your email address will not be published. Required fields are marked *