Skip to content

Commit 380fcfc

Browse files
fix: duplicate columns in schema cache (#552)
Fixes the duplicate hover bug of #528
1 parent 83934ed commit 380fcfc

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

.sqlx/query-b0163e58e9c646e3af524174081b74ab7e7938e9516ea21513265c49d304b6ea.json renamed to .sqlx/query-1cc58ddce2b52b5d6f6519cde339f258bce50342c2d5cce036dba4f9062cf811.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/pgt_schema_cache/src/queries/columns.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ with
1919
available_indexes as (
2020
select
2121
unnest (ix.indkey) as attnum,
22-
ix.indisprimary as is_primary,
23-
ix.indisunique as is_unique,
22+
bool_or(ix.indisprimary) as is_primary,
23+
bool_or(ix.indisunique) as is_unique,
2424
ix.indrelid as table_oid
2525
from
2626
pg_catalog.pg_class c
2727
join pg_catalog.pg_index ix on c.oid = ix.indexrelid
2828
where
2929
c.relkind = 'i'
30+
group by table_oid, attnum
3031
)
3132
select
3233
atts.attname as name,

crates/pgt_schema_cache/src/schema_cache.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ pub trait SchemaCacheItem {
165165

166166
#[cfg(test)]
167167
mod tests {
168-
use sqlx::PgPool;
168+
use std::collections::HashSet;
169+
170+
use sqlx::{Executor, PgPool};
169171

170172
use crate::SchemaCache;
171173

@@ -175,4 +177,33 @@ mod tests {
175177
.await
176178
.expect("Couldnt' load Schema Cache");
177179
}
180+
181+
#[sqlx::test(migrator = "pgt_test_utils::MIGRATIONS")]
182+
async fn it_does_not_have_duplicate_entries(test_db: PgPool) {
183+
// we had some duplicate columns in the schema_cache because of indices including the same column multiple times.
184+
// the columns were unnested as duplicates in the query
185+
let setup = r#"
186+
CREATE TABLE public.mfa_factors (
187+
id uuid PRIMARY KEY,
188+
factor_name text NOT NULL
189+
);
190+
191+
-- a second index on id!
192+
CREATE INDEX idx_mfa_user_factor ON public.mfa_factors(id, factor_name);
193+
"#;
194+
195+
test_db.execute(setup).await.unwrap();
196+
197+
let cache = SchemaCache::load(&test_db)
198+
.await
199+
.expect("Couldn't load Schema Cache");
200+
201+
let set: HashSet<String> = cache
202+
.columns
203+
.iter()
204+
.map(|c| format!("{}.{}.{}", c.schema_name, c.table_name, c.name))
205+
.collect();
206+
207+
assert_eq!(set.len(), cache.columns.len());
208+
}
178209
}

0 commit comments

Comments
 (0)