Commit 3d43247a authored by Ian Baum's avatar Ian Baum Committed by Gabriel Mazetto

Add gitlab:db:active task

To be used for setting up GEO replication in the omnibus-gitalb package.
Intended to help prevent users from accidentally deleting actively used
databases.

* Returns 0 if there is sign that the database has been modified by the
  user
* Returns 1 if there is no sign that the database has been modified by
  the user
parent e3f99409
---
title: Add gitlab:db:active task
merge_request: 48083
author:
type: fixed
...@@ -203,5 +203,25 @@ namespace :gitlab do ...@@ -203,5 +203,25 @@ namespace :gitlab do
Gitlab::AppLogger.error(e) Gitlab::AppLogger.error(e)
raise raise
end end
desc 'Check if there have been user additions to the database'
task active: :environment do
if ActiveRecord::Base.connection.migration_context.needs_migration?
puts "Migrations pending. Database not active"
exit 1
end
# A list of projects that GitLab creates automatically on install/upgrade
# gc = Gitlab::CurrentSettings.current_application_settings
seed_projects = [Gitlab::CurrentSettings.current_application_settings.self_monitoring_project]
if (Project.count - seed_projects.count {|x| !x.nil? }).eql?(0)
puts "No user created projects. Database not active"
exit 1
end
puts "Found user created projects. Database active"
exit 0
end
end end
end end
...@@ -261,6 +261,34 @@ RSpec.describe 'gitlab:db namespace rake task' do ...@@ -261,6 +261,34 @@ RSpec.describe 'gitlab:db namespace rake task' do
end end
end end
describe 'active' do
using RSpec::Parameterized::TableSyntax
let(:task) { 'gitlab:db:active' }
let(:self_monitoring) { double('self_monitoring') }
where(:needs_migration, :self_monitoring_project, :project_count, :exit_status, :exit_code) do
true | nil | nil | 1 | false
false | :self_monitoring | 1 | 1 | false
false | nil | 0 | 1 | false
false | :self_monitoring | 2 | 0 | true
end
with_them do
it 'exits 0 or 1 depending on user modifications to the database' do
allow_any_instance_of(ActiveRecord::MigrationContext).to receive(:needs_migration?).and_return(needs_migration)
allow_any_instance_of(ApplicationSetting).to receive(:self_monitoring_project).and_return(self_monitoring_project)
allow(Project).to receive(:count).and_return(project_count)
expect { run_rake_task(task) }.to raise_error do |error|
expect(error).to be_a(SystemExit)
expect(error.status).to eq(exit_status)
expect(error.success?).to be(exit_code)
end
end
end
end
def run_rake_task(task_name, arguments = '') def run_rake_task(task_name, arguments = '')
Rake::Task[task_name].reenable Rake::Task[task_name].reenable
Rake.application.invoke_task("#{task_name}#{arguments}") Rake.application.invoke_task("#{task_name}#{arguments}")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment