diff --git a/migrations/2018-01-18-172821_use_jsonb_for_features/down.sql b/migrations/2018-01-18-172821_use_jsonb_for_features/down.sql new file mode 100644 index 00000000000..75c881634ff --- /dev/null +++ b/migrations/2018-01-18-172821_use_jsonb_for_features/down.sql @@ -0,0 +1,3 @@ +ALTER TABLE versions ALTER COLUMN features DROP NOT NULL; +ALTER TABLE versions ALTER COLUMN features DROP DEFAULT; +ALTER TABLE versions ALTER COLUMN features SET DATA TYPE text; diff --git a/migrations/2018-01-18-172821_use_jsonb_for_features/up.sql b/migrations/2018-01-18-172821_use_jsonb_for_features/up.sql new file mode 100644 index 00000000000..16b1c27af0b --- /dev/null +++ b/migrations/2018-01-18-172821_use_jsonb_for_features/up.sql @@ -0,0 +1,3 @@ +ALTER TABLE versions ALTER COLUMN features SET DATA TYPE jsonb USING features::jsonb; +ALTER TABLE versions ALTER COLUMN features SET DEFAULT '{}'; +ALTER TABLE versions ALTER COLUMN features SET NOT NULL; diff --git a/src/models/version.rs b/src/models/version.rs index d97440271fd..8bf7313e4f5 100644 --- a/src/models/version.rs +++ b/src/models/version.rs @@ -24,7 +24,7 @@ pub struct Version { pub updated_at: NaiveDateTime, pub created_at: NaiveDateTime, pub downloads: i32, - pub features: HashMap>, + pub features: serde_json::Value, pub yanked: bool, pub license: Option, } @@ -34,7 +34,7 @@ pub struct Version { pub struct NewVersion { crate_id: i32, num: String, - features: String, + features: serde_json::Value, license: Option, } @@ -118,7 +118,7 @@ impl NewVersion { license: Option, license_file: Option<&str>, ) -> CargoResult { - let features = serde_json::to_string(features)?; + let features = serde_json::to_value(features)?; let mut new_version = NewVersion { crate_id, @@ -197,15 +197,12 @@ impl Queryable for Version { NaiveDateTime, NaiveDateTime, i32, - Option, + serde_json::Value, bool, Option, ); fn build(row: Self::Row) -> Self { - let features = row.6 - .map(|s| serde_json::from_str(&s).unwrap()) - .unwrap_or_else(HashMap::new); Version { id: row.0, crate_id: row.1, @@ -213,7 +210,7 @@ impl Queryable for Version { updated_at: row.3, created_at: row.4, downloads: row.5, - features, + features: row.6, yanked: row.7, license: row.8, } diff --git a/src/schema.rs b/src/schema.rs index 89da3e45425..d62d73ee600 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -848,10 +848,10 @@ table! { downloads -> Int4, /// The `features` column of the `versions` table. /// - /// Its SQL type is `Nullable`. + /// Its SQL type is `Jsonb`. /// /// (Automatically generated by Diesel.) - features -> Nullable, + features -> Jsonb, /// The `yanked` column of the `versions` table. /// /// Its SQL type is `Bool`. diff --git a/src/views/mod.rs b/src/views/mod.rs index 82f43e628cc..5fd157f2c96 100644 --- a/src/views/mod.rs +++ b/src/views/mod.rs @@ -1,4 +1,5 @@ use chrono::NaiveDateTime; +use serde_json; use std::collections::HashMap; use models::DependencyKind; @@ -187,7 +188,7 @@ pub struct EncodableVersion { pub created_at: NaiveDateTime, // NOTE: Used by shields.io, altering `downloads` requires a PR with shields.io pub downloads: i32, - pub features: HashMap>, + pub features: serde_json::Value, pub yanked: bool, // NOTE: Used by shields.io, altering `license` requires a PR with shields.io pub license: Option, @@ -211,7 +212,6 @@ mod tests { use super::*; use chrono::NaiveDate; use serde_json; - use std::collections::HashMap; #[test] fn category_dates_serializes_to_rfc3339() { @@ -277,7 +277,7 @@ mod tests { updated_at: NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 11), created_at: NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 12), downloads: 0, - features: HashMap::new(), + features: serde_json::from_str("{}").unwrap(), yanked: false, license: None, links: EncodableVersionLinks {