From 57f6ffdb8672fc7e48d5846a0ab7507b08cb24c5 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Thu, 7 Jan 2016 09:46:03 -0700 Subject: [PATCH] Infer schema properly when array columns are used Note: We don't ever actually use the added column, because we don't need to. Its presence alone is enough to cause the tests to fail. This implementation is a hack. It's awful. I need to write a more extensible system for type lookups. But right now I don't care. This is blocking 0.4, and I can fix it in 0.5. Fixes #65 --- CHANGELOG.md | 3 +++ diesel_codegen/src/schema_inference/mod.rs | 19 ++++++++++++++++--- .../20160107090901_add_tags_to_posts/down.sql | 1 + .../20160107090901_add_tags_to_posts/up.sql | 1 + diesel_tests/tests/schema.rs | 2 ++ 5 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 diesel_tests/migrations/20160107090901_add_tags_to_posts/down.sql create mode 100644 diesel_tests/migrations/20160107090901_add_tags_to_posts/up.sql 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(), } } }