Skip to content

Commit

Permalink
Support for PostgreSQL's ltree data type.
Browse files Browse the repository at this point in the history
  • Loading branch information
robworley committed Jan 4, 2013
1 parent cfb3019 commit e209107
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 3 deletions.
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,9 @@
## Rails 4.0.0 (unreleased) ## ## Rails 4.0.0 (unreleased) ##


* Support for PostgreSQL's ltree data type.

*Rob Worley*

* Fix undefined method `to_i` when calling `new` on a scope that uses an Array. * Fix undefined method `to_i` when calling `new` on a scope that uses an Array.
Fixes #8718, #8734. Fixes #8718, #8734.


Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ def self.registered_type?(name)
register_type 'circle', OID::Identity.new register_type 'circle', OID::Identity.new
register_type 'hstore', OID::Hstore.new register_type 'hstore', OID::Hstore.new
register_type 'json', OID::Json.new register_type 'json', OID::Json.new
register_type 'ltree', OID::Identity.new


register_type 'int4range', OID::IntRange.new register_type 'int4range', OID::IntRange.new
alias_type 'int8range', 'int4range' alias_type 'int8range', 'int4range'
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ def simplified_type(field_type)
:decimal :decimal
when 'hstore' when 'hstore'
:hstore :hstore
when 'ltree'
:ltree
# Network address types # Network address types
when 'inet' when 'inet'
:inet :inet
Expand Down Expand Up @@ -275,6 +277,10 @@ def hstore(name, options = {})
column(name, 'hstore', options) column(name, 'hstore', options)
end end


def ltree(name, options = {})
column(name, 'ltree', options)
end

def inet(name, options = {}) def inet(name, options = {})
column(name, 'inet', options) column(name, 'inet', options)
end end
Expand Down Expand Up @@ -340,7 +346,8 @@ def new_column_definition(base, name, type)
macaddr: { name: "macaddr" }, macaddr: { name: "macaddr" },
uuid: { name: "uuid" }, uuid: { name: "uuid" },
json: { name: "json" }, json: { name: "json" },
intrange: { name: "int4range" } intrange: { name: "int4range" },
ltree: { name: "ltree" }
} }


include Quoting include Quoting
Expand Down
4 changes: 3 additions & 1 deletion activerecord/test/cases/adapters/postgresql/datatype_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class PostgresqlTimestampWithZone < ActiveRecord::Base
class PostgresqlUUID < ActiveRecord::Base class PostgresqlUUID < ActiveRecord::Base
end end


class PostgresqlLtree < ActiveRecord::Base
end

class PostgresqlDataTypeTest < ActiveRecord::TestCase class PostgresqlDataTypeTest < ActiveRecord::TestCase
self.use_transactional_fixtures = false self.use_transactional_fixtures = false


Expand Down Expand Up @@ -97,7 +100,6 @@ def test_data_type_of_network_address_types
assert_equal :inet, @first_network_address.column_for_attribute(:inet_address).type assert_equal :inet, @first_network_address.column_for_attribute(:inet_address).type
assert_equal :macaddr, @first_network_address.column_for_attribute(:mac_address).type assert_equal :macaddr, @first_network_address.column_for_attribute(:mac_address).type
end end

def test_data_type_of_bit_string_types def test_data_type_of_bit_string_types
assert_equal :string, @first_bit_string.column_for_attribute(:bit_string).type assert_equal :string, @first_bit_string.column_for_attribute(:bit_string).type
assert_equal :string, @first_bit_string.column_for_attribute(:bit_string_varying).type assert_equal :string, @first_bit_string.column_for_attribute(:bit_string_varying).type
Expand Down
44 changes: 44 additions & 0 deletions activerecord/test/cases/adapters/postgresql/ltree_test.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,44 @@
# encoding: utf-8

require "cases/helper"
require 'active_record/base'
require 'active_record/connection_adapters/postgresql_adapter'

class PostgresqlLtreeTest < ActiveRecord::TestCase
class Ltree < ActiveRecord::Base
self.table_name = 'ltrees'
end

def setup
@connection = ActiveRecord::Base.connection
begin
@connection.transaction do
@connection.create_table('ltrees') do |t|
t.ltree 'path'
end
end
rescue ActiveRecord::StatementInvalid
return skip "do not test on PG without ltree"
end
@column = Ltree.columns.find { |c| c.name == 'path' }
end

def teardown
@connection.execute 'drop table if exists ltrees'
end

def test_column
assert_equal :ltree, @column.type
end

def test_write
x = Ltree.new(:path => '1.2.3.4')
assert x.save!
end

def test_select
@connection.execute "insert into ltrees (path) VALUES ('1.2.3')"
x = Ltree.first
assert_equal('1.2.3', x.path)
end
end
7 changes: 7 additions & 0 deletions activerecord/test/cases/schema_dumper_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -280,6 +280,13 @@ def test_schema_dump_includes_hstores_shorthand_definition
end end
end end


def test_schema_dump_includes_ltrees_shorthand_definition
output = standard_dump
if %r{create_table "postgresql_ltrees"} =~ output
assert_match %r[t.ltree "path"], output
end
end

def test_schema_dump_includes_arrays_shorthand_definition def test_schema_dump_includes_arrays_shorthand_definition
output = standard_dump output = standard_dump
if %r{create_table "postgresql_arrays"} =~ output if %r{create_table "postgresql_arrays"} =~ output
Expand Down
11 changes: 10 additions & 1 deletion activerecord/test/schema/postgresql_specific_schema.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,6 @@
ActiveRecord::Schema.define do 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 %w(postgresql_tsvectors postgresql_hstores postgresql_arrays postgresql_moneys postgresql_numbers postgresql_times postgresql_network_addresses postgresql_bit_strings 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_intrange_data_type).each do |table_name| postgresql_oids postgresql_xml_data_type defaults geometrics postgresql_timestamp_with_zones postgresql_partitioned_table postgresql_partitioned_table_parent postgresql_json_data_type postgresql_intrange_data_type).each do |table_name|
execute "DROP TABLE IF EXISTS #{quote_table_name table_name}" execute "DROP TABLE IF EXISTS #{quote_table_name table_name}"
end end
Expand Down Expand Up @@ -89,6 +89,15 @@
_SQL _SQL
end end


if 't' == select_value("select 'ltree'=ANY(select typname from pg_type)")
execute <<_SQL
CREATE TABLE postgresql_ltrees (
id SERIAL PRIMARY KEY,
path ltree
);
_SQL
end

if 't' == select_value("select 'json'=ANY(select typname from pg_type)") if 't' == select_value("select 'json'=ANY(select typname from pg_type)")
execute <<_SQL execute <<_SQL
CREATE TABLE postgresql_json_data_type ( CREATE TABLE postgresql_json_data_type (
Expand Down

0 comments on commit e209107

Please sign in to comment.