feat: rename + delete vocabularies, blocked when in use (#30)
This commit is contained in:
@@ -293,6 +293,88 @@ pub async fn delete_term(
|
||||
Ok(crate::DeleteOutcome::Deleted)
|
||||
}
|
||||
|
||||
/// Rename a vocabulary's key, recording an `updated` audit entry. Returns `false` if no
|
||||
/// such vocabulary. A unique-key collision surfaces as the underlying sqlx error (23505).
|
||||
pub async fn rename_vocabulary(
|
||||
conn: &mut sqlx::PgConnection,
|
||||
actor: AuditActor,
|
||||
id: VocabularyId,
|
||||
key: &str,
|
||||
) -> Result<bool, sqlx::Error> {
|
||||
let updated = sqlx::query("UPDATE vocabulary SET key = $2 WHERE id = $1")
|
||||
.bind(id.to_uuid())
|
||||
.bind(key)
|
||||
.execute(&mut *conn)
|
||||
.await?
|
||||
.rows_affected();
|
||||
|
||||
if updated == 0 {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
audit::record(
|
||||
&mut *conn,
|
||||
&NewAuditEvent {
|
||||
actor,
|
||||
action: AuditAction::Updated,
|
||||
entity_type: VOCABULARY_ENTITY_TYPE.to_owned(),
|
||||
entity_id: id.to_uuid(),
|
||||
changes: Vec::new(),
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// Delete a vocabulary unless it still has terms or is bound by a field definition
|
||||
/// (both would otherwise hit the FK `RESTRICT`). Records a `deleted` audit entry.
|
||||
pub async fn delete_vocabulary(
|
||||
conn: &mut sqlx::PgConnection,
|
||||
actor: AuditActor,
|
||||
id: VocabularyId,
|
||||
) -> Result<crate::DeleteOutcome, sqlx::Error> {
|
||||
let exists = sqlx::query_scalar::<_, i32>("SELECT 1 FROM vocabulary WHERE id = $1")
|
||||
.bind(id.to_uuid())
|
||||
.fetch_optional(&mut *conn)
|
||||
.await?;
|
||||
|
||||
if exists.is_none() {
|
||||
return Ok(crate::DeleteOutcome::NotFound);
|
||||
}
|
||||
|
||||
let count: i64 = sqlx::query_scalar(
|
||||
"SELECT (SELECT count(*) FROM term WHERE vocabulary_id = $1) \
|
||||
+ (SELECT count(*) FROM field_definition WHERE vocabulary_id = $1)",
|
||||
)
|
||||
.bind(id.to_uuid())
|
||||
.fetch_one(&mut *conn)
|
||||
.await?;
|
||||
|
||||
if count > 0 {
|
||||
return Ok(crate::DeleteOutcome::InUse { count });
|
||||
}
|
||||
|
||||
sqlx::query("DELETE FROM vocabulary WHERE id = $1")
|
||||
.bind(id.to_uuid())
|
||||
.execute(&mut *conn)
|
||||
.await?;
|
||||
|
||||
audit::record(
|
||||
&mut *conn,
|
||||
&NewAuditEvent {
|
||||
actor,
|
||||
action: AuditAction::Deleted,
|
||||
entity_type: VOCABULARY_ENTITY_TYPE.to_owned(),
|
||||
entity_id: id.to_uuid(),
|
||||
changes: Vec::new(),
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(crate::DeleteOutcome::Deleted)
|
||||
}
|
||||
|
||||
fn map_vocabulary(row: sqlx::postgres::PgRow) -> Result<Vocabulary, sqlx::Error> {
|
||||
Ok(Vocabulary {
|
||||
id: VocabularyId::from_uuid(row.try_get("id")?),
|
||||
|
||||
@@ -313,3 +313,75 @@ async fn delete_term_blocks_when_referenced_then_succeeds(pool: PgPool) {
|
||||
.unwrap();
|
||||
assert_eq!(gone, DeleteOutcome::NotFound);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "../db/migrations")]
|
||||
async fn rename_vocabulary_changes_key(pool: PgPool) {
|
||||
let db = Db::from_pool(pool);
|
||||
let mut tx = db.pool().begin().await.unwrap();
|
||||
let v = vocab::create_vocabulary(&mut tx, AuditActor::System, "old")
|
||||
.await
|
||||
.unwrap();
|
||||
let existed = vocab::rename_vocabulary(&mut tx, AuditActor::System, v.id, "new")
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(existed);
|
||||
tx.commit().await.unwrap();
|
||||
|
||||
assert!(
|
||||
vocab::vocabulary_by_key(db.pool(), "new")
|
||||
.await
|
||||
.unwrap()
|
||||
.is_some()
|
||||
);
|
||||
assert!(
|
||||
vocab::vocabulary_by_key(db.pool(), "old")
|
||||
.await
|
||||
.unwrap()
|
||||
.is_none()
|
||||
);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "../db/migrations")]
|
||||
async fn delete_vocabulary_blocks_when_it_has_terms(pool: PgPool) {
|
||||
use db::DeleteOutcome;
|
||||
|
||||
let db = Db::from_pool(pool);
|
||||
let mut tx = db.pool().begin().await.unwrap();
|
||||
let v = vocab::create_vocabulary(&mut tx, AuditActor::System, "material")
|
||||
.await
|
||||
.unwrap();
|
||||
vocab::add_term(
|
||||
&mut tx,
|
||||
AuditActor::System,
|
||||
&NewTerm {
|
||||
vocabulary_id: v.id,
|
||||
external_uri: None,
|
||||
labels: vec![LocalizedLabel {
|
||||
lang: "sv".into(),
|
||||
label: "Trä".into(),
|
||||
}],
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let blocked = vocab::delete_vocabulary(&mut tx, AuditActor::System, v.id)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(blocked, DeleteOutcome::InUse { count: 1 });
|
||||
|
||||
let empty = vocab::create_vocabulary(&mut tx, AuditActor::System, "empty")
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
vocab::delete_vocabulary(&mut tx, AuditActor::System, empty.id)
|
||||
.await
|
||||
.unwrap(),
|
||||
DeleteOutcome::Deleted
|
||||
);
|
||||
|
||||
let gone = vocab::delete_vocabulary(&mut tx, AuditActor::System, empty.id)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(gone, DeleteOutcome::NotFound);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user