Moves 'none' type resolution to rake task

pull/644/head
Hagen Schink 11 years ago committed by Michael Frister
parent 77743c7fff
commit 000a6ea505
  1. 27
      db/migrate/20131024140048_migrate_timelines_options.rb
  2. 41
      db/seeds/production.rb
  3. 78
      lib/tasks/fix_missing_none_type_in_timelines_config.rake

@ -52,6 +52,17 @@ class MigrateTimelinesOptions < ActiveRecord::Migration
pe_type_id_map)),
nil)
end
if contains_none_element?
puts "\n\n"\
"ATTENTION:"\
"\n\n"\
"The timelines configurations reference the 'none' type. The\n"\
"'none' type is created in the production seed. Thus, AFTER the\n"\
"production seed, you need to run rake task\n"\
"'migrations:timelines:fix_missing_none_type_in_timelines_config'."\
"\n\n\n\n"
end
end
def down
@ -65,6 +76,14 @@ class MigrateTimelinesOptions < ActiveRecord::Migration
end
end
def contains_none_element?
@contains_none_element
end
def contains_none_element=(value)
@contains_none_element = value
end
private
PE_TYPE_KEY = 'planning_element_types'
@ -72,9 +91,11 @@ class MigrateTimelinesOptions < ActiveRecord::Migration
VERTICAL_PE_TYPES = 'vertical_planning_elements'
def migrate_timelines_options(options, pe_id_map, pe_type_id_map)
calling_class = self
Proc.new do |timelines_opts|
timelines_opts = rename_columns timelines_opts, options
timelines_opts = migrate_planning_element_types timelines_opts, pe_type_id_map
timelines_opts = migrate_planning_element_types timelines_opts, pe_type_id_map, calling_class
timelines_opts = migrate_planning_element_time_types timelines_opts, pe_type_id_map
timelines_opts = migrate_vertical_planning_elements timelines_opts, pe_id_map
@ -82,11 +103,13 @@ class MigrateTimelinesOptions < ActiveRecord::Migration
end
end
def migrate_planning_element_types(timelines_opts, pe_type_id_map)
def migrate_planning_element_types(timelines_opts, pe_type_id_map, calling_class)
pe_types = []
pe_types = timelines_opts[PE_TYPE_KEY].delete_if { |t| t.nil? } if timelines_opts.has_key? PE_TYPE_KEY
calling_class.contains_none_element = calling_class.contains_none_element? || pe_types.empty?
pe_types = pe_types.empty? ? new_ids_of_former_pes
: pe_types.map { |p| pe_type_id_map[p] }

@ -28,7 +28,6 @@
# add seeds specific for the production-environment here
require_relative '../migrate/migration_utils/timelines'
standard_type = Type.find_by_is_standard(true)
@ -57,43 +56,3 @@ end
[WorkPackage, Journal::WorkPackageJournal].each do |klass|
klass.update_all({ :type_id => standard_type.id }, { :type_id => [0, nil] })
end
class MigrateTimelinesOptions < ActiveRecord::Migration
include Migration::Utils
def initialize(standard_type)
@standard_type = standard_type
end
def migrate
say_with_time_silently "Set 'none' type id in timelines options" do
update_column_values('timelines',
['options'],
update_options(add_none_type_id),
nil)
end
end
def add_none_type_id
Proc.new do |timelines_opts|
add_none_type_id_to_options timelines_opts
end
end
PE_TYPE_KEY = 'planning_element_types'
def add_none_type_id_to_options(options)
pe_types = []
pe_types = options[PE_TYPE_KEY] if options.has_key? PE_TYPE_KEY
pe_types.map! { |t| (t == 0) ? @standard_type.id : t }
options[PE_TYPE_KEY] = pe_types
options
end
end
timelines_migrator = MigrateTimelinesOptions.new(standard_type)
timelines_migrator.migrate

@ -0,0 +1,78 @@
#-- encoding: UTF-8
#-- copyright
# OpenProject is a project management system.
#
# Copyright (C) 2012-2013 the OpenProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# See doc/COPYRIGHT.rdoc for more details.
#++
require_relative '../../db/migrate/migration_utils/utils'
namespace :migrations do
namespace :timelines do
desc "Fixes missing 'none' type in timelines configuration"
task :fix_missing_none_type_in_timelines_config => :environment do |task|
standard_type = Type.find_by_is_standard(true)
if standard_type.nil?
raise "No standard type exists! You have to run the production seed "\
"beforehand."
end
migrator_class = create_migrator_class
timelines_migrator = migrator_class.new(standard_type)
timelines_migrator.migrate
end
private
PE_TYPE_KEY = 'planning_element_types'
# We create the migration class dynamically because rake would try to
# execute the migration on 'rake db:migrate' otherwise.
#
# We inherit from 'ActiveRecord::Migration' to reuse code already
# written for the migrations.
def create_migrator_class
Class.new(ActiveRecord::Migration) do
include Migration::Utils
def initialize(standard_type)
@standard_type = standard_type
end
def migrate
say_with_time_silently "Set 'none' type id in timelines options" do
update_column_values('timelines',
['options'],
update_options(add_none_type_id),
nil)
end
end
def add_none_type_id
Proc.new do |timelines_opts|
add_none_type_id_to_options timelines_opts
end
end
def add_none_type_id_to_options(options)
pe_types = []
pe_types = options[PE_TYPE_KEY] if options.has_key? PE_TYPE_KEY
pe_types.map! { |t| (t == 0) ? @standard_type.id : t }
options[PE_TYPE_KEY] = pe_types
options
end
end
end
end
end
Loading…
Cancel
Save