aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2014-07-24 14:27:41 +0200
committerYves Senn <yves.senn@gmail.com>2014-07-24 14:27:41 +0200
commit2a67e12fdb832a6b6e94bed1a0b05290019f5514 (patch)
tree8ff6954c1eaba775d438e50119069d0b4400c558 /activerecord
parentf3478f84130dc358ac8f563233d00fbcdcc67a30 (diff)
downloadrails-2a67e12fdb832a6b6e94bed1a0b05290019f5514.tar.gz
rails-2a67e12fdb832a6b6e94bed1a0b05290019f5514.tar.bz2
rails-2a67e12fdb832a6b6e94bed1a0b05290019f5514.zip
pg, `change_column_default, :table, :column, nil` issues `DROP DEFAULT`.
Closes #16261. [Matthew Draper, Yves Senn] Using `DEFAULT NULL` results in the same behavior as `DROP DEFAULT`. However, PostgreSQL will cast the default to the columns type, which leaves us with a default like "default NULL::character varying". /cc @matthewd
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md7
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb10
2 files changed, 16 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 43232443e7..f71a887f32 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,10 @@
+* `change_column_default :table, :column, nil` with PostgreSQL will issue a
+ `DROP DEFAULT` instead of a `DEFAULT NULL` query.
+
+ Fixes #16261.
+
+ *Matthew Draper*, *Yves Senn*
+
* Allow to specify a type for the foreign key column in `references`
and `add_reference`.
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
index e09672d239..7042817672 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
@@ -423,8 +423,16 @@ module ActiveRecord
def change_column_default(table_name, column_name, default)
clear_cache!
column = column_for(table_name, column_name)
+ return unless column
- execute "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} SET DEFAULT #{quote_default_value(default, column)}" if column
+ alter_column_query = "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} %s"
+ if default.nil?
+ # <tt>DEFAULT NULL</tt> results in the same behavior as <tt>DROP DEFAULT</tt>. However, PostgreSQL will
+ # cast the default to the columns type, which leaves us with a default like "default NULL::character varying".
+ execute alter_column_query % "DROP DEFAULT"
+ else
+ execute alter_column_query % "SET DEFAULT #{quote_default_value(default, column)}"
+ end
end
def change_column_null(table_name, column_name, null, default = nil)