diff options
author | Yves Senn <yves.senn@gmail.com> | 2014-06-03 16:17:59 +0200 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2014-06-03 16:17:59 +0200 |
commit | fcd5e56b862ffea9a8c1cea0c622472c5346474b (patch) | |
tree | 8fad1200ad20cc9d913163eafeb261d18c4afc41 /activerecord | |
parent | 51f247107f63b8c6c3a2ba689d26b3ed51237ab2 (diff) | |
download | rails-fcd5e56b862ffea9a8c1cea0c622472c5346474b.tar.gz rails-fcd5e56b862ffea9a8c1cea0c622472c5346474b.tar.bz2 rails-fcd5e56b862ffea9a8c1cea0c622472c5346474b.zip |
pg, preserve type when schema dumping bit and bit varying columns.
Diffstat (limited to 'activerecord')
7 files changed, 44 insertions, 12 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index d9065c7afc..c471879885 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,4 +1,8 @@ -* Preserve type when dumping PostgreSQL point types. +* Preserve type when dumping PostgreSQL bit and bit varying columns. + + *Yves Senn* + +* Preserve type when dumping PostgreSQL point columns. *Yves Senn* diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb index 2494e19f84..33a98b4fcb 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb @@ -2,6 +2,7 @@ require 'active_record/connection_adapters/postgresql/oid/infinity' require 'active_record/connection_adapters/postgresql/oid/array' require 'active_record/connection_adapters/postgresql/oid/bit' +require 'active_record/connection_adapters/postgresql/oid/bit_varying' require 'active_record/connection_adapters/postgresql/oid/bytea' require 'active_record/connection_adapters/postgresql/oid/cidr' require 'active_record/connection_adapters/postgresql/oid/date' diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/bit.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/bit.rb index dc077993c5..3073f8ff30 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/bit.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/bit.rb @@ -2,7 +2,11 @@ module ActiveRecord module ConnectionAdapters module PostgreSQL module OID # :nodoc: - class Bit < Type::String + class Bit < Type::Value + def type + :bit + end + def type_cast(value) if ::String === value case value diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb new file mode 100644 index 0000000000..054af285bb --- /dev/null +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb @@ -0,0 +1,13 @@ +module ActiveRecord + module ConnectionAdapters + module PostgreSQL + module OID # :nodoc: + class BitVarying < OID::Bit + def type + :bit_varying + end + end + end + end + end +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 deaea12408..ca2966466c 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb @@ -71,6 +71,14 @@ module ActiveRecord def point(name, options = {}) column(name, 'point', options) end + + def bit(name, options) + column(name, 'bit', options) + end + + def bit_varying(name, options) + column(name, 'bit varying', 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 61694674ab..c900d61390 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -104,7 +104,9 @@ module ActiveRecord json: { name: "json" }, ltree: { name: "ltree" }, citext: { name: "citext" }, - point: { name: "point"} + point: { name: "point"}, + bit: { name: "bit"}, + bit_varying: { name: "bit varying" } } OID = PostgreSQL::OID #:nodoc: @@ -434,7 +436,7 @@ module ActiveRecord m.alias_type 'bpchar', 'varchar' m.register_type 'bool', Type::Boolean.new m.register_type 'bit', OID::Bit.new - m.alias_type 'varbit', 'bit' + m.register_type 'varbit', OID::BitVarying.new m.alias_type 'timestamptz', 'timestamp' m.register_type 'date', OID::Date.new m.register_type 'time', OID::Time.new diff --git a/activerecord/test/cases/adapters/postgresql/bit_string_test.rb b/activerecord/test/cases/adapters/postgresql/bit_string_test.rb index 2ca97eabab..8e88acf4db 100644 --- a/activerecord/test/cases/adapters/postgresql/bit_string_test.rb +++ b/activerecord/test/cases/adapters/postgresql/bit_string_test.rb @@ -13,8 +13,8 @@ class PostgresqlBitStringTest < ActiveRecord::TestCase @connection = ActiveRecord::Base.connection @connection.transaction do @connection.create_table('postgresql_bit_strings') do |t| - t.column :a_bit, "bit(8)", default: "00000011" - t.column :a_bit_varying, "bit varying", default: "0011" + t.bit :a_bit, default: "00000011", limit: 8 + t.bit_varying :a_bit_varying, default: "0011" end end end @@ -25,9 +25,9 @@ class PostgresqlBitStringTest < ActiveRecord::TestCase def test_bit_string_column column = PostgresqlBitString.columns_hash["a_bit"] - assert_equal :string, column.type + assert_equal :bit, column.type assert_equal "bit(8)", column.sql_type - assert column.text? + assert_not column.text? assert_not column.number? assert_not column.binary? assert_not column.array @@ -35,9 +35,9 @@ class PostgresqlBitStringTest < ActiveRecord::TestCase def test_bit_string_varying_column column = PostgresqlBitString.columns_hash["a_bit_varying"] - assert_equal :string, column.type + assert_equal :bit_varying, column.type assert_equal "bit varying", column.sql_type - assert column.text? + assert_not column.text? assert_not column.number? assert_not column.binary? assert_not column.array @@ -53,8 +53,8 @@ class PostgresqlBitStringTest < ActiveRecord::TestCase def test_schema_dumping output = dump_table_schema("postgresql_bit_strings") - assert_match %r{t\.string\s+"a_bit",\s+default: "00000011"$}, output - assert_match %r{t\.string\s+"a_bit_varying",\s+default: "0011"$}, output + assert_match %r{t\.bit\s+"a_bit",\s+default: "00000011"$}, output + assert_match %r{t\.bit_varying\s+"a_bit_varying",\s+default: "0011"$}, output end def test_assigning_invalid_hex_string_raises_exception |