Skip to content

Instantly share code, notes, and snippets.

Created October 22, 2015 13:22
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/bff84ece3f7be588f74b to your computer and use it in GitHub Desktop.
Save anonymous/bff84ece3f7be588f74b to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojo/Pg.pm b/lib/Mojo/Pg.pm
index f473f21..1f852d7 100644
--- a/lib/Mojo/Pg.pm
+++ b/lib/Mojo/Pg.pm
@@ -25,6 +25,7 @@ has pubsub => sub {
weaken $pubsub->{pg};
return $pubsub;
};
+has 'search_path';
our $VERSION = '2.13';
@@ -74,9 +75,15 @@ sub new { @_ > 1 ? shift->SUPER::new->from_string(@_) : shift->SUPER::new }
sub _dequeue {
my $self = shift;
+
while (my $dbh = shift @{$self->{queue} || []}) { return $dbh if $dbh->ping }
my $dbh = DBI->connect(map { $self->$_ } qw(dsn username password options));
+ if (my $s = $self->search_path) {
+ my $search_path = join ', ', map { $dbh->quote_identifier($_) } @$s;
+ $dbh->do("set search_path to $search_path");
+ }
$self->emit(connection => $dbh);
+
return $dbh;
}
@@ -284,6 +291,13 @@ efficiently, by sharing a single database connection with many consumers.
# Notify a channel
$pg->pubsub->notify(news => 'PostgreSQL rocks!');
+=head2 search_path
+
+ my $search_path = $pg->search_path;
+ $pg = $pg->search_path(['foo', 'public']);
+
+Schema search path assigned to all new connections.
+
=head2 username
my $username = $pg->username;
diff --git a/t/database.t b/t/database.t
index 9f32fb4..13fd225 100644
--- a/t/database.t
+++ b/t/database.t
@@ -14,6 +14,13 @@ use Mojo::Pg;
my $pg = Mojo::Pg->new($ENV{TEST_ONLINE});
ok $pg->db->ping, 'connected';
+# Custom search_path
+$pg = Mojo::Pg->new($ENV{TEST_ONLINE});
+$pg->search_path(['foo', 'bar']);
+is_deeply $pg->db->query('show search_path')->hash,
+ {search_path => 'foo, bar'}, 'right structure';
+$pg = Mojo::Pg->new($ENV{TEST_ONLINE});
+
# Blocking select
is_deeply $pg->db->query('select 1 as one, 2 as two, 3 as three')->hash,
{one => 1, two => 2, three => 3}, 'right structure';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment