mysociety / alaveteli

Provide a Freedom of Information request system for your jurisdiction
https://alaveteli.org
Other
387 stars 195 forks source link

Make load-sample-data idempotent #1903

Open garethrees opened 9 years ago

garethrees commented 9 years ago

Running script/load-sample-data after it has already been run causes the task to exit with a horrible error. Its not immediately obvious that it's caused by the data already existing.

The simplest thing that comes to mind is a check along the lines of:

unless User.count.zero?
  puts <<-EOF
There is already data in your database so sample data cannot be loaded.
If you want to erase your data and start again, run the folowing:
    rake db:drop
    rake db:create
    script/load-sample-data
EOF
exit 1
end

Hi Gareth

This is after a (EC2) install and the "Next Steps" admin/superuser stuff:

This is what I get:

alaveteli@ip-172-31-15-58:~/alaveteli/config$ sudo su - alaveteli
[sudo] password for alaveteli: 
alaveteli@ip-172-31-15-58:~/alaveteli/config$ cd ..
alaveteli@ip-172-31-15-58:~/alaveteli$ script/load-sample-data
/home/alaveteli/bundle/ruby/1.8/gems/activerecord-3.2.19/lib/active_record/connection_adapters/postgresql_adapter.rb:650:in `async_exec': PG::Error: ERROR:  permission denied: "RI_ConstraintTrigger_16559" is a system trigger (ActiveRecord::StatementInvalid)
: ALTER TABLE "schema_migrations" ENABLE TRIGGER ALL;ALTER TABLE "info_requests" ENABLE TRIGGER ALL;ALTER TABLE "incoming_messages" ENABLE TRIGGER ALL;ALTER TABLE "public_body_versions" ENABLE TRIGGER ALL;ALTER TABLE "post_redirects" ENABLE TRIGGER ALL;ALTER TABLE "user_info_request_sent_alerts" ENABLE TRIGGER ALL;ALTER TABLE "outgoing_messages" ENABLE TRIGGER ALL;ALTER TABLE "has_tag_string_tags" ENABLE TRIGGER ALL;ALTER TABLE "acts_as_xapian_jobs" ENABLE TRIGGER ALL;ALTER TABLE "track_things" ENABLE TRIGGER ALL;ALTER TABLE "raw_emails" ENABLE TRIGGER ALL;ALTER TABLE "public_bodies" ENABLE TRIGGER ALL;ALTER TABLE "comments" ENABLE TRIGGER ALL;ALTER TABLE "track_things_sent_emails" ENABLE TRIGGER ALL;ALTER TABLE "info_request_events" ENABLE TRIGGER ALL;ALTER TABLE "users" ENABLE TRIGGER ALL;ALTER TABLE "mail_server_logs" ENABLE TRIGGER ALL;ALTER TABLE "holidays" ENABLE TRIGGER ALL;ALTER TABLE "public_body_translations" ENABLE TRIGGER ALL;ALTER TABLE "profile_photos" ENABLE TRIGGER ALL;ALTER TABLE "censor_rules" ENABLE TRIGGER ALL;ALTER TABLE "mail_server_log_dones" ENABLE TRIGGER ALL;ALTER TABLE "foi_attachments" ENABLE TRIGGER ALL;ALTER TABLE "purge_requests" ENABLE TRIGGER ALL;ALTER TABLE "request_classifications" ENABLE TRIGGER ALL;ALTER TABLE "spam_addresses" ENABLE TRIGGER ALL;ALTER TABLE "info_request_batches_public_bodies" ENABLE TRIGGER ALL;ALTER TABLE "info_request_batches" ENABLE TRIGGER ALL;ALTER TABLE "public_body_change_requests" ENABLE TRIGGER ALL
  from /home/alaveteli/bundle/ruby/1.8/gems/activerecord-3.2.19/lib/active_record/connection_adapters/postgresql_adapter.rb:650:in `execute'
  from /home/alaveteli/bundle/ruby/1.8/gems/activerecord-3.2.19/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `log'
  from /home/alaveteli/bundle/ruby/1.8/gems/activesupport-3.2.19/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  from /home/alaveteli/bundle/ruby/1.8/gems/activerecord-3.2.19/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
  from /home/alaveteli/bundle/ruby/1.8/gems/activerecord-3.2.19/lib/active_record/connection_adapters/postgresql_adapter.rb:649:in `execute'
  from /home/alaveteli/bundle/ruby/1.8/gems/activerecord-3.2.19/lib/active_record/connection_adapters/postgresql_adapter.rb:526:in `disable_referential_integrity'
  from /home/alaveteli/bundle/ruby/1.8/gems/activerecord-3.2.19/lib/active_record/fixtures.rb:476:in `create_fixtures'
  from /dev/stdin:15
  from /dev/stdin:14:in `each'
  from /dev/stdin:14
  from /home/alaveteli/bundle/ruby/1.8/gems/railties-3.2.19/lib/rails/commands.rb:64:in `eval'
  from /home/alaveteli/bundle/ruby/1.8/gems/railties-3.2.19/lib/rails/commands/runner.rb:52
  from /home/alaveteli/bundle/ruby/1.8/gems/railties-3.2.19/lib/rails/commands.rb:64:in `require'
  from /home/alaveteli/bundle/ruby/1.8/gems/railties-3.2.19/lib/rails/commands.rb:64
  from script/rails:6:in `require'
  from script/rails:6
Loaded fixtures. You may now wish to run script/update-xapian-index
alaveteli@ip-172-31-15-58:~/alaveteli$
crowbot commented 9 years ago

I think this is caused because the alaveteli user on EC2 doesn't have superuser permissions on the database, I don't think it's anything to do with load-sample-data already having been run. Renaming accordingly.

garethrees commented 9 years ago

Just a couple of thoughts off the top of my head…