Skip to content

Commit

Permalink
Ignore expression indexes and indexes on rowids in Sqlite (#1855)
Browse files Browse the repository at this point in the history
* ignore expression indexes and indexes on rowids

* formatting
  • Loading branch information
do4gr committed Apr 15, 2021
1 parent 5ae5d7a commit 55c200a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
Expand Up @@ -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()
Expand Down
17 changes: 11 additions & 6 deletions libs/sql-schema-describer/src/sqlite.rs
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit 55c200a

Please sign in to comment.