Conversation
…Record When DataMigrate::DatabaseTasks extends ActiveRecord::Tasks::DatabaseTasks, the mattr_accessor methods (structure_dump_flags and structure_load_flags) are not properly inherited because they store values on the original module. This causes a NameError when db:prepare:with_data calls dump_schema, which internally calls structure_dump_flags_for that tries to access these accessors: NameError: undefined local variable or method 'structure_dump_flags' for module primary This fix adds delegation methods that forward calls to the original ActiveRecord::Tasks::DatabaseTasks module. Fixes issue when using db:prepare:with_data with structure.sql format in Rails 7.2+.
|
Hi Granti! Thanks a lot for fixing this. it looks like when a data_schema.rb file is present, the current solution explodes? Just wanted to report it, seems its limited to the codepath when using I'm pretty sure it would still be broken without your patch. |
When schema_format is :sql, prepare was loading data_schema.rb through structure_load. Delegate AR load/dump to ActiveRecord::Tasks::DatabaseTasks and always load data_schema as ruby. Co-authored-by: Cursor <cursoragent@cursor.com>
houndci-bot
left a comment
There was a problem hiding this comment.
Some files could not be reviewed due to errors:
.rubocop.yml: Lint/BlockAlignment has the wrong namespace - should be Layout
.rubocop.yml: Lint/BlockAlignment has the wrong namespace - should be Layout .rubocop.yml: Lint/ConditionPosition has the wrong namespace - should be Layout Warning: unrecognized cop Lint/LiteralInCondition found in .rubocop.yml Error: Unknown Ruby version 3.1 found in `TargetRubyVersion` parameter (in .rubocop.yml). Supported versions: 2.1, 2.2, 2.3, 2.4, 2.5
|
Hi Vincent, Thank you for testing this against your test bed, and for the clear repro. That was very helpful. You were right: the parse error is a separate bug from the I have pushed a follow-up commit that keeps the two schemas separate, in line with
If you have a moment to re-run Thanks again, |
Hound was failing because .hound.yml used the old ruby: key and default RuboCop 0.54 cannot parse TargetRubyVersion 3.1 or the renamed cops in .ruby-style.yml. Pin Hound to RuboCop 1.22.1, modernize the style file, and extract prepare schema helpers so new code stays within Metrics limits. Co-authored-by: Cursor <cursoragent@cursor.com>
houndci-bot
left a comment
There was a problem hiding this comment.
Some files could not be reviewed due to errors:
Configuration file not found: .ruby-style.yml
Configuration file not found: .ruby-style.yml
Fix: delegate structure_dump_flags and structure_load_flags to ActiveRecord
Problem
When running
rails db:prepare:with_datawithstructure.sqlformat on Rails 7.2+, the following error occurs:Root Cause
DataMigrate::DatabaseTasksusesextend ActiveRecord::Tasks::DatabaseTasksto copy methods from ActiveRecord. However,structure_dump_flagsandstructure_load_flagsare defined asmattr_accessoron the original module, which stores values onActiveRecord::Tasks::DatabaseTasksitself.When
dump_schemais called fromDataMigrate::DatabaseTasks, it internally callsstructure_dump_flags_for, which tries to accessstructure_dump_flags- but that accessor doesn't exist onDataMigrate::DatabaseTasks.Solution
Add delegation methods that forward calls to the original
ActiveRecord::Tasks::DatabaseTasksmodule:Testing
Reproduction Steps
config.active_record.schema_format = :sqldata_migrategemrails db:prepare:with_dataAfter this fix, the command completes successfully.