Fix juju db upstart rewrites

Cherry-pick and fairly extensive edit from master.
Merge pull request #434 from howbazaar/backport-fix-juju-db-upstart-rewrites

Fix juju db upstart rewrites

Cherry-pick and fairly extensive edit from master.