X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/c0d9c656eefcb684c72fd2a1bca20b8b9c60df96..85c13201abf4a9f19dd112992a04fa65da63369c:/services/api/app/controllers/database_controller.rb diff --git a/services/api/app/controllers/database_controller.rb b/services/api/app/controllers/database_controller.rb index 4a872ebb02..dddc3400b4 100644 --- a/services/api/app/controllers/database_controller.rb +++ b/services/api/app/controllers/database_controller.rb @@ -1,9 +1,31 @@ +# Copyright (C) The Arvados Authors. All rights reserved. +# +# SPDX-License-Identifier: AGPL-3.0 + class DatabaseController < ApplicationController skip_before_filter :find_object_by_uuid skip_before_filter :render_404_if_no_object before_filter :admin_required def reset - raise ArvadosModel::PermissionDeniedError unless ENV['RAILS_ENV'] == 'test' + raise ArvadosModel::PermissionDeniedError unless Rails.env == 'test' + + # Sanity check: If someone has actually logged in here, this might + # not really be a throwaway database. Client test suites should + # use @example.com email addresses when creating user records, so + # we can tell they're not valuable. + user_uuids = User. + where('email is null or email not like ?', '%@example.com'). + collect(&:uuid) + fixture_uuids = + YAML::load_file(File.expand_path('../../../test/fixtures/users.yml', + __FILE__)). + values.collect { |u| u['uuid'] } + unexpected_uuids = user_uuids - fixture_uuids + if unexpected_uuids.any? + logger.error("Running in test environment, but non-fixture users exist: " + + "#{unexpected_uuids}") + raise ArvadosModel::PermissionDeniedError + end require 'active_record/fixtures' @@ -11,26 +33,53 @@ class DatabaseController < ApplicationController fixturesets = Dir.glob(Rails.root.join('test', 'fixtures', '*.yml')). collect { |yml| yml.match(/([^\/]*)\.yml$/)[1] } - # Delete existing fixtures (and everything else) from fixture - # tables - fixturesets.each do |x| - x.classify.constantize.unscoped.delete_all - end + # Don't reset keep_services: clients need to discover our + # integration-testing keepstores, not test fixtures. + fixturesets -= %w[keep_services] + + table_names = '"' + ActiveRecord::Base.connection.tables.join('","') + '"' - # create_fixtures() is a no-op for cached fixture sets, so uncache - # them all. - ActiveRecord::Fixtures.reset_cache - ActiveRecord::Fixtures. - create_fixtures(Rails.root.join('test', 'fixtures'), fixturesets) + attempts_left = 20 + begin + ActiveRecord::Base.transaction do + # Avoid deadlock by locking all tables before doing anything + # drastic. + ActiveRecord::Base.connection.execute \ + "LOCK TABLE #{table_names} IN ACCESS EXCLUSIVE MODE" + + # Delete existing fixtures (and everything else) from fixture + # tables + fixturesets.each do |x| + x.classify.constantize.unscoped.delete_all + end + + # create_fixtures() is a no-op for cached fixture sets, so + # uncache them all. + ActiveRecord::FixtureSet.reset_cache + ActiveRecord::FixtureSet. + create_fixtures(Rails.root.join('test', 'fixtures'), fixturesets) + + # Dump cache of permissions etc. + Rails.cache.clear + ActiveRecord::Base.connection.clear_query_cache + + # Reload database seeds + DatabaseSeeds.install + end + rescue ActiveRecord::StatementInvalid => e + if "#{e.inspect}" =~ /deadlock detected/i and (attempts_left -= 1) > 0 + logger.info "Waiting for lock -- #{e.inspect}" + sleep 0.5 + retry + end + raise + end - # Dump cache of permissions etc. - Rails.cache.clear - ActiveRecord::Base.connection.clear_query_cache + require 'refresh_permission_view' - # Reload database seeds - DatabaseSeeds.install + refresh_permission_view # Done. - render json: {success: true} + send_json success: true end end