Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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

5 changes: 3 additions & 2 deletions crates/pgt_schema_cache/src/queries/columns.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ with
available_indexes as (
select
unnest (ix.indkey) as attnum,
ix.indisprimary as is_primary,
ix.indisunique as is_unique,
bool_or(ix.indisprimary) as is_primary,
bool_or(ix.indisunique) as is_unique,
ix.indrelid as table_oid
from
pg_catalog.pg_class c
join pg_catalog.pg_index ix on c.oid = ix.indexrelid
where
c.relkind = 'i'
group by table_oid, attnum
)
select
atts.attname as name,
Expand Down
33 changes: 32 additions & 1 deletion crates/pgt_schema_cache/src/schema_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ pub trait SchemaCacheItem {

#[cfg(test)]
mod tests {
use sqlx::PgPool;
use std::collections::HashSet;

use sqlx::{Executor, PgPool};

use crate::SchemaCache;

Expand All @@ -175,4 +177,33 @@ mod tests {
.await
.expect("Couldnt' load Schema Cache");
}

#[sqlx::test(migrator = "pgt_test_utils::MIGRATIONS")]
async fn it_does_not_have_duplicate_entries(test_db: PgPool) {
// we had some duplicate columns in the schema_cache because of indices including the same column multiple times.
// the columns were unnested as duplicates in the query
let setup = r#"
CREATE TABLE public.mfa_factors (
id uuid PRIMARY KEY,
factor_name text NOT NULL
);

-- a second index on id!
CREATE INDEX idx_mfa_user_factor ON public.mfa_factors(id, factor_name);
"#;

test_db.execute(setup).await.unwrap();

let cache = SchemaCache::load(&test_db)
.await
.expect("Couldn't load Schema Cache");

let set: HashSet<String> = cache
.columns
.iter()
.map(|c| format!("{}.{}.{}", c.schema_name, c.table_name, c.name))
.collect();

assert_eq!(set.len(), cache.columns.len());
}
}