sapwood

1.2 to 1.3

Because we're making the transition to using PostgreSQL from MySQL (for searching purposes), we're back to a pretty manual upgrade to v1.3. Let's get to it.

Upgrade Server (production)

If you are upgrading your production server, first ssh into the server. And it's always good practice to get your server up to date first.

$ sudo apt-get update
$ sudo apt-get upgrade -y

Setup PostgreSQL (production)

If you don't have PostgreSQL installed, let's start with that to minimize the amount of time you are down.

Install PostgreSQL

You'll want to do this on your production server, since your development server doesn't use a database.

$ sudo apt-get -y install postgresql libpq-dev postgresql-contrib

Add Database User

Next, let's add the database user. You can use the same username and password as before, or make up a new one. Just make note of what you are using.

$ sudo -u postgres psql

postgres=# CREATE ROLE [db_user] LOGIN CREATEDB PASSWORD '[db_pass]';

Update Code

Change into your Sapwood project directory. Remember, this is different depending on your setup.

$ cd ~/apps/sapwood

Upgrade your code to the latest release.

$ git checkout release
$ git pull origin release

Install our new gems.

$ bundle install

Transition Content (production)

It's probably not a bad idea to get your database backed up first, although we're moving to PostgreSQL, so this doesn't matter too much.

$ RAILS_ENV=production bundle exec rake sapwood:db:backup

Next, we're going to store your database content to file using yaml_db:

$ RAILS_ENV=production bundle exec rake db:data:dump

Then, swap out your database config. It should look something like this for your new PostgreSQL setup:

config/database.yml

production:
  adapter: postgresql
  database: [db_name]
  username: [db_user]
  password: [db_pass]
  encoding: unicode
  host: localhost
  pool: 5
  timeout: 5000

If it's working right, you should be able to create and migrate your new database.

$ RAILS_ENV=production bundle exec rake db:create
$ RAILS_ENV=production bundle exec rake db:migrate

Then bring the new content into the pg database:

$ RAILS_ENV=production bundle exec rake db:data:load

Open Up PostgreSQL

To allow for remote connections, you will need to change a few settings on your production server.

There are two config files you need to adjust:

/etc/postgresql/9.3/main/pg_hba.conf

# add this to the bottom of the file
host    all             all             0.0.0.0/0               trust

/etc/postgresql/9.3/main/postgresql.conf

# change this
listen_addresses = 'localhost'          # what IP address(es) to listen on;

# to this
listen_addresses = '*'          # what IP address(es) to listen on;

Then enable 5432 port if you're using ufw:

$ sudo ufw allow 5432

And restart postgres:

$ sudo service postgresql restart

Transition Content (development)

In development, all you need to do is swap out your database config so that you connect to the remote PostgreSQL database. This should look something like this:

config/database.yml

development:
  adapter: postgresql
  encoding: unicode
  pool: 5
  host: [prod_ip]
  port: 5432
  username: [db_user]
  password: [db_pass]
  database: [db_name]

Precompile Assets (production)

You'll need to update your content and precompile your assets.

$ RAILS_ENV=production bundle exec rake assets:precompile

Restart Server

Then stop and start your server (this is better than restarting in this case). In development, this is:

$ bundle exec rails s

In production:

$ sudo service unicorn_taproot stop
$ sudo service unicorn_taproot start

Now you should be up and running!

The only thing I've noticed that's odd with this transition is that templates that were allowed to have themselves as children are unset. All you have to do is go back in the templates and choose itself as a child for any template that allowed itself to be a child of itself.