Rails: Upgrading a Legacy Rails App Part 1
Upgrading a legacy rails app is a chore for every rails developer. Knowing the right strategy can save you a lot of time. We'll be showing you exactly how to go about it in this episode!
We will also be showing you why you should set up the see seeds.rb file, for the application.
Also check out our previous episode where we discuss why Rails is more relevant now than ever.
Upgrading Rails 4.2
If we try booting up our app right after cloning chances are it probably won't work. We'll need to update the rails version, let's do that first!
# in Gemfile
# gem 'rails', '220.127.116.11'
gem 'rails', '4.2.10'
We'll upgrade to the latest stable release for 4.2. Go ahead and run bundle update to upgrade rails. We should now be able to successfully install all of our dependencies. Once the update process is done let's boot up our app with rails s .
If we head to localhost:3000 in our browser we will get an error telling us that we haven't setup the database.
Configuring up the Database
Let's configure config/database.yml . Modify the settings so your rails app can connect to the database. All I had to do was change the username . But your configuration may differ. Then let's run rake db:setup and boot our app up again.
At this point we should be able to hit the landing page of our application.
Creating the Admin User
At this point we'll need to create an admin user if we're going to be doing anything. Let's go ahead and do that in the console.
name: "Zack Siri",
We should now be able to login as the admin if we head over to localhost:3000/admin . Go ahead and create a post and publish it.
Rails Seeds.rb file
To save ourselves and anyone else who inherit the project from having to spend time figuring out how to start / setup the admin user in the future we can put the code above in the seeds.rb file. Go ahead and copy the above code and put it in the seeds file. We can try out our seeds file by running the below sequence
rake db:drop # drops the existing database
rake db:setup # setup the new database
rake db:seed # runs the db/seeds.rb
That should be it! We now have a working Rails 4.2.10 application. We will continue the upgrade to 5.2 in the next episode. If you would like to see what the changes are from 4.2.10 to 5.2.1 check out railsdiff.org