Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

use faster algorithm for fixing timestamp inconsistencies

ulferts40d10e0f0bac
diff --git a/db/migrate/20230608151123_add_validity_period_to_journals.rb b/db/migrate/20230608151123_add_validity_period_to_journals.rb
index 40c97406f6c..63957690589 100644
--- a/db/migrate/20230608151123_add_validity_period_to_journals.rb
+++ b/db/migrate/20230608151123_add_validity_period_to_journals.rb
@@ -17,108 +17,128 @@ class AddValidityPeriodToJournals < ActiveRecord::Migration[7.0]
end
def fix_all_journal_timestamps
- max_attempts = attempts = (ENV["MAX_JOURNAL_TIMESTAMPS_ATTEMPTS"].presence && ENV["MAX_JOURNAL_TIMESTAMPS_ATTEMPTS"].to_i) ||
- Journal.all.maximum(:version)
+ return unless current_max_journal_version
- invalid_journables = nil
+ say "Fixing potential timestamp inconsistencies on existing journals."
- loop do
- invalid_journables = fix_journal_timestamps(invalid_journables)
+ say "Creating utility table to improve the performance of subsequent actions."
+ create_successor_journals_utility_table
- break if invalid_journables.empty?
+ current_max_journal_version.downto(1).each do |version|
+ say_with_time "Fixing timestamps for journals with version #{version}." do
+ fixed_journals = fix_journal_timestamps(version)
- if attempts == 0
- raise <<~MSG.squish
- There are still journals with timestamps after their successors timestamp.
- Aborting after #{max_attempts}.
- Run the migration again with the env variable MAX_JOURNAL_TIMESTAMPS_ATTEMPTS set to a higher value.
- MSG
- else
- say "Journals with timestamps after their successors timestamp remain in the database. Retrying..."
+ say "Fixed timestamps for #{fixed_journals.cmdtuples} journals.", true if fixed_journals.cmdtuples.positive?
end
-
- attempts -= 1
end
- say "All journals' timestamps in the database are correct."
+ say "Done."
+
+ drop_successor_journals_utility_table
end
- def fix_journal_timestamps(invalid_journables)
- limit_condition = invalid_journables ? "AND journable_id IN (#{invalid_journables.uniq.join(', ')})" : ""
-
- # Update journals with their timestamps after the timestamp of their successor
- # (as identified by the journal belonging to the same journable and having the smallest version
- # larger than the journal's).
- # If one such is found, the:
- # * created_at is set to be the minimum of the journal's and the successor's created_at. But of the successor's value some
- # small amount needs to be subtracted as later on the validity_period is calculated as a range from the predecessor's
- # created_at to the successor's created_at. If the two values would be equal, the range would be empty
- # resulting in an error.
- # * updated_at is only altered if created_at and updated_at are equal, meaning the journal has never been updated.
- # A journal might very well be updated after its successor was created, because as a note can be updated
- # by the user at anytime.
- # This might have the consequence of the updated_at wrongfully being after the created_at of the successor,
- # but this will not be invalid.
-
- updated = select_rows <<~SQL.squish
- UPDATE journals
- SET
- created_at = LEAST(journals.created_at, successors.created_at - interval '1 ms'),
- updated_at = CASE
- WHEN journals.created_at = journals.updated_at
- THEN LEAST(journals.created_at, successors.created_at - interval '1 ms')
- ELSE journals.updated_at
- END
-
- FROM (
+ def create_successor_journals_utility_table
+ suppress_messages do
+ create_table :successor_journals, id: false do |t|
+ t.references :predecessor, null: true, index: false
+ t.references :successor, null: true, index: false
+ end
+
+ execute <<~SQL.squish
+ INSERT INTO
+ successor_journals
SELECT
- predecessors.id,
- successors.created_at
+ predecessors.id predecessor_id,
+ successors.id successor_id
FROM
journals predecessors
LEFT JOIN LATERAL (SELECT DISTINCT ON (journable_type, journable_id) *
- FROM journals successors
- WHERE successors.version > predecessors.version
- AND successors.journable_id = predecessors.journable_id
- AND successors.journable_type = predecessors.journable_type
- AND successors.created_at <= predecessors.created_at
- ORDER BY successors.journable_type ASC,
+ FROM journals successors
+ WHERE successors.version > predecessors.version
+ AND successors.journable_id = predecessors.journable_id
+ AND successors.journable_type = predecessors.journable_type
+ ORDER BY successors.journable_type ASC,
successors.journable_id ASC,
successors.version ASC) successors
ON successors.journable_id = predecessors.journable_id
AND successors.journable_type = predecessors.journable_type
- ) successors
- WHERE successors.id = journals.id
- AND successors.created_at <= journals.created_at
- #{limit_condition}
- RETURNING journals.journable_id
- SQL
+ SQL
+
+ add_index :successor_journals, :predecessor_id
+ add_index :successor_journals, :successor_id
+ end
+ end
+
+ def drop_successor_journals_utility_table
+ suppress_messages do
+ drop_table :successor_journals
+ end
+ end
- updated.flatten
+ def current_max_journal_version
+ @current_max_journal_version ||= suppress_messages { select_one("SELECT MAX(version) FROM journals")["max"] }
+ end
+
+ # Update journals with their timestamps after the timestamp of their successor
+ # (as identified by the journal belonging to the same journable and having the smallest version
+ # larger than the journal's).
+ # If one such is found, the:
+ # * created_at is set to be the minimum of the journal's and the successor's created_at. But of the successor's value some
+ # small amount needs to be subtracted as later on the validity_period is calculated as a range from the predecessor's
+ # created_at to the successor's created_at. If the two values would be equal, the range would be empty
+ # resulting in an error.
+ # * updated_at is only altered if created_at and updated_at are equal, meaning the journal has never been updated.
+ # A journal might very well be updated after its successor was created, because as a note can be updated
+ # by the user at anytime.
+ # This might have the consequence of the updated_at wrongfully being after the created_at of the successor,
+ # but this will not be invalid.
+ def fix_journal_timestamps(version)
+ suppress_messages do
+ execute <<~SQL.squish
+ UPDATE journals
+ SET
+ created_at = LEAST(journals.created_at, successors.created_at - INTERVAL '1ms'),
+ updated_at = CASE
+ WHEN journals.created_at = journals.updated_at
+ THEN LEAST(journals.created_at, successors.created_at - INTERVAL '1ms')
+ ELSE journals.updated_at
+ END
+ FROM successor_journals, journals successors
+ WHERE successor_journals.predecessor_id = journals.id AND successor_journals.successor_id = successors.id
+ AND successors.created_at <= journals.created_at
+ AND journals.version = #{version}
+ SQL
+ end
end
def write_validity_period
- execute <<~SQL.squish
- UPDATE journals
- SET validity_period = values.validity_period
- FROM (
- SELECT
- predecessors.id,
- tstzrange(predecessors.created_at, successors.created_at) validity_period
- FROM
- journals predecessors
- LEFT JOIN LATERAL (SELECT DISTINCT ON (journable_type, journable_id) *
- FROM journals successors
- WHERE successors.version > predecessors.version
- AND successors.journable_id = predecessors.journable_id
- ORDER BY successors.journable_type ASC,
- successors.journable_id ASC,
- successors.version ASC) successors
- ON successors.journable_id = predecessors.journable_id
- AND successors.journable_type = predecessors.journable_type
- ) values
- WHERE values.id = journals.id
- SQL
+ say_with_time "Writing validity periods for journals." do
+ suppress_messages do
+ execute <<~SQL.squish
+ UPDATE journals
+ SET validity_period = values.validity_period
+ FROM (
+ SELECT
+ predecessors.id,
+ tstzrange(predecessors.created_at, successors.created_at) validity_period
+ FROM
+ journals predecessors
+ LEFT JOIN LATERAL (SELECT DISTINCT ON (journable_type, journable_id) *
+ FROM journals successors
+ WHERE successors.version > predecessors.version
+ AND successors.journable_id = predecessors.journable_id
+ ORDER BY successors.journable_type ASC,
+ successors.journable_id ASC,
+ successors.version ASC) successors
+ ON successors.journable_id = predecessors.journable_id
+ AND successors.journable_type = predecessors.journable_type
+ ) values
+ WHERE values.id = journals.id
+ SQL
+ end
+ end
+
+ say "Done."
end
def add_validity_period_constraint