提交 3870ce06 编写于 作者: R Rafael Mendonça França

Merge pull request #14766 from eric-chahin/migration_bug

Changed change_column in PG schema_statements.rb to make sure that the u...
......@@ -182,6 +182,15 @@ def quoted_date(value) #:nodoc:
end
result
end
# Does not quote function default values for UUID columns
def quote_default_value(value, column) #:nodoc:
if column.type == :uuid && value =~ /\(\)/
value
else
quote(value)
end
end
end
end
end
......
......@@ -187,6 +187,10 @@ def columns(table_name)
end
end
def column_for(table_name, column_name) #:nodoc:
columns(table_name).detect { |c| c.name == column_name.to_s }
end
# Returns the current database name.
def current_database
query('select current_database()', 'SCHEMA')[0][0]
......@@ -404,13 +408,15 @@ def change_column(table_name, column_name, type, options = {})
# Changes the default value of a table column.
def change_column_default(table_name, column_name, default)
clear_cache!
execute "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} SET DEFAULT #{quote(default)}"
column = column_for(table_name, column_name)
execute "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} SET DEFAULT #{quote_default_value(default, column)}" if column
end
def change_column_null(table_name, column_name, null, default = nil)
clear_cache!
unless null || default.nil?
execute("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL")
column = column_for(table_name, column_name)
execute("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote_default_value(default, column)} WHERE #{quote_column_name(column_name)} IS NULL") if column
end
execute("ALTER TABLE #{quote_table_name(table_name)} ALTER #{quote_column_name(column_name)} #{null ? 'DROP' : 'SET'} NOT NULL")
end
......
......@@ -40,6 +40,19 @@ class UUIDType < ActiveRecord::Base
drop_table "uuid_data_type"
end
def test_change_column_default
@connection.add_column :uuid_data_type, :thingy, :uuid, null: false, default: "uuid_generate_v1()"
UUIDType.reset_column_information
column = UUIDType.columns.find { |c| c.name == 'thingy' }
assert_equal "uuid_generate_v1()", column.default_function
@connection.change_column :uuid_data_type, :thingy, :uuid, null: false, default: "uuid_generate_v4()"
UUIDType.reset_column_information
column = UUIDType.columns.find { |c| c.name == 'thingy' }
assert_equal "uuid_generate_v4()", column.default_function
end
def test_data_type_of_uuid_types
column = UUIDType.columns_hash["guid"]
assert_equal :uuid, column.type
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册