fix: namespace already taken

This commit is contained in:
Zack Fu Zi Xiang 2024-11-03 15:08:38 +08:00
parent a5a5a6b59f
commit d6d7e7312e
No known key found for this signature in database
4 changed files with 40 additions and 5 deletions

View File

@ -0,0 +1,22 @@
{
"db_name": "PostgreSQL",
"query": "\n SELECT EXISTS(\n SELECT 1\n FROM af_workspace\n WHERE publish_namespace = $1\n )\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "exists",
"type_info": "Bool"
}
],
"parameters": {
"Left": [
"Text"
]
},
"nullable": [
null
]
},
"hash": "e4567475b82642745a7a96ec273efe7b549beedbf7e17b5b18d459334c29aa82"
}

View File

@ -35,7 +35,6 @@ pub async fn select_user_is_collab_publisher_for_all_views(
#[inline]
pub async fn select_workspace_publish_namespace_exists<'a, E: Executor<'a, Database = Postgres>>(
executor: E,
workspace_id: &Uuid,
namespace: &str,
) -> Result<bool, AppError> {
let res = sqlx::query_scalar!(
@ -43,11 +42,9 @@ pub async fn select_workspace_publish_namespace_exists<'a, E: Executor<'a, Datab
SELECT EXISTS(
SELECT 1
FROM af_workspace
WHERE workspace_id = $1
AND publish_namespace = $2
WHERE publish_namespace = $1
)
"#,
workspace_id,
namespace,
)
.fetch_one(executor)

View File

@ -91,7 +91,7 @@ pub async fn set_workspace_namespace(
new_namespace: &str,
) -> Result<(), AppError> {
check_workspace_namespace(new_namespace).await?;
if select_workspace_publish_namespace_exists(pg_pool, workspace_id, new_namespace).await? {
if select_workspace_publish_namespace_exists(pg_pool, new_namespace).await? {
return Err(AppError::PublishNamespaceAlreadyTaken(
"publish namespace is already taken".to_string(),
));

View File

@ -44,6 +44,22 @@ async fn test_set_publish_namespace_set() {
.await
.unwrap();
{
// another workspace cannot have the same namespace
let (c2, _user) = generate_unique_registered_user_client().await;
let workspace_id_2 = get_first_workspace_string(&c2).await;
let err = c2
.set_workspace_publish_namespace(&workspace_id_2.to_string(), &namespace)
.await
.unwrap_err();
assert_eq!(
err.code,
ErrorCode::PublishNamespaceAlreadyTaken,
"{:?}",
err
);
}
{
// cannot set the same namespace
let err = c