Permalink
Browse files

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
  • Loading branch information...
1 parent 6eee49a commit 0bef1682382c3497792aad48a6927bccb4c39c4e @dhh dhh committed Jul 5, 2005
@@ -398,6 +398,10 @@ def change_column(table_name, column_name, type, options = {})
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 @@ def format_log_entry(message, dump = nil)
end
def add_column_options!(sql, options)
- sql << " DEFAULT '#{options[:default]}'" if options[:default]
+ sql << " DEFAULT '#{options[:default]}'" unless options[:default].nil?
end
end
@@ -198,9 +198,17 @@ def drop_database(name)
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
@@ -151,10 +151,13 @@ def schema_search_path
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 @@ def rename_column(table_name, column_name, new_column_name)
def remove_index(table_name, column_name)
execute "DROP INDEX #{table_name}_#{column_name}_index"
end
-
+
private
BYTEA_COLUMN_TYPE_OID = 17
@@ -108,9 +108,11 @@ def test_change_column
def test_change_column_with_new_default
Person.connection.add_column "people", "administrator", :boolean, :default => 1
+ Person.reset_column_information
assert Person.new.administrator?
assert_nothing_raised { Person.connection.change_column "people", "administrator", :boolean, :default => 0 }
+ Person.reset_column_information
assert !Person.new.administrator?
end

0 comments on commit 0bef168

Please sign in to comment.