Skip to content

Actually use JSON storage for JSON data #1237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions migrations/2018-01-18-172821_use_jsonb_for_features/down.sql
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 3 additions & 0 deletions migrations/2018-01-18-172821_use_jsonb_for_features/up.sql
Original file line number Diff line number Diff line change
@@ -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;
13 changes: 5 additions & 8 deletions src/models/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct Version {
pub updated_at: NaiveDateTime,
pub created_at: NaiveDateTime,
pub downloads: i32,
pub features: HashMap<String, Vec<String>>,
pub features: serde_json::Value,
pub yanked: bool,
pub license: Option<String>,
}
Expand All @@ -34,7 +34,7 @@ pub struct Version {
pub struct NewVersion {
crate_id: i32,
num: String,
features: String,
features: serde_json::Value,
license: Option<String>,
}

Expand Down Expand Up @@ -118,7 +118,7 @@ impl NewVersion {
license: Option<String>,
license_file: Option<&str>,
) -> CargoResult<Self> {
let features = serde_json::to_string(features)?;
let features = serde_json::to_value(features)?;

let mut new_version = NewVersion {
crate_id,
Expand Down Expand Up @@ -197,23 +197,20 @@ impl Queryable<versions::SqlType, Pg> for Version {
NaiveDateTime,
NaiveDateTime,
i32,
Option<String>,
serde_json::Value,
bool,
Option<String>,
);

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,
num: semver::Version::parse(&row.2).unwrap(),
updated_at: row.3,
created_at: row.4,
downloads: row.5,
features,
features: row.6,
yanked: row.7,
license: row.8,
}
Expand Down
4 changes: 2 additions & 2 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -848,10 +848,10 @@ table! {
downloads -> Int4,
/// The `features` column of the `versions` table.
///
/// Its SQL type is `Nullable<Varchar>`.
/// Its SQL type is `Jsonb`.
///
/// (Automatically generated by Diesel.)
features -> Nullable<Varchar>,
features -> Jsonb,
/// The `yanked` column of the `versions` table.
///
/// Its SQL type is `Bool`.
Expand Down
6 changes: 3 additions & 3 deletions src/views/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use chrono::NaiveDateTime;
use serde_json;
use std::collections::HashMap;

use models::DependencyKind;
Expand Down Expand Up @@ -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<String, Vec<String>>,
pub features: serde_json::Value,
pub yanked: bool,
// NOTE: Used by shields.io, altering `license` requires a PR with shields.io
pub license: Option<String>,
Expand All @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down