Upgrading Rails 7 alpha to the future versions (and fix bugs in generated files)

I already have many Rails projects, but today we are starting a new project and I would be happy to use Rails 7 directly.

It is an alpha version, but I’m pretty sure that within a few months when we release in production there will be a stable version of Rails 7.

My only concern is about the scaffold generated by rails new. What if there are some bugs in the Rails alpha and we end up with buggy code forever in our git repository?

Is there any way to make sure that when the final release of Rails 7 is out we can check all the generated files?

My only idea is to run rails app:update when the final release of Rails 7 is available… although it’s not really an update, since we are already on Rails 7. Is that the correct approach to fix any files that were wrong in the alpha?

I would not let the tail wag the dog like this. The scaffolding is a once in a lifetime feature, and once it runs, nothing (certainly not the rake app:update) will change it. Read up on what that task does, by the way. It’s very limited.

You can fix the scaffolding by copying the templates into your app, I believe at lib/tasks/templates, and editing them there. Remember, though, that will only affect new models that you scaffold after making these changes and additions. Nothing affects the files that are output by the scaffolding process after they have been created.

Walter

If you want to be as close as possible to the final version you can:

  • Keep your config/master.key in a safe place
  • Backup your app directory
  • Make sure your local git repository is in clean state
  • Remove all default Rails files from your app except git ignored files
  • Create a new app using the latest version
  • In the new app directory run git clean -fdX to remove all ignored files by git
  • Copy all directory content to your original app
  • Go to your app and run git diff, or see the changes in your favorite text editor or GUI application
  • Review the changes, keep what you want and remove what you don’t want

I recommend looking at https://railsdiff.org/ instead and cherry-pick manually what you want.

3 Likes

Here’s an easy way: when Rails 7.0 final comes out run the exact same rails new command you will use today, and let it overwrite all your existing files. You will notice that some files will be skipped because they are identical, some will be changed because they aren’t.

Then it’s just a matter of using diff to see what changed and fix as needed. I’ve been updating an app since 5.1, so I’ve done this a few times. Works fine.

3 Likes

https://railsdiff.org is a super helpful tool after v7 is released. I won’t update the config unless I have to until v7 is released.

Saves me a ton of work. Started a new project (rails 7) in august or September and not had a single issue that wasn’t already present on previous rails versions.

1 Like