Skip to content

Commit

Permalink
dependency rework
Browse files Browse the repository at this point in the history
  • Loading branch information
Fleeym committed Feb 17, 2024
1 parent 8bec82f commit 72a887b
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 165 deletions.
10 changes: 6 additions & 4 deletions migrations/20240102213218_first_migration.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,24 @@ CREATE TABLE mod_gd_versions (

CREATE TABLE dependencies (
dependent_id INTEGER NOT NULL,
dependency_id INTEGER NOT NULL,
dependency_id TEXT NOT NULL,
version TEXT NOT NULL,
compare version_compare NOT NULL,
importance dependency_importance NOT NULL,
PRIMARY KEY (dependent_id, dependency_id),
FOREIGN KEY (dependent_id) REFERENCES mod_versions(id),
FOREIGN KEY (dependency_id) REFERENCES mod_versions(id)
FOREIGN KEY (dependency_id) REFERENCES mods(id)
);

CREATE TABLE incompatibilities (
mod_id INTEGER NOT NULL,
incompatibility_id INTEGER NOT NULL,
incompatibility_id TEXT NOT NULL,
version TEXT NOT NULL,
compare version_compare NOT NULL,
importance incompatibility_importance NOT NULL,
PRIMARY KEY (mod_id, incompatibility_id),
FOREIGN KEY (mod_id) REFERENCES mod_versions(id),
FOREIGN KEY (incompatibility_id) REFERENCES mod_versions(id)
FOREIGN KEY (incompatibility_id) REFERENCES mods(id)
);

CREATE TABLE developers (
Expand Down
105 changes: 19 additions & 86 deletions src/types/mod_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,22 +187,18 @@ impl ModJson {
Ok(json)
}

pub async fn query_dependencies(
&self,
pool: &mut PgConnection,
) -> Result<Vec<DependencyCreate>, ApiError> {
pub fn prepare_dependencies_for_create(&self) -> Result<Vec<DependencyCreate>, ApiError> {
let deps = match self.dependencies.as_ref() {
None => return Err(ApiError::InternalError),
None => return Ok(vec![]),
Some(d) => d,
};

if deps.is_empty() {
return Err(ApiError::InternalError);
return Ok(vec![]);
}

let mut ret: Vec<DependencyCreate> = vec![];

// I am going to n+1 this, I am sorry, will optimize later
for i in deps {
let (dependency_ver, compare) = match split_version_and_compare(i.version.as_str()) {
Err(_) => {
Expand All @@ -213,61 +209,29 @@ impl ModJson {
}
Ok((ver, compare)) => (ver, compare),
};

let versions = sqlx::query!(
"SELECT id, version FROM mod_versions WHERE mod_id = $1 and validated = true",
i.id
)
.fetch_all(&mut *pool)
.await;
let versions = match versions {
Err(_) => return Err(ApiError::DbError),
Ok(v) => v,
};
if versions.is_empty() {
return Err(ApiError::BadRequest(format!(
"Couldn't find dependency {} on the index",
i.id
)));
}
let mut found = false;
for j in versions {
// This should never fail (I hope)
let parsed = semver::Version::parse(&j.version).unwrap();
if compare_versions(&parsed, &dependency_ver, &compare) {
ret.push(DependencyCreate {
dependency_id: j.id,
compare,
importance: i.importance,
});
found = true;
break;
}
}
if !found {
return Err(ApiError::BadRequest(format!(
"Couldn't find dependency version that satisfies semver compare {}",
i.version
)));
}
ret.push(DependencyCreate {
dependency_id: i.id.clone(),
version: dependency_ver.to_string(),
compare,
importance: i.importance,
});
}

Ok(ret)
}

pub async fn query_incompatibilities(
pub fn prepare_incompatibilities_for_create(
&self,
pool: &mut PgConnection,
) -> Result<Vec<IncompatibilityCreate>, ApiError> {
let incompat = match self.incompatibilities.as_ref() {
None => return Err(ApiError::InternalError),
None => return Ok(vec![]),
Some(d) => d,
};

if incompat.is_empty() {
return Err(ApiError::InternalError);
return Ok(vec![]);
}
let mut ret: Vec<_> = vec![];
let mut ret: Vec<IncompatibilityCreate> = vec![];

for i in incompat {
let (ver, compare) = match split_version_and_compare(i.version.as_str()) {
Expand All @@ -279,43 +243,12 @@ impl ModJson {
}
Ok((ver, compare)) => (ver, compare),
};

let versions = sqlx::query!(
"SELECT id, version FROM mod_versions WHERE mod_id = $1",
i.id
)
.fetch_all(&mut *pool)
.await;
let versions = match versions {
Err(_) => return Err(ApiError::DbError),
Ok(v) => v,
};
if versions.is_empty() {
return Err(ApiError::BadRequest(format!(
"Couldn't find incompatibility {} on the index",
i.id
)));
}
let mut found = false;
for j in versions {
// This should never fail (I hope)
let parsed = semver::Version::parse(&j.version).unwrap();
if compare_versions(&parsed, &ver, &compare) {
ret.push(IncompatibilityCreate {
incompatibility_id: j.id,
compare,
importance: i.importance,
});
found = true;
break;
}
}
if !found {
return Err(ApiError::BadRequest(format!(
"Couldn't find incompatibility version that satisfies semver compare {}",
i.version
)));
}
ret.push(IncompatibilityCreate {
incompatibility_id: i.id.clone(),
version: ver.to_string(),
compare,
importance: i.importance,
});
}

Ok(ret)
Expand Down
Loading

0 comments on commit 72a887b

Please sign in to comment.