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

Chore/new seeds #479

Merged
merged 16 commits into from
Jan 19, 2024
Merged

Chore/new seeds #479

merged 16 commits into from
Jan 19, 2024

Conversation

tsubik
Copy link
Collaborator

@tsubik tsubik commented Jan 8, 2024

Old seeds did not work. Instead of fixing them, I decided to use a subset of anonymized production data in the form of fixtures. Why fixtures? Because loading up fixtures in Rails is very quick and well-supported.

This will also allow using better test data for e2e testing in both Portal and Observation Tool applications. For this, I also added custom e2e environment and new rake tasks (e2e.rake). Recreating database between tests is much quicker now.

To recreate fixtures, you have to load production database locally and run:

rails seeds:generate_fixtures

Copy link
Contributor

@santostiago santostiago left a comment

Choose a reason for hiding this comment

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

Looks great.
When do you want to load the fixtures? To set up the dev environment?

@@ -38,7 +38,7 @@ class User < ApplicationRecord
# Include default devise modules.
TEMP_EMAIL_REGEX = /\Achange@tmp/
PERMISSIONS = %w[operator ngo ngo_manager government]
PROTECTED_NICKNAMES = %w[admin superuser about root fti otp faq contact user operator ngo].freeze

Copy link
Contributor

Choose a reason for hiding this comment

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

ehehe.. this thing was already there when I grabbed the project (like, a month after it started) and I always wanted to removed it and never did.
Now it's done :D

@@ -0,0 +1,85 @@
require "active_record/fixtures"
Copy link
Contributor

Choose a reason for hiding this comment

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

I have no idea what this file does :D

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sorry, I will add additional comment why it's here. When creating and then loading fixtures, I had foreign key exceptions, but in Rails 7 the exception has no details. In rails 7.1 this is fixed and you know which object is causing those errors, the error message is a way better. That's why I took that fragment of code from Rails 7.1 in form of extension here.


private

def drop_e2e_db_template
Copy link
Contributor

Choose a reason for hiding this comment

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

mmm.. cool.
How do the PG templates work?

Copy link
Collaborator Author

@tsubik tsubik Jan 19, 2024

Choose a reason for hiding this comment

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

I would say creating a database from template is like restoring from a backup but a way way faster. I can now in frontend applications after certain tests run those rake tasks and restore db to initial state with tests data in 1 or 2 seconds.

namespace :seeds do
desc "Generate fixtures to use as development or e2e database. First run db:restore_from_file or db:restore_from_server to get latest data from production."
task generate_fixtures: :environment do
generate_for_model "Country"
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe save an array of models and iterate over them calling generate_for_model?

Copy link
Contributor

Choose a reason for hiding this comment

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

mmm... ok, I just saw later in the code that it won't work for all.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, I started as you said, with array of models, then that changed to hash, but ended with this as this is the most flexible way.

@tsubik tsubik merged commit 5eefd20 into develop Jan 19, 2024
3 checks passed
@tsubik tsubik deleted the chore/new-seeds branch January 19, 2024 16:22
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.

None yet

2 participants