|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +#-- copyright |
| 4 | +# OpenProject is an open source project management software. |
| 5 | +# Copyright (C) the OpenProject GmbH |
| 6 | +# |
| 7 | +# This program is free software; you can redistribute it and/or |
| 8 | +# modify it under the terms of the GNU General Public License version 3. |
| 9 | +# |
| 10 | +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
| 11 | +# Copyright (C) 2006-2013 Jean-Philippe Lang |
| 12 | +# Copyright (C) 2010-2013 the ChiliProject Team |
| 13 | +# |
| 14 | +# This program is free software; you can redistribute it and/or |
| 15 | +# modify it under the terms of the GNU General Public License |
| 16 | +# as published by the Free Software Foundation; either version 2 |
| 17 | +# of the License, or (at your option) any later version. |
| 18 | +# |
| 19 | +# This program is distributed in the hope that it will be useful, |
| 20 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | +# GNU General Public License for more details. |
| 23 | +# |
| 24 | +# You should have received a copy of the GNU General Public License |
| 25 | +# along with this program; if not, write to the Free Software |
| 26 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 27 | +# |
| 28 | +# See COPYRIGHT and LICENSE files for more details. |
| 29 | +#++ |
| 30 | + |
| 31 | +class MigrateVersionsToSprints < ActiveRecord::Migration[8.0] |
| 32 | + def up |
| 33 | + return if sprint_versions_with_work_package_ids.none? |
| 34 | + |
| 35 | + sprint_versions_with_work_package_ids.find_each do |version| |
| 36 | + sprint = create_sprint(version) |
| 37 | + migrate_work_packages_to_sprint(sprint, version.wp_ids) |
| 38 | + end |
| 39 | + |
| 40 | + Backlogs::MigrateVersionSprintJournalsJob.perform_later |
| 41 | + end |
| 42 | + |
| 43 | + def down |
| 44 | + raise ActiveRecord::IrreversibleMigration |
| 45 | + end |
| 46 | + |
| 47 | + private |
| 48 | + |
| 49 | + def sprint_versions_with_work_package_ids |
| 50 | + # Load versions used as sprints including work package ids associated with the version. |
| 51 | + # Since the same version can be used as a sprint in one project but not in another, |
| 52 | + # the work package ids are filtered by projects where the version is used as a sprint. |
| 53 | + Version.joins(:version_settings, :work_packages) |
| 54 | + .where(version_settings: { display: VersionSetting::DISPLAY_LEFT }) |
| 55 | + .where("work_packages.project_id = version_settings.project_id") |
| 56 | + .group("versions.id") |
| 57 | + .select("versions.*, array_agg(DISTINCT work_packages.id) AS wp_ids") |
| 58 | + end |
| 59 | + |
| 60 | + def create_sprint(version) |
| 61 | + Agile::Sprint.create!( |
| 62 | + name: version.name, |
| 63 | + project_id: version.project_id, |
| 64 | + status: version.status == "open" ? "in_planning" : "completed", |
| 65 | + start_date: version.start_date, |
| 66 | + finish_date: version.effective_date |
| 67 | + ) |
| 68 | + end |
| 69 | + |
| 70 | + def migrate_work_packages_to_sprint(sprint, wp_ids) |
| 71 | + WorkPackage.where(id: wp_ids).update_all(sprint_id: sprint.id) |
| 72 | + end |
| 73 | +end |
0 commit comments