From 909818b93b8f1bd4d7053a1c5d8135b9b0cbe865 Mon Sep 17 00:00:00 2001
From: Prathamesh Sonpatki <csonpatki@gmail.com>
Date: Sun, 24 Jan 2016 16:46:12 +0530
Subject: Pare back default `index` option for the migration generator

- Using `references` or `belongs_to` in migrations will always add index
  for the referenced column by default, without adding `index:true` option
  to generated migration file.
- Users can opt out of this by passing `index: false`.
- Legacy migrations won't be affected by this change. They will continue
  to run as they were before.
- Fixes #18146
---
 .../connection_adapters/abstract/schema_definitions.rb       |  2 +-
 activerecord/lib/active_record/migration/compatibility.rb    | 12 ++++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

(limited to 'activerecord/lib')

diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
index 1cda23dc1d..690e0ba957 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
@@ -69,7 +69,7 @@ module ActiveRecord
       def initialize(
         name,
         polymorphic: false,
-        index: false,
+        index: true,
         foreign_key: false,
         type: :integer,
         **options
diff --git a/activerecord/lib/active_record/migration/compatibility.rb b/activerecord/lib/active_record/migration/compatibility.rb
index 1b94573870..5d742b523b 100644
--- a/activerecord/lib/active_record/migration/compatibility.rb
+++ b/activerecord/lib/active_record/migration/compatibility.rb
@@ -5,6 +5,12 @@ module ActiveRecord
 
       module FourTwoShared
         module TableDefinition
+          def references(*, **options)
+            options[:index] ||= false
+            super
+          end
+          alias :belongs_to :references
+
           def timestamps(*, **options)
             options[:null] = true if options[:null].nil?
             super
@@ -24,6 +30,12 @@ module ActiveRecord
           end
         end
 
+        def add_reference(*, **options)
+          options[:index] ||= false
+          super
+        end
+        alias :add_belongs_to :add_reference
+
         def add_timestamps(*, **options)
           options[:null] = true if options[:null].nil?
           super
-- 
cgit v1.2.3