From 55c200a72d3e137700be053aed2df61f23d92a47 Mon Sep 17 00:00:00 2001 From: Matthias Oertel Date: Thu, 15 Apr 2021 10:34:27 +0200 Subject: [PATCH] Ignore expression indexes and indexes on rowids in Sqlite (#1855) * ignore expression indexes and indexes on rowids * formatting --- .../tests/tables/mod.rs | 28 +++++++++++++++++++ libs/sql-schema-describer/src/sqlite.rs | 17 +++++++---- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/introspection-engine/introspection-engine-tests/tests/tables/mod.rs b/introspection-engine/introspection-engine-tests/tests/tables/mod.rs index 980a5530069c..97ca37023146 100644 --- a/introspection-engine/introspection-engine-tests/tests/tables/mod.rs +++ b/introspection-engine/introspection-engine-tests/tests/tables/mod.rs @@ -725,6 +725,34 @@ async fn partial_indexes_should_be_ignored_on_mysql(api: &TestApi) -> crate::Tes Ok(()) } +#[test_each_connector(tags("sqlite"))] +async fn expression_indexes_should_be_ignored_on_sqlite(api: &TestApi) -> crate::TestResult { + api.barrel() + .execute_with_schema( + |migration| { + migration.create_table("Blog", move |t| { + t.add_column("id", types::primary()); + t.add_column("author", types::text()); + }); + migration.inject_custom("CREATE INDEX author_lowercase_index ON Blog(LOWER(author));") + }, + api.schema_name(), + ) + .await?; + + let dm = indoc! {r##" + model Blog { + id Int @id @default(autoincrement()) + author String + } + "##}; + + let result = &api.introspect().await?; + api.assert_eq_datamodels(&dm, result); + + Ok(()) +} + #[test_each_connector(tags("mysql"))] async fn casing_should_not_lead_to_mix_ups(api: &TestApi) -> crate::TestResult { api.barrel() diff --git a/libs/sql-schema-describer/src/sqlite.rs b/libs/sql-schema-describer/src/sqlite.rs index 24f065b87197..441c2bb6d18a 100644 --- a/libs/sql-schema-describer/src/sqlite.rs +++ b/libs/sql-schema-describer/src/sqlite.rs @@ -433,7 +433,7 @@ impl SqlSchemaDescriber { // Exclude partial indices .filter(|row| !row.get("partial").and_then(|partial| partial.as_bool()).unwrap()); - for row in filtered_rows { + 'index_loop: for row in filtered_rows { let is_unique = row.get("unique").and_then(|x| x.as_bool()).expect("get unique"); let name = row.get("name").and_then(|x| x.to_string()).expect("get name"); let mut index = Index { @@ -449,12 +449,17 @@ impl SqlSchemaDescriber { let result_set = self.conn.query_raw(&sql, &[]).await.expect("querying for index info"); trace!("Got index description results: {:?}", result_set); for row in result_set.into_iter() { - let pos = row.get("seqno").and_then(|x| x.as_i64()).expect("get seqno") as usize; - let col_name = row.get("name").and_then(|x| x.to_string()).expect("get name"); - if index.columns.len() <= pos { - index.columns.resize(pos + 1, "".to_string()); + //if the index is on a rowid or expression, the name of the column will be null, we ignore these for now + match row.get("name").and_then(|x| x.to_string()) { + Some(name) => { + let pos = row.get("seqno").and_then(|x| x.as_i64()).expect("get seqno") as usize; + if index.columns.len() <= pos { + index.columns.resize(pos + 1, "".to_string()); + } + index.columns[pos] = name; + } + None => break 'index_loop, } - index.columns[pos] = col_name; } indices.push(index)