Proposal - Output the migration code, after the generate has run. (Was issue #50095)

Following conversation with @fatkorrum (Proposal - Output the migration code, after the generate has run. · Issue #50095 · rails/rails · GitHub)

Motivation

So that we help developers even more, when writing migrations, output the migration code after it has been run. Since the migration code is typically small, we could output it straight after the migration has run, and save you going to open that file, if it has successfully added the field (in this instance).

Steps to reproduce

rails g migration AddCodeToCountries code:string

Expected behavior

  invoke  active_record
  create    db/migrate/20231118142828_add_code_to_countries.rb
  code     

class AddCodeToCountries < ActiveRecord::Migration[7.0] def change add_column :countries, :code, :string end end

Actual behavior

  invoke  active_record
  create    db/migrate/20231118142828_add_code_to_countries.rb

more db/migrate/20231118142828_add_code_to_countries.rb

class AddCodeToCountries < ActiveRecord::Migration[7.0] def change add_column :countries, :code, :string end end

Also happy to implement, with tests. Would love to do it mob programming style and involve as many rubyists as possible. But first, is it good rails thinking?

That’s a fun idea. One thing I have often wished for, along these lines, was an option to immediately open the resulting migration in my editor, since I often need to make further adjustments after generating the raw migration (setting defaults and null options, for example). I would love to have an --edit / -e option to throw in there that would pipe the resulting file into my editor and save me some mousing.

Walter

1 Like

This can be helpful for developers to quickly verify the changes made by the migration without manually opening the migration file.

1 Like

Would that have to be a system command though?

I use this trick for years - Open last generated migration easily in your editor (Example)

I do not have a strong opinion on how much this will be useful. But personally, I do not find it hard to double click the generated migration file name on the terminal and just cat it or open in the editor.

Thanks! That’s excellent.

Walter