Skip to content

Commit

Permalink
update rake task to accomodate rails 5.1 to 5.2 change of db:load_con…
Browse files Browse the repository at this point in the history
…fig sending new prereqs

CI test workflows and local dev setup requires db:create rake task. However in Rails 5.2, there was an introduction of new behavior of db:load_config (a task within db:create) which causes db:create to fail. (This is a known issue from Rails and behavior introduced after 5.1.7).

To fix, we delete the new environment prerequisite of db:load_config, (db:load_config depending on environment means the app would load with all observers, sphinx indices, concerns etc which trigger a db connection, BUT can't be established because the DB does not exist yet.).

However, just removing the prerequisite, will get db:create to work, but loading schema will stop working because db:load_config needs that prereq and tehrefore we bring it back after db:create, knowing we have a db to connect to.
  • Loading branch information
yuenmichelle1 committed Oct 22, 2024
1 parent e8b3516 commit b44f92b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 37 deletions.
53 changes: 16 additions & 37 deletions Gemfile.next.lock
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,12 @@ GEM
aws-sdk-resources (2.3.23)
aws-sdk-core (= 2.3.23)
base64 (0.2.0)
benchmark-ips (2.13.0)
benchmark-ips (2.14.0)
bigdecimal (3.1.8)
builder (3.3.0)
codeclimate-test-reporter (0.6.0)
simplecov (>= 0.7.1, < 1.0.0)
coderay (1.1.3)
colorize (1.1.0)
concurrent-ruby (1.3.4)
congestion (0.1.0)
connection_pool (>= 2.0)
Expand All @@ -82,8 +81,6 @@ GEM
date (3.3.4)
diff-lcs (1.5.1)
docile (1.1.5)
domain_name (0.5.20190701)
unf (>= 0.0.5, < 1.0.0)
erubi (1.13.0)
et-orbi (1.2.11)
tzinfo
Expand All @@ -92,7 +89,7 @@ GEM
factory_bot_rails (5.2.0)
factory_bot (~> 5.2.0)
railties (>= 4.2.0)
faraday (1.10.3)
faraday (1.10.4)
faraday-em_http (~> 1.0)
faraday-em_synchrony (~> 1.0)
faraday-excon (~> 1.1)
Expand All @@ -115,11 +112,11 @@ GEM
faraday-patron (1.0.0)
faraday-rack (1.0.0)
faraday-retry (1.0.3)
faraday_middleware (1.2.0)
faraday_middleware (1.2.1)
faraday (~> 1.0)
ffi (1.16.3)
formatador (1.1.0)
fugit (1.11.0)
fugit (1.11.1)
et-orbi (~> 1, >= 1.2.11)
raabro (~> 1.4)
globalid (1.1.0)
Expand All @@ -140,9 +137,6 @@ GEM
rspec (>= 2.99.0, < 4.0)
hashdiff (1.1.1)
honeybadger (4.5.6)
http-accept (1.7.0)
http-cookie (1.0.6)
domain_name (~> 0.5)
i18n (1.14.6)
concurrent-ruby (~> 1.0)
its-it (1.3.0)
Expand Down Expand Up @@ -186,9 +180,6 @@ GEM
net-smtp
marcel (1.0.4)
method_source (1.1.0)
mime-types (3.5.2)
mime-types-data (~> 3.2015)
mime-types-data (3.2024.0806)
mini_mime (1.1.5)
mini_portile2 (2.8.7)
minitest (5.25.1)
Expand All @@ -207,8 +198,7 @@ GEM
timeout
net-smtp (0.5.0)
net-protocol
netrc (0.11.0)
newrelic_rpm (9.12.0)
newrelic_rpm (9.14.0)
nio4r (2.7.3)
nokogiri (1.13.10)
mini_portile2 (~> 2.8.0)
Expand All @@ -221,13 +211,13 @@ GEM
coderay (~> 1.1)
method_source (~> 1.0)
public_suffix (5.1.1)
puma (6.4.2)
puma (6.4.3)
nio4r (~> 2.0)
pundit (1.1.0)
activesupport (>= 3.0.0)
raabro (1.4.0)
racc (1.8.1)
rack (2.2.9)
rack (2.2.10)
rack-cors (1.0.6)
rack (>= 1.6.0)
rack-protection (3.2.0)
Expand Down Expand Up @@ -260,33 +250,28 @@ GEM
method_source
rake (>= 0.8.7)
thor (>= 0.19.0, < 2.0)
rainbow (3.0.0)
rake (13.2.1)
rb-fsevent (0.11.2)
rb-inotify (0.11.1)
ffi (~> 1.0)
redis (3.3.5)
request_store (1.7.0)
rack (>= 1.4)
rest-client (2.1.0)
http-accept (>= 1.7.0, < 2.0)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 4.0)
netrc (~> 0.8)
rexml (3.3.5)
strscan
rexml (3.3.8)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.0)
rspec-core (3.13.2)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.1)
rspec-expectations (3.13.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-its (1.3.0)
rspec-core (>= 3.0.0)
rspec-expectations (>= 3.0.0)
rspec-mocks (3.13.1)
rspec-mocks (3.13.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-rails (4.1.2)
Expand Down Expand Up @@ -342,22 +327,16 @@ GEM
actionpack (>= 5.2)
activesupport (>= 5.2)
sprockets (>= 3.0.0)
strscan (3.1.0)
ten_years_rails (0.2.0)
actionview
activesupport
colorize (>= 0.8.1)
rest-client (>= 2.0.2)
ten_years_rails (1.0.2)
actionview (~> 5.2.3)
rainbow (~> 3.0.0)
thor (1.3.2)
thread_safe (0.3.6)
timecop (0.9.10)
timeout (0.4.1)
tzinfo (1.2.11)
thread_safe (~> 0.1)
unf (0.1.4)
unf_ext
unf_ext (0.0.9.1)
webmock (3.23.1)
webmock (3.24.0)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
Expand Down
24 changes: 24 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
require File.expand_path('../config/application', __FILE__)

Rails.application.load_tasks

# In Rails 5.2 the `load_config` task was made dependent on `environment`
# to enable credentials reading, see https://github.com/rails/rails/pull/31135
# This causes the whole app to initialize before `db:create` and that
# causes a database connection for observers, sphinx, concerns, etc, that cant be established
# when DB does not exist yet.
# See SO: https://stackoverflow.com/questions/72147515/rails-5-2-load-order-breaks-dbcreate
# While clearing the environment prerequisite of load_config will help db:create
# and create the db, other tasks like db:schema:load and our panoptes talk rake tasks
# will actually need the prereq, and therefore we bring back the environment prereq
# for other rake tasks. We do this to keep our CI test workflows and local dev environment setup functional.

Rake::Task['db:load_config'].prerequisites.delete("environment")
Rake::Task.tasks.select { |task|
next if task.name == "db:create"
task.name.start_with?("db:") && task.prerequisites.include?("load_config")
}.each { |task|
task.prerequisites.insert(task.prerequisites.index("load_config"), "environment")
}
Rake::Task.tasks.select { |task|
task.prerequisites.include?("db:load_config")
}.each { |task|
task.prerequisites.insert(task.prerequisites.index("db:load_config"), "environment")
}
1 change: 1 addition & 0 deletions app/models/subject.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Subject < ApplicationRecord
self.primary_key = :id
include Focusable
belongs_to :project
end

0 comments on commit b44f92b

Please sign in to comment.