From: milki Date: Sun, 13 Feb 2011 21:04:33 +0000 (-0800) Subject: Patch to schema->deploy against a file X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=91d0c99f913a7d2a03a159c4f14ecde89c43a1bf;p=dbsrgits%2FDBIx-Class-Historic.git Patch to schema->deploy against a file Fixes skipped lines when a comment is followed by a statement Explicitly split on single line comments and not just ;\n Add some FIXME comments on the state of deploy --- diff --git a/Changes b/Changes index 9b2f887..d21fa4b 100644 --- a/Changes +++ b/Changes @@ -16,6 +16,8 @@ Revision history for DBIx::Class from 0542ec57 and 4c2b30d6) - Fix problems with M.A.D. under CGI::SpeedyCGI (RT#65131) - Better error handling when prepare() fails silently + - Fixes skipped lines when a comment is followed by a statement + when deploying a schema via sql file 0.08127 2011-01-19 16:40 (UTC) * New Features / Changes diff --git a/lib/DBIx/Class.pm b/lib/DBIx/Class.pm index dcc2a46..0b11c0b 100644 --- a/lib/DBIx/Class.pm +++ b/lib/DBIx/Class.pm @@ -338,6 +338,8 @@ mattlaw: Matt Lawrence michaelr: Michael Reddick +milki: Jonathan Chu + ned: Neil de Carteret nigel: Nigel Metheringham diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index fc66f3b..6278956 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -2730,6 +2730,7 @@ sub deployment_statements { my $filename = $schema->ddl_filename($type, $version, $dir); if(-f $filename) { + # FIXME replace this block when a proper sane sql parser is available my $file; open($file, "<$filename") or $self->throw_exception("Can't open $filename ($!)"); @@ -2768,12 +2769,14 @@ sub deployment_statements { return wantarray ? @ret : $ret[0]; } +# FIXME deploy() currently does not accurately report sql errors +# Will always return true while errors are warned sub deploy { my ($self, $schema, $type, $sqltargs, $dir) = @_; my $deploy = sub { my $line = shift; - return if($line =~ /^--/); return if(!$line); + return if($line =~ /^--/); # next if($line =~ /^DROP/m); return if($line =~ /^BEGIN TRANSACTION/m); return if($line =~ /^COMMIT/m); @@ -2795,7 +2798,8 @@ sub deploy { } } elsif (@statements == 1) { - foreach my $line ( split(";\n", $statements[0])) { + # split on single line comments and end of statements + foreach my $line ( split(/\s*--.*\n|;\n/, $statements[0])) { $deploy->( $line ); } } diff --git a/t/lib/test_deploy/DBICTest-Schema-1.x-SQLite.sql b/t/lib/test_deploy/DBICTest-Schema-1.x-SQLite.sql new file mode 100644 index 0000000..87486ef --- /dev/null +++ b/t/lib/test_deploy/DBICTest-Schema-1.x-SQLite.sql @@ -0,0 +1,11 @@ +-- +-- This table line should not be skipped +-- +CREATE TABLE artist ( + artistid INTEGER PRIMARY KEY NOT NULL, + name varchar(100), + rank integer NOT NULL DEFAULT 13, + charfield char(10) +); + +CREATE INDEX artist_name_hookidx ON artist (name); -- This line should error if artist was not parsed correctly diff --git a/t/storage/deploy.t b/t/storage/deploy.t index d2bca43..3b9435b 100644 --- a/t/storage/deploy.t +++ b/t/storage/deploy.t @@ -2,6 +2,7 @@ use strict; use warnings; use Test::More; +use Test::Exception; use lib qw(t/lib); use DBICTest; @@ -16,6 +17,13 @@ BEGIN { use File::Spec; use Path::Class qw/dir/; use File::Path qw/make_path remove_tree/; + +lives_ok( sub { + my $parse_schema = DBICTest->init_schema(no_deploy => 1); + $parse_schema->deploy({},'t/lib/test_deploy'); + $parse_schema->resultset("Artist")->all(); +}, 'artist table deployed correctly' ); + my $schema = DBICTest->init_schema(); my $var = dir (qw| t var create_ddl_dir |);