diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-07-05 09:16:23 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-07-05 09:16:23 +0000 |
commit | 0bef1682382c3497792aad48a6927bccb4c39c4e (patch) | |
tree | 27fd3d77cea760d956e85b666676949176880f73 /activerecord/lib | |
parent | 6eee49aa2e8407100471678f456de4d99c873ee4 (diff) | |
download | rails-0bef1682382c3497792aad48a6927bccb4c39c4e.tar.gz rails-0bef1682382c3497792aad48a6927bccb4c39c4e.tar.bz2 rails-0bef1682382c3497792aad48a6927bccb4c39c4e.zip |
Made default changes work in both postgresql and mysql #1612 [Tobias Luetke]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1703 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib')
3 files changed, 21 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index e335ab4e07..b440f9b6ba 100755 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -398,6 +398,10 @@ module ActiveRecord raise NotImplementedError, "change_column is not implemented" end + def change_column_default(table_name, column_name, default) + raise NotImplementedError, "change_column_default is not implemented" + end + def supports_migrations? false end @@ -482,7 +486,7 @@ module ActiveRecord end def add_column_options!(sql, options) - sql << " DEFAULT '#{options[:default]}'" if options[:default] + sql << " DEFAULT '#{options[:default]}'" unless options[:default].nil? end end diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index 14343b2750..017e1ba5a5 100755 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -198,9 +198,17 @@ module ActiveRecord def create_database(name) execute "CREATE DATABASE #{name}" end + + def change_column_default(table_name, column_name, default) + current_type = select_one("SHOW COLUMNS FROM #{table_name} LIKE '#{column_name}'")["Type"] + + change_column(table_name, column_name, current_type, { :default => default }) + end def change_column(table_name, column_name, type, options = {}) - change_column_sql = "ALTER TABLE #{table_name} MODIFY #{column_name} #{type}" + options[:default] ||= select_one("SHOW COLUMNS FROM #{table_name} LIKE '#{column_name}'")["Default"] + + change_column_sql = "ALTER TABLE #{table_name} CHANGE #{column_name} #{column_name} #{type_to_sql(type, options[:limit])}" add_column_options!(change_column_sql, options) execute(change_column_sql) end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 16acf321da..b7ba2431c4 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -151,10 +151,13 @@ module ActiveRecord end def change_column(table_name, column_name, type, options = {}) - change_column_sql = "ALTER TABLE #{table_name} ALTER COLUMN #{column_name} TYPE #{type}" - add_column_options!(change_column_sql, options) - execute(change_column_sql) + execute = "ALTER TABLE #{table_name} ALTER #{column_name} TYPE #{type}" + change_column_default(table_name, column_name, options[:default]) unless options[:default].nil? end + + def change_column_default(table_name, column_name, default) + execute "ALTER TABLE #{table_name} ALTER COLUMN #{column_name} SET DEFAULT '#{default}'" + end def rename_column(table_name, column_name, new_column_name) execute "ALTER TABLE #{table_name} RENAME COLUMN #{column_name} TO #{new_column_name}" @@ -163,7 +166,7 @@ module ActiveRecord def remove_index(table_name, column_name) execute "DROP INDEX #{table_name}_#{column_name}_index" end - + private BYTEA_COLUMN_TYPE_OID = 17 |