Skip to content

Fix: delegate structure_dump_flags and structure_load_flags to ActiveRecord#371

Open
granti wants to merge 1 commit intoilyakatz:mainfrom
granti:fix/rails-72-structure-dump-flags-error
Open

Fix: delegate structure_dump_flags and structure_load_flags to ActiveRecord#371
granti wants to merge 1 commit intoilyakatz:mainfrom
granti:fix/rails-72-structure-dump-flags-error

Conversation

@granti
Copy link
Copy Markdown

@granti granti commented Apr 16, 2026

Fix: delegate structure_dump_flags and structure_load_flags to ActiveRecord

Problem

When running rails db:prepare:with_data with structure.sql format on Rails 7.2+, the following error occurs:

NameError: undefined local variable or method 'structure_dump_flags' for module primary

          if structure_dump_flags.is_a?(Hash)
             ^^^^^^^^^^^^^^^^^^^^
Did you mean?  structure_dump_flags_for

Root Cause

DataMigrate::DatabaseTasks uses extend ActiveRecord::Tasks::DatabaseTasks to copy methods from ActiveRecord. However, structure_dump_flags and structure_load_flags are defined as mattr_accessor on the original module, which stores values on ActiveRecord::Tasks::DatabaseTasks itself.

When dump_schema is called from DataMigrate::DatabaseTasks, it internally calls structure_dump_flags_for, which tries to access structure_dump_flags - but that accessor doesn't exist on DataMigrate::DatabaseTasks.

Solution

Add delegation methods that forward calls to the original ActiveRecord::Tasks::DatabaseTasks module:

def self.structure_dump_flags
  ActiveRecord::Tasks::DatabaseTasks.structure_dump_flags
end

def self.structure_load_flags
  ActiveRecord::Tasks::DatabaseTasks.structure_load_flags
end

Testing

  • Added 5 new tests for the delegation methods
  • Verified all 75 tests pass on Rails 7.2, 8.0, and 8.1
  • Tests cover:
    • Proper delegation to ActiveRecord::Tasks::DatabaseTasks
    • Handling nil values
    • No NameError when accessing the flags

Reproduction Steps

  1. Create a Rails 7.2+ app with config.active_record.schema_format = :sql
  2. Add data_migrate gem
  3. Run rails db:prepare:with_data
  4. Observe NameError

After this fix, the command completes successfully.

…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+.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant