diff options
author | Yves Senn <yves.senn@gmail.com> | 2014-04-01 19:42:22 +0200 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2014-04-01 19:52:34 +0200 |
commit | f7a6b115fea9f675190a79b701c7034214678f19 (patch) | |
tree | e185ba5ab64b915a6d5c630fe35fd40e681ffe24 /activerecord/test | |
parent | fc0b98da0b83329474922a920a6070d370091228 (diff) | |
download | rails-f7a6b115fea9f675190a79b701c7034214678f19.tar.gz rails-f7a6b115fea9f675190a79b701c7034214678f19.tar.bz2 rails-f7a6b115fea9f675190a79b701c7034214678f19.zip |
PostgreSQL, register custom domains. Closes #14305.
This patch registers custom domains in our OID-type_map.
They will behave exactly as the type specified by `pg_type.typbasetype`.
/cc @matthewd
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/adapters/postgresql/domain_test.rb | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/activerecord/test/cases/adapters/postgresql/domain_test.rb b/activerecord/test/cases/adapters/postgresql/domain_test.rb new file mode 100644 index 0000000000..fdf5ce974d --- /dev/null +++ b/activerecord/test/cases/adapters/postgresql/domain_test.rb @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +require "cases/helper" +require 'active_record/base' +require 'active_record/connection_adapters/postgresql_adapter' + +class PostgresqlDomainTest < ActiveRecord::TestCase + class PostgresqlDomain < ActiveRecord::Base + self.table_name = "postgresql_domains" + end + + def setup + # reset connection to bust all cached statement plans + connection_spec = ActiveRecord::Base.remove_connection + ActiveRecord::Base.establish_connection(connection_spec) + + @connection = ActiveRecord::Base.connection + @connection.transaction do + @connection.execute "CREATE DOMAIN custom_money as numeric(8,2)" + @connection.create_table('postgresql_domains') do |t| + t.column :price, :custom_money + end + end + + # reload type map after creating the enum type + @connection.send(:reload_type_map) + end + + teardown do + @connection.execute 'DROP TABLE IF EXISTS postgresql_domains' + @connection.execute 'DROP DOMAIN IF EXISTS custom_money' + end + + def test_column + column = PostgresqlDomain.columns_hash["price"] + assert_equal :decimal, column.type + assert_equal "custom_money", column.sql_type + assert column.number? + assert_not column.text? + assert_not column.binary? + assert_not column.array + end + + def test_domain_acts_like_basetype + PostgresqlDomain.create price: "" + record = PostgresqlDomain.first + assert_nil record.price + + record.price = "34.15" + record.save! + + assert_equal BigDecimal.new("34.15"), record.reload.price + end +end |