Add schema dumping support for PostgreSQL geometric data types

This commit is contained in:
Ryuta Kamizono 2015-11-16 06:22:47 +09:00
parent 8c9f80521b
commit 3991ebcb36
3 changed files with 77 additions and 11 deletions

@ -1,3 +1,7 @@
* Add schema dumping support for PostgreSQL geometric data types.
*Ryuta Kamizono*
* Except keys of `build_record`'s argument from `create_scope` in `initialize_attributes`.
Fixes #21893.

@ -100,6 +100,12 @@ class PostgreSQLAdapter < AbstractAdapter
ltree: { name: "ltree" },
citext: { name: "citext" },
point: { name: "point" },
line: { name: "line" },
lseg: { name: "lseg" },
box: { name: "box" },
path: { name: "path" },
polygon: { name: "polygon" },
circle: { name: "circle" },
bit: { name: "bit" },
bit_varying: { name: "bit varying" },
money: { name: "money" },
@ -455,15 +461,15 @@ def initialize_type_map(m) # :nodoc:
m.register_type 'macaddr', OID::SpecializedString.new(:macaddr)
m.register_type 'citext', OID::SpecializedString.new(:citext)
m.register_type 'ltree', OID::SpecializedString.new(:ltree)
m.register_type 'line', OID::SpecializedString.new(:line)
m.register_type 'lseg', OID::SpecializedString.new(:lseg)
m.register_type 'box', OID::SpecializedString.new(:box)
m.register_type 'path', OID::SpecializedString.new(:path)
m.register_type 'polygon', OID::SpecializedString.new(:polygon)
m.register_type 'circle', OID::SpecializedString.new(:circle)
# FIXME: why are we keeping these types as strings?
m.alias_type 'interval', 'varchar'
m.alias_type 'path', 'varchar'
m.alias_type 'line', 'varchar'
m.alias_type 'polygon', 'varchar'
m.alias_type 'circle', 'varchar'
m.alias_type 'lseg', 'varchar'
m.alias_type 'box', 'varchar'
register_class_with_precision m, 'time', Type::Time
register_class_with_precision m, 'timestamp', OID::DateTime

@ -167,16 +167,18 @@ def test_legacy_mutation
end
class PostgresqlGeometricTest < ActiveRecord::PostgreSQLTestCase
include SchemaDumpingHelper
class PostgresqlGeometric < ActiveRecord::Base; end
setup do
@connection = ActiveRecord::Base.connection
@connection.create_table("postgresql_geometrics") do |t|
t.column :a_line_segment, :lseg
t.column :a_box, :box
t.column :a_path, :path
t.column :a_polygon, :polygon
t.column :a_circle, :circle
t.lseg :a_line_segment
t.box :a_box
t.path :a_path
t.polygon :a_polygon
t.circle :a_circle
end
end
@ -233,6 +235,60 @@ def test_geometric_function
objs = PostgresqlGeometric.find_by_sql "SELECT isclosed(a_path) FROM postgresql_geometrics ORDER BY id ASC"
assert_equal [false, true], objs.map(&:isclosed)
end
def test_schema_dumping
output = dump_table_schema("postgresql_geometrics")
assert_match %r{t\.lseg\s+"a_line_segment"$}, output
assert_match %r{t\.box\s+"a_box"$}, output
assert_match %r{t\.path\s+"a_path"$}, output
assert_match %r{t\.polygon\s+"a_polygon"$}, output
assert_match %r{t\.circle\s+"a_circle"$}, output
end
end
class PostgreSQLGeometricLineTest < ActiveRecord::PostgreSQLTestCase
include SchemaDumpingHelper
class PostgresqlLine < ActiveRecord::Base; end
setup do
unless ActiveRecord::Base.connection.send(:postgresql_version) >= 90400
skip("line type is not fully implemented")
end
@connection = ActiveRecord::Base.connection
@connection.create_table("postgresql_lines") do |t|
t.line :a_line
end
end
teardown do
@connection.drop_table 'postgresql_lines', if_exists: true
end
def test_geometric_line_type
g = PostgresqlLine.new(
a_line: '{2.0, 3, 5.5}'
)
g.save!
h = PostgresqlLine.find(g.id)
assert_equal '{2,3,5.5}', h.a_line
end
def test_alternative_format_line_type
g = PostgresqlLine.new(
a_line: '(2.0, 3), (4.0, 6.0)'
)
g.save!
h = PostgresqlLine.find(g.id)
assert_equal '{1.5,-1,0}', h.a_line
end
def test_schema_dumping_for_line_type
output = dump_table_schema("postgresql_lines")
assert_match %r{t\.line\s+"a_line"$}, output
end
end
class PostgreSQLGeometricTypesTest < ActiveRecord::PostgreSQLTestCase