Added helpful logging for the schema_from_schema_loader ScriptHelper
[dbsrgits/DBIx-Class-DeploymentHandler.git] / lib / DBIx / Class / DeploymentHandler / DeployMethod / SQL / Translator / ScriptHelpers.pm
index ee210fe..622dc29 100644 (file)
@@ -8,6 +8,9 @@ use Sub::Exporter::Progressive -setup => {
 };
 
 use List::Util 'first';
+use Text::Brew 'distance';
+use Try::Tiny;
+use DBIx::Class::DeploymentHandler::LogImporter qw(:dlog);
 
 sub dbh {
    my ($code) = @_;
@@ -43,6 +46,10 @@ 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}});
+
+   $opts->{debug} = 1
+      if !exists $opts->{debug} && $ENV{DBICDH_TRACE};
+
    sub {
       my ($schema, $versions) = @_;
 
@@ -54,8 +61,33 @@ sub schema_from_schema_loader {
       my $new_schema = DBIx::Class::Schema::Loader::make_schema_at(
         'SHSchema::' . $count++, $opts, \@ci
       );
+
+      Dlog_debug {
+         "schema_from_schema_loader generated the following sources: $_"
+      } [ $new_schema->sources ];
       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 $_;
+      }
    }
 }