* 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)
+* Using DROP VIEW instead of OR REPLACE clause in the Pg producer, as replace only allows replacement with same number of columns (wreis)
+* Added support for DROP VIEW and fixed CREATE VIEW statement in the sqlite producer (wreis)
# ----------------------------------------------------------
# 0.09001 2008-08-19
use warnings;
use vars qw[ $DEBUG $WARN $VERSION %used_names ];
$VERSION = sprintf "%d.%02d", q$Revision: 1.29 $ =~ /(\d+)\.(\d+)/;
-$DEBUG = 1 unless defined $DEBUG;
+$DEBUG = 0 unless defined $DEBUG;
use SQL::Translator::Schema::Constants;
use SQL::Translator::Utils qw(debug header_comment);
# -------------------------------------------------------------------
sub produce {
my $translator = shift;
- $DEBUG = $translator->debug;
- $WARN = $translator->show_warnings;
+ local $DEBUG = $translator->debug;
+ local $WARN = $translator->show_warnings;
my $no_comments = $translator->no_comments;
my $add_drop_table = $translator->add_drop_table;
my $schema = $translator->schema;
for my $view ( $schema->get_views ) {
push @table_defs, create_view($view, {
- add_replace_view => $add_drop_table,
+ add_drop_view => $add_drop_table,
quote_table_names => $qt,
quote_field_names => $qf,
no_comments => $no_comments,
my ($view, $options) = @_;
my $qt = $options->{quote_table_names} || '';
my $qf = $options->{quote_field_names} || '';
+ my $add_drop_view = $options->{add_drop_view};
my $view_name = $view->name;
debug("PKG: Looking at view '${view_name}'\n");
my $create = '';
$create .= "--\n-- View: ${qt}${view_name}${qt}\n--\n"
unless $options->{no_comments};
+ $create .= "DROP VIEW ${qt}${view_name}${qt};\n" if $add_drop_view;
$create .= 'CREATE';
- $create .= ' OR REPLACE' if $options->{add_replace_view};
my $extra = $view->extra;
$create .= " TEMPORARY" if exists($extra->{temporary}) && $extra->{temporary};
}
for my $view ( $schema->get_views ) {
- push @table_defs, create_view($view, {no_comments => $no_comments,});
+ push @table_defs, create_view($view, {
+ add_drop_view => $add_drop_table,
+ no_comments => $no_comments,
+ });
}
# $create .= "COMMIT;\n";
sub create_view {
my ($view, $options) = @_;
+ my $add_drop_view = $options->{add_drop_view};
my $view_name = $view->name;
debug("PKG: Looking at view '${view_name}'\n");
my $extra = $view->extra;
my $create = '';
$create .= "--\n-- View: ${view_name}\n--\n" unless $options->{no_comments};
+ $create .= "DROP VIEW IF EXISTS $view_name;\n" if $add_drop_view;
$create .= 'CREATE';
$create .= " TEMPORARY" if exists($extra->{temporary}) && $extra->{temporary};
$create .= ' VIEW';
$create .= " ${view_name}";
if( my $sql = $view->sql ){
- $create .= " AS (\n ${sql}\n )";
+ $create .= " AS\n ${sql}";
}
$create .= ";\n\n";
return $create;
-CREATE OR REPLACE VIEW "email_list" ( "email" ) AS (
+DROP VIEW "email_list";
+CREATE VIEW "email_list" ( "email" ) AS (
SELECT email FROM Basic WHERE email IS NOT NULL
);
my $create_opts = { add_replace_view => 1, no_comments => 1 };
my $view1_sql1 = SQL::Translator::Producer::PostgreSQL::create_view($view1, $create_opts);
-my $view_sql_replace = "CREATE OR REPLACE VIEW view_foo ( id, name ) AS (
+my $view_sql_replace = "CREATE 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 $create2_opts = { add_replace_view => 1, no_comments => 1 };
my $view2_sql1 = SQL::Translator::Producer::PostgreSQL::create_view($view2, $create2_opts);
-my $view2_sql_replace = "CREATE OR REPLACE TEMPORARY VIEW view_foo2 AS (
+my $view2_sql_replace = "CREATE TEMPORARY VIEW view_foo2 AS (
SELECT id, name FROM thing
) WITH CASCADED CHECK OPTION;\n\n";
is($view2_sql1, $view2_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL 2');
);
-CREATE VIEW email_list AS (
- SELECT email FROM Basic WHERE email IS NOT NULL
- );
+DROP VIEW IF EXISTS email_list;
+CREATE VIEW email_list AS
+ SELECT email FROM Basic WHERE email IS NOT NULL;
COMMIT;