From 51e165808b8fdb0323d50aa7de973303224b7c7b Mon Sep 17 00:00:00 2001 From: kennyj Date: Wed, 10 Jul 2013 00:56:55 +0900 Subject: [PATCH 1/2] Fix typo. --- activerecord/test/cases/schema_dumper_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index a48ae1036f2..32f86f9c886 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -299,7 +299,7 @@ class SchemaDumperTest < ActiveRecord::TestCase def test_schema_dump_includes_uuid_shorthand_definition output = standard_dump - if %r{create_table "poistgresql_uuids"} =~ output + if %r{create_table "postgresql_uuids"} =~ output assert_match %r{t.uuid "guid"}, output end end From 4734e4ed3181bf8c8dca959078b7e6295e702719 Mon Sep 17 00:00:00 2001 From: kennyj Date: Wed, 10 Jul 2013 02:14:38 +0900 Subject: [PATCH 2/2] Migration dump UUID default functions to schema.rb. Fixes #10751. --- activerecord/CHANGELOG.md | 5 +++++ .../connection_adapters/postgresql_adapter.rb | 9 ++++++--- activerecord/test/cases/adapters/postgresql/uuid_test.rb | 1 + 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 1eb2f2d130a..13c7bb85d01 100644 --- a/activerecord/CHANGELOG.md +++ b/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 with belongs to reflection with option `touch: true`. diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 98126249df6..5a9d93ef574 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -45,16 +45,18 @@ module ActiveRecord module ConnectionAdapters # PostgreSQL-specific extensions to column definitions in a table. class PostgreSQLColumn < Column #:nodoc: - attr_accessor :array + attr_accessor :array, :default_function # Instantiates a new PostgreSQL column definition in a table. def initialize(name, default, oid_type, sql_type = nil, null = true) @oid_type = oid_type + default_value = self.class.extract_value_from_default(default) + @default_function = default if !default_value && default && default =~ /.+\(.*\)/ if sql_type =~ /\[\]$/ @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 @array = false - super(name, self.class.extract_value_from_default(default), sql_type, null) + super(name, default_value, sql_type, null) end end @@ -435,6 +437,7 @@ module ActiveRecord def prepare_column_options(column, types) spec = super 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 end diff --git a/activerecord/test/cases/adapters/postgresql/uuid_test.rb b/activerecord/test/cases/adapters/postgresql/uuid_test.rb index b573d488078..20d7254b82d 100644 --- a/activerecord/test/cases/adapters/postgresql/uuid_test.rb +++ b/activerecord/test/cases/adapters/postgresql/uuid_test.rb @@ -61,6 +61,7 @@ class PostgresqlUUIDTest < ActiveRecord::TestCase schema = StringIO.new ActiveRecord::SchemaDumper.dump(@connection, schema) 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