Skip to content

Commit

Permalink
Merge pull request #8098 from opf/fix/time_entry_fix_rake_task
Browse files Browse the repository at this point in the history
also fix the time entries with root activities
  • Loading branch information
machisuji committed Mar 4, 2020
2 parents b1795ec + 7494773 commit 0dfa010
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions lib/tasks/time_entry_activities.rake
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,36 @@ namespace 'openproject' do
next
end

check_statement = <<~SQL
SELECT
te_source.id,
enumerations.parent_id
FROM
time_entries te_source
INNER JOIN
enumerations
ON te_source.activity_id = enumerations.id AND enumerations.parent_id IS NOT NULL AND enumerations.type = 'TimeEntryActivity'
SQL

select_statement = <<~SQL
SELECT te_source.id, enumerations.parent_id FROM time_entries te_source INNER JOIN enumerations ON te_source.activity_id = enumerations.id AND enumerations.parent_id IS NOT NULL AND enumerations.type = 'TimeEntryActivity'
SELECT
te_source.id,
COALESCE(enumerations.parent_id, enumerations.id) activity_id
FROM time_entries te_source
LEFT OUTER JOIN
enumerations
ON te_source.activity_id = enumerations.id
WHERE enumerations.type = 'TimeEntryActivity'
SQL

entries = begin
connection = ActiveRecord::Base
.establish_connection(ENV['BACKUP_DATABASE_URL'])
.connection
connection.select_all(select_statement)

if connection.select_all(check_statement).any?
connection.select_all(select_statement)
end
rescue PG::ConnectionBad, ActiveRecord::NoDatabaseError, LoadError => e
puts <<~MSG
Expand All @@ -64,7 +85,7 @@ namespace 'openproject' do
connection&.close
end

unless entries.any?
if entries.nil? || entries.empty?
puts <<~MSG
Expand All @@ -75,7 +96,7 @@ namespace 'openproject' do
next
end

entries_string = entries.map { |entry| "(#{entry['id']}, #{entry['parent_id']})" }.join(', ')
entries_string = entries.map { |entry| "(#{entry['id']}, #{entry['activity_id']})" }.join(', ')

update_statement = <<~SQL
UPDATE time_entries
Expand Down

0 comments on commit 0dfa010

Please sign in to comment.