summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortiagonbotelho <tiagonbotelho@hotmail.com>2016-08-10 18:18:29 +0100
committertiagonbotelho <tiagonbotelho@hotmail.com>2016-08-19 17:04:15 +0100
commitc1d77e29600713018c5750f4d9eebecd3868a4be (patch)
tree1d7ce42c48521613c630f3b4136ad4d3850b4418
parent4ccba6bf2ddd48d66cd9cd8c6cee5eae19691cbb (diff)
downloadgitlab-ce-c1d77e29600713018c5750f4d9eebecd3868a4be.tar.gz
adds migration that enables a forked project to know where the original project isfork-network
-rw-r--r--db/migrate/20160810165809_add_fork_origin_id_to_projects.rb52
-rw-r--r--db/schema.rb2
2 files changed, 53 insertions, 1 deletions
diff --git a/db/migrate/20160810165809_add_fork_origin_id_to_projects.rb b/db/migrate/20160810165809_add_fork_origin_id_to_projects.rb
new file mode 100644
index 00000000000..1b1cd287d50
--- /dev/null
+++ b/db/migrate/20160810165809_add_fork_origin_id_to_projects.rb
@@ -0,0 +1,52 @@
+# See http://doc.gitlab.com/ce/development/migration_style_guide.html
+# for more information on how to write migrations for GitLab.
+
+class AddForkOriginIdToProjects < ActiveRecord::Migration
+ include Gitlab::Database::MigrationHelpers
+ include Gitlab::Database
+
+ DOWNTIME = true
+
+ class Project < ActiveRecord::Base
+ self.table_name = 'projects'
+ end
+
+ def up
+ add_column :projects, :origin_fork_id, :integer, default: nil, null: true
+
+ transaction do
+ Project.joins('INNER JOIN forked_project_links ON projects.id = forked_project_links.forked_to_project_id')
+ .select('projects.id, forked_project_links.forked_from_project_id').find_in_batches do |rows|
+ forked_origin_ids = {}
+
+ rows.each do |project|
+ origin_fork_id = project.forked_from_project_id
+ next_fork = %Q{SELECT forked_from_project_id FROM forked_project_links WHERE forked_to_project_id = #{origin_fork_id}}
+
+ result = select_all(next_fork)
+ until result.first.nil?
+ if forked_origin_ids.key?(origin_fork_id)
+ origin_fork_id = forked_origin_ids[origin_fork_id]
+ break
+ end
+
+ origin_fork_id = result.first["forked_from_project_id"]
+ result = select_all(next_fork)
+ end
+
+ forked_origin_ids[project.id] = origin_fork_id
+ end
+
+ whens = forked_origin_ids.map do |(project_id, origin_id)|
+ "WHEN id = #{project_id} THEN #{origin_id}"
+ end
+
+ Project.where(id: forked_origin_ids.keys).update_all("origin_fork_id = CASE #{whens.join(' ')} END")
+ end
+ end
+ end
+
+ def down
+ remove_column :projects, :origin_fork_id
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 71980a6d51f..5030119ebac 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20160804150737) do
+ActiveRecord::Schema.define(version: 20160810165809) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"