diff --git a/CHANGELOG.md b/CHANGELOG.md index 415fd8ecd9df..9ed0d4e06b71 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,9 @@ for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/ * `#[changeset_for]` can now be used with structs containing only a single field other than `id`. Fixes [#66](https://github.com/sgrif/diesel/issues/66). +* `infer_schema!` properly works with array columns. Fixes + [#65](https://github.com/sgrif/diesel/issues/65). + ## [0.3.0] 2015-12-04 ### Changed diff --git a/diesel_codegen/src/schema_inference/mod.rs b/diesel_codegen/src/schema_inference/mod.rs index d0294f4c876a..7a36a9b8bf7d 100644 --- a/diesel_codegen/src/schema_inference/mod.rs +++ b/diesel_codegen/src/schema_inference/mod.rs @@ -144,11 +144,24 @@ fn table_oid(conn: &Connection, table_name: &str) -> QueryResult { fn column_def_tokens(cx: &mut ExtCtxt, attr: &PgAttr) -> Vec { let column_name = str_to_ident(&attr.column_name); - let type_name = str_to_ident(&capitalize(&attr.type_name)); + let tpe = determine_column_type(cx, attr); + quote_tokens!(cx, $column_name -> $tpe,) +} + +fn determine_column_type(cx: &mut ExtCtxt, attr: &PgAttr) -> P { + let tpe; + if attr.type_name.starts_with("_") { + let subtype = str_to_ident(&capitalize(&attr.type_name[1..])); + tpe = quote_ty!(cx, Array<$subtype>); + } else { + let type_name = str_to_ident(&capitalize(&attr.type_name)); + tpe = quote_ty!(cx, $type_name); + } + if attr.nullable { - quote_tokens!(cx, $column_name -> Nullable<$type_name>,) + quote_ty!(cx, Nullable<$tpe>) } else { - quote_tokens!(cx, $column_name -> $type_name,) + tpe } } diff --git a/diesel_tests/migrations/20160107090901_add_tags_to_posts/down.sql b/diesel_tests/migrations/20160107090901_add_tags_to_posts/down.sql new file mode 100644 index 000000000000..7b003877ccbe --- /dev/null +++ b/diesel_tests/migrations/20160107090901_add_tags_to_posts/down.sql @@ -0,0 +1 @@ +ALTER TABLE posts DROP COLUMN tags; diff --git a/diesel_tests/migrations/20160107090901_add_tags_to_posts/up.sql b/diesel_tests/migrations/20160107090901_add_tags_to_posts/up.sql new file mode 100644 index 000000000000..12b6189e9bd2 --- /dev/null +++ b/diesel_tests/migrations/20160107090901_add_tags_to_posts/up.sql @@ -0,0 +1 @@ +ALTER TABLE posts ADD COLUMN tags varchar[] NOT NULL DEFAULT '{}'; diff --git a/diesel_tests/tests/schema.rs b/diesel_tests/tests/schema.rs index da2e8e0147e0..0cef19a7ab7e 100644 --- a/diesel_tests/tests/schema.rs +++ b/diesel_tests/tests/schema.rs @@ -37,6 +37,7 @@ pub struct Post { pub user_id: i32, pub title: String, pub body: Option, + pub tags: Vec, } impl Post { @@ -46,6 +47,7 @@ impl Post { user_id: user_id, title: title.to_string(), body: body.map(|s| s.to_string()), + tags: Vec::new(), } } }