Commit 8eb2ad31 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch '212883-fix-different-behavior-between-db-migrate-up-down-and-redo' into 'master'

Fix different behavior between db:migrate:up/down and redo

See merge request gitlab-org/gitlab!28363
parents 99a5a587 64a0f20f
......@@ -80,16 +80,19 @@ namespace :gitlab do
end
desc 'This adjusts and cleans db/structure.sql - it runs after db:structure:dump'
task :clean_structure_sql do
task :clean_structure_sql do |task_name|
structure_file = 'db/structure.sql'
schema = File.read(structure_file)
File.open(structure_file, 'wb+') do |io|
Gitlab::Database::SchemaCleaner.new(schema).clean(io)
end
# Allow this task to be called multiple times, as happens when running db:migrate:redo
Rake::Task[task_name].reenable
end
# Inform Rake that gitlab:schema:fix_structure_sql should be run every time rake db:structure:dump is run
# Inform Rake that gitlab:schema:clean_structure_sql should be run every time rake db:structure:dump is run
Rake::Task['db:structure:dump'].enhance do
Rake::Task['gitlab:db:clean_structure_sql'].invoke
end
......
......@@ -98,6 +98,39 @@ describe 'gitlab:db namespace rake task' do
end
end
describe 'clean_structure_sql' do
let_it_be(:clean_rake_task) { 'gitlab:db:clean_structure_sql' }
let_it_be(:test_task_name) { 'gitlab:db:_test_multiple_structure_cleans' }
let_it_be(:structure_file) { 'db/structure.sql' }
let_it_be(:input) { 'this is structure data' }
let(:output) { StringIO.new }
before do
allow(File).to receive(:read).with(structure_file).and_return(input)
allow(File).to receive(:open).with(structure_file, any_args).and_yield(output)
end
after do
Rake::Task[test_task_name].clear if Rake::Task.task_defined?(test_task_name)
end
it 'can be executed multiple times within another rake task' do
Rake::Task.define_task(test_task_name => :environment) do
expect_next_instance_of(Gitlab::Database::SchemaCleaner) do |cleaner|
expect(cleaner).to receive(:clean).with(output)
end
Rake::Task[clean_rake_task].invoke
expect_next_instance_of(Gitlab::Database::SchemaCleaner) do |cleaner|
expect(cleaner).to receive(:clean).with(output)
end
Rake::Task[clean_rake_task].invoke
end
run_rake_task(test_task_name)
end
end
def run_rake_task(task_name)
Rake::Task[task_name].reenable
Rake.application.invoke_task task_name
......
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