Skip to content

Store feature flags per release in database #1129

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 1 commit into from
Oct 29, 2020
Merged
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
23 changes: 19 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -53,6 +53,7 @@ lol_html = "0.2"
font-awesome-as-a-crate = { path = "crates/font-awesome-as-a-crate" }
dashmap = "3.11.10"
string_cache = "0.8.0"
postgres-types = { version = "0.1.3", features = ["derive"] }

# Async
tokio = { version = "0.2.22", features = ["rt-threaded"] }
34 changes: 31 additions & 3 deletions src/db/add_package.rs
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ use std::{
};

use crate::{
db::types::Feature,
docbuilder::{BuildResult, DocCoverage},
error::Result,
index::api::{CrateData, CrateOwner, ReleaseData},
@@ -42,6 +43,7 @@ pub(crate) fn add_package_into_database(
let dependencies = convert_dependencies(metadata_pkg);
let rustdoc = get_rustdoc(metadata_pkg, source_dir).unwrap_or(None);
let readme = get_readme(metadata_pkg, source_dir).unwrap_or(None);
let features = get_features(metadata_pkg);
let is_library = metadata_pkg.is_library();

let rows = conn.query(
@@ -52,12 +54,12 @@ pub(crate) fn add_package_into_database(
homepage_url, description, description_long, readme,
authors, keywords, have_examples, downloads, files,
doc_targets, is_library, doc_rustc_version,
documentation_url, default_target
documentation_url, default_target, features
)
VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9,
$10, $11, $12, $13, $14, $15, $16, $17, $18,
$19, $20, $21, $22, $23, $24, $25
$19, $20, $21, $22, $23, $24, $25, $26
)
ON CONFLICT (crate_id, version) DO UPDATE
SET release_time = $3,
@@ -82,7 +84,8 @@ pub(crate) fn add_package_into_database(
is_library = $22,
doc_rustc_version = $23,
documentation_url = $24,
default_target = $25
default_target = $25,
features = $26
RETURNING id",
&[
&crate_id,
@@ -110,6 +113,7 @@ pub(crate) fn add_package_into_database(
&res.rustc_version,
&metadata_pkg.documentation,
&default_target,
&features,
],
)?;

@@ -213,6 +217,30 @@ fn convert_dependencies(pkg: &MetadataPackage) -> Vec<(String, String, String)>
.collect()
}

/// Reads features and converts them to Vec<Feature> with default being first
fn get_features(pkg: &MetadataPackage) -> Vec<Feature> {
let mut features = Vec::with_capacity(pkg.features.len());
if let Some(subfeatures) = pkg.features.get("default") {
features.push(Feature::new("default".into(), subfeatures.clone()));
};
features.extend(
pkg.features
.iter()
.filter(|(name, _)| *name != "default")
.map(|(name, subfeatures)| Feature::new(name.clone(), subfeatures.clone())),
);
features.extend(get_optional_dependencies(pkg));
features
}

fn get_optional_dependencies(pkg: &MetadataPackage) -> Vec<Feature> {
pkg.dependencies
.iter()
.filter(|dep| dep.optional)
.map(|dep| Feature::new(dep.name.clone(), Vec::new()))
.collect()
}

/// Reads readme if there is any read defined in Cargo.toml of a Package
fn get_readme(pkg: &MetadataPackage, source_dir: &Path) -> Result<Option<String>> {
let readme_path = source_dir.join(pkg.readme.as_deref().unwrap_or("README.md"));
19 changes: 18 additions & 1 deletion src/db/migrate.rs
Original file line number Diff line number Diff line change
@@ -457,7 +457,24 @@ pub fn migrate(version: Option<Version>, conn: &mut Client) -> CratesfyiResult<(
DROP COLUMN total_items_needing_examples,
DROP COLUMN items_with_examples;
"
)
),
migration!(
context,
// version
19,
// description
"Add features that are available for given release",
// upgrade query
"
CREATE TYPE feature AS (name TEXT, subfeatures TEXT[]);
ALTER TABLE releases ADD COLUMN features feature[];
",
// downgrade query
"
ALTER TABLE releases DROP COLUMN features;
DROP TYPE feature;
"
),
];

for migration in migrations {
1 change: 1 addition & 0 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
@@ -15,3 +15,4 @@ mod delete;
pub(crate) mod file;
mod migrate;
mod pool;
pub(crate) mod types;
15 changes: 15 additions & 0 deletions src/db/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use postgres_types::{FromSql, ToSql};
use serde::Serialize;

#[derive(Debug, Clone, PartialEq, Serialize, FromSql, ToSql)]
#[postgres(name = "feature")]
pub struct Feature {
name: String,
subfeatures: Vec<String>,
}

impl Feature {
pub fn new(name: String, subfeatures: Vec<String>) -> Self {
Feature { name, subfeatures }
}
}
11 changes: 11 additions & 0 deletions src/test/fakes.rs
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ use crate::storage::Storage;
use crate::utils::{Dependency, MetadataPackage, Target};
use chrono::{DateTime, Utc};
use failure::Error;
use std::collections::HashMap;
use std::sync::Arc;

#[must_use = "FakeRelease does nothing until you call .create()"]
@@ -48,11 +49,21 @@ impl<'a> FakeRelease<'a> {
name: "fake-dependency".into(),
req: "^1.0.0".into(),
kind: None,
optional: false,
}],
targets: vec![Target::dummy_lib("fake_package".into(), None)],
readme: None,
keywords: vec!["fake".into(), "package".into()],
authors: vec!["Fake Person <[email protected]>".into()],
features: [
("default".into(), vec!["feature1".into(), "feature3".into()]),
("feature1".into(), Vec::new()),
("feature2".into(), vec!["feature1".into()]),
("feature3".into(), Vec::new()),
]
.iter()
.cloned()
Comment on lines +64 to +65
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.collect::<HashMap<String, Vec<String>>>(),
},
build_result: BuildResult {
rustc_version: "rustc 2.0.0-nightly (000000000 1970-01-01)".into(),
2 changes: 2 additions & 0 deletions src/utils/cargo_metadata.rs
Original file line number Diff line number Diff line change
@@ -77,6 +77,7 @@ pub(crate) struct Package {
pub(crate) readme: Option<String>,
pub(crate) keywords: Vec<String>,
pub(crate) authors: Vec<String>,
pub(crate) features: HashMap<String, Vec<String>>,
}

impl Package {
@@ -131,6 +132,7 @@ pub(crate) struct Dependency {
pub(crate) name: String,
pub(crate) req: String,
pub(crate) kind: Option<String>,
pub(crate) optional: bool,
}

#[derive(Deserialize, Serialize)]