foodcoops / foodsoft

Web-based software to manage a non-profit food coop (product catalog, ordering, accounting, job scheduling).
https://foodcoops.net/
Other
326 stars 146 forks source link

Dev seed (small.en) leads to error #1049

Closed twothreenine closed 7 months ago

twothreenine commented 7 months ago

When I try to set Foodsoft up for development via Docker and run

docker compose -f docker-compose-dev.yml run --rm foodsoft \
  bundle exec rake db:schema:load db:seed:small.en

I get

ActiveRecord::InvalidForeignKey: Mysql2::Error: Cannot delete or update a parent row: a foreign key constraint fails

This can be solved by:

  1. skipping this step and starting the container, accessing http://localhost:2080 (phpMyAdmin) and deleting all tables (development -> select all tables -> delete)
  2. There's an error that active_storage_blobs couldn't be deleted (similar error as above), trying again to delete all tables succeeds ... and then I forgot how we solved it
yksflip commented 7 months ago

yeah I can reproduce this error. Look's like that the FinancialTransactionClass is already created somewhere else and then this fails in the seed as the key/name is already taken

lentschi commented 7 months ago

It seems that rake db:schema:load is not intended to always succeed for existing dbs - see discussion here: https://github.com/rails/rails/issues/41491

Since rake foodsoft:setup_development_docker already ran rake db:setup (which includes schema loading and minimal seeding) it cannot be run again without first emptying the db.

yksflip commented 7 months ago

ah okay, so we could either try to rewrite the seeds to check whether these initial records already exists (find_or_create_by doesn't work with the uniqueness constraint somehow rails#36027, but first_or_create worked for me) or we add a note to the docs that you'd have to recreate a plain database first before loading seeds

lentschi commented 7 months ago

or we add a note to the docs that you'd have to recreate a plain database first before loading seeds

Seems like the easiest path, however...: rake db:drop db:create db:schema:load db:seed:small.en fails as db:create doesn't work in our current docker setup (the connection string includes the development db). So the user would always have a manual step here.

So maybe your other approach would be the easiest way to fix it.