# 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 {
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}) {
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" };
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 {
--- /dev/null
+use strict;
+use warnings;
+
+use Test::More tests => 1;
+
+use DBIx::Class::DeploymentHandler::DeployMethod::SQL::Translator;
+
+*split_sql_chunk =
+*DBIx::Class::DeploymentHandler::DeployMethod::SQL::Translator::_split_sql_chunk;
+
+is_deeply [ split_sql_chunk( <<'END' ) ], [ 'SELECT * FROM YADAH END' ];
+BEGIN
+ -- stuff
+ SELECT * FROM YADAH
+END;
+END
+