diff options
author | James Lopez <james@jameslopez.es> | 2016-02-01 17:47:11 +0300 |
---|---|---|
committer | James Lopez <james@jameslopez.es> | 2016-02-01 17:47:11 +0300 |
commit | 927ab48101d44976e174b13323d085aa5f846155 (patch) | |
tree | e8a1ccc7068b343c472db950ee5d81c69c8aa18e /db/migrate/20160129135155_remove_dot_atom_path_ending_of_projects.rb | |
parent | 7dffec2c43116fa339f967b1005f23442752ce0d (diff) |
WIP - refactored migration
Diffstat (limited to 'db/migrate/20160129135155_remove_dot_atom_path_ending_of_projects.rb')
-rw-r--r-- | db/migrate/20160129135155_remove_dot_atom_path_ending_of_projects.rb | 49 |
1 files changed, 42 insertions, 7 deletions
diff --git a/db/migrate/20160129135155_remove_dot_atom_path_ending_of_projects.rb b/db/migrate/20160129135155_remove_dot_atom_path_ending_of_projects.rb index c7b986aca91..622b0f5db08 100644 --- a/db/migrate/20160129135155_remove_dot_atom_path_ending_of_projects.rb +++ b/db/migrate/20160129135155_remove_dot_atom_path_ending_of_projects.rb @@ -1,8 +1,12 @@ class RemoveDotAtomPathEndingOfProjects < ActiveRecord::Migration + include Gitlab::ShellAdapter class ProjectPath - def initilize(old_path) + attr_reader :old_path, :id + + def initialize(old_path, id) @old_path = old_path + @id = id end def clean_path @@ -10,7 +14,7 @@ class RemoveDotAtomPathEndingOfProjects < ActiveRecord::Migration end end - module PathCleaner + class PathCleaner def initialize(path) @path = path end @@ -30,7 +34,7 @@ class RemoveDotAtomPathEndingOfProjects < ActiveRecord::Migration end def cleaned_path - @_cleaned_path ||= path.gsub(/\.atom\z/, '-atom') + @_cleaned_path ||= @path.gsub(/\.atom\z/, '-atom') end def path_exists?(path) @@ -38,17 +42,48 @@ class RemoveDotAtomPathEndingOfProjects < ActiveRecord::Migration end end + def projects_with_dot_atom + select_all("SELECT id, path FROM projects WHERE lower(path) LIKE '%.atom'") + end + def up projects_with_dot_atom.each do |project| - remove_dot(project) + binding.pry + project_path = ProjectPath.new(project['path'], project['id']) + clean_path(project_path) if move_path(project_path) end end private - def remove_dot(project) - #TODO + def clean_path(project_path) + execute "UPDATE projects SET path = '#{project_path.clean_path}' WHERE id = #{project.id}" end - + def move_path(project_path) + # Based on RemovePeriodsAtEndsOfUsernames + # Don't attempt to move if original path only contains periods. + return if project_path.clean_path =~ /\A\.+\z/ + if gitlab_shell.mv_namespace(project_path.old_path, project_path.clean_path) + # If repositories moved successfully we need to remove old satellites + # and send update instructions to users. + # However we cannot allow rollback since we moved namespace dir + # So we basically we mute exceptions in next actions + begin + gitlab_shell.rm_satellites(project_path.old_path) + # We cannot send update instructions since models and mailers + # can't safely be used from migrations as they may be written for + # later versions of the database. + # send_update_instructions + rescue + # Returning false does not rollback after_* transaction but gives + # us information about failing some of tasks + false + end + else + # if we cannot move namespace directory we should avoid + # db changes in order to prevent out of sync between db and fs + false + end + end end |