mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Changed change_column in PG schema_statements.rb to make sure that the uuid_generate function was not being quoted.
This commit is contained in:
parent
3fce111b3b
commit
eaa3949d36
3 changed files with 30 additions and 2 deletions
|
@ -182,6 +182,15 @@ module ActiveRecord
|
|||
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 @@ module ActiveRecord
|
|||
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 @@ module ActiveRecord
|
|||
# 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 PostgresqlUUIDTest < ActiveRecord::TestCase
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue