Skip to content

Commit

Permalink
Adds migration and schema dump support for INET, CIDR, and MACADDR
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Seaver committed May 5, 2012
1 parent acb3984 commit f7b8fbd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
Expand Up @@ -197,6 +197,13 @@ def simplified_type(field_type)
:decimal
when 'hstore'
:hstore
# Network address types
when 'inet'
:inet
when 'cidr'
:cidr
when 'macaddr'
:macaddr
# Character types
when /^(?:character varying|bpchar)(?:\(\d+\))?$/
:string
Expand All @@ -211,9 +218,6 @@ def simplified_type(field_type)
# Geometric types
when /^(?:point|line|lseg|box|"?path"?|polygon|circle)$/
:string
# Network address types
when /^(?:cidr|inet|macaddr)$/
:string
# Bit strings
when /^bit(?: varying)?(?:\(\d+\))?$/
:string
Expand Down Expand Up @@ -282,6 +286,18 @@ def tsvector(*args)
def hstore(name, options = {})
column(name, 'hstore', options)
end

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

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

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

ADAPTER_NAME = 'PostgreSQL'
Expand All @@ -301,7 +317,10 @@ def hstore(name, options = {})
:boolean => { :name => "boolean" },
:xml => { :name => "xml" },
:tsvector => { :name => "tsvector" },
:hstore => { :name => "hstore" }
:hstore => { :name => "hstore" },
:inet => { :name => "inet" },
:cidr => { :name => "cidr" },
:macaddr => { :name => "macaddr" }
}

# Returns 'PostgreSQL' as adapter name for identification purposes.
Expand Down
6 changes: 3 additions & 3 deletions activerecord/test/cases/adapters/postgresql/datatype_test.rb
Expand Up @@ -86,9 +86,9 @@ def test_data_type_of_time_types
end

def test_data_type_of_network_address_types
assert_equal :string, @first_network_address.column_for_attribute(:cidr_address).type
assert_equal :string, @first_network_address.column_for_attribute(:inet_address).type
assert_equal :string, @first_network_address.column_for_attribute(:mac_address).type
assert_equal :cidr, @first_network_address.column_for_attribute(:cidr_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
end

def test_data_type_of_bit_string_types
Expand Down
21 changes: 21 additions & 0 deletions activerecord/test/cases/schema_dumper_test.rb
Expand Up @@ -236,6 +236,27 @@ def test_schema_dump_includes_xml_shorthand_definition
end
end

def test_schema_dump_includes_inet_shorthand_definition
output = standard_dump
if %r{create_table "postgresql_network_address"} =~ output
assert_match %r{t.inet "inet_address"}, output
end
end

def test_schema_dump_includes_cidr_shorthand_definition
output = standard_dump
if %r{create_table "postgresql_network_address"} =~ output
assert_match %r{t.cidr "cidr_address"}, output
end
end

def test_schema_dump_includes_macaddr_shorthand_definition
output = standard_dump
if %r{create_table "postgresql_network_address"} =~ output
assert_match %r{t.macaddr "macaddr_address"}, output
end
end

def test_schema_dump_includes_hstores_shorthand_definition
output = standard_dump
if %r{create_table "postgresql_hstores"} =~ output
Expand Down

0 comments on commit f7b8fbd

Please sign in to comment.