Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelvlach committed Dec 20, 2024
1 parent ba930c2 commit 50fc624
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 15 deletions.
Binary file added agdb/storage2
Binary file not shown.
2 changes: 1 addition & 1 deletion agdb_api/rust/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl<T: HttpClient> AgdbApi<T> {
&self,
owner: &str,
db: &str,
queries: &Vec<QueryType>,
queries: &[QueryType],
) -> AgdbApiResult<(u16, Vec<QueryResult>)> {
self.client
.post(
Expand Down
5 changes: 4 additions & 1 deletion agdb_server/openapi.json

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

37 changes: 29 additions & 8 deletions agdb_server/src/routes/admin/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ use crate::action::db_clear::DbClear;
use crate::action::db_convert::DbConvert;
use crate::action::db_copy::DbCopy;
use crate::action::db_delete::DbDelete;
use crate::action::db_exec::DbExec;
use crate::action::db_optimize::DbOptimize;
use crate::action::db_remove::DbRemove;
use crate::action::db_rename::DbRename;
use crate::action::db_restore::DbRestore;
use crate::action::ClusterActionResult;
use crate::cluster::Cluster;
use crate::config::Config;
use crate::db_pool::DbPool;
Expand Down Expand Up @@ -329,7 +331,7 @@ pub(crate) async fn delete(
let (commit_index, _result) = cluster.exec(DbDelete { owner, db }).await?;

Ok((
StatusCode::CREATED,
StatusCode::NO_CONTENT,
[("commit-index", commit_index.to_string())],
))
}
Expand All @@ -354,22 +356,41 @@ pub(crate) async fn delete(
pub(crate) async fn exec(
_admin: AdminId,
State(db_pool): State<DbPool>,
State(cluster): State<Cluster>,
State(config): State<Config>,
Path((owner, db)): Path<(String, String)>,
Json(queries): Json<Queries>,
) -> ServerResponse<(StatusCode, Json<QueriesResults>)> {
) -> ServerResponse<impl IntoResponse> {
let db_name = db_name(&owner, &db);
let required_role = required_role(&queries);

let results = if required_role == DbUserRole::Read {
db_pool.exec(&db_name, queries).await?
let (commit_index, results) = if required_role == DbUserRole::Read {
(0, db_pool.exec(&db_name, queries).await?)
} else {
db_pool
.exec_mut(&owner, &db, &db_name, &config.admin, queries, &config)
let mut index = 0;
let mut results = Vec::new();

if let (i, ClusterActionResult::QueryResults(r)) = cluster
.exec(DbExec {
user: config.admin.clone(),
owner,
db,
queries,
})
.await?
{
index = i;
results = r;
}

(index, results)
};

Ok((StatusCode::OK, Json(QueriesResults(results))))
Ok((
StatusCode::OK,
[("commit-index", commit_index.to_string())],
Json(QueriesResults(results)),
))
}

#[utoipa::path(get,
Expand Down Expand Up @@ -472,7 +493,7 @@ pub(crate) async fn remove(
let (commit_index, _result) = cluster.exec(DbRemove { owner, db }).await?;

Ok((
StatusCode::CREATED,
StatusCode::NO_CONTENT,
[("commit-index", commit_index.to_string())],
))
}
Expand Down
4 changes: 2 additions & 2 deletions agdb_server/src/routes/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ pub(crate) async fn delete(
let (commit_index, _result) = cluster.exec(DbDelete { owner, db }).await?;

Ok((
StatusCode::CREATED,
StatusCode::NO_CONTENT,
[("commit-index", commit_index.to_string())],
))
}
Expand Down Expand Up @@ -577,7 +577,7 @@ pub(crate) async fn remove(
let (commit_index, _result) = cluster.exec(DbRemove { owner, db }).await?;

Ok((
StatusCode::CREATED,
StatusCode::NO_CONTENT,
[("commit-index", commit_index.to_string())],
))
}
Expand Down
6 changes: 3 additions & 3 deletions agdb_server/tests/routes/admin_db_exec_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ async fn db_not_found() -> anyhow::Result<()> {
server.api.user_login(ADMIN, ADMIN).await?;
let status = server
.api
.admin_db_exec("owner", "db", &vec![])
.admin_db_exec("owner", "db", &[])
.await
.unwrap_err()
.status;
Expand All @@ -130,7 +130,7 @@ async fn non_admin() -> anyhow::Result<()> {
server.api.user_login(owner, owner).await?;
let status = server
.api
.admin_db_exec(owner, "db", &vec![])
.admin_db_exec(owner, "db", &[])
.await
.unwrap_err()
.status;
Expand All @@ -143,7 +143,7 @@ async fn no_token() -> anyhow::Result<()> {
let server = TestServer::new().await?;
let status = server
.api
.admin_db_exec("owner", "db", &vec![])
.admin_db_exec("owner", "db", &[])
.await
.unwrap_err()
.status;
Expand Down
79 changes: 79 additions & 0 deletions agdb_server/tests/routes/cluster_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,85 @@ async fn db() -> anyhow::Result<()> {
Ok(())
}

#[tokio::test]
async fn db_admin() -> anyhow::Result<()> {
let (_leader, servers) = create_cluster(2).await?;
servers[0]
.client
.write()
.await
.cluster_login(ADMIN, ADMIN)
.await?;

let client = servers[0].client.read().await;
client.admin_db_add(ADMIN, "db1", DbType::Memory).await?;
let db = &client.db_list().await?.1[0];
assert_eq!(db.name, "admin/db1");
assert_eq!(db.db_type, DbType::Memory);

client.admin_db_backup(ADMIN, "db1").await?;
let db = &client.db_list().await?.1[0];
assert_ne!(db.backup, 0);
client.admin_db_restore(ADMIN, "db1").await?;

let db = client.db_clear(ADMIN, "db1", DbResource::Backup).await?.1;
assert_eq!(db.backup, 0);

client
.admin_db_convert(ADMIN, "db1", DbType::Mapped)
.await?;
let db = &client.db_list().await?.1[0];
assert_eq!(db.db_type, DbType::Mapped);

client.admin_db_copy(ADMIN, "db1", ADMIN, "db2").await?;
let db = &client.db_list().await?.1[1];
assert_eq!(db.name, "admin/db2");
client.admin_db_backup(ADMIN, "db2").await?;

client.admin_db_remove(ADMIN, "db2").await?;
assert_eq!(client.db_list().await?.1.len(), 1);

client.admin_db_add(ADMIN, "db2", DbType::Memory).await?;
let db = &client.db_list().await?.1[1];
assert_eq!(db.name, "admin/db2");
assert_ne!(db.backup, 0);

client.admin_db_delete(ADMIN, "db2").await?;
assert_eq!(client.db_list().await?.1.len(), 1);

client
.admin_db_exec(
ADMIN,
"db1",
&[QueryBuilder::insert().nodes().count(100).query().into()],
)
.await?;
let node_count = client
.admin_db_exec(
ADMIN,
"db1",
&[QueryBuilder::select().node_count().query().into()],
)
.await?
.1[0]
.elements[0]
.values[0]
.value
.to_u64()
.unwrap();
assert_eq!(node_count, 100);

let orig_size = client.admin_db_list().await?.1[0].size;
let db_size = client.admin_db_optimize(ADMIN, "db1").await?.1.size;
assert!(db_size < orig_size);

client.admin_db_rename(ADMIN, "db1", ADMIN, "db2").await?;
let db = &client.db_list().await?.1[0];
assert_eq!(db.name, "admin/db2");

Ok(())
}

#[tokio::test]
async fn status() {
let server = TestServer::new().await.unwrap();
Expand Down

0 comments on commit 50fc624

Please sign in to comment.