Commit 4af5d608 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'mysql_drop_all_tables' into 'master'

Disable MySQL foreign key checks before dropping all tables

Fixes #20237. Disables MySQL foreign key checks before dropping all tables in a restore. MySQL doesn't honor `CASCADE` without a special flag when the database/tables are created. In order to drop the tables we need to disable foreign key checks. After the drop, re-enable the key checks.

WIP: Pending confirmation from customer that this fix works.

See merge request !5472
Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent 2a2ce716
......@@ -7,6 +7,7 @@ v 8.10.2 (unreleased)
- Use project ID in repository cache to prevent stale data from persisting across projects. !5460
- Fix issue with autocomplete search not working with enter key. !5466
- Add iid to MR API response. !5468
- Disable MySQL foreign key checks before dropping all tables. !5472
- Don't show comment button in gutter of diffs on MR discussion tab
v 8.10.1
......
......@@ -25,6 +25,10 @@ namespace :gitlab do
desc 'Drop all tables'
task :drop_tables => :environment do
connection = ActiveRecord::Base.connection
# If MySQL, turn off foreign key checks
connection.execute('SET FOREIGN_KEY_CHECKS=0') if Gitlab::Database.mysql?
tables = connection.tables
tables.delete 'schema_migrations'
# Truncate schema_migrations to ensure migrations re-run
......@@ -35,6 +39,9 @@ namespace :gitlab do
# MySQL: http://dev.mysql.com/doc/refman/5.7/en/drop-table.html
# Add `IF EXISTS` because cascade could have already deleted a table.
tables.each { |t| connection.execute("DROP TABLE IF EXISTS #{connection.quote_table_name(t)} CASCADE") }
# If MySQL, re-enable foreign key checks
connection.execute('SET FOREIGN_KEY_CHECKS=1') if Gitlab::Database.mysql?
end
desc 'Configures the database by running migrate, or by loading the schema and seeding if needed'
......
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