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 # Don't reset keep_services: clients need to discover our
33 # integration-testing keepstores, not test fixtures.
34 fixturesets -= %w[keep_services]
36 table_names = '"' + ActiveRecord::Base.connection.tables.join('","') + '"'
40 ActiveRecord::Base.transaction do
41 # Avoid deadlock by locking all tables before doing anything
43 ActiveRecord::Base.connection.execute \
44 "LOCK TABLE #{table_names} IN ACCESS EXCLUSIVE MODE"
46 # Delete existing fixtures (and everything else) from fixture
48 fixturesets.each do |x|
49 x.classify.constantize.unscoped.delete_all
52 # create_fixtures() is a no-op for cached fixture sets, so
54 ActiveRecord::FixtureSet.reset_cache
55 ActiveRecord::FixtureSet.
56 create_fixtures(Rails.root.join('test', 'fixtures'), fixturesets)
58 # Dump cache of permissions etc.
60 ActiveRecord::Base.connection.clear_query_cache
62 # Reload database seeds
65 rescue ActiveRecord::StatementInvalid => e
66 if "#{e.inspect}" =~ /deadlock detected/i and (attempts_left -= 1) > 0
67 logger.info "Waiting for lock -- #{e.inspect}"
75 send_json success: true