Skip to content

Commit ab5fcdb

Browse files
Merge pull request diesel-rs#1033 from alexcameron89/add_citext_type
Add support for Citext Postgres Type
2 parents 1cd32e9 + 2981d91 commit ab5fcdb

6 files changed

Lines changed: 32 additions & 3 deletions

File tree

diesel/src/pg/types/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ pub mod sql_types {
9595
#[doc(hidden)]
9696
pub type Bpchar = ::types::VarChar;
9797

98+
#[doc(hidden)]
99+
pub type Citext = ::types::Text;
100+
98101
#[cfg(feature = "serde_json")]
99102
/// The JSON SQL type. This type can only be used with `feature =
100103
/// "serde_json"`

diesel_infer_schema/src/pg.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ pub fn determine_column_type(attr: &ColumnInformation) -> Result<ColumnType, Box
1111
&attr.type_name
1212
};
1313

14-
let tpe_is_varchar = tpe.to_lowercase() == "varchar";
14+
let diesel_alias_without_postgres_coercion = match &*tpe.to_lowercase() {
15+
"varchar" | "citext" => Some(tpe),
16+
_ => None,
17+
};
1518

1619
// Postgres doesn't coerce varchar[] to text[] so print out a message to inform
1720
// the user.
18-
if tpe_is_varchar && is_array {
19-
writeln!(&mut stderr(), "The column `{}` is of type `varchar[]`. This will cause problems when using Diesel. You should consider changing the column type to `text[]`.", attr.column_name)?;
21+
if let (true, Some(tpe)) = (is_array, diesel_alias_without_postgres_coercion) {
22+
writeln!(&mut stderr(), "The column `{}` is of type `{}[]`. This will cause problems when using Diesel. You should consider changing the column type to `text[]`.", attr.column_name, tpe)?;
2023
}
2124

2225
Ok(ColumnType {

diesel_tests/tests/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ mod joins;
2828
mod macros;
2929
mod order;
3030
mod perf_details;
31+
#[cfg(feature = "postgres")]
32+
mod postgres_types;
3133
mod schema;
3234
mod schema_dsl;
3335
mod schema_inference;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use schema::*;
2+
use diesel::*;
3+
4+
#[test]
5+
fn ci_text_exists_and_coerces_from_text() {
6+
use schema::citext_table::dsl::*;
7+
8+
let conn = connection();
9+
conn.execute("INSERT INTO citext_table (citext_field) VALUES ('foo'::citext), ('bar'::citext)")
10+
.unwrap();
11+
let data = citext_table
12+
.filter(citext_field.eq("foo"))
13+
.select(citext_field)
14+
.load::<String>(&conn);
15+
let expected = vec!["foo".into()];
16+
17+
assert_eq!(Ok(expected), data);
18+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE citext_table;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CREATE EXTENSION IF NOT EXISTS citext;
2+
CREATE TABLE citext_table (citext_field CITEXT PRIMARY KEY);

0 commit comments

Comments
 (0)