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