Fix ORA-01442: column to be modified to NOT NULL is already NOT NULL
Daniel Ruoso [Mon, 14 Apr 2008 17:29:23 +0000 (17:29 +0000)]
lib/SQL/Translator/Producer/Oracle.pm
t/54-oracle-alter-field.t
t/data/oracle/schema_diff_a.yaml
t/data/oracle/schema_diff_b.yaml

index 1d00b8d..98f78c9 100644 (file)
@@ -433,6 +433,13 @@ sub alter_field {
     my ($field_create, $field_defs, $trigger_defs, $field_comments) =
       create_field($to_field, $options, {});
 
+    # Fix ORA-01442
+    if ($to_field->is_nullable && !$from_field->is_nullable) {
+        die 'Cannot remove NOT NULL from table field';
+    } elsif (!$from_field->is_nullable && !$to_field->is_nullable) {
+        @$field_defs = map { s/ NOT NULL//; $_} @$field_defs;
+    }
+
     my $table_name = $to_field->table->name;
     my $table_name_ur = unreserve( $table_name );
 
index c8cd4b9..c4f4584 100644 (file)
@@ -9,7 +9,7 @@ use SQL::Translator;
 use SQL::Translator::Diff;
 
 BEGIN {
-    maybe_plan(2, 'SQL::Translator::Parser::YAML',
+    maybe_plan(3, 'SQL::Translator::Parser::YAML',
                   'SQL::Translator::Producer::Oracle');
 }
 
@@ -46,5 +46,7 @@ my $d = SQL::Translator::Diff->new
 my $diff = $d->compute_differences->produce_diff_sql || die $d->error;
 
 ok($diff, 'Diff generated.');
-like($diff, '/ALTER TABLE d_operator MODIFY \( name nvarchar2\(10\) NOT NULL \)/',
+like($diff, '/ALTER TABLE d_operator MODIFY \( name nvarchar2\(10\) \)/',
+     'Alter table generated.');
+like($diff, '/ALTER TABLE d_operator MODIFY \( other nvarchar2\(10\) NOT NULL \)/',
      'Alter table generated.');
index ab0d955..9b66596 100644 (file)
@@ -40,5 +40,16 @@ schema:
           order: 59
           size:
             - 0
+        other:
+          data_type: varchar2(10)
+          default_value: ~
+          extra: {}
+          is_nullable: 1
+          is_primary_key: 0
+          is_unique: 0
+          name: other
+          order: 59
+          size:
+            - 0
       name: d_operator
       order: 11
index 1fbbf90..9f050c5 100644 (file)
@@ -40,5 +40,16 @@ schema:
           order: 59
           size:
             - 0
+        other:
+          data_type: nvarchar2(10)
+          default_value: ~
+          extra: {}
+          is_nullable: 0
+          is_primary_key: 0
+          is_unique: 0
+          name: other
+          order: 59
+          size:
+            - 0
       name: d_operator
       order: 11