You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The query PRAGMA index_list(table_name) return one row per index, and each row is used to get the column name with a PRAGMA index_info(index_name). As the index is an expression index, the column name is nil.
A regular expression is used to get the expression used to create the index from the SQL. The regexp is /\bON\b\s*"?(\w+?)"?\s*\((?<expressions>.+?)\)(?:\s*WHERE\b\s*(?<where>.+))?\z/i. This regexp does not match the trailing comment appended by marginalia, so expressions is set to nil, so columns is set to nil too, and some code asks for columns.size and produces the error NoMethodError: undefined method 'size' for nil:NilClass.
How to fix?
That's where I need help. Options I've seen so far:
use a regexp which ignores trailing comments.
using # /\bON\b\s*"?(\w+?)"?\s*\((?<expressions>.+?)\)(?:\s*WHERE\b\s*(?<where>.+))?(?:\s\/\*.*\*\/)?\z/i =~ index_sql will work. I can issue a pull request against rails but it would not be fixed in the 6.0 branch.
monkey patch ActiveRecord::ConnectionAdapters::SQLite3::SchemaStatements#indexes with the good regexp.
I would prefer avoiding that.
disable marginalia for some SQL commands.
Is it possible?
What do you recommend here?
The text was updated successfully, but these errors were encountered:
Migrating from Rails 5.2 to Rails 6.0.4.1, and I got this error when loading schema in sqlite:
I understood where the issue comes from and I need help about how it can be fixed. Here are the reproducing steps and the explanation for this error.
Reproducing steps
Create a rails 6.0 app with marginalia and two migrations: one adding an expression index, the second adding another index on the same table.
set the content of this migration to
then add another migration
set the content to
run
to see the following error
It fails at the last migration
Why does it happen?
When adding an index, activerecord checks if the index name exists and get the list of indexes defined on the table. Fetching the list of indexes is done in lib/active_record/connection_adapters/sqlite3/schema_statements.rb.
The query
PRAGMA index_list(table_name)
return one row per index, and each row is used to get the column name with aPRAGMA index_info(index_name)
. As the index is an expression index, the column name is nil.This is handled here:
https://github.com/rails/rails/blob/0d304eae601f085274b2e2c04316e025b443da62/activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rb#L24
https://github.com/rails/rails/blob/0d304eae601f085274b2e2c04316e025b443da62/activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rb#L32-L34
A regular expression is used to get the expression used to create the index from the SQL. The regexp is
/\bON\b\s*"?(\w+?)"?\s*\((?<expressions>.+?)\)(?:\s*WHERE\b\s*(?<where>.+))?\z/i
. This regexp does not match the trailing comment appended by marginalia, soexpressions
is set tonil
, socolumns
is set to nil too, and some code asks forcolumns.size
and produces the errorNoMethodError: undefined method 'size' for nil:NilClass
.How to fix?
That's where I need help. Options I've seen so far:
using
# /\bON\b\s*"?(\w+?)"?\s*\((?<expressions>.+?)\)(?:\s*WHERE\b\s*(?<where>.+))?(?:\s\/\*.*\*\/)?\z/i =~ index_sql
will work. I can issue a pull request against rails but it would not be fixed in the 6.0 branch.ActiveRecord::ConnectionAdapters::SQLite3::SchemaStatements#indexes
with the good regexp.I would prefer avoiding that.
Is it possible?
What do you recommend here?
The text was updated successfully, but these errors were encountered: