Title: [879] trunk/activerecord-jdbc: JRUBY-1905: add_column fix for derby and hsqldb (Stephen Bannasch)

Diff

Modified: trunk/activerecord-jdbc/History.txt (878 => 879)


--- trunk/activerecord-jdbc/History.txt	2008-01-10 19:01:50 UTC (rev 878)
+++ trunk/activerecord-jdbc/History.txt	2008-01-13 06:12:23 UTC (rev 879)
@@ -1,3 +1,7 @@
+== 0.7.2
+
+- Fix for add_column for derby, hsqldb, and postgresql (JRUBY-1905; Stephen Bannasch)
+
 == 0.7.1
 
 - Add adapter and driver for H2 courtesy of Caleb Land

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


--- trunk/activerecord-jdbc/lib/jdbc_adapter/jdbc_derby.rb	2008-01-10 19:01:50 UTC (rev 878)
+++ trunk/activerecord-jdbc/lib/jdbc_adapter/jdbc_derby.rb	2008-01-13 06:12:23 UTC (rev 879)
@@ -195,7 +195,21 @@
       end
       ""
     end
-    
+
+
+    def add_column(table_name, column_name, type, options = {})
+      if option_not_null = options[:null] == false
+        option_not_null = options.delete(:null)
+      end
+      add_column_sql = "ALTER TABLE #{quote_table_name(table_name)} ADD #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
+      add_column_options!(add_column_sql, options)
+      execute(add_column_sql)
+      if option_not_null
+        alter_column_sql = "ALTER TABLE #{quote_table_name(table_name)} ALTER #{quote_column_name(column_name)} NOT NULL"
+      end
+    end
+
+    # I don't think this method is ever called ??? (stepheneb)
     def create_column(name, refid, colno)
       stmt = COLUMN_TYPE_STMT % [refid, strip_quotes(name)]
       coldef = ""

Modified: trunk/activerecord-jdbc/lib/jdbc_adapter/jdbc_hsqldb.rb (878 => 879)


--- trunk/activerecord-jdbc/lib/jdbc_adapter/jdbc_hsqldb.rb	2008-01-10 19:01:50 UTC (rev 878)
+++ trunk/activerecord-jdbc/lib/jdbc_adapter/jdbc_hsqldb.rb	2008-01-13 06:12:23 UTC (rev 879)
@@ -117,6 +117,18 @@
       '0'
     end
 
+    def add_column(table_name, column_name, type, options = {})
+      if option_not_null = options[:null] == false
+        option_not_null = options.delete(:null)
+      end
+      add_column_sql = "ALTER TABLE #{quote_table_name(table_name)} ADD #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
+      add_column_options!(add_column_sql, options)
+      execute(add_column_sql)
+      if option_not_null
+        alter_column_sql = "ALTER TABLE #{quote_table_name(table_name)} ALTER #{quote_column_name(column_name)} NOT NULL"
+      end
+    end
+
     def change_column(table_name, column_name, type, options = {}) #:nodoc:
       execute "ALTER TABLE #{table_name} ALTER COLUMN #{column_name} #{type_to_sql(type, options[:limit])}"
     end

Modified: trunk/activerecord-jdbc/lib/jdbc_adapter/jdbc_postgre.rb (878 => 879)


--- trunk/activerecord-jdbc/lib/jdbc_adapter/jdbc_postgre.rb	2008-01-10 19:01:50 UTC (rev 878)
+++ trunk/activerecord-jdbc/lib/jdbc_adapter/jdbc_postgre.rb	2008-01-13 06:12:23 UTC (rev 879)
@@ -297,8 +297,11 @@
     
     def add_column(table_name, column_name, type, options = {})
       execute("ALTER TABLE #{table_name} ADD #{column_name} #{type_to_sql(type, options[:limit])}")
-      execute("ALTER TABLE #{table_name} ALTER #{column_name} SET NOT NULL") if options[:null] == false
       change_column_default(table_name, column_name, options[:default]) unless options[:default].nil?
+      if options[:null] == false
+        execute("UPDATE #{table_name} SET #{column_name} = '#{options[:default]}'") if options[:default]
+        execute("ALTER TABLE #{table_name} ALTER #{column_name} SET NOT NULL")
+      end
     end
 
     def change_column(table_name, column_name, type, options = {}) #:nodoc:

Modified: trunk/activerecord-jdbc/lib/jdbc_adapter/version.rb (878 => 879)


--- trunk/activerecord-jdbc/lib/jdbc_adapter/version.rb	2008-01-10 19:01:50 UTC (rev 878)
+++ trunk/activerecord-jdbc/lib/jdbc_adapter/version.rb	2008-01-13 06:12:23 UTC (rev 879)
@@ -1,5 +1,5 @@
 module JdbcAdapter
   module Version
-    VERSION = "0.7.1"
+    VERSION = "0.7.2"
   end
 end
\ No newline at end of file

Modified: trunk/activerecord-jdbc/test/jdbc_common.rb (878 => 879)


--- trunk/activerecord-jdbc/test/jdbc_common.rb	2008-01-10 19:01:50 UTC (rev 878)
+++ trunk/activerecord-jdbc/test/jdbc_common.rb	2008-01-13 06:12:23 UTC (rev 879)
@@ -2,6 +2,7 @@
 require 'rubygems'
 require 'models/auto_id'
 require 'models/entry'
+require 'models/add_not_null_column_to_table'
 require 'simple'
 require 'has_many_through'
 require 'test/unit'

Added: trunk/activerecord-jdbc/test/models/add_not_null_column_to_table.rb (0 => 879)


--- trunk/activerecord-jdbc/test/models/add_not_null_column_to_table.rb	                        (rev 0)
+++ trunk/activerecord-jdbc/test/models/add_not_null_column_to_table.rb	2008-01-13 06:12:23 UTC (rev 879)
@@ -0,0 +1,12 @@
+require 'rubygems'
+require 'active_record'
+
+class AddNotNullColumnToTable < ActiveRecord::Migration
+  def self.up
+    add_column :entries, :color, :string, :null => false, :default => "blue"
+  end
+
+  def self.down
+    remove_column :entries, :color
+  end
+end

Modified: trunk/activerecord-jdbc/test/simple.rb (878 => 879)


--- trunk/activerecord-jdbc/test/simple.rb	2008-01-10 19:01:50 UTC (rev 878)
+++ trunk/activerecord-jdbc/test/simple.rb	2008-01-13 06:12:23 UTC (rev 879)
@@ -142,6 +142,11 @@
     assert ActiveRecord::Base.connected?
   end
 
+  def test_add_not_null_column_to_table
+    AddNotNullColumnToTable.up
+    AddNotNullColumnToTable.down
+  end
+
   class Animal < ActiveRecord::Base; end
   def test_fetching_columns_for_nonexistent_table_should_raise
     assert_raises(ActiveRecord::ActiveRecordError) do
_______________________________________________
Jruby-extras-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/jruby-extras-devel

Reply via email to