Recently I started working on a Rails project without having a good name picked out, so I gave the application a temporary one for the time being. I ended up with a Github repository and a Heroku app that used that temporary name, but after finally thinking of the perfect name, I naturally wanted to update it everywhere. This is how I did it!
I'll go over how I changed the repository name on Github and the name of the app on Heroku, and how you can go about changing the name of the databases used by Rails.
For reference, this is the setup of my app:
- Rails 6.0.3.2
- Ruby 2.7.1
- Code on Github
- Deployment to Heroku
- PostgreSQL database in both production and development
Rename the Github repository
To change the name on Github, simply go to the repository page online, navigate to repository's Settings page and change the name using the "Repository name" field.
Github will take care of forwarding to the new URL whenever the old URL is accessed, for example when you push or pull, but I think it would be better to make sure the remote URLs on your local machine also match up.
To update the remote on your computer, first, grab the new url from the repository page on Github, which you can get from the "Clone with SSH" button.
It should look something like this:
git@github.com:your-username/reponame.git
Next, navigate to the directory where your Rails app lives on your local machine.
You can check the currently registered remote URL that connects the git repository to Github with the following command (assuming remote is named "origin", which it should be unless you changed it):
git config --get remote.origin.url
At this point, it should still display the old remote URL, something like git@github.com:your-username/old-repo-name.git
.
To update it, enter the following command using the URL you copied from Github:
git remote set-url origin git@github.com:your-username/new-reponame.git
You can double check that it got updated by running the git config --get remote.origin.url
command again.
Okay! That should take care of the Github side! Next, let's update things on the Heroku side.
Rename the app on Heroku
This is done easiest from the command line because it will also automatically update the remote URL for Heroku as well. Type the following command in the command line from within the app's directory, replacing new-name
with new name of your app:
heroku apps:rename new-name
Simple as that! You can check to make sure the remote URL for Heroku has been updated with the following command:
git config --get remote.heroku.url
It should look something like https://git.heroku.com/new-name.git
.
If you navigate to your Heroku dashboard online, you will see that everything has been updated!
Well, almost everything... If you had the Github repository connected to Heroku under the "Deploy" settings, it will still have the old Github URL. Though Github will automatically redirect any access to this URL, you might want to update it here too. Just use the "disconnect" button to disconnect and then reconnect to the newly renamed repository.
Rename the Rails databases
Although you don't technically have to rename the databases, you may want to anyways just to be consistent.
When you first ran rails new
command, it creates a file for database configuration, the config/database.yml
file. The databases are named after the app name you passed along to the rails new
command, so in the database.yml
file you will find settings that look something like this (where app_name
is the name of the app):
# config/database.yml
# ... other code
development:
<<: *default
database: app_name_development
test:
<<: *default
database: app_name_test
production:
<<: *default
database: app_name_production
username: app_name
password: <%= ENV['APP_NAME_DATABASE_PASSWORD'] %>
Development/Test database configuration
Depending on how you set up your data for the development environment (if at all), you can run into a few different situations:
- You have a
db/seeds.rb
file that sets up all the data. - You did not use the
db/seeds.rb
file, but the data created while testing out the app in development isn't that important and you don't care if it gets erased. - You did not use the
db/seeds.rb
file, but you want to keep the data created while testing out the app in development, so it should NOT get erased.
Situation 1: You have a db/seeds.rb
file for all your data
First, drop (= destroy) the database with this rails command:
rails db:drop
If successful, you'll see the following output:
$ rails db:drop
Dropped database 'app_name_development'
Dropped database 'app_name_test'
Rename the development and test databases in the config/database.yml
file, replacing new_name
with the actual name you want to use:
# config/database.yml
# ... other code
development:
<<: *default
database: new_name_development
test:
<<: *default
database: new_name_test
# ... more code
Run rails db:setup
to recreate the databases and fill it with your seed data.
That's it! Everything should work like before.
Situation 2: You don't have seed data, but don't care if everything is erased
First, drop (= destroy) the database with this rails command:
rails db:drop
If successful, you'll see the following output:
$ rails db:drop
Dropped database 'app_name_development'
Dropped database 'app_name_test'
Rename the test and development databases in the config/database.yml
file as described above.
Then run rails db:create
and rails db:migrate
to set up a sparkling new database with a new name.
Situation 3: You don't have seed data, but you want to make sure no data gets erased.
I am using a PostgreSQL database in my development environment and have PostgreSQL installed locally, so this explanation will be specific to this setup. If you're using a different database locally like Sqlite, you'll have to look up the specific methods for that database.
Using a database dump (backup) file
If you do need to backup data you had for development, you can use the pg_dump
tool provided by your PostgreSQL database.
I used this tutorial, Backing Up and Restoring A Database, as a reference, so check it out for more in-depth explanation.
Create the database dump file
Create the backup file with the following command on the command line from within the Rails app directory, replacing app_name_development
with the actual name of your database listed in the config/database.yml
file:
pg_dump -F t app_name_development > development_backup.tar
-F t
formats the file into a tar
file. development_backup.tar
is the name for the backup file, which you can name it anything you want.
Learn more about the pg_dump
command in the PostgreSQL documentation
After the running the above command, a database backup file should have been created in the root directory of your Rails app.
Drop the current database
Drop (= destroy) the database with the Rails command (this will erase everything, so make sure your backup was created):
rails db:drop
If successful, you'll see the following output:
$ rails db:drop
Dropped database 'app_name_development'
Dropped database 'app_name_test'
If you run the Rails server and try to access the app, you'll also see a ActiveRecord::NoDatabaseError
.
Rename and recreate the database
Rename the development and test databases in the config/database.yml
file, which still contains the old database names.
# config/database.yml
# ... other code
development:
<<: *default
database: old_app_name_development
test:
<<: *default
database: old_app_name_test
Rename all the things with the old_app_name
to the new app name.
After that, run rails db:create
in the command line to create the newly named database. You will see output like this:
$ rails db:create
Created database 'new_name_development'
Created database 'new_name_test'
Restore the database from the backup file
To restore from the database backup, you can use pg_restore
command, replacing new_name_development
with actual name in your database.yml
:
pg_restore -d new_name_development development_backup.tar
The -d new_name_development
option lets you connect to given database (here it's new_name_development
) and restore directly into it. development_backup.tar
specifies the name and location of the dump file that you created earlier.
And there you go! Your development and test database are now renamed and the previous data has been restored for development.
Production database configuration
The production database is the one that Heroku uses when running the deployed app. Rails generated the following production database configuration when I first created the app:
# config/database.yml
# ... other code
production:
<<: *default
database: app_name_production
username: app_name
password: <%= ENV['APP_NAME_DATABASE_PASSWORD'] %>
In my case, I just have a basic Heroku setup and don't have any custom database configurations. If that is the case, then you don't actually need this configuration because Heroku uses it's own configuration through an environment variable set as ENV["DATABASE_URL"]
, which will override the above configuration in the yaml file.
In other words, renaming the production database here doesn't do anything. Instead, I would recommend changing the configuration to explicitly say that you are using the environment variable (also recommended in the Rails Guides).
In fact, you may have a comment in your database.yml
just above the production
settings that tells you how to do this depending on how the Rails app was initially set up:
# config/database.yml
# ... Other settings
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
# production:
# url: <%= ENV['DATABASE_URL'] %>
So, as a last step, change the production database configuration inside database.yml
so that it looks like this:
# config/database.yml
# ... Other settings
production:
url: <%= ENV['DATABASE_URL'] %>
And we are finished! Everything should now be renamed to the correct app name :D
Top comments (1)
You should also rename the module in the
application.rb
at least