test cases for recent changes and test DBs other than mysql
[dbsrgits/DBIx-Class-DeploymentHandler.git] / lib / DBIx / Class / DeploymentHandler / DeployMethod / SQL / Translator.pm
index 167e455..2bab1e4 100644 (file)
@@ -24,6 +24,14 @@ use File::Spec::Functions;
 
 with 'DBIx::Class::DeploymentHandler::HandlesDeploy';
 
+with 'DBIx::Class::DeploymentHandler::WithApplicatorDumple' => {
+    interface_role       => 'DBIx::Class::DeploymentHandler::HandlesMigrationSchema',
+    class_name           => 'DBIx::Class::DeploymentHandler::MigrationSchema::SchemaLoader',
+    delegate_name        => 'schema_provider',
+    attributes_to_assume => ['schema'],
+    attributes_to_copy   => [qw( schema )],
+  };
+
 has ignore_ddl => (
   isa      => 'Bool',
   is       => 'ro',
@@ -89,9 +97,9 @@ has schema_version => (
 # this will probably never get called as the DBICDH
 # will be passing down a schema_version normally, which
 # is built the same way, but we leave this in place
-sub _build_schema_version { 
+sub _build_schema_version {
   my $self = shift;
-  $self->schema->schema_version 
+  $self->schema->schema_version
 }
 
 sub __ddl_consume_with_prefix {
@@ -243,13 +251,7 @@ sub _run_sql_array {
   my ($self, $sql) = @_;
   my $storage = $self->storage;
 
-  $sql = [grep {
-    $_ && # remove blank lines
-    !/^(BEGIN|BEGIN TRANSACTION|COMMIT)/ # strip txn's
-  } map {
-    s/^\s+//; s/\s+$//; # trim whitespace
-    join '', grep { !/^--/ } split /\n/ # remove comments
-  } @$sql];
+  $sql = [ _split_sql_chunk( @$sql ) ];
 
   Dlog_trace { "Running SQL $_" } $sql;
   foreach my $line (@{$sql}) {
@@ -266,6 +268,30 @@ sub _run_sql_array {
   return join "\n", @$sql
 }
 
+# split a chunk o' SQL into statements
+sub _split_sql_chunk {
+    my @sql = map { split /;\n/, $_ } @_;
+
+    for ( @sql ) {
+        # strip transactions
+        s/^(?:BEGIN|BEGIN TRANSACTION|COMMIT).*//mgi;
+
+        # trim whitespaces
+        s/^\s+|\s+$//mg;
+
+        # remove comments
+        s/^--.*//gm;
+
+        # remove blank lines
+        s/^\n//mg;
+
+        # put on single line
+        s/\n/ /g;
+    }
+
+    return @sql;
+}
+
 sub _run_sql {
   my ($self, $filename) = @_;
   log_debug { "Running SQL from $filename" };
@@ -285,7 +311,7 @@ sub _run_perl {
   if ($@) {
     croak "$filename failed to compile: $@";
   } elsif (ref $fn eq 'CODE') {
-    $fn->($self->schema, $versions)
+    $fn->($self->migration_schema, $versions)
   } else {
     croak "$filename should define an anonymouse sub that takes a schema but it didn't!";
   }
@@ -613,19 +639,10 @@ sub _read_sql_file {
   my ($self, $file)  = @_;
   return unless $file;
 
+   local $/ = undef;  #sluuuuuurp
+
   open my $fh, '<', $file;
-  my @data = split /;\n/, join '', <$fh>;
-  close $fh;
-
-  @data = grep {
-    $_ && # remove blank lines
-    !/^(BEGIN|BEGIN TRANSACTION|COMMIT)/ # strip txn's
-  } map {
-    s/^\s+//; s/\s+$//; # trim whitespace
-    join '', grep { !/^--/ } split /\n/ # remove comments
-  } @data;
-
-  return \@data;
+  return [ _split_sql_chunk( <$fh> ) ];
 }
 
 sub downgrade_single_step {