Revert Last Migration

Imagine a situation in which you prepared some migrations to pre - populate data in the database, but after a while you need to add new pre- existing data. In the context of a cooking app, a new ingredient becomes available, and you want to add it to the existing database.

Surely you can re-create the table all over again, but you want to do it in production, on a live database.

In order to do that we must first enable revert` (there will be more run time commands in the future) Fluent command.

In configure.swift add this code in configure function:

  //Adds Fluent’s commands to the CommandConfig. Currently only the RevertCommand at "revert".
  var commandConfig = CommandConfig.default()

To see what run commands are available for a Vapor app, just type:

<path to your Vapor app>/Run -h

After I added extra command, I got this as a result:

  routes Displays all registered routes
  revert Reverts migrations that have been previously prepared.
         By default, only the latest batch of migrations will be reverted.
   serve (default) Begins serving the app over HTTP

And by typing:

<path to your Vapor app>/Run revert

I have reverted the last migration:

mmj$ <path to your Vapor app>Run revert
[ INFO ] Migrating 'psql' database (FluentProvider.swift:28)
[ INFO ] Migrations complete (FluentProvider.swift:32)
[ INFO ] Revert last batch of migrations requested (RevertCommand.swift:57)
[ WARNING ] This will revert the last batch of migrations for all configured databases (RevertCommand.swift:58)
Are you sure you want to revert the last batch of migrations?
y/n> y
[ INFO ] Reverting last batch of migrations on 'psql' database (RevertCommand.swift:65)
[ INFO ] Reverting migration 'PopulateTypes' (MigrationContainer.swift:94)
[ INFO ] Reverting migration 'Type' (MigrationContainer.swift:94)
[ INFO ] Succesfully reverted last batch of migrations (RevertCommand.swift:69)

Just an information. The default command is serve, so when you start your Vapor app, you may as well have typed:

mmj$ <path to your Vapor app>Run serve

Prev: Adding Pagination To Vapor Query

Next: Stop / Start Docker Apps

Vapor 3 Tutorial Start

#pub #vapor #postgresql #ddl #migration #revert