Optimise hits indexes for postgres write

* Remove things killing write performance
* Add indices that will work
Fix order-dependent spec

* (again, was last fixed in 10230827ccbdaafe51a3e84f71875b73b9a99636 but that
   moved when import mappings arrived!)
Batch preview: make order explicit

* We depended on order(:id) implicitly but we can't rely on that working
  in Postgres. Make it explicit.
Make Postgres features not in schema.rb available

* We're only using this feature right now. If we use more, we'll need a
  better way of dealing with them. Switching to structure.sql can't and
  won't help.
Home page titles/furls in schema.rb

* While rebases from master are happening to this Postgres branch,
  we need to commit schema changes (as they're happening to
  structure.sql over there)
Remove the backported PostgreSQLAdapter

* Now Rails 4 is merged, no longer necessary
Fix Site#hit_total_count grouping
Fully qualify filter ordering
Fix batch specs

* Order-dependent assumption (go by path)
* Array cast error in Factory Girl (supply tags as simple string)
Rails 4 knows about Postgres extensions

* So use the idiomatic migration commands (taking advantage of the fact
  there's non-dev PG DB at time of writing to re-use the original migration)
* Remove the now-redundant explicit SQL support for specs
Fix leaderboard scope

* Couple of mySQL-only quotes
* Simplify date window clause for Postgres
Don't let Jenkins drop the DB

* as CREATE requires SuperUser
* the transition test user is not a superuser
Rely on peer auth in development and test environments

It's simpler to have a postgres role with the same name as the Unix
user and use peer authentication in these environments. This is
different from production, but should work for CI as well.

To do this we need to not specify user, password or a host to use
for connecting to the database.
Use template0 to create the database in dev and test

It's not possible to specify encoding when using template1, because
it might conflict with an encoding set in the template. The Puppet
module we use in production defaults to template0, so this is still
consistent between environments:

    https://github.com/puppetlabs/puppetlabs-postgresql/blob/3.4.2/manifests/server/db.pp#L11
Extend rake db:create to enable pgcrypto extension

This extension can only be enabled by a superuser, but we don't want
to be running the app or tests as a superuser, so we need to do
this as the postgres user.

This allows us to use db:setup and db:reset as usual in dev and test.

This is a temporary measure and can be removed when we no longer need
pgcrypto (after removing path_hash).
Drop and recreate the database each time in CI

db:schema:load isn't able to keep the schema completely as it is
in the schema file, which is particularly a problem for branch
builds. Dropping the database each time is cleaner and ensures that
tests are running with only the specified tables in the database.
Now that we are enabling pgcrypto as part of db:create, db:reset
can do this for us.
Set redis namespace to 'transition-postgres'

This is a temporary measure to ensure that when the postgres-master
branch is deployed to preview it doesn't share a queue in redis for
sidekiq jobs. The sidekiq workers already have different names, but
without this namespacing change they are all picking up jobs from
the same queue, which is possibly causing jobs for transition to
fail. I think they could be being picked up by transition-postgres's
workers, which wouldn't be able to do anything in the postgres
database right now but would leave the jobs in an unfinished state
in the mysql database, so they would appear in the flash message
as though they hadn't started.

We can set the namespacing in the config which is overwritten on
deployment, but should switch to do that on master, rebase this
change onto master and revert this change to minimise differences
between the branches.
Replace the clunky direct transaction SQL

* With ActiveRecord semantic equivalent
Use COPY FROM STDIN for hits import

* Does not require superuser
* Is surprisingly faster. For UKTI import (average of 8 runs, 10 taken,
  lowest and highest timings discarded):
   * 17.9s  when copying whole file in one buffer (File.open(...).read,
              this commit)
   * 18.26s for line-by-line (File.open(...).each, not committed)
   * 21.3s  for older, file-based approach
* ANALYZE removed after hits_staging COPY. Was not adding anything and was a
  cargo-cult leftover.
Be explicit about popular tag ordering

* COUNT first, then name
* Fixes intermittent failure where order was unknown and would push out
  tags with equal count
* Also, tighten up the criteria for "Then I should see popular tags" step. Now
  checks count as well as fuller list. We weren't testing what we thought we were!