# ----------------------------------------------------------
#
# ----------------------------------------------------------
+* 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)
$meth ? map {
my $sql = $meth->( (ref $_ eq 'ARRAY' ? @$_ : $_), $self->producer_options );
- $sql ? ("$sql;") : ();
+ $sql ? ("$sql") : ();
} @{ $flattened_diffs{$_} }
: $self->ignore_missing_methods
? "-- $producer_class cant $_"
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;
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);
# 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 {
if( my $sql = $view->sql ){
$create .= " AS (\n ${sql}\n )";
}
- $create .= ";\n\n";
+# $create .= "";
return $create;
}
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";
#
#
$create .= "\n)";
$create .= generate_table_options($table) || '';
- $create .= ";\n\n";
+# $create .= ";\n\n";
return $drop ? ($drop,$create) : $create;
}
} 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);
}
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
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);
}
# 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");
}
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"));
}
# -------------------------------------------------------------------
if( my $sql = $view->sql ){
$create .= " AS\n ${sql}";
}
- $create .= ";\n\n";
return $create;
}
#
# 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";
}
#
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
@{$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
rename_field
alter_create_index
alter_create_constraint
- alter_table/);
+ 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 {
# 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',
});
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,
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
$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
$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',
ADD UNIQUE UC_person_id (person_id),
ADD UNIQUE UC_age_name (age, name),
ENGINE=InnoDB;
+
DROP TABLE deleted;
+
COMMIT;
+
## END OF DIFF
}
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
}
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;
DROP FOREIGN KEY bar_fk,
ADD CONSTRAINT foo_fk FOREIGN KEY (employee_id) REFERENCES foo (id);
+
COMMIT;
+
## END OF DIFF
# Test quoting works too.
{ 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;
DROP FOREIGN KEY bar_fk,
ADD CONSTRAINT foo_fk FOREIGN KEY (employee_id) REFERENCES `foo` (id);
+
COMMIT;
+
## END OF DIFF
}
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;
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
});
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;
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,
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,
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
$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
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),
`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,
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;
$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";
}
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');
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');
}
BEGIN {
- maybe_plan(1, 'SQL::Translator::Parser::XML::SQLFairy',
+ maybe_plan(2, 'SQL::Translator::Parser::XML::SQLFairy',
'SQL::Translator::Producer::SQLite');
}
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',
);
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');
+
+
--- /dev/null
+#!/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');
+}