diff --git a/sn0int-registry/src/models.rs b/sn0int-registry/src/models.rs index 504e6d79..cf57efbd 100644 --- a/sn0int-registry/src/models.rs +++ b/sn0int-registry/src/models.rs @@ -231,13 +231,21 @@ impl Release { */ } - pub fn find(id: i32, version: &str, connection: &PgConnection) -> Result { - releases::table.filter(releases::columns::module_id.eq(id)) + pub fn find(module_id: i32, version: &str, connection: &PgConnection) -> Result { + releases::table.filter(releases::columns::module_id.eq(module_id)) .filter(releases::columns::version.eq(version)) .first::(connection) .map_err(Error::from) } + pub fn try_find(module_id: i32, version: &str, connection: &PgConnection) -> Result> { + releases::table.filter(releases::columns::module_id.eq(module_id)) + .filter(releases::columns::version.eq(version)) + .first::(connection) + .optional() + .map_err(Error::from) + } + pub fn id(id: i32, connection: &PgConnection) -> Result { releases::table.find(id) .first::(connection) diff --git a/sn0int-registry/src/routes/api.rs b/sn0int-registry/src/routes/api.rs index f54e4728..bdabaea1 100644 --- a/sn0int-registry/src/routes/api.rs +++ b/sn0int-registry/src/routes/api.rs @@ -93,7 +93,17 @@ fn publish(name: String, upload: Json, session: AuthHeader, conn connection.transaction::<_, Error, _>(|| { let module = Module::update_or_create(&user, &name, &metadata.description, &connection)?; - module.add_version(&version, &upload.code, &connection)?; + + match Release::try_find(module.id, &version, &connection)? { + Some(release) => { + // if the code is identical, pretend we published the version + if release.code != upload.code { + bail!("Version number already in use") + } + }, + None => module.add_version(&version, &upload.code, &connection)?, + } + Ok(()) })?; diff --git a/src/api.rs b/src/api.rs index 465fc3e7..76301045 100644 --- a/src/api.rs +++ b/src/api.rs @@ -40,7 +40,7 @@ impl Client { pub fn request(&self, mut request: RequestBuilder, body: Body) -> Result { if let Some(session) = &self.session { - info!("Adding session token"); + info!("Adding session token to request"); request.header("Auth", session.as_str()); }