forked from openstreetmap/openstreetmap-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added version to notes table and table old_notes
Added migration script for adding version column to existing table notes (will become current_notes) and adding new table old_notes (will become notes) which looks like notes but with note_id instead of id and timestamp instead of updated_at, created_at and closed_at.
- Loading branch information
1 parent
a6c6f26
commit ba1a558
Showing
5 changed files
with
181 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# == Schema Information | ||
# | ||
# Table name: old_notes | ||
# | ||
# note_id :bigint(8) not null | ||
# latitude :integer not null | ||
# longitude :integer not null | ||
# tile :bigint(8) not null | ||
# timestamp :datetime not null | ||
# status :enum not null | ||
# version :bigint(8) default(1), not null | ||
# | ||
# Indexes | ||
# | ||
# index_old_notes_on_tile (tile) | ||
# index_old_notes_on_timestamp (timestamp) | ||
# | ||
|
||
class OldNote < ApplicationRecord | ||
include GeoRecord | ||
|
||
has_many :comments, -> { left_joins(:author).where(:visible => true, :users => { :status => [nil, "active", "confirmed"] }).order(:created_at) }, :class_name => "NoteComment", :foreign_key => :note_id | ||
has_many :all_comments, -> { left_joins(:author).order(:created_at) }, :class_name => "NoteComment", :foreign_key => :note_id, :inverse_of => :note | ||
has_many :subscriptions, :class_name => "NoteSubscription" | ||
has_many :subscribers, :through => :subscriptions, :source => :user | ||
|
||
validates :id, :uniqueness => true, :presence => { :on => :update }, | ||
:numericality => { :on => :update, :only_integer => true } | ||
validates :latitude, :longitude, :numericality => { :only_integer => true } | ||
validates :closed_at, :presence => true, :if => proc { :status == "closed" } | ||
validates :status, :inclusion => %w[open closed hidden] | ||
|
||
validate :validate_position | ||
|
||
scope :visible, -> { where.not(:status => "hidden") } | ||
scope :invisible, -> { where(:status => "hidden") } | ||
|
||
after_initialize :set_defaults | ||
|
||
DEFAULT_FRESHLY_CLOSED_LIMIT = 7.days | ||
|
||
# Sanity check the latitude and longitude and add an error if it's broken | ||
def validate_position | ||
errors.add(:base, "Note is not in the world") unless in_world? | ||
end | ||
|
||
# Close a note | ||
def close | ||
self.status = "closed" | ||
self.closed_at = Time.now.utc | ||
save | ||
end | ||
|
||
# Reopen a note | ||
def reopen | ||
self.status = "open" | ||
self.closed_at = nil | ||
save | ||
end | ||
|
||
# Check if a note is visible | ||
def visible? | ||
status != "hidden" | ||
end | ||
|
||
# Check if a note is closed | ||
def closed? | ||
!closed_at.nil? | ||
end | ||
|
||
def freshly_closed? | ||
return false unless closed? | ||
|
||
Time.now.utc < freshly_closed_until | ||
end | ||
|
||
def freshly_closed_until | ||
return nil unless closed? | ||
|
||
closed_at + DEFAULT_FRESHLY_CLOSED_LIMIT | ||
end | ||
|
||
# Return the author object, derived from the first comment | ||
def author | ||
comments.first.author | ||
end | ||
|
||
# Return the author IP address, derived from the first comment | ||
def author_ip | ||
comments.first.author_ip | ||
end | ||
|
||
private | ||
|
||
# Fill in default values for new notes | ||
def set_defaults | ||
self.status = "open" unless attribute_present?(:status) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
class AddNotesVersions < ActiveRecord::Migration[7.2] | ||
def change | ||
add_column :notes, :version, :bigint, :null => false, :default => 1 | ||
|
||
create_table :old_notes, :id => false, :primary_key => [:note_id, :version] do |t| | ||
t.bigint :note_id, :null => false | ||
t.integer :latitude, :null => false | ||
t.integer :longitude, :null => false | ||
t.bigint :tile, :null => false | ||
t.datetime :timestamp, :null => false | ||
t.column :status, "note_status_enum", :null => false | ||
t.bigint :version, :null => false, :default => 1 | ||
end | ||
|
||
add_index :old_notes, :tile | ||
add_index :old_notes, :timestamp | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class BackfillNotesVersions < ActiveRecord::Migration[7.2] | ||
class Note < ApplicationRecord; end | ||
class NoteComment < ApplicationRecord; end | ||
|
||
def change | ||
Note.in_batches(of: 1000) do |notes| | ||
note_ids = notes.pluck(:id) | ||
groups_of_comments = NoteComment.where(note_id: note_ids).order(:created_at).group_by(&:note_id) | ||
|
||
values = notes.flat_map do |note| | ||
version = 1 | ||
comments = groups_of_comments[note.id] | ||
comments.map do |comment| | ||
if %w[opened reopened closed hidden].include?(comment.event) | ||
new_status = %w[opened reopened].include?(comment.event) ? "open" : comment.event | ||
"(#{note.id}, #{note.latitude}, #{note.longitude}, #{note.tile}, '#{comment.created_at}', '#{new_status}'::note_status_enum, #{version})".tap { version += 1 } | ||
else | ||
nil | ||
end | ||
end.compact | ||
end.join(", ") | ||
|
||
unless values.empty? | ||
sql = "INSERT INTO old_notes (note_id, latitude, longitude, tile, timestamp, status, version) VALUES #{values}" | ||
ActiveRecord::Base.connection.execute(sql) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters