Skip to content

Instantly share code, notes, and snippets.

@mrenvoize
Last active January 12, 2017 11:47
Show Gist options
  • Save mrenvoize/240039e57e1f0789a4caa92d75a61151 to your computer and use it in GitHub Desktop.
Save mrenvoize/240039e57e1f0789a4caa92d75a61151 to your computer and use it in GitHub Desktop.
package Rebus;
use Mojo::Base 'Mojolicious';
use Rebus::Schema;
use DBIx::Class::DeploymentHandler;
use strict;
use warnings;
has schema => sub {
my $self = shift;
my $config = $self->config;
my $dbic_connect_attrs = {quote_names => 1};
if ($config->{'dbi'}->{'driver'} eq 'Pg') {
$dbic_connect_attrs->{'pg_enable_utf8'} = 1;
$dbic_connect_attrs->{'on_connect_do'}
= ["CREATE SCHEMA IF NOT EXISTS list AUTHORIZATION $config->{'dbi'}->{'username'}", "SET search_path TO list"];
}
return Rebus::Schema->connect(
"dbi:$config->{'dbi'}->{'driver'}:"
. "database=$config->{'dbi'}->{'database'};"
. "host=$config->{'dbi'}->{'host'};"
. "port=$config->{'dbi'}->{'port'}",
"$config->{'dbi'}->{'username'}", "$config->{'dbi'}->{'password'}", $dbic_connect_attrs
);
};
# This method will run once at server start
sub startup {
my $app = shift;
# Load config
$app->plugin('Config');
# Load database handlers
$app->helper(db => sub { $app->app->schema });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment