Skip to content

Instantly share code, notes, and snippets.

@pianosnake
Created December 4, 2013 05:54
Show Gist options
  • Select an option

  • Save pianosnake/7783023 to your computer and use it in GitHub Desktop.

Select an option

Save pianosnake/7783023 to your computer and use it in GitHub Desktop.
Basic Ruby script to listen for Postgres notifications from a central database and update a client database.
require 'rubygems'
require 'sequel'
require 'json'
require 'pg'
DB_central = Sequel.connect('postgres://xxx:xxx@hostname/central_db')
DB_client1 = Sequel.connect('postgres://xxx:xxx@hostname/client1_db')
DB_central.listen :usersupdate, {loop: true} do |channel, pid, payload|
puts "#{channel} -- #{pid} -- #{payload}"
j = JSON.parse(payload)
if j && j["id"]
DB_client1.
from(:users).
where(id: j["id"]).
update(email: j["email"], name: j["name"])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment