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

Add scripts to save and restore baseline data #12947

Merged
merged 3 commits into from
Oct 31, 2024

Conversation

mkllnk
Copy link
Member

@mkllnk mkllnk commented Oct 25, 2024

What? Why?

Sometimes a staging database gets changed by pull requests which are not finished yet. Those changes should be reverted and some of these changes may actually break the application. So here are a couple of scripts for testers to execute on the server.

  • Save baseline data when you have a good pull request that gets merged and you want to keep your test data.
  • Restore baseline data after you tested a pull request with migrations and that pull request is not merged as is. You will lose test data.
  • Restore baseline data after you've done lots of destructive testing, deleting records that you would like back.

What should we test?

# Log into the staging server with ssh and run these:
~/apps/openfoodnetwork/current/script/ci/save-staging-baseline.sh
~/apps/openfoodnetwork/current/script/ci/restore-staging-baseline.sh

Release notes

Changelog Category (reviewers may add a label for the release notes):

  • User facing changes
  • API changes (V0, V1, DFC or Webhook)
  • Technical changes only
  • Feature toggled

The title of the pull request will be included in the release notes.

Dependencies

Documentation updates

We could delete them all but I want use some of their wisdom for new CI
scripts.
@mkllnk mkllnk added the technical changes only These pull requests do not contain user facing changes and are grouped in release notes label Oct 25, 2024
@mkllnk mkllnk self-assigned this Oct 25, 2024
@mkllnk mkllnk marked this pull request as ready for review October 25, 2024 04:13
Copy link
Collaborator

@rioug rioug left a comment

Choose a reason for hiding this comment

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

Looks good 👍

script/ci/restore-staging-baseline.sh Show resolved Hide resolved
Copy link
Member

@dacook dacook left a comment

Choose a reason for hiding this comment

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

Good one 👍
I like that it only ever stores one copy, and that it's compressed so we don't need to worry about disk usage ballooning out.

I have a couple of concerns though, can you please take a look?

script/ci/restore-staging-baseline.sh Show resolved Hide resolved
exit 1
fi

dropdb -h localhost -U "$DB_USER" "$DB_DATABASE" --if-exists
Copy link
Member

Choose a reason for hiding this comment

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

I think postgres blocks you from dropping a db if there's still active connections, do we need to stop puma and sidekiq?

Copy link
Member

Choose a reason for hiding this comment

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

Note, I had an issue before when Metabase was also connected, remotely. We have a solution to force close the connection here:
https://github.com/openfoodfoundation/ofn-install/blob/master/roles/db_restore/tasks/restore_database.yml#L15-L20

@mkllnk mkllnk requested a review from dacook October 29, 2024 02:00
Copy link
Member

@dacook dacook left a comment

Choose a reason for hiding this comment

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

👍

@filipefurtad0
Copy link
Contributor

Hey @mkllnk ,

Thanks for this. Maybe a good way to verify all works as expected, could be to:

  • Stage this PR, then Log into the staging server with ssh and save the baseline, by running:
    ~/apps/openfoodnetwork/current/script/ci/save-staging-baseline.sh

Then, I think we could test for two distinct scenarios:
i) create some test data - like a new order or user
ii) run a migration which could break things somehow, or just change the DB structure somehow

Then finally, run
~/apps/openfoodnetwork/current/script/ci/restore-staging-baseline.sh

And verify changes i) and ii) are correctly reverted.

I'll give this a try.

@filipefurtad0 filipefurtad0 self-assigned this Oct 31, 2024
@filipefurtad0 filipefurtad0 added pr-staged-uk staging.openfoodnetwork.org.uk pr-staged-fr staging.coopcircuits.fr and removed pr-staged-uk staging.openfoodnetwork.org.uk labels Oct 31, 2024
@filipefurtad0
Copy link
Contributor

Staging failed in staging-UK:

https://github.com/openfoodfoundation/openfoodnetwork/actions/runs/11620487686

But worked for staging-FR:

https://github.com/openfoodfoundation/openfoodnetwork/actions/runs/11620645440

Running the command for saving the baseline, took a couple of minutes to complete, and yielded:

Last login: Wed Oct 30 21:50:00 2024 from 220.253.199.227
openfoodnetwork@vps-2a25f087:~$ ~/apps/openfoodnetwork/current/script/ci/save-staging-baseline.sh
Staging baseline data saved.

Making changes:

  • created an order -> https://staging.coopcircuits.fr/orders/R555075285?order_token=22284cd168ce8d3a
  • removed a migration -> down 20240828203544 Fully enable admin style v3 flag

After running the restore command:

Yielded quite a lot of output, among which these lines:

ERROR:  permission denied to create extension "pg_stat_statements"
HINT:  Must be superuser to create this extension.
ERROR:  extension "pg_stat_statements" does not exist

And ended with:

ERROR:  must be member of role "postgres"
ERROR:  must be member of role "postgres"
ERROR:  must be member of role "postgres"
ERROR:  must be member of role "postgres"
ERROR:  must be member of role "postgres"
ERROR:  must be member of role "postgres"
Staging baseline data restored.

Checked that:

  • I should not be able to access the order confirmation page -> https://staging.coopcircuits.fr/orders/R555075285?order_token=22284cd168ce8d3a -> this now throws an error 500, I'd say this is expected
  • the changed migration should be up again -> up 20240828203544 Fully enable admin style v3 flag

Looking great!! 🎉

Merging.

@filipefurtad0 filipefurtad0 removed the pr-staged-fr staging.coopcircuits.fr label Oct 31, 2024
@filipefurtad0 filipefurtad0 merged commit 996d2f0 into openfoodfoundation:master Oct 31, 2024
54 of 55 checks passed
@mkllnk mkllnk deleted the staging-baseline branch November 1, 2024 02:51
filipefurtad0 added a commit to filipefurtad0/openfoodnetwork that referenced this pull request Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
technical changes only These pull requests do not contain user facing changes and are grouped in release notes
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Staging breaks when conflicting database migrations are deployed
4 participants