Skip to content

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

What does this MR do?

The rails db:migrate:redo command has different behavior than running a rails db:migrate:down followed by rails db:migrate:up. When running the redo command, the gitlab:db:clean_structure_sql task is only run after the down step, resulting in the final structure.sql not being stripped of comments etc.

This is due to rake only allowing the task to be run once in an execution context, so we have to reenable the task after it runs so it execute the second time.

Output of rails db:migrate:redo --trace

Before

** Invoke db:migrate:redo (first_time)
** Invoke db:load_config (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:load_config
** Execute db:migrate:redo
** Invoke db:rollback (first_time)
** Invoke db:load_config
** Execute db:rollback
** Invoke db:_dump (first_time)
** Execute db:_dump
** Invoke db:structure:dump (first_time)
** Invoke db:load_config
** Execute db:structure:dump
** Invoke gitlab:db:clean_structure_sql (first_time)
** Execute gitlab:db:clean_structure_sql
** Invoke db:migrate (first_time)
** Invoke db:load_config
** Invoke schema_version_check (first_time)
** Invoke environment
** Execute schema_version_check
** Execute db:migrate
** Invoke db:_dump (first_time)
** Execute db:_dump
** Invoke db:structure:dump (first_time)
** Invoke db:load_config
** Execute db:structure:dump
** Invoke gitlab:db:clean_structure_sql

After

** Invoke db:migrate:redo (first_time)
** Invoke db:load_config (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:load_config
** Execute db:migrate:redo
** Invoke db:rollback (first_time)
** Invoke db:load_config
** Execute db:rollback
** Invoke db:_dump (first_time)
** Execute db:_dump
** Invoke db:structure:dump (first_time)
** Invoke db:load_config
** Execute db:structure:dump
** Invoke gitlab:db:clean_structure_sql (first_time)
** Execute gitlab:db:clean_structure_sql
** Invoke db:migrate (first_time)
** Invoke db:load_config
** Invoke schema_version_check (first_time)
** Invoke environment
** Execute schema_version_check
** Execute db:migrate
** Invoke db:_dump (first_time)
** Execute db:_dump
** Invoke db:structure:dump (first_time)
** Invoke db:load_config
** Execute db:structure:dump
** Invoke gitlab:db:clean_structure_sql (first_time)
** Execute gitlab:db:clean_structure_sql

Related issue: #212883 (closed)

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Edited by 🤖 GitLab Bot 🤖

Merge request reports