Skip to content

Commit

Permalink
databases_list Core api route: add total for pagination (#2926)
Browse files Browse the repository at this point in the history
  • Loading branch information
PopDaph authored Dec 19, 2023
1 parent 90b1aa3 commit 96a039c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
7 changes: 5 additions & 2 deletions core/bin/dust_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1848,12 +1848,15 @@ async fn databases_list(
"Failed to list databases",
Some(e),
),
Ok(dbs) => (
Ok((dbs, total)) => (
StatusCode::OK,
Json(APIResponse {
error: None,
response: Some(json!({
"databases": dbs
"databases": dbs,
"offset": query.offset,
"limit": query.limit,
"total": total,
})),
}),
),
Expand Down
5 changes: 2 additions & 3 deletions core/src/data_sources/data_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1828,15 +1828,14 @@ impl DataSource {
));

// Delete databases (concurrently).
let databases = store
let (databases, total) = store
.list_databases(&self.project, &self.data_source_id, None)
.await?;
try_join_all(databases.iter().map(|db| db.delete(store.clone()))).await?;

utils::done(&format!(
"Deleted databases: data_source_id={} database_count={}",
self.data_source_id,
databases.len(),
self.data_source_id, total,
));

// Delete data source and documents (SQL).
Expand Down
22 changes: 20 additions & 2 deletions core/src/stores/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1944,7 +1944,7 @@ impl Store for PostgresStore {
project: &Project,
data_source_id: &str,
limit_offset: Option<(usize, usize)>,
) -> Result<Vec<Database>> {
) -> Result<(Vec<Database>, usize)> {
let project_id = project.project_id();
let data_source_id = data_source_id.to_string();

Expand Down Expand Up @@ -1993,7 +1993,25 @@ impl Store for PostgresStore {
})
.collect::<Result<Vec<_>>>()?;

Ok(databases)
let total = match limit_offset {
None => databases.len(),
Some(_) => {
let stmt = c
.prepare(
"SELECT COUNT(*) FROM databases \
INNER JOIN data_sources ON databases.data_source = data_sources.id \
WHERE data_sources.project = $1 AND data_sources.data_source_id = $2",
)
.await?;
let t: i64 = c
.query_one(&stmt, &[&project_id, &data_source_id])
.await?
.get(0);
t as usize
}
};

Ok((databases, total))
}

async fn assign_live_sqlite_worker_to_database(
Expand Down
2 changes: 1 addition & 1 deletion core/src/stores/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ pub trait Store {
project: &Project,
data_source_id: &str,
limit_offset: Option<(usize, usize)>,
) -> Result<Vec<Database>>;
) -> Result<(Vec<Database>, usize)>;
async fn assign_live_sqlite_worker_to_database(
&self,
project: &Project,
Expand Down

0 comments on commit 96a039c

Please sign in to comment.