Skip to content

Commit

Permalink
fix a bug for empty publications
Browse files Browse the repository at this point in the history
If a publication didn't have any tables, it was not returned by the api.
  • Loading branch information
imor committed Oct 16, 2024
1 parent 76516a5 commit 8eee282
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions api/src/db/publications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,11 @@ pub async fn read_publication(
let mut query = String::new();
query.push_str(
r#"
select p.pubname, pt.schemaname, pt.tablename from pg_publication p
join pg_publication_tables pt on p.pubname = pt.pubname
select p.pubname,
pt.schemaname as "schemaname?",
pt.tablename as "tablename?"
from pg_publication p
left join pg_publication_tables pt on p.pubname = pt.pubname
where
p.puballtables = false
and p.pubinsert = true
Expand All @@ -160,9 +163,11 @@ pub async fn read_publication(
} else {
name = Some(pub_name);
}
let schema = row.get("schemaname");
let name = row.get("tablename");
tables.push(Table { schema, name });
let schema: Option<String> = row.get("schemaname?");
let name: Option<String> = row.get("tablename?");
if let (Some(schema), Some(name)) = (schema, name) {
tables.push(Table { schema, name });
}
}

let publication = name.map(|name| Publication { name, tables });
Expand All @@ -174,8 +179,11 @@ pub async fn read_all_publications(
options: &PgConnectOptions,
) -> Result<Vec<Publication>, sqlx::Error> {
let query = r#"
select p.pubname, pt.schemaname, pt.tablename from pg_publication p
join pg_publication_tables pt on p.pubname = pt.pubname
select p.pubname,
pt.schemaname as "schemaname?",
pt.tablename as "tablename?"
from pg_publication p
left join pg_publication_tables pt on p.pubname = pt.pubname
where
p.puballtables = false
and p.pubinsert = true
Expand All @@ -190,10 +198,13 @@ pub async fn read_all_publications(

for row in connection.fetch_all(query).await? {
let pub_name: String = row.get("pubname");
let schema = row.get("schemaname");
let name = row.get("tablename");
let schema: Option<String> = row.get("schemaname?");
let name: Option<String> = row.get("tablename?");
let tables = pub_name_to_tables.entry(pub_name).or_default();
tables.push(Table { schema, name });

if let (Some(schema), Some(name)) = (schema, name) {
tables.push(Table { schema, name });
}
}

let publications = pub_name_to_tables
Expand Down

0 comments on commit 8eee282

Please sign in to comment.