Published on

Nx Commands For Large Migrations

Authors

Migrate command

These 2 flags make the migration script even more powerful. When you have 10 or more migrations to run at once, it's helpful to be able to review the migrations individually afterwards.

--create-commits

This flag creates individual commits for each migration which is helpful when you have several to run.

--create-prefix

This flag lets you prefix each commit from the flag above.

Seeing it all together:

nx migrate --run-migrations --create-commits --commit-prefix="Run Migration: AUTOMATED - " --verbose

Save the migration output

The more migrations you run, the more output you'll recieve. So much so that your shell may overflow the scroll history, leaving you unable to scroll back to read the output and see what happened.

To avoid that you can pipe the output to text file. Assuming you're on mac or linux, the following lets you pipe the output of any command to a local file.

{COMMAND} 2>&1 | tee ~/Desktop/migration-output.txt

# Full example

nx migrate --run-migrations --create-commits --commit-prefix="Run Migration: AUTOMATED - " --verbose 2>&1 | tee ~/Desktop/migration-output.txt

2>&1

This will pipe both stderr and the output to the rest of the command - source

Run previous migrations

If you've run a migration before, you can get into a situation where Nx thinks it has already run the necessary migrations. However, you can rerun the migrations as if it's the first time by using --from to tell Nx where to start the migrations from and --exclude-applied-migrations which will collect package updates and migrations starting with the version supplied in to --from regardless of what is installed locally, while excluding migrations that should have been applied on previous updates”.

nx migrate latest --from=nx@14.5.0 --exclude-applied-migrations

If you're using this command this and upgrading an Angular Nx project the Nx and Angular Version Compatibility Matrix is helpful to compare the compatability of previousv versions of Angular and Nx.

Thanks for reading!

Kevin