Skip to content

Commit 25f0e65

Browse files
committed
Actually use JSON storage for JSON data
We also never use `features` as a `HashMap<String, Vec<String>>`, so we can just pass it through from the database in a lower level representation.
1 parent 04ca810 commit 25f0e65

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ALTER TABLE versions ALTER COLUMN features DROP NOT NULL;
2+
ALTER TABLE versions ALTER COLUMN features DROP DEFAULT;
3+
ALTER TABLE versions ALTER COLUMN features SET DATA TYPE text;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ALTER TABLE versions ALTER COLUMN features SET DATA TYPE jsonb USING features::jsonb;
2+
ALTER TABLE versions ALTER COLUMN features SET DEFAULT '{}';
3+
ALTER TABLE versions ALTER COLUMN features SET NOT NULL;

src/schema.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -748,10 +748,10 @@ table! {
748748
downloads -> Int4,
749749
/// The `features` column of the `versions` table.
750750
///
751-
/// Its SQL type is `Nullable<Varchar>`.
751+
/// Its SQL type is `Jsonb`.
752752
///
753753
/// (Automatically generated by Diesel.)
754-
features -> Nullable<Varchar>,
754+
features -> Jsonb,
755755
/// The `yanked` column of the `versions` table.
756756
///
757757
/// Its SQL type is `Bool`.

src/version/mod.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub struct Version {
3131
pub updated_at: NaiveDateTime,
3232
pub created_at: NaiveDateTime,
3333
pub downloads: i32,
34-
pub features: HashMap<String, Vec<String>>,
34+
pub features: serde_json::Value,
3535
pub yanked: bool,
3636
pub license: Option<String>,
3737
}
@@ -41,7 +41,7 @@ pub struct Version {
4141
pub struct NewVersion {
4242
crate_id: i32,
4343
num: String,
44-
features: String,
44+
features: serde_json::Value,
4545
license: Option<String>,
4646
}
4747

@@ -55,7 +55,7 @@ pub struct EncodableVersion {
5555
#[serde(with = "::util::rfc3339")] pub updated_at: NaiveDateTime,
5656
#[serde(with = "::util::rfc3339")] pub created_at: NaiveDateTime,
5757
pub downloads: i32,
58-
pub features: HashMap<String, Vec<String>>,
58+
pub features: serde_json::Value,
5959
pub yanked: bool,
6060
pub license: Option<String>,
6161
pub links: VersionLinks,
@@ -145,7 +145,7 @@ impl NewVersion {
145145
license: Option<String>,
146146
license_file: Option<&str>,
147147
) -> CargoResult<Self> {
148-
let features = serde_json::to_string(features)?;
148+
let features = serde_json::to_value(features)?;
149149

150150
let mut new_version = NewVersion {
151151
crate_id: crate_id,
@@ -224,24 +224,21 @@ impl Queryable<versions::SqlType, Pg> for Version {
224224
NaiveDateTime,
225225
NaiveDateTime,
226226
i32,
227-
Option<String>,
227+
serde_json::Value,
228228
bool,
229229
Option<String>,
230230
Option<NaiveDateTime>,
231231
);
232232

233233
fn build(row: Self::Row) -> Self {
234-
let features = row.6
235-
.map(|s| serde_json::from_str(&s).unwrap())
236-
.unwrap_or_else(HashMap::new);
237234
Version {
238235
id: row.0,
239236
crate_id: row.1,
240237
num: semver::Version::parse(&row.2).unwrap(),
241238
updated_at: row.3,
242239
created_at: row.4,
243240
downloads: row.5,
244-
features: features,
241+
features: row.6,
245242
yanked: row.7,
246243
license: row.8,
247244
}

0 commit comments

Comments
 (0)