1 class DatabaseController < ApplicationController
2 skip_before_filter :find_object_by_uuid
3 skip_before_filter :render_404_if_no_object
4 before_filter :admin_required
6 raise ArvadosModel::PermissionDeniedError unless Rails.env == 'test'
8 # Sanity check: If someone has actually logged in here, this might
9 # not really be a throwaway database. Client test suites should
10 # use @example.com email addresses when creating user records, so
11 # we can tell they're not valuable.
13 where('email is null or email not like ?', '%@example.com').
16 YAML::load_file(File.expand_path('../../../test/fixtures/users.yml',
18 values.collect { |u| u['uuid'] }
19 unexpected_uuids = user_uuids - fixture_uuids
20 if unexpected_uuids.any?
21 logger.error("Running in test environment, but non-fixture users exist: " +
22 "#{unexpected_uuids}")
23 raise ArvadosModel::PermissionDeniedError
26 require 'active_record/fixtures'
28 # What kinds of fixtures do we have?
29 fixturesets = Dir.glob(Rails.root.join('test', 'fixtures', '*.yml')).
30 collect { |yml| yml.match(/([^\/]*)\.yml$/)[1] }
32 table_names = '"' + ActiveRecord::Base.connection.tables.join('","') + '"'
36 ActiveRecord::Base.transaction do
37 # Avoid deadlock by locking all tables before doing anything
39 ActiveRecord::Base.connection.execute \
40 "LOCK TABLE #{table_names} IN ACCESS EXCLUSIVE MODE"
42 # Delete existing fixtures (and everything else) from fixture
44 fixturesets.each do |x|
45 x.classify.constantize.unscoped.delete_all
48 # create_fixtures() is a no-op for cached fixture sets, so
50 ActiveRecord::Fixtures.reset_cache
51 ActiveRecord::Fixtures.
52 create_fixtures(Rails.root.join('test', 'fixtures'), fixturesets)
54 # Dump cache of permissions etc.
56 ActiveRecord::Base.connection.clear_query_cache
58 # Reload database seeds
61 rescue ActiveRecord::StatementInvalid => e
62 if "#{e.inspect}" =~ /deadlock detected/i and (attempts_left -= 1) > 0
63 logger.info "Waiting for lock -- #{e.inspect}"
71 send_json success: true