Commit ea59a84f authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'fix/25635' into 'master'

Fix 8.15 RC1 requires quite amount of downtime

See https://gitlab.com/gitlab-org/gitlab-ce/issues/25635

See merge request !8106
parents 03e26def ad4ec446
...@@ -16,6 +16,6 @@ class FillRoutesTable < ActiveRecord::Migration ...@@ -16,6 +16,6 @@ class FillRoutesTable < ActiveRecord::Migration
end end
def down def down
Route.delete_all(source_type: 'Namespace') execute("DELETE FROM routes WHERE source_type = 'Namespace'")
end end
end end
...@@ -8,15 +8,23 @@ class FillProjectsRoutesTable < ActiveRecord::Migration ...@@ -8,15 +8,23 @@ class FillProjectsRoutesTable < ActiveRecord::Migration
DOWNTIME_REASON = 'No new projects should be created during data copy' DOWNTIME_REASON = 'No new projects should be created during data copy'
def up def up
if Gitlab::Database.postgresql?
execute <<-EOF execute <<-EOF
INSERT INTO routes INSERT INTO routes (source_id, source_type, path)
(source_id, source_type, path) (SELECT DISTINCT ON (namespaces.path, projects.path) projects.id, 'Project', concat(namespaces.path, '/', projects.path)
(SELECT projects.id, 'Project', concat(namespaces.path, '/', projects.path) FROM projects FROM projects INNER JOIN namespaces ON projects.namespace_id = namespaces.id
INNER JOIN namespaces ON projects.namespace_id = namespaces.id) ORDER BY namespaces.path, projects.path, projects.id DESC)
EOF EOF
else
execute <<-EOF
INSERT INTO routes (source_id, source_type, path)
(SELECT projects.id, 'Project', concat(namespaces.path, '/', projects.path)
FROM projects INNER JOIN namespaces ON projects.namespace_id = namespaces.id)
EOF
end
end end
def down def down
Route.delete_all(source_type: 'Project') execute("DELETE FROM routes WHERE source_type = 'Project'")
end end
end end
...@@ -7,20 +7,21 @@ class RemoveDuplicatesFromRoutes < ActiveRecord::Migration ...@@ -7,20 +7,21 @@ class RemoveDuplicatesFromRoutes < ActiveRecord::Migration
DOWNTIME = false DOWNTIME = false
def up def up
select_all("SELECT path FROM #{quote_table_name(:routes)} GROUP BY path HAVING COUNT(*) > 1").each do |row| # We can skip this migration when running a PostgreSQL database because
path = connection.quote(row['path']) # we use an optimized query in the "FillProjectsRoutesTable" migration
execute(%Q{ # to fill these values that avoid duplicate entries in the routes table.
DELETE FROM #{quote_table_name(:routes)} return unless Gitlab::Database.mysql?
WHERE path = #{path}
AND id != ( execute <<-EOF
SELECT id FROM ( DELETE duplicated_rows.*
SELECT max(id) AS id FROM routes AS duplicated_rows
FROM #{quote_table_name(:routes)} INNER JOIN (
WHERE path = #{path} SELECT path, MAX(id) as max_id
) max_ids FROM routes
) GROUP BY path
}) HAVING COUNT(*) > 1
end ) AS good_rows ON good_rows.path = duplicated_rows.path AND good_rows.max_id <> duplicated_rows.id;
EOF
end end
def down def down
......
...@@ -13,7 +13,7 @@ class AddNameIndexToNamespace < ActiveRecord::Migration ...@@ -13,7 +13,7 @@ class AddNameIndexToNamespace < ActiveRecord::Migration
end end
def down def down
if index_exists?(:namespaces, :name) if index_exists?(:namespaces, [:name, :parent_id])
remove_index :namespaces, [:name, :parent_id] remove_index :namespaces, [:name, :parent_id]
end end
end end
......
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