Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #14766 from eric-chahin/migration_bug

Changed change_column in PG schema_statements.rb to make sure that the u...
Conflicts:
	activerecord/test/cases/adapters/postgresql/uuid_test.rb
  • Loading branch information...
commit 00fd52bb0d1fe2201bc7bffae3d89493f4ae7612 1 parent 6047c6b
@rafaelfranca rafaelfranca authored
View
6 activerecord/CHANGELOG.md
@@ -1,3 +1,9 @@
+* Do not quote uuid default value on `change_column`.
+
+ Fixes #14604.
+
+ *Eric Chahin*
+
* The comparison between `Relation` and `CollectionProxy` should be consistent.
Example:
View
9 activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb
@@ -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
View
10 activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
@@ -190,6 +190,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]
@@ -406,13 +410,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
View
13 activerecord/test/cases/adapters/postgresql/uuid_test.rb
@@ -60,6 +60,19 @@ def test_schema_dumper_for_uuid_primary_key
assert_match(/\bcreate_table "pg_uuids", id: :uuid, default: "uuid_generate_v1\(\)"/, schema.string)
assert_match(/t\.uuid "other_uuid", default: "uuid_generate_v4\(\)"/, schema.string)
end
+
+ def test_change_column_default
+ @connection.add_column :pg_uuids, :thingy, :uuid, null: false, default: "uuid_generate_v1()"
+ UUID.reset_column_information
+ column = UUID.columns.find { |c| c.name == 'thingy' }
+ assert_equal "uuid_generate_v1()", column.default_function
+
+ @connection.change_column :pg_uuids, :thingy, :uuid, null: false, default: "uuid_generate_v4()"
+
+ UUID.reset_column_information
+ column = UUID.columns.find { |c| c.name == 'thingy' }
+ assert_equal "uuid_generate_v4()", column.default_function
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.