From: Arthur Axel 'fREW' Schmidt Date: Fri, 3 Aug 2012 01:25:44 +0000 (-0500) Subject: helpful error wrapping for invalid source usage X-Git-Tag: v0.002200~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits%2FDBIx-Class-DeploymentHandler.git;a=commitdiff_plain;h=ae521c5583a2dd44484d4c27cd6cf01cb066c838 helpful error wrapping for invalid source usage --- diff --git a/Changes b/Changes index 21f40a4..0e836a3 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,8 @@ Revision history for {{$dist->name}} {{$NEXT}} + - Added much more helpful error for "Can't find source for..." error in + migration scripts, based on code from DBIx::Class::Migration - Test suite now is fully parallelizable - Switch ::ScriptHelpers to Sub::Exporter::Progressive - Switch from Test::Exception to Test::Fatal diff --git a/dist.ini b/dist.ini index ece671e..f270775 100644 --- a/dist.ini +++ b/dist.ini @@ -42,3 +42,4 @@ Test::Requires = 0.06 Context::Preserve = 0.01 File::Temp = 0 Sub::Exporter::Progressive = 0 +Text::Brew = 0.02 diff --git a/lib/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator/ScriptHelpers.pm b/lib/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator/ScriptHelpers.pm index ee210fe..77d52ac 100644 --- a/lib/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator/ScriptHelpers.pm +++ b/lib/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator/ScriptHelpers.pm @@ -8,6 +8,8 @@ use Sub::Exporter::Progressive -setup => { }; use List::Util 'first'; +use Text::Brew 'distance'; +use Try::Tiny; sub dbh { my ($code) = @_; @@ -43,6 +45,7 @@ sub schema_from_schema_loader { warn 'using "current" naming in a deployment script is begging for problems. Just Say No.' if $opts->{naming} eq 'current' || (ref $opts->{naming} eq 'HASH' && first { $_ eq 'current' } values %{$opts->{naming}}); + sub { my ($schema, $versions) = @_; @@ -55,7 +58,28 @@ sub schema_from_schema_loader { 'SHSchema::' . $count++, $opts, \@ci ); my $sl_schema = $new_schema->connect(@ci); - $code->($sl_schema, $versions) + try { + $code->($sl_schema, $versions) + } catch { + if (m/Can't find source for (.+?) at/) { + my @presentsources = map { + (distance($_, $1))[0] < 3 ? "$_ <== Possible Match\n" : "$_\n"; + } $sl_schema->sources; + + die <<"ERR"; +$_ +You are seeing this error because the DBIx::Class::ResultSource in your +migration script called "$1" is not part of the schema that ::Schema::Loader +has inferred from your existing database. + +To help you debug this issue, here's a list of the actual sources that the +schema available to your migration knows about: + + @presentsources +ERR + } + die $_; + } } } diff --git a/t/deploy_methods/script-helpers.t b/t/deploy_methods/script-helpers.t index c621f53..97031dc 100644 --- a/t/deploy_methods/script-helpers.t +++ b/t/deploy_methods/script-helpers.t @@ -5,6 +5,7 @@ use warnings; use Test::More; use DBIx::Class::DeploymentHandler::DeployMethod::SQL::Translator::ScriptHelpers ':all';; +use Test::Fatal; use lib 't/lib'; @@ -83,6 +84,28 @@ subtest schema_from_schema_loader => sub { RaiseError => 1, ignore_version => 1, }); + + subtest '({ dbh_maker => ..., ... })' => $build_sl_test->({ + dbh_maker => sub { DBICDHTest->dbh }, + RaiseError => 1, + ignore_version => 1, + }); + + subtest 'error handling' => sub { + my $outer_schema = DBICVersion::Schema->connect( + 'dbi:SQLite::memory:', undef, undef, + { RaiseError => 1 }, + { ignore_version => 1 }, + ); + $outer_schema->deploy; + like(exception { + schema_from_schema_loader({ naming => 'v4' }, sub { + my ($schema, $versions) = @_; + + $schema->resultset('foo') + })->($outer_schema, [2,3]); + }, qr/Foo <== Possible Match/, 'correct error'); + }; }; done_testing;