Fix juju db upstart rewrites

Cherry-pick and fairly extensive edit from master.