Use optimized query to fill the routes table when running PostgreSQL

parent f140ae88
...@@ -8,12 +8,20 @@ class FillProjectsRoutesTable < ActiveRecord::Migration ...@@ -8,12 +8,20 @@ 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
......
...@@ -7,6 +7,11 @@ class RemoveDuplicatesFromRoutes < ActiveRecord::Migration ...@@ -7,6 +7,11 @@ class RemoveDuplicatesFromRoutes < ActiveRecord::Migration
DOWNTIME = false DOWNTIME = false
def up def up
# 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?
select_all("SELECT path FROM #{quote_table_name(:routes)} GROUP BY path HAVING COUNT(*) > 1").each do |row| select_all("SELECT path FROM #{quote_table_name(:routes)} GROUP BY path HAVING COUNT(*) > 1").each do |row|
path = connection.quote(row['path']) path = connection.quote(row['path'])
execute(%Q{ execute(%Q{
......
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