1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Added tsvector Datatype Support

Applied Patch from https://rails.lighthouseapp.com/projects/8994/tickets/5577-suport-from-tsvector-data-type-in-postgresql
This commit is contained in:
Benjamin Fritsch 2011-04-26 09:47:40 +02:00
parent 724a786677
commit 714b4a82f4
4 changed files with 51 additions and 2 deletions

View file

@ -95,6 +95,9 @@ module ActiveRecord
# XML type
when 'xml'
:xml
# tsvector type
when 'tsvector'
:tsvector
# Arrays
when /^\D+\[\]$/
:string
@ -186,6 +189,11 @@ module ActiveRecord
options = args.extract_options!
column(args[0], 'xml', options)
end
def tsvector(*args)
options = args.extract_options!
column(args[0], 'tsvector', options)
end
end
ADAPTER_NAME = 'PostgreSQL'
@ -203,7 +211,8 @@ module ActiveRecord
:date => { :name => "date" },
:binary => { :name => "bytea" },
:boolean => { :name => "boolean" },
:xml => { :name => "xml" }
:xml => { :name => "xml" },
:tsvector => { :name => "tsvector" }
}
# Returns 'PostgreSQL' as adapter name for identification purposes.

View file

@ -3,6 +3,9 @@ require "cases/helper"
class PostgresqlArray < ActiveRecord::Base
end
class PostgresqlTsvector < ActiveRecord::Base
end
class PostgresqlMoney < ActiveRecord::Base
end
@ -34,6 +37,9 @@ class PostgresqlDataTypeTest < ActiveRecord::TestCase
@connection.execute("INSERT INTO postgresql_arrays (commission_by_quarter, nicknames) VALUES ( '{35000,21000,18000,17000}', '{foo,bar,baz}' )")
@first_array = PostgresqlArray.find(1)
@connection.execute("INSERT INTO postgresql_tsvectors (text_vector) VALUES (' ''text'' ''vector'' ')")
@first_tsvector = PostgresqlTsvector.find(1)
@connection.execute("INSERT INTO postgresql_moneys (wealth) VALUES ('567.89'::money)")
@connection.execute("INSERT INTO postgresql_moneys (wealth) VALUES ('-567.89'::money)")
@first_money = PostgresqlMoney.find(1)
@ -62,6 +68,10 @@ class PostgresqlDataTypeTest < ActiveRecord::TestCase
assert_equal :string, @first_array.column_for_attribute(:nicknames).type
end
def test_data_type_of_tsvector_types
assert_equal :tsvector, @first_tsvector.column_for_attribute(:text_vector).type
end
def test_data_type_of_money_types
assert_equal :decimal, @first_money.column_for_attribute(:wealth).type
end
@ -95,11 +105,26 @@ class PostgresqlDataTypeTest < ActiveRecord::TestCase
assert_equal '{foo,bar,baz}', @first_array.nicknames
end
def test_tsvector_values
assert_equal "'text' 'vector'", @first_tsvector.text_vector
end
def test_money_values
assert_equal 567.89, @first_money.wealth
assert_equal(-567.89, @second_money.wealth)
end
def test_update_tsvector
new_text_vector = "'new' 'text' 'vector'"
assert @first_tsvector.text_vector = new_text_vector
assert @first_tsvector.save
assert @first_tsvector.reload
assert @first_tsvector.text_vector = new_text_vector
assert @first_tsvector.save
assert @first_tsvector.reload
assert_equal @first_tsvector.text_vector, new_text_vector
end
def test_number_values
assert_equal 123.456, @first_number.single
assert_equal 123456.789, @first_number.double

View file

@ -203,6 +203,13 @@ class SchemaDumperTest < ActiveRecord::TestCase
assert_match %r{t.xml "data"}, output
end
end
def test_schema_dump_includes_tsvector_shorthand_definition
output = standard_dump
if %r{create_table "postgresql_tsvectors"} =~ output
assert_match %r{t.tsvector "text_vector"}, output
end
end
end
def test_schema_dump_keeps_large_precision_integer_columns_as_decimal

View file

@ -1,6 +1,6 @@
ActiveRecord::Schema.define do
%w(postgresql_arrays postgresql_moneys postgresql_numbers postgresql_times postgresql_network_addresses postgresql_bit_strings
%w(postgresql_tsvectors postgresql_arrays postgresql_moneys postgresql_numbers postgresql_times postgresql_network_addresses postgresql_bit_strings
postgresql_oids postgresql_xml_data_type defaults geometrics postgresql_timestamp_with_zones).each do |table_name|
execute "DROP TABLE IF EXISTS #{quote_table_name table_name}"
end
@ -55,6 +55,14 @@ _SQL
nicknames TEXT[]
);
_SQL
execute <<_SQL
CREATE TABLE postgresql_tsvectors (
id SERIAL PRIMARY KEY,
text_vector tsvector
);
_SQL
execute <<_SQL
CREATE TABLE postgresql_moneys (
id SERIAL PRIMARY KEY,