From: Arthur Axel 'fREW' Schmidt Date: Thu, 20 May 2010 03:40:21 +0000 (-0500) Subject: refactoring to allow more code reuse X-Git-Tag: v0.001000_11~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f36afe83b92e3948ebcdf68c1f3e03fd46000e70;p=dbsrgits%2FDBIx-Class-DeploymentHandler.git refactoring to allow more code reuse --- diff --git a/lib/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator.pm b/lib/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator.pm index c602bf9..a706b62 100644 --- a/lib/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator.pm +++ b/lib/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator.pm @@ -97,7 +97,7 @@ method __ddl_consume_with_prefix($type, $versions, $prefix) { } opendir my($dh), $dir; - my %files = map { $_ => "$dir/$_" } grep { /\.(?:sql|pl)$/ && -f "$dir/$_" } readdir $dh; + my %files = map { $_ => "$dir/$_" } grep { /\.(?:sql|pl|sql-\w+)$/ && -f "$dir/$_" } readdir $dh; closedir $dh; if (-d $common) { @@ -153,14 +153,12 @@ method _ddl_schema_down_produce_filename($type, $versions, $dir) { return catfile( $dirname, '001-auto.sql'); } -method _run_sql($filename) { +method _run_sql_array($sql) { my $storage = $self->storage; - log_debug { "[DBICDH] Running SQL from $filename" }; - my @sql = @{$self->_read_sql_file($filename)}; - my $sql .= join "\n", @sql; - log_trace { "[DBICDH] Running SQL $sql" }; - foreach my $line (@sql) { + my $ret = join "\n", @$sql; + log_trace { "[DBICDH] Running SQL $sql" }; + foreach my $line (@{$sql}) { $storage->_query_start($line); try { # do a dbh_do cycle here, as we need some error checking in @@ -172,7 +170,12 @@ method _run_sql($filename) { } $storage->_query_end($line); } - return $sql + return $ret +} + +method _run_sql($filename) { + log_debug { "[DBICDH] Running SQL from $filename" }; + return $self->_run_sql_array($self->_read_sql_file($filename)); } method _run_perl($filename) { @@ -192,13 +195,22 @@ method _run_perl($filename) { carp "$filename should define an anonymouse sub that takes a schema but it didn't!"; } } - -method _run_serialized_sql($filename, $type) { +{ + my $json; + + method _run_serialized_sql($filename, $type) { + if ($type eq 'json') { + require JSON; + $json ||= JSON->new->pretty; + my @sql = @{$json->decode($filename)}; + } else { + croak "A file ($filename) got to deploy that wasn't sql or perl!"; + } + } } method _run_sql_and_perl($filenames) { - my $storage = $self->storage; my @files = @{$filenames}; my $guard = $self->schema->txn_scope_guard if $self->txn_wrap;