From: Andy Allan Date: Wed, 23 Feb 2022 15:17:21 +0000 (+0000) Subject: Enable active_record.belongs_to_required_by_default X-Git-Tag: live~1739^2 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/619ac4c5b2fbdbc610007376ee11e3921e475520 Enable active_record.belongs_to_required_by_default This switches the logic so that belongs_to parent objects must exist by default, and marks the optional ones explicitly. This is reflected in the null/not_null status on the relevant db columns. --- diff --git a/app/models/access_token.rb b/app/models/access_token.rb index 1a5ff8553..53590a594 100644 --- a/app/models/access_token.rb +++ b/app/models/access_token.rb @@ -36,8 +36,8 @@ # class AccessToken < OauthToken - belongs_to :user - belongs_to :client_application + belongs_to :user, :optional => true + belongs_to :client_application, :optional => true scope :valid, -> { where(:invalidated_at => nil) } diff --git a/app/models/client_application.rb b/app/models/client_application.rb index 1b2faafbb..d9d6b2df8 100644 --- a/app/models/client_application.rb +++ b/app/models/client_application.rb @@ -31,7 +31,7 @@ # class ClientApplication < ApplicationRecord - belongs_to :user + belongs_to :user, :optional => true has_many :tokens, :class_name => "OauthToken", :dependent => :delete_all has_many :access_tokens has_many :oauth2_verifiers diff --git a/app/models/issue.rb b/app/models/issue.rb index c94fe56a7..207af63c4 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -32,9 +32,9 @@ class Issue < ApplicationRecord belongs_to :reportable, :polymorphic => true - belongs_to :reported_user, :class_name => "User" - belongs_to :user_resolved, :class_name => "User", :foreign_key => :resolved_by - belongs_to :user_updated, :class_name => "User", :foreign_key => :updated_by + belongs_to :reported_user, :class_name => "User", :optional => true + belongs_to :user_resolved, :class_name => "User", :foreign_key => :resolved_by, :optional => true + belongs_to :user_updated, :class_name => "User", :foreign_key => :updated_by, :optional => true has_many :reports, :dependent => :destroy has_many :comments, :class_name => "IssueComment", :dependent => :destroy diff --git a/app/models/note_comment.rb b/app/models/note_comment.rb index eed917eee..999d04507 100644 --- a/app/models/note_comment.rb +++ b/app/models/note_comment.rb @@ -25,7 +25,7 @@ class NoteComment < ApplicationRecord belongs_to :note, :touch => true - belongs_to :author, :class_name => "User" + belongs_to :author, :class_name => "User", :optional => true validates :id, :uniqueness => true, :presence => { :on => :update }, :numericality => { :on => :update, :only_integer => true } diff --git a/app/models/oauth_token.rb b/app/models/oauth_token.rb index affdcdeb8..b4519efe2 100644 --- a/app/models/oauth_token.rb +++ b/app/models/oauth_token.rb @@ -36,8 +36,8 @@ # class OauthToken < ApplicationRecord - belongs_to :client_application - belongs_to :user + belongs_to :client_application, :optional => true + belongs_to :user, :optional => true scope :authorized, -> { where("authorized_at IS NOT NULL and invalidated_at IS NULL") } diff --git a/app/models/old_node.rb b/app/models/old_node.rb index 3caf0442b..3261c9a0e 100644 --- a/app/models/old_node.rb +++ b/app/models/old_node.rb @@ -46,7 +46,7 @@ class OldNode < ApplicationRecord validate :validate_position belongs_to :changeset - belongs_to :redaction + belongs_to :redaction, :optional => true belongs_to :current_node, :class_name => "Node", :foreign_key => "node_id" has_many :old_tags, :class_name => "OldNodeTag", :foreign_key => [:node_id, :version] diff --git a/app/models/old_relation.rb b/app/models/old_relation.rb index 29edb90ad..e948cae5e 100644 --- a/app/models/old_relation.rb +++ b/app/models/old_relation.rb @@ -31,7 +31,7 @@ class OldRelation < ApplicationRecord include Redactable belongs_to :changeset - belongs_to :redaction + belongs_to :redaction, :optional => true belongs_to :current_relation, :class_name => "Relation", :foreign_key => "relation_id" has_many :old_members, -> { order(:sequence_id) }, :class_name => "OldRelationMember", :foreign_key => [:relation_id, :version] diff --git a/app/models/old_way.rb b/app/models/old_way.rb index 7af8906b1..03c281e5a 100644 --- a/app/models/old_way.rb +++ b/app/models/old_way.rb @@ -31,7 +31,7 @@ class OldWay < ApplicationRecord include Redactable belongs_to :changeset - belongs_to :redaction + belongs_to :redaction, :optional => true belongs_to :current_way, :class_name => "Way", :foreign_key => "way_id" has_many :old_nodes, :class_name => "OldWayNode", :foreign_key => [:way_id, :version] diff --git a/app/models/trace.rb b/app/models/trace.rb index b9823ec90..feefc0773 100644 --- a/app/models/trace.rb +++ b/app/models/trace.rb @@ -31,8 +31,8 @@ class Trace < ApplicationRecord self.table_name = "gpx_files" belongs_to :user, :counter_cache => true - has_many :tags, :class_name => "Tracetag", :foreign_key => "gpx_id", :dependent => :delete_all, :inverse_of => "trace" - has_many :points, :class_name => "Tracepoint", :foreign_key => "gpx_id", :dependent => :delete_all, :inverse_of => "trace" + has_many :tags, :class_name => "Tracetag", :foreign_key => "gpx_id", :dependent => :delete_all, :inverse_of => :trace + has_many :points, :class_name => "Tracepoint", :foreign_key => "gpx_id", :dependent => :delete_all, :inverse_of => :trace scope :visible, -> { where(:visible => true) } scope :visible_to, ->(u) { visible.where("visibility IN ('public', 'identifiable') OR user_id = ?", u) } diff --git a/app/models/user_block.rb b/app/models/user_block.rb index 47ff9ff08..46a8c282b 100644 --- a/app/models/user_block.rb +++ b/app/models/user_block.rb @@ -30,7 +30,7 @@ class UserBlock < ApplicationRecord belongs_to :user, :class_name => "User" belongs_to :creator, :class_name => "User" - belongs_to :revoker, :class_name => "User" + belongs_to :revoker, :class_name => "User", :optional => true PERIODS = Settings.user_block_periods diff --git a/config/application.rb b/config/application.rb index 7b576d38f..10a2372db 100644 --- a/config/application.rb +++ b/config/application.rb @@ -35,10 +35,6 @@ module OpenStreetMap # This has defaulted to false since rails 6.0 config.action_view.default_enforce_utf8 = true - # This defaults to true from rails 5.0 but our code doesn't comply - # with it at all so we turn it off - config.active_record.belongs_to_required_by_default = false unless Settings.status == "database_offline" - # Use SQL instead of Active Record's schema dumper when creating the database. # This is necessary if your schema can't be completely dumped by the schema dumper, # like if you have constraints or database-specific column types