Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add a get_schema method to Lystyng::Schema. Use it.
  • Loading branch information
davorg committed Jan 14, 2016
1 parent 0d48cc5 commit c11f0ce
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
18 changes: 18 additions & 0 deletions lib/Lystyng/Schema.pm
Expand Up @@ -16,5 +16,23 @@ __PACKAGE__->load_namespaces;


# You can replace this text with custom code or comments, and it will be preserved on regeneration

sub get_schema {
my @errors;
foreach (qw[LYSTYNG_DB_SERVER LYSTYNG_DB_NAME
LYSTYNG_DB_USER LYSTYNG_DB_PASS]) {
push @errors, $_ unless defined $ENV{$_};
}

if (@errors) {
die("Missing connection info: @errors");
}

return __PACKAGE__->connect(
"dbi:mysql:hostname=$ENV{LYSTYNG_DB_SERVER};database=$ENV{LYSTYNG_DB_NAME}",
$ENV{LYSTYNG_DB_USER}, $ENV{LYSTYNG_DB_PASS}
);
}

__PACKAGE__->meta->make_immutable(inline_constructor => 0);
1;
6 changes: 2 additions & 4 deletions t/003_user_admin.t
Expand Up @@ -12,10 +12,8 @@ use Lystyng::Schema;
my $app = Lystyng->to_app;
my $test = Plack::Test->create($app);

my $sch = Lystyng::Schema->connect(
"dbi:mysql:database=$ENV{LYSTYNG_DB_NAME}",
$ENV{LYSTYNG_DB_USER}, $ENV{LYSTYNG_DB_PASS},
);
my $sch = eval { Lystyng::Schema->get_schema };
BAIL_OUT($@) if $@;

my %route = (
register => 200,
Expand Down
7 changes: 2 additions & 5 deletions t/004_public_urls.t
Expand Up @@ -26,11 +26,8 @@ for (keys %route) {
"response status is $route{$_} for /$_";
}

my $sch = Lystyng::Schema->connect(
"dbi:mysql:database=$ENV{LYSTYNG_DB_NAME}",
$ENV{LYSTYNG_DB_USER},
$ENV{LYSTYNG_DB_PASS},
) or BAIL_OUT("Can't connect to database");
my $sch = eval { Lystyng::Schema->get_schema };
BAIL_OUT("Can't connect to database: $@") if $@;

my $user = $sch->resultset('User')->create({
username => 'test',
Expand Down
17 changes: 3 additions & 14 deletions t/Test/Role/WithSchema.pm
Expand Up @@ -17,20 +17,9 @@ has schema => (
);

sub _build_schema {
my @errors;
foreach (qw[LYSTYNG_DB_SERVER LYSTYNG_DB_NAME
LYSTYNG_DB_USER LYSTYNG_DB_PASS]) {
push @errors, $_ unless defined $ENV{$_};
}

if (@errors) {
BAIL_OUT("Missing connection info: @errors");
}

return Lystyng::Schema->connect(
"dbi:mysql:hostname=$ENV{LYSTYNG_DB_SERVER};database=$ENV{LYSTYNG_DB_NAME}",
$ENV{LYSTYNG_DB_USER}, $ENV{LYSTYNG_DB_PASS}
);
my $schema = eval { Lystyng::Schema->get_schema };
BAIL_OUT($@) if $@;
return $schema;
}

1;

0 comments on commit c11f0ce

Please sign in to comment.