Rails On Subversion

When I develop a Rails project, I always do the development on my local Windows laptop and then deploy it to a Linux server.

So even if I am the only one working on that project, I still use Subversion (SVN) because it makes it easier to deploy and automatically creates a backup.

Setting up Rails on SVN is pretty straightforward – just import your entire Rails application subdirectory tree. However, there are a few things you can do to fine-tune this, mainly excluding files that don’t need to be put in SVN, and creating some templates for those files that will change when you deploy it.

Local Environment

The config/environment.rb file is where the environment variables are set. However, there will sometimes be variables that are needed for the project no matter where it is deployed, and others that are location or platform dependent. For instance, if you are using ActionMailer it is likely that your delivery_method and server_settings will be different on your development computer than on your deployment computer.

So we could put all these in environment.rb and create a template for it. However the problem with this method is that if we later want to add some variable assignments to environment.rb, they will not be picked up by our deployment computer.

The solution is to break the environment into two files: those that are the same for the application no matter where it is deployed, and those that might change. I leave the first type in environment.rb and create a file local_environment.rb for the second. Then just add the following line to the end of environment.rb:

require 'local_environment'

Template Files

These files will change depending on where you are deploying your application, so it is best to create a template file and then tell SVN to ignore the real file. This way, you can make local changes to the file and they won’t get committed back to SVN. This prevents other deployments from getting messed up when they are updated from SVN.

For the following files, copy the file to a new file with a .template extension, then ignore the original file. For instance, copy database.yml to database.yml.template then ignore database.yml.

  • config/database.yml
  • config/local_environment.rb
  • public/dispatch.cgi
  • public/dispatch.fcgi
  • public/.htaccess

To ignore a file in Eclipse using Subclipse, first copy the file to a temp directory, then delete the file in Eclipse. Next, commit the parent directory to delete the file in the SVN repository. Finally, copy the file back, refresh the directory in Eclipse, select the file, then choose Team -> Add to svn:ignore.

Files to Ignore

Some files just don’t need to get checked in. These include the log files as well as the session files.

  • tmp/sessions/ruby_sess
  • log/.log
  • db/schema.rb

The reason that the individual files are ignored rather than the whole directory is so that when you do the initial checkout, the directories will still be created.

Initial Checkout

After doing the initial checkout on server, you will need to do a few things.

  1. Copy the template files back to the original filename and edit as necessary. Remember to use absolute paths in the config files (like for the Flickr cache file)
  2. Set execute permissions on the public/dispatch files: chmod 755 dispatch.*
  3. Depending on the permissions of the server, you might need to set the group of the log and tmp directories to apache and set write access to the group.