Skip to content
This repository
Browse code

Add join table migration generator

For instance, running

    rails g migration CreateMediaJoinTable artists musics:uniq

will create a migration with

    create_join_table :artists, :musics do |t|
      # t.index [:artist_id, :music_id]
      t.index [:music_id, :artist_id], unique: true
    end
  • Loading branch information...
commit 211d88b71b3df2ae161b23579a79f8e937132388 1 parent d481170
Aleksey Magusev authored July 11, 2012
7  activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ -206,9 +206,12 @@ def create_join_table(table_1, table_2, options = {})
206 206
         column_options = options.delete(:column_options) || {}
207 207
         column_options.reverse_merge!({:null => false})
208 208
 
  209
+        t1_column, t2_column = [table_1, table_2].map{ |t| "#{t.to_s.singularize}_id" }
  210
+
209 211
         create_table(join_table_name, options.merge!(:id => false)) do |td|
210  
-          td.integer :"#{table_1.to_s.singularize}_id", column_options
211  
-          td.integer :"#{table_2.to_s.singularize}_id", column_options
  212
+          td.integer t1_column, column_options
  213
+          td.integer t2_column, column_options
  214
+          yield td if block_given?
212 215
         end
213 216
       end
214 217
 
6  activerecord/lib/active_record/migration/join_table.rb
@@ -4,13 +4,11 @@ module JoinTable #:nodoc:
4 4
       private
5 5
 
6 6
       def find_join_table_name(table_1, table_2, options = {})
7  
-        options.delete(:table_name) { join_table_name(table_1, table_2) }
  7
+        options.delete(:table_name) || join_table_name(table_1, table_2)
8 8
       end
9 9
 
10 10
       def join_table_name(table_1, table_2)
11  
-        tables_names = [table_1, table_2].map(&:to_s).sort
12  
-
13  
-        tables_names.join("_").to_sym
  11
+        [table_1, table_2].sort.join("_").to_sym
14 12
       end
15 13
     end
16 14
   end
23  activerecord/lib/rails/generators/active_record/migration/migration_generator.rb
@@ -11,15 +11,28 @@ def create_migration_file
11 11
       end
12 12
 
13 13
       protected
14  
-        attr_reader :migration_action
  14
+      attr_reader :migration_action, :join_tables
15 15
 
16  
-        def set_local_assigns!
17  
-          if file_name =~ /^(add|remove)_.*_(?:to|from)_(.*)/
18  
-            @migration_action = $1
19  
-            @table_name       = $2.pluralize
  16
+      def set_local_assigns!
  17
+        case file_name
  18
+        when /^(add|remove)_.*_(?:to|from)_(.*)/
  19
+          @migration_action = $1
  20
+          @table_name       = $2.pluralize
  21
+        when /join_table/
  22
+          if attributes.length == 2
  23
+            @migration_action = 'join'
  24
+            @join_tables      = attributes.map(&:name)
  25
+
  26
+            set_index_names
20 27
           end
21 28
         end
  29
+      end
22 30
 
  31
+      def set_index_names
  32
+        attributes.each_with_index do |attr, i|
  33
+          attr.index_name = [attr, attributes[i - 1]].map{ |a| :"#{a.name.singularize}_id"}
  34
+        end
  35
+      end
23 36
     end
24 37
   end
25 38
 end
10  activerecord/lib/rails/generators/active_record/migration/templates/migration.rb
@@ -12,6 +12,14 @@ def change
12 12
   <%- end -%>
13 13
 <%- end -%>
14 14
   end
  15
+<%- elsif migration_action == 'join' -%>
  16
+  def change
  17
+    create_join_table :<%= join_tables.first %>, :<%= join_tables.second %> do |t|
  18
+    <%- attributes.each do |attribute| -%>
  19
+      <%= '# ' unless attribute.has_index? -%>t.index <%= attribute.index_name %><%= attribute.inject_index_options %>
  20
+    <%- end -%>
  21
+    end
  22
+  end
15 23
 <%- else -%>
16 24
   def up
17 25
 <% attributes.each do |attribute| -%>
@@ -40,4 +48,4 @@ def down
40 48
 <%- end -%>
41 49
   end
42 50
 <%- end -%>
43  
-end
  51
+end
20  activerecord/test/cases/migration/create_join_table_test.rb
@@ -42,22 +42,36 @@ def test_create_join_table_with_the_proper_order
42 42
       end
43 43
 
44 44
       def test_create_join_table_with_the_table_name
45  
-        connection.create_join_table :artists, :musics, :table_name => :catalog
  45
+        connection.create_join_table :artists, :musics, table_name: :catalog
46 46
 
47 47
         assert_equal %w(artist_id music_id), connection.columns(:catalog).map(&:name).sort
48 48
       end
49 49
 
50 50
       def test_create_join_table_with_the_table_name_as_string
51  
-        connection.create_join_table :artists, :musics, :table_name => 'catalog'
  51
+        connection.create_join_table :artists, :musics, table_name: 'catalog'
52 52
 
53 53
         assert_equal %w(artist_id music_id), connection.columns(:catalog).map(&:name).sort
54 54
       end
55 55
 
56 56
       def test_create_join_table_with_column_options
57  
-        connection.create_join_table :artists, :musics, :column_options => {:null => true}
  57
+        connection.create_join_table :artists, :musics, column_options: {null: true}
58 58
 
59 59
         assert_equal [true, true], connection.columns(:artists_musics).map(&:null)
60 60
       end
  61
+
  62
+      def test_create_join_table_without_indexes
  63
+        connection.create_join_table :artists, :musics
  64
+
  65
+        assert connection.indexes(:artists_musics).blank?
  66
+      end
  67
+
  68
+      def test_create_join_table_with_index
  69
+        connection.create_join_table :artists, :musics do |t|
  70
+          t.index [:artist_id, :music_id]
  71
+        end
  72
+
  73
+        assert_equal [%w(artist_id music_id)], connection.indexes(:artists_musics).map(&:columns)
  74
+      end
61 75
     end
62 76
   end
63 77
 end
3  railties/lib/rails/generators/generated_attribute.rb
@@ -8,6 +8,7 @@ class GeneratedAttribute
8 8
 
9 9
       attr_accessor :name, :type
10 10
       attr_reader   :attr_options
  11
+      attr_writer   :index_name
11 12
 
12 13
       class << self
13 14
         def parse(column_definition)
@@ -94,7 +95,7 @@ def human_name
94 95
       end
95 96
 
96 97
       def index_name
97  
-        if reference?
  98
+        @index_name ||= if reference?
98 99
           polymorphic? ? %w(id type).map { |t| "#{name}_#{t}" } : "#{name}_id"
99 100
         else
100 101
           name
13  railties/test/generators/migration_generator_test.rb
@@ -167,6 +167,19 @@ def test_add_migration_with_references_options
167 167
     end
168 168
   end
169 169
 
  170
+  def test_create_join_table_migration
  171
+    migration = "add_media_join_table"
  172
+    run_generator [migration, "artists", "musics:uniq"]
  173
+
  174
+    assert_migration "db/migrate/#{migration}.rb" do |content|
  175
+      assert_method :change, content do |up|
  176
+        assert_match(/create_join_table :artists, :musics/, up)
  177
+        assert_match(/# t.index \[:artist_id, :music_id\]/, up)
  178
+        assert_match(/  t.index \[:music_id, :artist_id\], unique: true/, up)
  179
+      end
  180
+    end
  181
+  end
  182
+
170 183
   def test_should_create_empty_migrations_if_name_not_start_with_add_or_remove
171 184
     migration = "create_books"
172 185
     run_generator [migration, "title:string", "content:text"]

0 notes on commit 211d88b

Please sign in to comment.
Something went wrong with that request. Please try again.