From: Arthur Axel 'fREW' Schmidt Date: Fri, 11 Mar 2011 02:44:25 +0000 (-0600) Subject: add drop_tables method X-Git-Tag: v0.11011~27^2~8 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f9356e0df16cf86e900662b484cb6a4ca7315b96;p=dbsrgits%2FSQL-Translator.git add drop_tables method --- diff --git a/lib/SQL/Translator/Generator/DDL/SQLServer.pm b/lib/SQL/Translator/Generator/DDL/SQLServer.pm index e9433eb..5852998 100644 --- a/lib/SQL/Translator/Generator/DDL/SQLServer.pm +++ b/lib/SQL/Translator/Generator/DDL/SQLServer.pm @@ -185,5 +185,30 @@ sub remove_table_constraints { " ALTER TABLE $q_name NOCHECK CONSTRAINT all;" } +sub drop_tables { + my ($self, $schema) = shift; + + if ($self->add_drop_table) { + my @tables = sort { $b->order <=> $a->order } $schema->get_tables; + return join "\n", ( + ( $self->add_comments ? ( + '--', + '-- Turn off constraints', + '--', + '', + ) : () ), + (map $self->remove_table_constraints($_), @tables), + ( $self->add_comments ? ( + '--', + '-- Drop tables', + '--', + '', + ) : () ), + (map $self->drop_table($_), @tables), + ) + } + return ''; +} + 1; diff --git a/lib/SQL/Translator/Generator/Role/DDL.pm b/lib/SQL/Translator/Generator/Role/DDL.pm index e8ddcf9..abdd46e 100644 --- a/lib/SQL/Translator/Generator/Role/DDL.pm +++ b/lib/SQL/Translator/Generator/Role/DDL.pm @@ -29,6 +29,14 @@ has unquoted_defaults => ( builder => '_build_unquoted_defaults', ); +has add_comments => ( + is => 'ro', +); + +has add_drop_table => ( + is => 'ro', +); + # would also be handy to have a required size set if there is such a thing sub field_name { $_[0]->quote($_[1]->name) } diff --git a/lib/SQL/Translator/Producer/SQLServer.pm b/lib/SQL/Translator/Producer/SQLServer.pm index f4b225c..af171aa 100644 --- a/lib/SQL/Translator/Producer/SQLServer.pm +++ b/lib/SQL/Translator/Producer/SQLServer.pm @@ -11,10 +11,13 @@ use SQL::Translator::Schema::Constants; use SQL::Translator::Utils qw(debug header_comment); use SQL::Translator::Generator::DDL::SQLServer; -my $future = SQL::Translator::Generator::DDL::SQLServer->new(); - sub produce { my $translator = shift; + my $future = SQL::Translator::Generator::DDL::SQLServer->new( + add_comments => !$translator->no_comments, + add_drop_tables => $translator->add_drop_table, + ); + my $no_comments = $translator->no_comments; my $add_drop_table = $translator->add_drop_table; my $schema = $translator->schema; @@ -23,21 +26,13 @@ sub produce { $output .= header_comment."\n" unless ($no_comments); # Generate the DROP statements. - if ($add_drop_table) { - my @tables = sort { $b->order <=> $a->order } $schema->get_tables; - $output .= "--\n-- Turn off constraints\n--\n\n" unless $no_comments; - $output .= join "\n", map $future->remove_table_constraints($_), @tables; - $output .= "\n"; - $output .= "--\n-- Drop tables\n--\n\n" unless $no_comments; - $output .= join "\n", map $future->drop_table($_), @tables; - $output .= "\n"; - } + $output .= $future->drop_tables; # these need to be added separately, as tables may not exist yet my @foreign_constraints = (); for my $table ( grep { $_->name } $schema->get_tables ) { - my $table_name_ur = unreserve($table->name); + my $table_name_ur = $future->quote($table->name); my ( @comments ); @@ -72,8 +67,6 @@ sub produce { return $output; } -sub unreserve { $future->quote($_[0]) } - 1; =pod