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

Seeding scripts #2330

Merged
merged 5 commits into from
Feb 8, 2023
Merged

Seeding scripts #2330

merged 5 commits into from
Feb 8, 2023

Conversation

pbanaszkiewicz
Copy link
Contributor

@pbanaszkiewicz pbanaszkiewicz commented Feb 6, 2023

This fixes #2239 by providing an alternative to fixtures from Django.

Seeding scripts give more control to the developer. This way it was possible to implement a check before given entity gets created.


Copy link
Contributor

@elichad elichad left a comment

Choose a reason for hiding this comment

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

The deprecate_models method doesn't work for me if I add names to DEPRECATED_X variables - suggestions should fix it. Looks good otherwise.

Does the seeding get run automatically during the deployment process?

_info(f"Start of {class_name} deprecation.")

for i, model_id in enumerate(model_id_list):
if not model_class._default_manager.filter({lookup_field: model_id}).exists():
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if not model_class._default_manager.filter({lookup_field: model_id}).exists():
if not model_class._default_manager.filter(**{lookup_field: model_id}).exists():

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! Thanks, fixing now.

continue

_info(f"{i} {class_name} <{model_id}> exists, removing.")
model_class._default_manager.filter({lookup_field: model_id}).delete()
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
model_class._default_manager.filter({lookup_field: model_id}).delete()
model_class._default_manager.filter(**{lookup_field: model_id}).delete()

@elichad
Copy link
Contributor

elichad commented Feb 8, 2023

If we wanted to update an existing entry (e.g. change the text of an email template), how would we do that with this setup?

@pbanaszkiewicz
Copy link
Contributor Author

@elichad

Does the seeding get run automatically during the deployment process?

Yes, it's part of Dockerfile CMD. When container starts up, start.sh script is invoked. This script runs some commands before starting a webserver:

  1. checks
  2. migration
  3. createcachetable
  4. now seeding
  5. superuser create (in another PR)
  6. finally a webserver.

If we wanted to update an existing entry (e.g. change the text of an email template), how would we do that with this setup?

That's a great question. It should be agreed with product users that they accept potential overwrite of their changes. In this case, I assumed that no changes to existing email templates should be lost as a result of seeding.

I'm using a very similar setup to update entries during seeding, the change lies in these lines (seed_models()):

        if model_class._default_manager.filter(**{lookup_field: model_id}).exists():
            _info(f"{i} {class_name} <{model_id}> already exists, skipping.")
            continue

When existing object is found, the code makes an update of specific fields from model definition instead of skipping.

@pbanaszkiewicz pbanaszkiewicz merged commit c898f0d into develop Feb 8, 2023
CI/CD for development branch automation moved this from In progress to Done Feb 8, 2023
@pbanaszkiewicz pbanaszkiewicz deleted the feature/2239-seeding-scripts branch February 10, 2023 20:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

Create script to apply only new data from fixtures
2 participants