From: Arthur Axel 'fREW' Schmidt Date: Thu, 24 Feb 2011 21:49:45 +0000 (-0600) Subject: Turn off constraints before dropping tables in SQL Server X-Git-Tag: v0.11008~9 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d02c3cd2ab9f3d37f23b499b018dc276f6797cbc;p=dbsrgits%2FSQL-Translator.git Turn off constraints before dropping tables in SQL Server --- diff --git a/Changes b/Changes index eb128a1..12420b5 100644 --- a/Changes +++ b/Changes @@ -5,6 +5,7 @@ * Add distinction of autoinc and regular primary keys to the GraphViz producer * Fix odd invocation of Test::More::pass() in t/36-filters.t (RT#64728) * Quote everything in SQL Server +* Turn off constraints before dropping tables in SQL Server # ---------------------------------------------------------- # 0.11007 2010-11-30 diff --git a/lib/SQL/Translator/Producer/SQLServer.pm b/lib/SQL/Translator/Producer/SQLServer.pm index ba644c6..c3ffee0 100644 --- a/lib/SQL/Translator/Producer/SQLServer.pm +++ b/lib/SQL/Translator/Producer/SQLServer.pm @@ -115,22 +115,21 @@ sub produce { my $output; $output .= header_comment."\n" unless ($no_comments); - # Generate the DROP statements. We do this in one block here as if we - # have fkeys we need to drop in the correct order otherwise they will fail - # due to the dependancies the fkeys setup. (There is no way to turn off - # fkey checking while we sort the schema like MySQL's set - # foreign_key_checks=0) - # We assume the tables are in the correct order to set them up as you need - # to have created a table to fkey to it. So the reverse order should drop - # them properly, fingers crossed... + # 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; + foreach my $table (@tables) { + my $name = $table->name; + my $q_name = unreserve($name); + $output .= "IF EXISTS (SELECT name FROM sysobjects WHERE name = '$name' AND type = 'U') ALTER TABLE $q_name NOCHECK CONSTRAINT all;\n" + } + $output .= "\n"; $output .= "--\n-- Drop tables\n--\n\n" unless $no_comments; - foreach my $table ( - sort { $b->order <=> $a->order } $schema->get_tables - ) { + foreach my $table (@tables) { my $name = $table->name; my $q_name = unreserve($name); - $output .= qq{IF EXISTS (SELECT name FROM sysobjects WHERE name = '$name' AND type = 'U') DROP TABLE $q_name;\n\n} + $output .= "IF EXISTS (SELECT name FROM sysobjects WHERE name = '$name' AND type = 'U') DROP TABLE $q_name;\n" } }