From: Jess Robinson Date: Wed, 8 Oct 2008 21:43:36 +0000 (+0000) Subject: By royal decree, produced statements in list context shall not end in a semi-colon... X-Git-Tag: v0.11008~297 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=24d9fe69f45176a383f9faf545211caa180ae118;p=dbsrgits%2FSQL-Translator.git By royal decree, produced statements in list context shall not end in a semi-colon, or any newlines! (They may contain newlines) Also fixed SQLite index statements, which were missing vital newlines --- diff --git a/Changes b/Changes index 9dfdf9a..6144f16 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,9 @@ # ---------------------------------------------------------- # # ---------------------------------------------------------- +* Fixed SQLite producer to end index statements in newlines, in scalar context +* Decreed that all list context statements shall not end in ; or ;\n +* Fixed SQLite, Diff and MySQL producers to agree with Decree. * Added support for CREATE VIEW + tests in the Pg producer (wreis) * Added support for CREATE VIEW + tests in the sqlite producer (groditi) * Added proper argument parsing and documentation to MySQL Parser and Producer (ribasushi) diff --git a/lib/SQL/Translator/Diff.pm b/lib/SQL/Translator/Diff.pm index 8078ce0..743c105 100644 --- a/lib/SQL/Translator/Diff.pm +++ b/lib/SQL/Translator/Diff.pm @@ -202,7 +202,7 @@ sub produce_diff_sql { $meth ? map { my $sql = $meth->( (ref $_ eq 'ARRAY' ? @$_ : $_), $self->producer_options ); - $sql ? ("$sql;") : (); + $sql ? ("$sql") : (); } @{ $flattened_diffs{$_} } : $self->ignore_missing_methods ? "-- $producer_class cant $_" @@ -235,30 +235,30 @@ sub produce_diff_sql { unshift @diffs, # Remove begin/commit here, since we wrap everything in one. - grep { $_ !~ /^(?:COMMIT|START(?: TRANSACTION)?|BEGIN(?: TRANSACTION)?);/ } $producer_class->can('produce')->($translator); + grep { $_ !~ /^(?:COMMIT|START(?: TRANSACTION)?|BEGIN(?: TRANSACTION)?)/ } $producer_class->can('produce')->($translator); } if (my @tables_to_drop = @{ $self->{tables_to_drop} || []} ) { my $meth = $producer_class->can('drop_table'); - push @diffs, $meth ? map( { $meth->($_, $self->producer_options) } @tables_to_drop ) + push @diffs, $meth ? ( map { $meth->($_, $self->producer_options) } @tables_to_drop) : $self->ignore_missing_methods ? "-- $producer_class cant drop_table" : die "$producer_class cant drop_table"; } if (@diffs) { - unshift @diffs, "BEGIN;\n"; - push @diffs, "\nCOMMIT;\n"; + unshift @diffs, "BEGIN"; + push @diffs, "\nCOMMIT"; } else { - @diffs = ("-- No differences found\n\n"); + @diffs = ("-- No differences found"); } if ( @diffs ) { if ( $self->target_db !~ /^(?:MySQL|SQLite)$/ ) { unshift(@diffs, "-- Target database @{[$self->target_db]} is untested/unsupported!!!"); } - return join( "\n", "-- Convert schema '$src_name' to '$tar_name':\n", @diffs); + return join '', map { $_ ? "$_;\n\n" : "\n" } ("-- Convert schema '$src_name' to '$tar_name':", @diffs); } return undef; diff --git a/lib/SQL/Translator/Producer/MySQL.pm b/lib/SQL/Translator/Producer/MySQL.pm index a9b8301..f09d5d7 100644 --- a/lib/SQL/Translator/Producer/MySQL.pm +++ b/lib/SQL/Translator/Producer/MySQL.pm @@ -270,10 +270,10 @@ sub produce { debug("PKG: Beginning production\n"); %used_names = (); - my $create; + my $create = ''; $create .= header_comment unless ($no_comments); # \todo Don't set if MySQL 3.x is set on command line - $create .= "SET foreign_key_checks=0;\n\n"; + my @create = "SET foreign_key_checks=0"; preprocess_schema($schema); @@ -311,9 +311,9 @@ sub produce { # print "@table_defs\n"; - push @table_defs, "SET foreign_key_checks=1;\n\n"; + push @table_defs, "SET foreign_key_checks=1"; - return wantarray ? ($create, @table_defs) : $create . join ('', @table_defs); + return wantarray ? ($create ? $create : (), @create, @table_defs) : ($create . join('', map { $_ ? "$_;\n\n" : () } (@create, @table_defs))); } sub create_view { @@ -355,7 +355,7 @@ sub create_view { if( my $sql = $view->sql ){ $create .= " AS (\n ${sql}\n )"; } - $create .= ";\n\n"; +# $create .= ""; return $create; } @@ -375,7 +375,7 @@ sub create_table my $create = ''; my $drop; $create .= "--\n-- Table: $qt$table_name$qt\n--\n" unless $options->{no_comments}; - $drop = qq[DROP TABLE IF EXISTS $qt$table_name$qt;\n] if $options->{add_drop_table}; + $drop = qq[DROP TABLE IF EXISTS $qt$table_name$qt] if $options->{add_drop_table}; $create .= "CREATE TABLE $qt$table_name$qt (\n"; # @@ -420,7 +420,7 @@ sub create_table # $create .= "\n)"; $create .= generate_table_options($table) || ''; - $create .= ";\n\n"; +# $create .= ";\n\n"; return $drop ? ($drop,$create) : $create; } @@ -803,14 +803,13 @@ sub batch_alter_table { } else { ( ) } } @{$diff_hash->{alter_create_constraint} }; - my $drop_stmt = ''; + my @drop_stmt; if (scalar keys %fks_to_alter) { $diff_hash->{alter_drop_constraint} = [ grep { !$fks_to_alter{$_->name} } @{ $diff_hash->{alter_drop_constraint} } ]; - $drop_stmt = batch_alter_table($table, { alter_drop_constraint => [ values %fks_to_alter ] }, $options) - . "\n"; + @drop_stmt = batch_alter_table($table, { alter_drop_constraint => [ values %fks_to_alter ] }, $options); } @@ -837,7 +836,7 @@ sub batch_alter_table { return unless @stmts; # Just zero or one stmts. return now - return "$drop_stmt@stmts;" unless @stmts > 1; + return (@drop_stmt,@stmts) unless @stmts > 1; # Now strip off the 'ALTER TABLE xyz' of all but the first one @@ -854,7 +853,7 @@ sub batch_alter_table { my $padd = " " x length($alter_table); - return $drop_stmt . join( ",\n", $first, map { s/$re//; $padd . $_ } @stmts) . ';'; + return @drop_stmt, join( ",\n", $first, map { s/$re//; $padd . $_ } @stmts); } @@ -866,7 +865,8 @@ sub drop_table { # Drop (foreign key) constraints so table drops cleanly my @sql = batch_alter_table($table, { alter_drop_constraint => [ grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints ] }, $options); - return join("\n", @sql, "DROP TABLE $qt$table$qt;"); + return (@sql, "DROP TABLE $qt$table$qt"); +# return join("\n", @sql, "DROP TABLE $qt$table$qt"); } diff --git a/lib/SQL/Translator/Producer/SQLite.pm b/lib/SQL/Translator/Producer/SQLite.pm index 3060875..174f6fe 100644 --- a/lib/SQL/Translator/Producer/SQLite.pm +++ b/lib/SQL/Translator/Producer/SQLite.pm @@ -63,29 +63,23 @@ sub produce { debug("PKG: Beginning production\n"); - my $create = ''; - $create .= header_comment unless ($no_comments); - $create .= "BEGIN TRANSACTION;\n\n"; + my @create = (); + push @create, header_comment unless ($no_comments); + push @create, 'BEGIN TRANSACTION'; - my @table_defs = (); for my $table ( $schema->get_tables ) { - my @defs = create_table($table, { no_comments => $no_comments, + push @create, create_table($table, { no_comments => $no_comments, add_drop_table => $add_drop_table,}); - my $create = shift @defs; - $create .= ";\n"; - push @table_defs, $create, map( { "$_;" } @defs), ""; } for my $view ( $schema->get_views ) { - push @table_defs, create_view($view, { + push @create, create_view($view, { add_drop_view => $add_drop_table, no_comments => $no_comments, }); } -# $create .= "COMMIT;\n"; - - return wantarray ? ($create, @table_defs, "COMMIT;\n") : join("\n", ($create, @table_defs, "COMMIT;\n")); + return wantarray ? (@create, "COMMIT") : join(";\n\n", (@create, "COMMIT;\n")); } # ------------------------------------------------------------------- @@ -145,7 +139,6 @@ sub create_view { if( my $sql = $view->sql ){ $create .= " AS\n ${sql}"; } - $create .= ";\n\n"; return $create; } @@ -167,18 +160,18 @@ sub create_table # # Header. # - my $create = ''; - $create .= "--\n-- Table: $table_name\n--\n" unless $no_comments; - $create .= qq[DROP TABLE $table_name;\n] if $add_drop_table; - $create .= "CREATE ${temp}TABLE $table_name (\n"; + my @create; + push @create, "--\n-- Table: $table_name\n--\n" unless $no_comments; + push @create, qq[DROP TABLE $table_name] if $add_drop_table; + my $create_table = "CREATE ${temp}TABLE $table_name (\n"; # # Comments # if ( $table->comments and !$no_comments ){ - $create .= "-- Comments: \n-- "; - $create .= join "\n-- ", $table->comments; - $create .= "\n--\n\n"; + $create_table .= "-- Comments: \n-- "; + $create_table .= join "\n-- ", $table->comments; + $create_table .= "\n--\n\n"; } # @@ -220,9 +213,9 @@ sub create_table push @constraint_defs, create_constraint($c); } - $create .= join(",\n", map { " $_" } @field_defs ) . "\n)"; + $create_table .= join(",\n", map { " $_" } @field_defs ) . "\n)"; - return ($create, @index_defs, @constraint_defs, @trigger_defs ); + return (@create, $create_table, @index_defs, @constraint_defs, @trigger_defs ); } sub create_field @@ -405,9 +398,10 @@ sub batch_alter_table { @{$diffs->{alter_field}} == 0 && @{$diffs->{drop_field}} == 0 ) { - return join("\n", map { +# return join("\n", map { + return map { my $meth = __PACKAGE__->can($_) or die __PACKAGE__ . " cant $_"; - map { my $sql = $meth->(ref $_ eq 'ARRAY' ? @$_ : $_); $sql ? ("$sql;") : () } @{ $diffs->{$_} } + map { my $sql = $meth->(ref $_ eq 'ARRAY' ? @$_ : $_); $sql ? ("$sql") : () } @{ $diffs->{$_} } } grep { @{$diffs->{$_}} } qw/rename_table @@ -419,7 +413,7 @@ sub batch_alter_table { rename_field alter_create_index alter_create_constraint - alter_table/); + alter_table/; } @@ -439,12 +433,13 @@ sub batch_alter_table { "INSERT INTO @{[$table_name]} SELECT @{[ join(', ', $old_table->get_fields)]} FROM @{[$table_name]}_temp_alter", "DROP TABLE @{[$table_name]}_temp_alter"; - return join(";\n", @sql, ""); + return @sql; +# return join("", @sql, ""); } sub drop_table { my ($table) = @_; - return "DROP TABLE $table;"; + return "DROP TABLE $table"; } sub rename_table { diff --git a/t/30sqlt-new-diff-mysql.t b/t/30sqlt-new-diff-mysql.t index 9e1237a..5a9997e 100644 --- a/t/30sqlt-new-diff-mysql.t +++ b/t/30sqlt-new-diff-mysql.t @@ -36,42 +36,59 @@ my ( $source_schema, $target_schema, $parsed_sql_schema ) = map { # Test for differences my $out = SQL::Translator::Diff::schema_diff( $source_schema, 'MySQL', $target_schema, 'MySQL', { no_batch_alters => 1, producer_options => { quote_table_names => 0 } } ); eq_or_diff($out, <<'## END OF DIFF', "Diff as expected"); --- Convert schema 'create1.yml' to 'create2.yml': +-- Convert schema 'create1.yml' to 'create2.yml':; BEGIN; SET foreign_key_checks=0; - CREATE TABLE added ( id integer(11) ); - SET foreign_key_checks=1; - ALTER TABLE old_name RENAME TO new_name; + ALTER TABLE employee DROP FOREIGN KEY FK5302D47D93FE702E; + ALTER TABLE person DROP INDEX UC_age_name; + ALTER TABLE person DROP INDEX u_name; + ALTER TABLE employee DROP COLUMN job_title; + ALTER TABLE new_name ADD COLUMN new_field integer; + ALTER TABLE person ADD COLUMN is_rock_star tinyint(4) DEFAULT '1'; + ALTER TABLE person CHANGE COLUMN person_id person_id integer(11) NOT NULL auto_increment; + ALTER TABLE person CHANGE COLUMN name name varchar(20) NOT NULL; + ALTER TABLE person CHANGE COLUMN age age integer(11) DEFAULT '18'; + ALTER TABLE person CHANGE COLUMN iq iq integer(11) DEFAULT '0'; + ALTER TABLE person CHANGE COLUMN description physical_description text; + ALTER TABLE person ADD UNIQUE INDEX unique_name (name); + ALTER TABLE employee ADD CONSTRAINT FK5302D47D93FE702E_diff FOREIGN KEY (employee_id) REFERENCES person (person_id); + ALTER TABLE person ADD UNIQUE UC_person_id (person_id); + ALTER TABLE person ADD UNIQUE UC_age_name (age, name); + ALTER TABLE person ENGINE=InnoDB; + ALTER TABLE deleted DROP FOREIGN KEY fk_fake; + DROP TABLE deleted; + COMMIT; + ## END OF DIFF $out = SQL::Translator::Diff::schema_diff($source_schema, 'MySQL', $target_schema, 'MySQL', @@ -81,24 +98,23 @@ $out = SQL::Translator::Diff::schema_diff($source_schema, 'MySQL', $target_schem }); eq_or_diff($out, <<'## END OF DIFF', "Diff as expected"); --- Convert schema 'create1.yml' to 'create2.yml': +-- Convert schema 'create1.yml' to 'create2.yml':; BEGIN; SET foreign_key_checks=0; - CREATE TABLE added ( id integer(11) ); - SET foreign_key_checks=1; - ALTER TABLE employee DROP COLUMN job_title; + ALTER TABLE old_name RENAME TO new_name, ADD COLUMN new_field integer; + ALTER TABLE person DROP INDEX UC_age_name, ADD COLUMN is_rock_star tinyint(4) DEFAULT '1', CHANGE COLUMN person_id person_id integer(11) NOT NULL auto_increment, @@ -109,10 +125,14 @@ ALTER TABLE person DROP INDEX UC_age_name, ADD UNIQUE UC_person_id (person_id), ADD UNIQUE UC_age_name (age, name), ENGINE=InnoDB; + ALTER TABLE deleted DROP FOREIGN KEY fk_fake; + DROP TABLE deleted; + COMMIT; + ## END OF DIFF @@ -120,9 +140,9 @@ COMMIT; $out = SQL::Translator::Diff::schema_diff($source_schema, 'MySQL', $source_schema, 'MySQL' ); eq_or_diff($out, <<'## END OF DIFF', "No differences found"); --- Convert schema 'create1.yml' to 'create1.yml': +-- Convert schema 'create1.yml' to 'create1.yml':; --- No differences found +-- No differences found; ## END OF DIFF @@ -148,24 +168,22 @@ eq_or_diff($out, <<'## END OF DIFF', "No differences found"); $field->size(0); $out = SQL::Translator::Diff::schema_diff($schema, 'MySQL', $target_schema, 'MySQL', { producer_options => { quote_table_names => 0 } } ); eq_or_diff($out, <<'## END OF DIFF', "No differences found"); --- Convert schema 'create.sql' to 'create2.yml': +-- Convert schema 'create.sql' to 'create2.yml':; BEGIN; SET foreign_key_checks=0; - CREATE TABLE added ( id integer(11) ); - SET foreign_key_checks=1; - ALTER TABLE employee DROP FOREIGN KEY FK5302D47D93FE702E, DROP COLUMN job_title, ADD CONSTRAINT FK5302D47D93FE702E_diff FOREIGN KEY (employee_id) REFERENCES person (person_id); + ALTER TABLE person DROP INDEX UC_age_name, DROP INDEX u_name, ADD COLUMN is_rock_star tinyint(4) DEFAULT '1', @@ -178,9 +196,12 @@ ALTER TABLE person DROP INDEX UC_age_name, ADD UNIQUE UC_person_id (person_id), ADD UNIQUE UC_age_name (age, name), ENGINE=InnoDB; + DROP TABLE deleted; + COMMIT; + ## END OF DIFF } @@ -218,16 +239,19 @@ COMMIT; my $out = SQL::Translator::Diff::schema_diff($s1, 'MySQL', $s2, 'MySQL' ); eq_or_diff($out, <<'## END OF DIFF', "Batch alter of constraints work for InnoDB"); --- Convert schema 'Schema 1' to 'Schema 2': +-- Convert schema 'Schema 1' to 'Schema 2':; BEGIN; ALTER TABLE employee DROP FOREIGN KEY FK5302D47D93FE702E_diff; + ALTER TABLE employee ADD COLUMN new integer, ADD CONSTRAINT FK5302D47D93FE702E_diff FOREIGN KEY (employee_id) REFERENCES person (person_id) ON DELETE CASCADE, ADD CONSTRAINT new_constraint FOREIGN KEY (employee_id) REFERENCES patty (fake); + COMMIT; + ## END OF DIFF } @@ -265,7 +289,7 @@ COMMIT; my $out = SQL::Translator::Diff::schema_diff($s1, 'MySQL', $s2, 'MySQL' ); eq_or_diff($out, <<'## END OF DIFF', "Alter/drop constraints works with rename table"); --- Convert schema 'Schema 3' to 'Schema 4': +-- Convert schema 'Schema 3' to 'Schema 4':; BEGIN; @@ -273,7 +297,9 @@ ALTER TABLE employee RENAME TO fnord, DROP FOREIGN KEY bar_fk, ADD CONSTRAINT foo_fk FOREIGN KEY (employee_id) REFERENCES foo (id); + COMMIT; + ## END OF DIFF # Test quoting works too. @@ -281,7 +307,7 @@ COMMIT; { producer_options => { quote_table_names => '`' } } ); eq_or_diff($out, <<'## END OF DIFF', "Quoting can be turned on"); --- Convert schema 'Schema 3' to 'Schema 4': +-- Convert schema 'Schema 3' to 'Schema 4':; BEGIN; @@ -289,6 +315,8 @@ ALTER TABLE `employee` RENAME TO `fnord`, DROP FOREIGN KEY bar_fk, ADD CONSTRAINT foo_fk FOREIGN KEY (employee_id) REFERENCES `foo` (id); + COMMIT; + ## END OF DIFF } diff --git a/t/30sqlt-new-diff-sqlite.t b/t/30sqlt-new-diff-sqlite.t index 946b02b..7f5ebb5 100644 --- a/t/30sqlt-new-diff-sqlite.t +++ b/t/30sqlt-new-diff-sqlite.t @@ -38,8 +38,9 @@ my $out = SQL::Translator::Diff::schema_diff( $source_schema, 'SQLite', $target_ output_db => 'SQLite', } ); + eq_or_diff($out, <<'## END OF DIFF', "Diff as expected"); --- Convert schema 'create1.yml' to 'create2.yml': +-- Convert schema 'create1.yml' to 'create2.yml':; BEGIN; @@ -47,22 +48,35 @@ CREATE TABLE added ( id int(11) ); - ALTER TABLE old_name RENAME TO new_name; + DROP INDEX FK5302D47D93FE702E; + DROP INDEX UC_age_name; + DROP INDEX u_name; --- SQL::Translator::Producer::SQLite cant drop_field + +-- SQL::Translator::Producer::SQLite cant drop_field; + ALTER TABLE new_name ADD COLUMN new_field int; + ALTER TABLE person ADD COLUMN is_rock_star tinyint(4) DEFAULT '1'; --- SQL::Translator::Producer::SQLite cant alter_field --- SQL::Translator::Producer::SQLite cant rename_field + +-- SQL::Translator::Producer::SQLite cant alter_field; + +-- SQL::Translator::Producer::SQLite cant rename_field; + CREATE UNIQUE INDEX unique_name_person ON person (name); + CREATE UNIQUE INDEX UC_person_id_person ON person (person_id); + CREATE UNIQUE INDEX UC_age_name_person ON person (age, name); + DROP TABLE deleted; + COMMIT; + ## END OF DIFF @@ -73,7 +87,7 @@ $out = SQL::Translator::Diff::schema_diff($source_schema, 'SQLite', $target_sche }); eq_or_diff($out, <<'## END OF DIFF', "Diff as expected"); --- Convert schema 'create1.yml' to 'create2.yml': +-- Convert schema 'create1.yml' to 'create2.yml':; BEGIN; @@ -81,25 +95,30 @@ CREATE TABLE added ( id int(11) ); - - CREATE TEMPORARY TABLE employee_temp_alter ( position varchar(50) NOT NULL, employee_id int(11) NOT NULL, PRIMARY KEY (position, employee_id) ); + INSERT INTO employee_temp_alter SELECT position, employee_id FROM employee; + DROP TABLE employee; + CREATE TABLE employee ( position varchar(50) NOT NULL, employee_id int(11) NOT NULL, PRIMARY KEY (position, employee_id) ); + INSERT INTO employee SELECT position, employee_id FROM employee_temp_alter; + DROP TABLE employee_temp_alter; ALTER TABLE old_name RENAME TO new_name; + ALTER TABLE new_name ADD COLUMN new_field int; + CREATE TEMPORARY TABLE person_temp_alter ( person_id INTEGER PRIMARY KEY NOT NULL, name varchar(20) NOT NULL, @@ -109,8 +128,11 @@ CREATE TEMPORARY TABLE person_temp_alter ( is_rock_star tinyint(4) DEFAULT '1', physical_description text ); + INSERT INTO person_temp_alter SELECT person_id, name, age, weight, iq, is_rock_star, physical_description FROM person; + DROP TABLE person; + CREATE TABLE person ( person_id INTEGER PRIMARY KEY NOT NULL, name varchar(20) NOT NULL, @@ -120,15 +142,22 @@ CREATE TABLE person ( is_rock_star tinyint(4) DEFAULT '1', physical_description text ); + CREATE UNIQUE INDEX unique_name_person02 ON person (name); + CREATE UNIQUE INDEX UC_person_id_person02 ON person (person_id); + CREATE UNIQUE INDEX UC_age_name_person02 ON person (age, name); + INSERT INTO person SELECT person_id, name, age, weight, iq, is_rock_star, physical_description FROM person_temp_alter; + DROP TABLE person_temp_alter; DROP TABLE deleted; + COMMIT; + ## END OF DIFF @@ -136,9 +165,9 @@ COMMIT; $out = SQL::Translator::Diff::schema_diff($source_schema, 'MySQL', $source_schema, 'MySQL' ); eq_or_diff($out, <<'## END OF DIFF', "No differences found"); --- Convert schema 'create1.yml' to 'create1.yml': +-- Convert schema 'create1.yml' to 'create1.yml':; --- No differences found +-- No differences found; ## END OF DIFF diff --git a/t/38-mysql-producer.t b/t/38-mysql-producer.t index d2703fe..fbd58e8 100644 --- a/t/38-mysql-producer.t +++ b/t/38-mysql-producer.t @@ -130,9 +130,9 @@ schema: EOSCHEMA my @stmts = ( -"SET foreign_key_checks=0;\n\n", +"SET foreign_key_checks=0", -"DROP TABLE IF EXISTS `thing`;\n", +"DROP TABLE IF EXISTS `thing`", "CREATE TABLE `thing` ( `id` unsigned int auto_increment, `name` varchar(32), @@ -140,9 +140,9 @@ my @stmts = ( `description` text character set utf8 collate utf8_general_ci, PRIMARY KEY (`id`), UNIQUE `idx_unique_name` (`name`) -) ENGINE=InnoDB DEFAULT CHARACTER SET latin1 COLLATE latin1_danish_ci;\n\n", +) ENGINE=InnoDB DEFAULT CHARACTER SET latin1 COLLATE latin1_danish_ci", -"DROP TABLE IF EXISTS `thing2`;\n", +"DROP TABLE IF EXISTS `thing2`", "CREATE TABLE `thing2` ( `id` integer, `foo` integer, @@ -155,15 +155,15 @@ my @stmts = ( PRIMARY KEY (`id`, `foo`), CONSTRAINT `fk_thing` FOREIGN KEY (`foo`) REFERENCES `thing` (`id`), CONSTRAINT `fk_thing_1` FOREIGN KEY (`foo2`) REFERENCES `thing` (`id`) -) ENGINE=InnoDB;\n\n", +) ENGINE=InnoDB", -"SET foreign_key_checks=1;\n\n" +"SET foreign_key_checks=1", ); my @stmts_no_drop = grep {$_ !~ /^DROP TABLE/} @stmts; -my $mysql_out = join("", @stmts_no_drop); +my $mysql_out = join(";\n\n", @stmts_no_drop) . ";\n\n"; my $sqlt; @@ -204,7 +204,7 @@ my $mysql_out = join("", @stmts_no_drop); $out = $sqlt->translate(\$yaml_in) or die "Translat eerror:".$sqlt->error; - eq_or_diff $out, join("", @stmts), "Output looks right with DROP TABLEs"; + eq_or_diff $out, join(";\n\n", @stmts) . ";\n\n", "Output looks right with DROP TABLEs"; is_deeply \@out, \@stmts, "Array output looks right with DROP TABLEs"; } @@ -385,7 +385,7 @@ is ( SQL SECURITY DEFINER VIEW view_foo ( id, name ) AS ( SELECT id, name FROM thing - );\n\n"; + )"; is($view1_sql1, $view_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL'); @@ -397,6 +397,6 @@ is ( my $view_sql_noreplace = "CREATE VIEW view_foo ( id, name ) AS ( SELECT id, name FROM thing - );\n\n"; + )"; is($view1_sql2, $view_sql_noreplace, 'correct "CREATE VIEW" SQL'); } diff --git a/t/48xml-to-sqlite.t b/t/48xml-to-sqlite.t index f35846f..7b92f76 100644 --- a/t/48xml-to-sqlite.t +++ b/t/48xml-to-sqlite.t @@ -12,7 +12,7 @@ use SQL::Translator::Schema::Constants; BEGIN { - maybe_plan(1, 'SQL::Translator::Parser::XML::SQLFairy', + maybe_plan(2, 'SQL::Translator::Parser::XML::SQLFairy', 'SQL::Translator::Producer::SQLite'); } @@ -33,13 +33,11 @@ my $sql = $sqlt->translate( filename => $xmlfile, ) or die $sqlt->error; -# print ">>$sql<<\n"; - eq_or_diff($sql, << "SQL"); BEGIN TRANSACTION; - DROP TABLE Basic; + CREATE TABLE Basic ( id INTEGER PRIMARY KEY NOT NULL, title varchar(100) NOT NULL DEFAULT 'hello', @@ -54,18 +52,55 @@ CREATE TABLE Basic ( ); CREATE INDEX titleindex_Basic ON Basic (title); + CREATE UNIQUE INDEX emailuniqueindex_Basic ON Basic (email); DROP TABLE Another; + CREATE TABLE Another ( id INTEGER PRIMARY KEY NOT NULL ); - DROP VIEW IF EXISTS email_list; CREATE VIEW email_list AS SELECT email FROM Basic WHERE email IS NOT NULL; - COMMIT; SQL + +# Test in list context +my @sql = $sqlt->translate( + from => 'XML-SQLFairy', + to => 'SQLite', + filename => $xmlfile, +) or die $sqlt->error; + +is_deeply(\@sql, + [ + 'BEGIN TRANSACTION', + 'DROP TABLE Basic', + 'CREATE TABLE Basic ( + id INTEGER PRIMARY KEY NOT NULL, + title varchar(100) NOT NULL DEFAULT \'hello\', + description text DEFAULT \'\', + email varchar(255), + explicitnulldef varchar, + explicitemptystring varchar DEFAULT \'\', + -- Hello emptytagdef + emptytagdef varchar DEFAULT \'\', + another_id int(10) DEFAULT \'2\', + timest timestamp +)', + 'CREATE INDEX titleindex_Basic02 ON Basic (title)', + 'CREATE UNIQUE INDEX emailuniqueindex_Basic02 ON Basic (email)', + 'DROP TABLE Another', + 'CREATE TABLE Another ( + id INTEGER PRIMARY KEY NOT NULL +)', + 'DROP VIEW IF EXISTS email_list; +CREATE VIEW email_list AS + SELECT email FROM Basic WHERE email IS NOT NULL', + 'COMMIT' + ], 'SQLite translate in list context matches'); + + diff --git a/t/56-sqlite-producer.t b/t/56-sqlite-producer.t new file mode 100644 index 0000000..4e2a969 --- /dev/null +++ b/t/56-sqlite-producer.t @@ -0,0 +1,36 @@ +#!/usr/bin/perl +# vim: set ft=perl: + +use strict; +use Test::More tests => 2; +use Test::SQL::Translator qw(maybe_plan); +use FindBin qw/$Bin/; + +use SQL::Translator::Schema::View; +use SQL::Translator::Producer::SQLite; + +{ + my $view1 = SQL::Translator::Schema::View->new( name => 'view_foo', + fields => [qw/id name/], + sql => 'SELECT id, name FROM thing', + extra => { + temporary => 1, + if_not_exists => 1, + }); + my $create_opts = { no_comments => 1 }; + my $view1_sql1 = SQL::Translator::Producer::SQLite::create_view($view1, $create_opts); + + my $view_sql_replace = "CREATE TEMPORARY VIEW IF NOT EXISTS view_foo AS + SELECT id, name FROM thing"; + is($view1_sql1, $view_sql_replace, 'correct "CREATE TEMPORARY VIEW" SQL'); + + + my $view2 = SQL::Translator::Schema::View->new( name => 'view_foo', + fields => [qw/id name/], + sql => 'SELECT id, name FROM thing',); + + my $view1_sql2 = SQL::Translator::Producer::SQLite::create_view($view2, $create_opts); + my $view_sql_noreplace = "CREATE VIEW view_foo AS + SELECT id, name FROM thing"; + is($view1_sql2, $view_sql_noreplace, 'correct "CREATE VIEW" SQL'); +}