From: giftnuss Date: Thu, 24 Nov 2011 00:13:24 +0000 (+0100) Subject: Change mysql parser to throw exceptions on unspecified default values (RT#4835) X-Git-Tag: v0.11011~54 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=34248db809db43ae0a4202bae70d6bfbec176c2d;p=dbsrgits%2FSQL-Translator.git Change mysql parser to throw exceptions on unspecified default values (RT#4835) --- diff --git a/lib/SQL/Translator/Parser/MySQL.pm b/lib/SQL/Translator/Parser/MySQL.pm index 5f8d75a..1a8c44b 100644 --- a/lib/SQL/Translator/Parser/MySQL.pm +++ b/lib/SQL/Translator/Parser/MySQL.pm @@ -602,7 +602,7 @@ default_val : $return = $item[2]; } | - /default/i /(?:')?[\w\d:.-]*(?:')?/ + /default/i /[\w\d:.-]+/ { $return = $item[2]; } diff --git a/t/02mysql-parser.t b/t/02mysql-parser.t index 62fa70b..5277e65 100644 --- a/t/02mysql-parser.t +++ b/t/02mysql-parser.t @@ -11,7 +11,7 @@ use SQL::Translator::Utils qw//; use Test::SQL::Translator qw(maybe_plan); BEGIN { - maybe_plan(329, "SQL::Translator::Parser::MySQL"); + maybe_plan(333, "SQL::Translator::Parser::MySQL"); SQL::Translator::Parser::MySQL->import('parse'); } @@ -830,7 +830,7 @@ ok ($@, 'Exception thrown on invalid version string'); id char(32) not null default '0' primary key, ssn varchar(12) NOT NULL default 'test single quotes like in you''re', user varchar(20) NOT NULL default 'test single quotes escaped like you\'re', - key using btree (ssn) + key using btree (ssn) );|; my $val = parse($tr, $data); @@ -869,3 +869,36 @@ ok ($@, 'Exception thrown on invalid version string'); is( $f3->default_value, "test single quotes escaped like you\\'re", "Single quote in default value is escaped properly" ); is( $f3->is_primary_key, 0, 'Field is not PK' ); } + +{ + # silence PR::D from spewing on STDERR + local ($::RD_ERRORS, $::RD_WARN,$::RD_HINT,$::RD_TRACE); + my $tr = SQL::Translator->new; + my $data = q|create table "sessions" ( + id char(32) not null default, + ssn varchar(12) NOT NULL default 'test single quotes like in you''re', + user varchar(20) NOT NULL default 'test single quotes escaped like you\'re', + key using btree (ssn) + );|; + + my $val= parse($tr,$data); + ok ($tr->error =~ /Parse failed\./, 'Parse failed error without default value'); +} + +{ + # make sure empty string default value still works + my $tr = SQL::Translator->new; + my $data = q|create table "sessions" ( + id char(32) not null DEFAULT '', + ssn varchar(12) NOT NULL default "", + key using btree (ssn) + );|; + my $val= parse($tr,$data); + + my @fields = $tr->schema->get_table('sessions')->get_fields; + is (scalar @fields, 2, 'Both fields parsed correctly'); + for (@fields) { + my $def = $_->default_value; + ok( (defined $def and $def eq ''), "Defaults on field $_ correct" ); + } +}