Skip to content

Commit

Permalink
Merge pull request rails#45461 from fatkodima/sqlite3-index-trailing-…
Browse files Browse the repository at this point in the history
…comment

Fix retrieving sqlite3 expression indexes created by sql annotated with trailing comment
  • Loading branch information
eileencodes authored Jul 5, 2022
2 parents 45eb295 + 2e12a6d commit 19f9922
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def indexes(table_name)
WHERE name = #{quote(row['name'])} AND type = 'index'
SQL

/\bON\b\s*"?(\w+?)"?\s*\((?<expressions>.+?)\)(?:\s*WHERE\b\s*(?<where>.+))?\z/i =~ index_sql
/\bON\b\s*"?(\w+?)"?\s*\((?<expressions>.+?)\)(?:\s*WHERE\b\s*(?<where>.+))?(?:\s*\/\*.*\*\/)?\z/i =~ index_sql

columns = exec_query("PRAGMA index_info(#{quote(row['name'])})", "SCHEMA").map do |col|
col["name"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,14 @@ def test_expression_index
end
end

def test_expression_index_with_trailing_comment
with_example_table do
@conn.execute "CREATE INDEX expression on ex (number % 10) /* comment */"
index = @conn.indexes("ex").find { |idx| idx.name == "expression" }
assert_equal "number % 10", index.columns
end
end

def test_expression_index_with_where
with_example_table do
@conn.add_index "ex", "id % 10, max(id, number)", name: "expression", where: "id > 1000"
Expand Down

0 comments on commit 19f9922

Please sign in to comment.