binmode STDOUT to not generate garbage in a UTF-8 environment (bug #71399)
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / SQLServer.pm
index ba644c6..bbd46da 100644 (file)
@@ -60,9 +60,9 @@ $DEBUG = 1 unless defined $DEBUG;
 use Data::Dumper;
 use SQL::Translator::Schema::Constants;
 use SQL::Translator::Utils qw(debug header_comment);
-use SQL::Translator::Shim;
+use SQL::Translator::ProducerUtils;
 
-my $shim = SQL::Translator::Shim->new( quote_chars => ['[', ']'] );
+my $util = SQL::Translator::ProducerUtils->new( quote_chars => ['[', ']'] );
 
 my %translate  = (
     date      => 'datetime',
@@ -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"
         }
     }
 
@@ -303,9 +302,18 @@ sub produce {
             }
             elsif ( $type eq UNIQUE ) {
                 $name = $name_ur || mk_name( $table_name . '_uc' );
-                $c_def =
-                    "CONSTRAINT $name UNIQUE " .
-                    '(' . join( ', ', @fields ) . ')';
+                my @nullable = grep { $_->is_nullable } $constraint->fields;
+                if (!@nullable) {
+                  $c_def =
+                      "CONSTRAINT $name UNIQUE " .
+                      '(' . join( ', ', @fields ) . ')';
+                } else {
+                   push @index_defs,
+                       "CREATE UNIQUE NONCLUSTERED INDEX $name_ur ON $table_name_ur (" .
+                          join( ', ', @fields ) . ')' .
+                          ' WHERE ' . join( ' AND ', map unreserve($_->name) . ' IS NOT NULL', @nullable ) . ';';
+                   next;
+                }
             }
             push @constraint_defs, $c_def;
         }
@@ -395,7 +403,7 @@ sub mk_name {
 }
 
 # -------------------------------------------------------------------
-sub unreserve { $shim->quote($_[0]) }
+sub unreserve { $util->quote($_[0]) }
 
 1;