Skip to content

Commit

Permalink
Migration dump UUID default functions to schema.rb. Fixes #10751.
Browse files Browse the repository at this point in the history
  • Loading branch information
kennyj committed Jul 9, 2013
1 parent 51e1658 commit 4734e4e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
5 changes: 5 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,8 @@
* Migration dump UUID default functions to schema.rb.
Fixes #10751.

*kennyj*

* Remove extra select and update queries on save/touch/destroy ActiveRecord model * Remove extra select and update queries on save/touch/destroy ActiveRecord model
with belongs to reflection with option `touch: true`. with belongs to reflection with option `touch: true`.


Expand Down
Expand Up @@ -45,16 +45,18 @@ def postgresql_connection(config)
module ConnectionAdapters module ConnectionAdapters
# PostgreSQL-specific extensions to column definitions in a table. # PostgreSQL-specific extensions to column definitions in a table.
class PostgreSQLColumn < Column #:nodoc: class PostgreSQLColumn < Column #:nodoc:
attr_accessor :array attr_accessor :array, :default_function
# Instantiates a new PostgreSQL column definition in a table. # Instantiates a new PostgreSQL column definition in a table.
def initialize(name, default, oid_type, sql_type = nil, null = true) def initialize(name, default, oid_type, sql_type = nil, null = true)
@oid_type = oid_type @oid_type = oid_type
default_value = self.class.extract_value_from_default(default)
@default_function = default if !default_value && default && default =~ /.+\(.*\)/
if sql_type =~ /\[\]$/ if sql_type =~ /\[\]$/
@array = true @array = true
super(name, self.class.extract_value_from_default(default), sql_type[0..sql_type.length - 3], null) super(name, default_value, sql_type[0..sql_type.length - 3], null)
else else
@array = false @array = false
super(name, self.class.extract_value_from_default(default), sql_type, null) super(name, default_value, sql_type, null)
end end
end end


Expand Down Expand Up @@ -435,6 +437,7 @@ def adapter_name
def prepare_column_options(column, types) def prepare_column_options(column, types)
spec = super spec = super
spec[:array] = 'true' if column.respond_to?(:array) && column.array spec[:array] = 'true' if column.respond_to?(:array) && column.array
spec[:default] = "\"#{column.default_function}\"" if column.respond_to?(:default_function) && column.default_function
spec spec
end end


Expand Down
1 change: 1 addition & 0 deletions activerecord/test/cases/adapters/postgresql/uuid_test.rb
Expand Up @@ -61,6 +61,7 @@ def test_schema_dumper_for_uuid_primary_key
schema = StringIO.new schema = StringIO.new
ActiveRecord::SchemaDumper.dump(@connection, schema) ActiveRecord::SchemaDumper.dump(@connection, schema)
assert_match(/\bcreate_table "pg_uuids", id: :uuid\b/, schema.string) assert_match(/\bcreate_table "pg_uuids", id: :uuid\b/, schema.string)
assert_match(/t\.uuid "other_uuid", default: "uuid_generate_v4\(\)"/, schema.string)
end end
end end


Expand Down

0 comments on commit 4734e4e

Please sign in to comment.