Ecto.Adapters.SQL.to_sql

Hi, how could I use this function to view the SQL generated by mix ash_postgres.migrate?
Ecto.Adapters.SQL.to_sql

I think that’s right. I’d like to see the sql that generates the tables.

I’m migrating into postgres locally and viewing the build sql in pgadmin. Rather slow workflow.

And then I’m jumping into Supabase and building out the tables there too :slight_smile:

There‘s a setting for ecto to log the sql it applies in migrations or you could dump the schema after migrations are done.

to_sql can only translate what is applicable to Ecto.Querys, not what migrations do.

1 Like

Aha, @LostKobrakai thanks for the pointer. Here’s what Github Copilot had to say (which seems like a pretty good summary):

To enable logging of SQL queries in Ecto migrations, you need to configure the log level in your Ecto repository configuration.

In your config.exs file (or the appropriate environment-specific config file), find the configuration for your Ecto repository and set the :loggers option to :debug.

Here’s an example:

config :my_app, MyApp.Repo,
  adapter: Ecto.Adapters.Postgres,
  username: "postgres",
  password: "postgres",
  database: "my_app_dev",
  hostname: "localhost",
  pool_size: 10,
  loggers: [Ecto.LogEntry, :debug]

Replace :my_app and MyApp.Repo with your actual application and repository module names.

With this configuration, Ecto will log all SQL queries it executes, including those from migrations, at the :debug level. You can see these logs in your console when you run migrations.

Please note that logging at the :debug level can generate a lot of output, so you may want to disable it or set it to a higher level (like :info or :warn) when you’re not actively debugging.

Not really. The :loggers config is deprecated since ecto 3.0 and again is about ecto queries not migrations. You want TIL: Log SQL generated by Ecto migration | Bartosz Górka

1 Like

mix ash_postgres.migrate will forward additional options to mix ecto.migrate, so you should be able to add those options :slight_smile: