diff options
author | Yves Senn <yves.senn@gmail.com> | 2014-06-03 17:54:25 +0200 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2014-06-03 17:54:25 +0200 |
commit | 9e86428d23074f4b5edb5a40bb6e72d9ecacfee4 (patch) | |
tree | 4826d2cb6fd4e5675b6fdc26edb158bfe0fbce4f | |
parent | 0690250e7bc1a989f87b3e32bcb0e5655ea526db (diff) | |
download | rails-9e86428d23074f4b5edb5a40bb6e72d9ecacfee4.tar.gz rails-9e86428d23074f4b5edb5a40bb6e72d9ecacfee4.tar.bz2 rails-9e86428d23074f4b5edb5a40bb6e72d9ecacfee4.zip |
pg, preserve money type when dumping schema and extract money default.
7 files changed, 39 insertions, 22 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index c471879885..02fa504c78 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,8 +1,5 @@ -* Preserve type when dumping PostgreSQL bit and bit varying columns. - - *Yves Senn* - -* Preserve type when dumping PostgreSQL point columns. +* Preserve type when dumping PostgreSQL point, bit, bit varying and money + columns. *Yves Senn* diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/money.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/money.rb index 697dceb7c2..d25eb256c2 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/money.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/money.rb @@ -7,6 +7,10 @@ module ActiveRecord class_attribute :precision + def type + :money + end + def scale 2 end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb index cb8c0b6f93..0867e5ef54 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb @@ -79,6 +79,10 @@ module ActiveRecord def bit_varying(name, options) column(name, :bit_varying, options) end + + def money(name, options) + column(name, :money, options) + end end class ColumnDefinition < ActiveRecord::ConnectionAdapters::ColumnDefinition diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index b80d722916..67570dad3c 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -107,6 +107,7 @@ module ActiveRecord point: { name: "point" }, bit: { name: "bit" }, bit_varying: { name: "bit varying" }, + money: { name: "money" }, } OID = PostgreSQL::OID #:nodoc: @@ -560,6 +561,8 @@ module ActiveRecord # JSON when /\A'(.*)'::json\z/ $1 + when /\A'(.*)'::money\z/ + $1 # Object identifier types when /\A-?\d+\z/ $1 diff --git a/activerecord/test/cases/adapters/postgresql/bit_string_test.rb b/activerecord/test/cases/adapters/postgresql/bit_string_test.rb index d63750a3ea..003573fda6 100644 --- a/activerecord/test/cases/adapters/postgresql/bit_string_test.rb +++ b/activerecord/test/cases/adapters/postgresql/bit_string_test.rb @@ -11,11 +11,9 @@ class PostgresqlBitStringTest < ActiveRecord::TestCase def setup @connection = ActiveRecord::Base.connection - @connection.transaction do - @connection.create_table('postgresql_bit_strings') do |t| - t.bit :a_bit, default: "00000011", limit: 8 - t.bit_varying :a_bit_varying, default: "0011", limit: 4 - end + @connection.create_table('postgresql_bit_strings') do |t| + t.bit :a_bit, default: "00000011", limit: 8 + t.bit_varying :a_bit_varying, default: "0011", limit: 4 end end diff --git a/activerecord/test/cases/adapters/postgresql/money_test.rb b/activerecord/test/cases/adapters/postgresql/money_test.rb index e109f1682b..a3a2a5ff23 100644 --- a/activerecord/test/cases/adapters/postgresql/money_test.rb +++ b/activerecord/test/cases/adapters/postgresql/money_test.rb @@ -1,20 +1,28 @@ # encoding: utf-8 - require "cases/helper" -require 'active_record/base' -require 'active_record/connection_adapters/postgresql_adapter' +require 'support/schema_dumping_helper' class PostgresqlMoneyTest < ActiveRecord::TestCase + include SchemaDumpingHelper + class PostgresqlMoney < ActiveRecord::Base; end setup do @connection = ActiveRecord::Base.connection @connection.execute("set lc_monetary = 'C'") + @connection.create_table('postgresql_moneys') do |t| + t.column "wealth", "money" + t.column "depth", "money", default: "150.55" + end + end + + teardown do + @connection.execute 'DROP TABLE IF EXISTS postgresql_moneys' end def test_column column = PostgresqlMoney.columns_hash["wealth"] - assert_equal :decimal, column.type + assert_equal :money, column.type assert_equal "money", column.sql_type assert_equal 2, column.scale assert column.number? @@ -23,6 +31,10 @@ class PostgresqlMoneyTest < ActiveRecord::TestCase assert_not column.array end + def test_default + assert_equal BigDecimal.new("150.55"), PostgresqlMoney.new.depth + end + def test_money_values @connection.execute("INSERT INTO postgresql_moneys (id, wealth) VALUES (1, '567.89'::money)") @connection.execute("INSERT INTO postgresql_moneys (id, wealth) VALUES (2, '-567.89'::money)") @@ -41,6 +53,12 @@ class PostgresqlMoneyTest < ActiveRecord::TestCase assert_equal(-2.25, column.type_cast("($2.25)")) end + def test_schema_dumping + output = dump_table_schema("postgresql_moneys") + assert_match %r{t\.money\s+"wealth",\s+scale: 2$}, output + assert_match %r{t\.money\s+"depth",\s+scale: 2,\s+default: 150.55$}, output + end + def test_create_and_update_money money = PostgresqlMoney.create(wealth: "987.65") assert_equal 987.65, money.wealth diff --git a/activerecord/test/schema/postgresql_specific_schema.rb b/activerecord/test/schema/postgresql_specific_schema.rb index d4da3b3bc1..e9294a11b9 100644 --- a/activerecord/test/schema/postgresql_specific_schema.rb +++ b/activerecord/test/schema/postgresql_specific_schema.rb @@ -1,6 +1,6 @@ ActiveRecord::Schema.define do - %w(postgresql_tsvectors postgresql_hstores postgresql_arrays postgresql_moneys postgresql_numbers postgresql_times postgresql_network_addresses postgresql_bit_strings postgresql_uuids postgresql_ltrees + %w(postgresql_tsvectors postgresql_hstores postgresql_arrays postgresql_moneys postgresql_numbers postgresql_times postgresql_network_addresses postgresql_uuids postgresql_ltrees postgresql_oids postgresql_xml_data_type defaults geometrics postgresql_timestamp_with_zones postgresql_partitioned_table postgresql_partitioned_table_parent postgresql_json_data_type postgresql_citext).each do |table_name| execute "DROP TABLE IF EXISTS #{quote_table_name table_name}" end @@ -118,13 +118,6 @@ _SQL end execute <<_SQL - CREATE TABLE postgresql_moneys ( - id SERIAL PRIMARY KEY, - wealth MONEY - ); -_SQL - - execute <<_SQL CREATE TABLE postgresql_numbers ( id SERIAL PRIMARY KEY, single REAL, |