Skip to content

Instantly share code, notes, and snippets.

/upsert.diff Secret

Created February 15, 2016 06:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/8b9c5fa349c97d015ef4 to your computer and use it in GitHub Desktop.
Save anonymous/8b9c5fa349c97d015ef4 to your computer and use it in GitHub Desktop.
diff --git a/lib/Minion/Backend/Pg.pm b/lib/Minion/Backend/Pg.pm
index 1f16402..00060a9 100644
--- a/lib/Minion/Backend/Pg.pm
+++ b/lib/Minion/Backend/Pg.pm
@@ -85,12 +85,12 @@ sub new {
sub register_worker {
my ($self, $id) = @_;
- my $sql
- = 'update minion_workers set notified = now() where id = ? returning 1';
- return $id if $id && $self->pg->db->query($sql, $id)->rows;
-
- $sql = 'insert into minion_workers (host, pid) values (?, ?) returning id';
- return $self->pg->db->query($sql, hostname, $$)->hash->{id};
+ return shift->pg->db->query(
+ 'insert into minion_workers (id, host, pid) values (?, ?, ?)
+ on conflict(id) do update set notified = now()
+ returning id
+ ', $id || $DBD::Pg::DBDPG_DEFAULT, $self->{host} //= hostname, $$
+ )->hash->{id};
}
sub remove_job {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment