TIL: Log SQL generated by Ecto migration

Many libraries that we use in everyday work have options that we may not know about. One of them may be the option to check what SQL code is executed in case of migration in Ecto.

Checking the generated SQL

Understanding exactly what SQL commands are running is helpful to ensure safe migrations. By default, Ecto does not log the raw SQL. However, we can change it.

The following code works for both migrate and rollback.

  mix ecto.migrate --log-migrations-sql --log-migrator-sql

Thanks to --log-migrations-sql, we can get a log of SQL commands created from our migration file. The --log-migrator-sql option allows us to determine what the migrator did (transactions, table lock).

It may seem unnecessary, but sometimes it can come in handy. Especially if we want to understand how certain operations are performed or how long it will take to complete the migration.

The indicated commands apply to version 3.7.1 and later. For older versions, use --log-sql.

I encourage you to read more in Hexdocs.

Get new posts and extra comments

You'll receive every new post with extra unpublished comments available only to the subscribers!

I won't send you spam. Unsubscribe at any time.