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

Ensure db:seed runs before spree_sample:load #4907

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

lsizani
Copy link

@lsizani lsizani commented Feb 6, 2023

Summary

When running bundle exec rake spree_sample:load against an empty database, an exception is raised in db/samples/tax_rates.rb on account of missing a Spree::Zone with the name "North America.

In this PR we:

  • Add db:seed as a dependent task of spree_sample:load
  • Check that Spree::Zone "North America" is present in db/samples/tax_rates.rb before running the rest of the seed file. This makes it consistent with db/samples/shipping_methods.rb

Checklist

Check out our PR guidelines for more details.

The following are mandatory for all PRs:

The following are not always needed:

  • 📖 I have updated the README to account for my changes.
  • 📑 I have documented new code with YARD.
  • 🛣️ I have opened a PR to update the guides.
  • ✅ I have added automated tests to cover my changes.
  • 📸 I have attached screenshots to demo visual changes.

@lsizani lsizani requested a review from a team as a code owner February 6, 2023 15:28
@github-actions github-actions bot added changelog:solidus_core Changes to the solidus_core gem changelog:solidus_sample Changes to the solidus_sample gem labels Feb 6, 2023
Copy link
Contributor

@waiting-for-dev waiting-for-dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for your collaboration, @lsizani 🙌 🙌 It's a great and long-due improvement 🙂

I left some suggestions. We can probably take the occasion to leverage the new flexibility also in the installer by removing the && @load_seed_data check here:

@load_sample_data = options[:sample] && @run_migrations && @load_seed_data

core/app/models/spree/order_merger.rb Show resolved Hide resolved
sample/lib/spree/sample.rb Show resolved Hide resolved
sample/solidus_sample.gemspec Show resolved Hide resolved
sample/spec/lib/load_sample_spec.rb Outdated Show resolved Hide resolved
@lsizani lsizani force-pushed the make-sample-load-depend-on-seed branch from cecf524 to 858e37f Compare February 7, 2023 07:29
@github-actions github-actions bot removed the changelog:solidus_core Changes to the solidus_core gem label Feb 7, 2023
Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I left a comment. Also, I'm curious about what happens if db:seed and sample:load are run independently, one after the other. Is db:seed executed twice in that case?

sample/db/samples/tax_rates.rb Outdated Show resolved Hide resolved
@lsizani
Copy link
Author

lsizani commented Feb 7, 2023

Thanks! I left a comment. Also, I'm curious about what happens if db:seed and sample:load are run independently, one after the other. Is db:seed executed twice in that case?

It will run db:seed twice in that case. I checked on this and I can confirm that the current seeds can be run multiple times without raising errors or duplicating data.

@lsizani lsizani force-pushed the make-sample-load-depend-on-seed branch from 858e37f to f20a8b1 Compare February 8, 2023 06:54
@github-actions github-actions bot added the changelog:solidus_core Changes to the solidus_core gem label Feb 8, 2023
@lsizani lsizani requested review from waiting-for-dev and kennyadsl and removed request for waiting-for-dev and kennyadsl February 8, 2023 07:01
@kennyadsl
Copy link
Member

@lsizani do you know if we have any spec that verify we can run seeds multiple times without breaking or creating duplicate records?

@lsizani
Copy link
Author

lsizani commented Feb 8, 2023

@lsizani do you know if we have any spec that verify we can run seeds multiple times without breaking or creating duplicate records?

We do not... But I'm sure I can write one

@lsizani lsizani force-pushed the make-sample-load-depend-on-seed branch 3 times, most recently from bb272d4 to e191991 Compare February 9, 2023 17:06
@github-actions github-actions bot added the changelog:solidus Changes to the solidus meta-gem label Feb 9, 2023
Copy link
Member

@adammathys adammathys left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Great change! LGTM!

Copy link
Contributor

@waiting-for-dev waiting-for-dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, it's perfect! ❤️ Last ask, could you please rebase from master so that CI runs? 🙏

@lsizani lsizani force-pushed the make-sample-load-depend-on-seed branch 2 times, most recently from 397224f to 320235f Compare February 10, 2023 10:50
Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed that with this change in the solidus installer, we are executing seeds twice: once during the seeds step and another during the samples step.

I think this is not ideal, no matter if it's safe to run seeds multiple times. Maybe we can merge populate_seed_data and load_sample_data to call a single rake command in case we want both?

@lsizani lsizani force-pushed the make-sample-load-depend-on-seed branch 2 times, most recently from 6a4fdff to 64a4c0f Compare February 16, 2023 08:07
@lsizani lsizani force-pushed the make-sample-load-depend-on-seed branch from 64a4c0f to a14fcd0 Compare February 16, 2023 16:36
@kennyadsl
Copy link
Member

Hey @lsizani! I lost track of this PR, I didn't notice you updated it following my suggestions. I will re-review it now, in the meantime there are some conflict with the main branch to address, if you have time for a rebase. Thanks again!

Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some questions, thanks again Lunga!

describe "Load seeds multiple times" do
it "doesn't duplicate records" do
4.times do
pid = fork { Spree::Core::Engine.load_seed }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this local variable be overridden at each cycle? Might this interfere with the process killing in the ensure block (it only always kills the last process but previous ones might still be executing)?

@@ -3,8 +3,11 @@
begin
north_america = Spree::Zone.find_by!(name: "North America")
rescue ActiveRecord::RecordNotFound
puts "Couldn't find 'North America' zone. Did you run `rake db:seed` first?"
puts "That task will set up the countries, states and zones required for Spree."
puts <<~TEXT
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this DX, thanks!

expect {
Spree::Core::Engine.load_seed
expect do
pid = fork { Spree::Core::Engine.load_seed }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need a separate process here instead of just running load_seed and load_sample one after the other?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog:solidus_core Changes to the solidus_core gem changelog:solidus_sample Changes to the solidus_sample gem changelog:solidus Changes to the solidus meta-gem
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants