Sha256: 02faea8cdf06df05d1c9a89f5bde1f2223d988eb0eaf1d8634a1d5a47bf28a03

Contents?: true

Size: 1.37 KB

Versions: 93

Compression:

Stored size: 1.37 KB

Contents

#!/usr/bin/env ruby
#
# Test script, demonstrating a non-poll notification for a table event.
#

BEGIN {
        require 'pathname'
        basedir = Pathname.new( __FILE__ ).expand_path.dirname.parent
        libdir = basedir + 'lib'
        $LOAD_PATH.unshift( libdir.to_s ) unless $LOAD_PATH.include?( libdir.to_s )
}

require 'pg'

TRIGGER_TABLE = %{
	CREATE TABLE IF NOT EXISTS test ( message text );
}

TRIGGER_FUNCTION = %{
CREATE OR REPLACE FUNCTION notify_test()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $$
    BEGIN
        NOTIFY woo;
        RETURN NULL;
    END
$$
}

DROP_TRIGGER = %{
DROP TRIGGER IF EXISTS notify_trigger ON test
}


TRIGGER = %{
CREATE TRIGGER notify_trigger
AFTER UPDATE OR INSERT OR DELETE
ON test
FOR EACH STATEMENT
EXECUTE PROCEDURE notify_test();
}

conn = PG.connect( :dbname => 'test' )

conn.exec( TRIGGER_TABLE )
conn.exec( TRIGGER_FUNCTION )
conn.exec( DROP_TRIGGER )
conn.exec( TRIGGER )

conn.exec( 'LISTEN woo' )  # register interest in the 'woo' event

notifications = []

puts "Now switch to a different term and run:",
     '',
     %{  psql test -c "insert into test values ('A message.')"},
	 ''

puts "Waiting up to 30 seconds for for an event!"
conn.wait_for_notify( 30 ) do |notify, pid|
	notifications << [ pid, notify ]
end

if notifications.empty?
	puts "Awww, I didn't see any events."
else
	puts "I got one from pid %d: %s" % notifications.first
end



Version data entries

93 entries across 93 versions & 6 rubygems

Version Path
jruby-pg-0.1-java sample/notify_wait.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/pg-0.20.0/sample/notify_wait.rb
pg-0.20.0-x86-mingw32 sample/notify_wait.rb
pg-0.20.0-x64-mingw32 sample/notify_wait.rb
pg-0.20.0 sample/notify_wait.rb
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/sample/notify_wait.rb
pg-0.19.1.pre20170124220800-x64-mingw32 sample/notify_wait.rb
pg-0.19.1.pre20170124220800-x86-mingw32 sample/notify_wait.rb
pg-0.19.1.pre20170124220800 sample/notify_wait.rb
pg-0.19.1.pre20170115074000-x64-mingw32 sample/notify_wait.rb
pg-0.19.1.pre20170115074000-x86-mingw32 sample/notify_wait.rb
pg-0.19.1.pre20170115074000 sample/notify_wait.rb
pg-0.19.0.pre20170115074000 sample/notify_wait.rb
pg-0.19.0-x86-mingw32 sample/notify_wait.rb
pg-0.19.0-x64-mingw32 sample/notify_wait.rb
pg-0.19.0 sample/notify_wait.rb
pg-0.19.0.pre20160904200247-x86-mingw32 sample/notify_wait.rb
pg-0.19.0.pre20160904200247-x64-mingw32 sample/notify_wait.rb
pg-0.19.0.pre20160904200247 sample/notify_wait.rb
pg-0.19.0.pre20160820113039-x86-mingw32 sample/notify_wait.rb