Title: [1015] trunk/activerecord-jdbc: Fix problem with AR 2.1 and Derby with ' DEFAULT NULL NULL' in column defs
Revision
1015
Author
nicksieger
Date
2008-06-04 17:19:01 -0400 (Wed, 04 Jun 2008)

Log Message

Fix problem with AR 2.1 and Derby with 'DEFAULT NULL NULL' in column defs

Modified Paths

Diff

Modified: trunk/activerecord-jdbc/lib/jdbc_adapter/jdbc_derby.rb (1014 => 1015)


--- trunk/activerecord-jdbc/lib/jdbc_adapter/jdbc_derby.rb	2008-06-04 19:04:54 UTC (rev 1014)
+++ trunk/activerecord-jdbc/lib/jdbc_adapter/jdbc_derby.rb	2008-06-04 21:19:01 UTC (rev 1015)
@@ -7,7 +7,7 @@
       config[:driver] ||= "org.apache.derby.jdbc.EmbeddedDriver"
       embedded_driver(config)
     end
-  end 
+  end
 
   module Derby
     def self.column_selector
@@ -17,10 +17,10 @@
     def self.adapter_selector
       [/derby/i, lambda {|cfg,adapt| adapt.extend(::JdbcSpec::Derby)}]
     end
-      
+
     def self.monkey_rails
       unless @already_monkeyd
-        # Needed because Rails is broken wrt to quoting of 
+        # Needed because Rails is broken wrt to quoting of
         # some values. Most databases are nice about it,
         # but not Derby. The real issue is that you can't
         # compare a CHAR value to a NUMBER column.
@@ -32,7 +32,7 @@
                                   construct_finder_sql_for_association_limiting(options, join_dependency),
                                   "#{name} Load IDs For Limited Eager Loading"
                                   ).collect { |row| connection.quote(row[primary_key], columns_hash[primary_key]) }.join(", ")
-          end           
+          end
         end
 
         @already_monkeyd = true
@@ -46,12 +46,12 @@
     def self.included(*args)
       monkey_rails
     end
-    
+
     module Column
       def value_to_binary(value)
         value.scan(/[0-9A-Fa-f]{2}/).collect {|v| v.to_i(16)}.pack("C*")
       end
-      
+
       def cast_to_date_or_time(value)
         return value if value.is_a? Date
         return nil if value.blank?
@@ -71,14 +71,14 @@
       end
 
       def simplified_type(field_type)
-        return :boolean if field_type =~ /smallint/i 
+        return :boolean if field_type =~ /smallint/i
         return :float if field_type =~ /real/i
         super
       end
     end
 
     include JdbcSpec::MissingFunctionalityHelper
-    
+
     def modify_types(tp)
       tp[:primary_key] = "int generated by default as identity NOT NULL PRIMARY KEY"
       tp[:integer][:limit] = nil
@@ -86,7 +86,14 @@
       tp[:boolean] = {:name => "smallint"}
       tp
     end
-    
+
+    # Override default -- fix case where ActiveRecord passes :default => nil, :null => true
+    def add_column_options!(sql, options)
+      options.delete(:default) if options.has_key?(:default) && options[:default].nil?
+      options.delete(:null) if options.has_key?(:null) && (options[:null].nil? || options[:null].true?)
+      super
+    end
+
     def classes_for_table_name(table)
       ActiveRecord::Base.send(:subclasses).select {|klass| klass.table_name == table}
     end
@@ -94,9 +101,9 @@
     # Set the sequence to the max value of the table's column.
     def reset_sequence!(table, column, sequence = nil)
       mpk = select_value("SELECT MAX(#{quote_column_name column}) FROM #{table}")
-      execute("ALTER TABLE #{table} ALTER COLUMN #{quote_column_name column} RESTART WITH #{mpk.to_i + 1}")      
+      execute("ALTER TABLE #{table} ALTER COLUMN #{quote_column_name column} RESTART WITH #{mpk.to_i + 1}")
     end
-      
+
     def reset_pk_sequence!(table, pk = nil, sequence = nil)
       klasses = classes_for_table_name(table)
       klass   = klasses.nil? ? nil : klasses.first
@@ -105,7 +112,7 @@
         reset_sequence!(klass.table_name, pk)
       end
     end
-    
+
     def primary_key(table_name) #:nodoc:
       primary_keys(table_name).first
     end
@@ -117,7 +124,7 @@
     def rename_table(name, new_name)
       execute "RENAME TABLE #{name} TO #{new_name}"
     end
-    
+
     COLUMN_INFO_STMT = "SELECT C.COLUMNNAME, C.REFERENCEID, C.COLUMNNUMBER FROM SYS.SYSCOLUMNS C, SYS.SYSTABLES T WHERE T.TABLEID = '%s' AND T.TABLEID = C.REFERENCEID ORDER BY C.COLUMNNUMBER"
 
     COLUMN_TYPE_STMT = "SELECT COLUMNDATATYPE, COLUMNDEFAULT FROM SYS.SYSCOLUMNS WHERE REFERENCEID = '%s' AND COLUMNNAME = '%s'"
@@ -129,7 +136,7 @@
       return name unless name
       %Q{"#{name}"}
     end
-    
+
     def strip_quotes(str)
       return str unless str
       return str unless /^(["']).*\1$/ =~ str
@@ -140,7 +147,7 @@
       return name unless name && name['"']
       name.gsub(/"/,'""')
     end
-    
+
     def reinstate_auto_increment(name, refid, coldef)
       stmt = AUTO_INC_STMT % [refid, strip_quotes(name)]
       data = ""
@@ -224,9 +231,9 @@
       end
       coldef
     end
-    
+
     SIZEABLE = %w(VARCHAR CLOB BLOB)
-    
+
     def structure_dump #:nodoc:
       definition=""
       rs = @connection.connection.meta_data.getTables(nil,nil,nil,["TABLE"].to_java(:string))
@@ -244,14 +251,14 @@
           elsif d1
             default = " DEFAULT #{d1}"
           end
-          
+
           type = rs2.getString(6)
           col_size = rs2.getString(7)
           nulling = (rs2.getString(18) == 'NO' ? " NOT NULL" : "")
-          create_col_string = add_quotes(expand_double_quotes(strip_quotes(col_name))) + 
-            " " + 
+          create_col_string = add_quotes(expand_double_quotes(strip_quotes(col_name))) +
+            " " +
             type +
-            (SIZEABLE.include?(type) ? "(#{col_size})" : "") + 
+            (SIZEABLE.include?(type) ? "(#{col_size})" : "") +
             nulling +
             default
           if !first_col
@@ -259,23 +266,23 @@
           else
             create_col_string = " #{create_col_string}"
           end
-          
+
           definition << create_col_string
-          
+
           first_col = false
         end
         definition << ");\n\n"
       end
       definition
     end
-    
+
     # Support for removing columns added via derby bug issue:
     # https://issues.apache.org/jira/browse/DERBY-1489
     #
     # This feature has not made it into a formal release and is not in Java 6.
     # If the normal strategy fails we fall back on a strategy by creating a new
     # table without the new column and there after moving the data to the new
-    # 
+    #
     def remove_column(table_name, column_name)
       begin
         execute "ALTER TABLE #{table_name} DROP COLUMN #{column_name} RESTRICT"
@@ -285,7 +292,7 @@
         end
       end
     end
-    
+
     # Notes about changing in Derby:
     #    http://db.apache.org/derby/docs/10.2/ref/rrefsqlj81859.html#rrefsqlj81859__rrefsqlj37860)
     #
@@ -338,7 +345,7 @@
         alter_table(table_name, :rename => {column_name => new_column_name})
       end
     end
-    
+
     def primary_keys(table_name)
       @connection.primary_keys table_name.to_s.upcase
     end
@@ -364,7 +371,7 @@
         name
       end
     end
-    
+
     def quoted_true
       '1'
     end

Modified: trunk/activerecord-jdbc/test/jdbc_common.rb (1014 => 1015)


--- trunk/activerecord-jdbc/test/jdbc_common.rb	2008-06-04 19:04:54 UTC (rev 1014)
+++ trunk/activerecord-jdbc/test/jdbc_common.rb	2008-06-04 21:19:01 UTC (rev 1015)
@@ -1,5 +1,7 @@
+require 'rubygems'
+# Specify version of activerecord here if desired
+# gem 'activerecord', '=2.1'
 require 'jdbc_adapter'
-require 'rubygems'
 require 'models/auto_id'
 require 'models/entry'
 require 'models/add_not_null_column_to_table'
_______________________________________________
Jruby-extras-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/jruby-extras-devel

Reply via email to