Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

load_tasks loads the active record database tasks twice causing them to execute twice in certain scenarios #178

Open
dat2 opened this issue Mar 15, 2023 · 1 comment

Comments

@dat2
Copy link

dat2 commented Mar 15, 2023

I've noticed that when I have a simple Rakefile like this

Bundler.require(:default)
StandaloneMigrations::Tasks.load_tasks

Running the command bin/rake db:schema:load with my structure.sql will fail with confusing errors, because my structure.sql file does not work when it's run twice.

I discovered these two lines below cause the db:schema:load task to be defined twice in rake, and thus rake executes them twice.

MinimalRailtieConfig.load_tasks

load "active_record/railties/databases.rake"

I was able to prevent it locally with the following monkey patch.

module Kernel
  alias old_load load

  def load(*args, **kwargs)
    return old_load(*args, **kwargs) unless args[0] == "active_record/railties/databases.rake"
    @@load ||= old_load(*args, **kwargs)
  end
end

According to rake, executing tasks twice when defining them twice is intended behaviour: ruby/rake#228 (comment)

@dat2
Copy link
Author

dat2 commented Mar 28, 2023

I finally figured this out. The issue boils down to incompatibility with the strong_migrations gem. Here's a very simple Rakefile to reproduce the issue.

require 'rails' # need to make sure defined?(Rails) is true for strong_migrations
require 'strong_migrations'
require 'standalone_migrations'

StandaloneMigrations::Tasks.load_tasks

strong_migrations through a series of require s calls load "active_record/railties/databases.rake".

https://github.com/ankane/strong_migrations/blob/1146d6836bda6c1d8b51287bba73613edfce9e82/lib/strong_migrations/railtie.rb#L2
https://github.com/rails/rails/blob/784f1c75a08b3b3611b4f100a890c26ace493138/activerecord/lib/active_record/railtie.rb#L57

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

No branches or pull requests

1 participant