Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Support for PostgreSQL citext data type. #12523

Merged
merged 1 commit into from

9 participants

lsylvester Rafael Mendonça França Carlos Antonio da Silva Robin Dupret Olek Janiszewski David Somers Yves Senn Paul Nikitochkin Aaron Patterson
lsylvester

citext makes it possible to use AR Hash finders for case-insensitive
matching as sql UPPER/LOWER functions are not needed.

This is an updated version of #9182 and #10231.

The schema.rb dumps now include citext columns and unknown OID warning that appeared in the test has been fixed by reconnecting to the database after the extension was enabled.

Paul Nikitochkin pftg commented on the diff
activerecord/test/schema/postgresql_specific_schema.rb
@@ -111,6 +111,15 @@
_SQL
end
+ if 't' == select_value("select 'citext'=ANY(select typname from pg_type)")
+ execute <<_SQL
Paul Nikitochkin
pftg added a note

May you indent this code.

None of the other create table statements in this file are indented. I think that it would be best to indent them all in a separate pull request if having them indented is preferred.

Paul Nikitochkin
pftg added a note

That PR will be rejected, as cosmetic changes. Better way to use code style for current changes.

Rafael Mendonça França Owner

It is fine in the way it is. I already tried to change the style on this file and I don't think it is worth

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Rafael Mendonça França

This pull request needs a rebase

Rafael Mendonça França

Could you add a test for the schema.rb dump. I didn't saw this being tested

lsylvester

Rebased, and test for schema.rb dump has been added

Carlos Antonio da Silva

@rafaelfranca do you have anything else to add here?

Robin Dupret
Collaborator

@carlosantoniodasilva @rafaelfranca : What do you think about improving the documentation and adding a note about this feature in the "Ruby on Rails 4.1 Release Notes" guide (if this will be in 4.1) ?

Olek Janiszewski

I'd also be interested in getting this merged. Anything I can do to help?

David Somers

:+1: I'm sad this got missed from 4.1; I'm getting bored of the citext errors in my console when I use it manually.

...erecord/test/cases/adapters/postgresql/citext_test.rb
((59 lines not shown))
+ def test_column
+ assert_equal :citext, @column.type
+ end
+
+ def test_write
+ x = Citext.new(cival: 'Some CI Text')
+ assert x.save!
+ end
+
+ def test_select_case_insensitive
+ @connection.execute "insert into citexts (cival) values('Cased Text')"
+ x = Citext.where(cival: 'cased text').first
+ assert_equal('Cased Text', x.cival)
+ end
+end
+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
activerecord/CHANGELOG.md
@@ -1,3 +1,8 @@
+* Support for Postgres citext data type enabling case-insensitive where
+ values without needing to wrap in UPPER/LOWER sql functions

PostgreSQL. citext with wrapped backticks for highlight. And please end with ..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Carlos Antonio da Silva

Can you rebase this from master? Thanks.

@rafaelfranca mind taking a look here? :heart:

...b/active_record/connection_adapters/postgresql/oid.rb
@@ -371,6 +371,7 @@ def self.registered_type?(name)
register_type 'circle', OID::Identity.new
register_type 'hstore', OID::Hstore.new
register_type 'json', OID::Json.new
+ register_type 'citext', OID::Identity.new
Yves Senn Owner
senny added a note

we should map it to OID::Text.new

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Yves Senn
Owner

@lsylvester can you push a rebased and squashed version of this PR? You can use brackets in the commit message to give credits to the authors:

git commit -m "register OID for PostgreSQL citex datatype [Author A & Author B]"
lsylvester

@senny I have rebased

Yves Senn
Owner

@lsylvester can we include citex_test.rb as the two referenced PR's do?

lsylvester lsylvester register OID for PostgreSQL citex datatype [Troy Kruthoff & Lachlan S…
…ylvester]

citext makes it possible to use AR Hash finders for case-insensitive matching as sql UPPER/LOWER functions are not needed.
b3e0da3
lsylvester

@senny I have added a citext_test.rb.

Yves Senn senny removed the openacademy label
Yves Senn senny merged commit 0e0b41d into from
Yves Senn
Owner

@lsylvester thank you for your patience :yellow_heart:

Aaron Patterson tenderlove referenced this pull request from a commit
Aaron Patterson tenderlove Merge branch 'master' into adequaterecord
* master: (108 commits)
  make tests pass on Ruby 2.2
  Use Sqlite3 adapter in examples
  use the body proxy to freeze headers
  just ask the response for the commit status, we do not need to ask the jar
  only write the jar if the response isn't committed
  Fix a grammatical error in the i18n guide [ci skip]
  use method_defined? to check whether or not a method is defined
  Enhance docs for update_attribute [ci-skip]
  Change usec to 0 on tests that compare seconds
  Unit test for mysql quote time usec
  Changelog entry for mysql56 microseconds
  Test microsecond on mysql 5.6
  MySQL 5.6 and later supports microsecond precision in datetime.
  [ci skip] Add documentation for original_fullpath.
  Remove mocking on save, when not necessary
  comment why we are modifying global state. [ci skip]
  `change_table` supports `citext`. Follow up to #12523.
  Removed unnecessary command "application"
  register OID for PostgreSQL citex datatype [Troy Kruthoff & Lachlan Sylvester]
  Fixes STI when 2+ levels deep.
  ...
66e0321
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 10, 2014
  1. lsylvester

    register OID for PostgreSQL citex datatype [Troy Kruthoff & Lachlan S…

    lsylvester authored
    …ylvester]
    
    citext makes it possible to use AR Hash finders for case-insensitive matching as sql UPPER/LOWER functions are not needed.
This page is out of date. Refresh to see the latest.
5 activerecord/CHANGELOG.md
View
@@ -1,3 +1,8 @@
+* Support for Postgres `citext` data type enabling case-insensitive where
+ values without needing to wrap in UPPER/LOWER sql functions.
+
+ *Troy Kruthoff*, *Lachlan Sylvester*
+
* Only save has_one associations if record has changes.
Previously after save related callbacks, such as `#after_commit`, were triggered when the has_one
object did not get saved to the db.
1  activerecord/lib/active_record/connection_adapters/postgresql/oid.rb
View
@@ -375,6 +375,7 @@ def self.registered_type?(name)
register_type 'circle', OID::Text.new
register_type 'hstore', OID::Hstore.new
register_type 'json', OID::Json.new
+ register_type 'citext', OID::Text.new
register_type 'ltree', OID::Text.new
register_type 'cidr', OID::Cidr.new
9 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
View
@@ -215,6 +215,8 @@ def simplified_type(field_type)
# Character types
when /^(?:character varying|bpchar)(?:\(\d+\))?$/
:string
+ when /^citext(?:\(\d+\))?$/
+ :citext
# Binary data types
when 'bytea'
:binary
@@ -393,6 +395,10 @@ def primary_key(name, type = :primary_key, options = {})
column name, type, options
end
+ def citext(name, options = {})
+ column(name, 'citext', options)
+ end
+
def column(name, type = nil, options = {})
super
column = self[name]
@@ -441,7 +447,8 @@ class Table < ActiveRecord::ConnectionAdapters::Table
macaddr: { name: "macaddr" },
uuid: { name: "uuid" },
json: { name: "json" },
- ltree: { name: "ltree" }
+ ltree: { name: "ltree" },
+ citext: { name: "citext" }
}
include Quoting
58 activerecord/test/cases/adapters/postgresql/citext_test.rb
View
@@ -0,0 +1,58 @@
+# encoding: utf-8
+
+require 'cases/helper'
+require 'active_record/base'
+require 'active_record/connection_adapters/postgresql_adapter'
+
+class PostgresqlCitextTest < ActiveRecord::TestCase
+ class Citext < ActiveRecord::Base
+ self.table_name = 'citexts'
+ end
+
+ def setup
+ @connection = ActiveRecord::Base.connection
+
+ unless @connection.extension_enabled?('citext')
+ @connection.enable_extension 'citext'
+ @connection.commit_db_transaction
+ end
+
+ @connection.reconnect!
+
+ @connection.transaction do
+ @connection.create_table('citexts') do |t|
+ t.citext 'cival'
+ end
+ end
+ @column = Citext.columns_hash['cival']
+ end
+
+ def teardown
+ @connection.execute 'DROP TABLE IF EXISTS citexts;'
+ @connection.execute 'DROP EXTENSION IF EXISTS citext CASCADE;'
+ end
+
+ def test_citext_enabled
+ assert @connection.extension_enabled?('citext')
+ end
+
+ def test_column_type
+ assert_equal :citext, @column.type
+ end
+
+ def test_column_sql_type
+ assert_equal 'citext', @column.sql_type
+ end
+
+ def test_write
+ x = Citext.new(cival: 'Some CI Text')
+ assert x.save!
+ end
+
+ def test_select_case_insensitive
+ @connection.execute "insert into citexts (cival) values('Cased Text')"
+ x = Citext.where(cival: 'cased text').first
+ assert_equal('Cased Text', x.cival)
+ end
+
+end
7 activerecord/test/cases/schema_dumper_test.rb
View
@@ -319,6 +319,13 @@ def test_schema_dump_includes_hstores_shorthand_definition
end
end
+ def test_schema_dump_includes_citext_shorthand_definition
+ output = standard_dump
+ if %r{create_table "postgresql_citext"} =~ output
+ assert_match %r[t.citext "text_citext"], output
+ end
+ end
+
def test_schema_dump_includes_ltrees_shorthand_definition
output = standard_dump
if %r{create_table "postgresql_ltrees"} =~ output
11 activerecord/test/schema/postgresql_specific_schema.rb
View
@@ -1,7 +1,7 @@
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 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).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_citext).each do |table_name|
execute "DROP TABLE IF EXISTS #{quote_table_name table_name}"
end
@@ -99,6 +99,15 @@
_SQL
end
+ if 't' == select_value("select 'citext'=ANY(select typname from pg_type)")
+ execute <<_SQL
Paul Nikitochkin
pftg added a note

May you indent this code.

None of the other create table statements in this file are indented. I think that it would be best to indent them all in a separate pull request if having them indented is preferred.

Paul Nikitochkin
pftg added a note

That PR will be rejected, as cosmetic changes. Better way to use code style for current changes.

Rafael Mendonça França Owner

It is fine in the way it is. I already tried to change the style on this file and I don't think it is worth

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ CREATE TABLE postgresql_citext (
+ id SERIAL PRIMARY KEY,
+ text_citext citext default ''::citext
+ );
+_SQL
+ end
+
if 't' == select_value("select 'json'=ANY(select typname from pg_type)")
execute <<_SQL
CREATE TABLE postgresql_json_data_type (
Something went wrong with that request. Please try again.