Skip to content
This repository
Browse code

Add rename_table to mysql, sqlite and postgres adapters for use in mi…

…grations

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2477 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit ebfddf35b9d4ebcdb5be37df77b83b2e1e16df97 1 parent 7e70fc1
authored
9  activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ -90,6 +90,13 @@ def create_table(name, options = {})
90 90
         create_sql << ") #{options[:options]}"
91 91
         execute create_sql
92 92
       end
  93
+      
  94
+      # Renames a table.
  95
+      # ===== Example
  96
+      #  rename_table('octopuses', 'octopi')
  97
+      def rename_table(name, new_name)
  98
+        raise NotImplementedError, "rename_table is not implemented"
  99
+      end
93 100
 
94 101
       # Drops a table from the database.
95 102
       def drop_table(name)
@@ -109,7 +116,7 @@ def add_column(table_name, column_name, type, options = {})
109 116
       #  remove_column(:suppliers, :qualification)
110 117
       def remove_column(table_name, column_name)
111 118
         execute "ALTER TABLE #{table_name} DROP #{column_name}"
112  
-      end      
  119
+      end
113 120
 
114 121
       # Changes the column's definition according to the new options.
115 122
       # See TableDefinition#column for details of the options you can use.
4  activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -258,6 +258,10 @@ def columns(table_name, name = nil)#:nodoc:
258 258
       def create_table(name, options = {}) #:nodoc:
259 259
         super(name, {:options => "ENGINE=InnoDB"}.merge(options))
260 260
       end
  261
+      
  262
+      def rename_table(name, new_name)
  263
+        execute "RENAME TABLE #{name} TO #{new_name}"
  264
+      end  
261 265
 
262 266
       def change_column_default(table_name, column_name, default) #:nodoc:
263 267
         current_type = select_one("SHOW COLUMNS FROM #{table_name} LIKE '#{column_name}'")["Type"]
4  activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -198,6 +198,10 @@ def schema_search_path=(schema_csv) #:nodoc:
198 198
       def schema_search_path #:nodoc:
199 199
         @schema_search_path ||= query('SHOW search_path')[0][0]
200 200
       end
  201
+      
  202
+      def rename_table(name, new_name)
  203
+        execute "ALTER TABLE #{name} RENAME TO #{new_name}"
  204
+      end
201 205
             
202 206
       def add_column(table_name, column_name, type, options = {})
203 207
         native_type = native_database_types[type]
4  activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
@@ -214,6 +214,10 @@ def remove_index(table_name, options={}) #:nodoc:
214 214
 
215 215
         execute "DROP INDEX #{index_name}"
216 216
       end
  217
+      
  218
+      def rename_table(name, new_name)
  219
+        move_table(name, new_name)
  220
+      end
217 221
 
218 222
       def add_column(table_name, column_name, type, options = {}) #:nodoc:
219 223
         alter_table(table_name) do |definition|
19  activerecord/test/migration_test.rb
@@ -180,6 +180,25 @@ def test_add_rename
180 180
       
181 181
     end
182 182
     
  183
+    def test_rename_table
  184
+      begin
  185
+        ActiveRecord::Base.connection.create_table :octopuses do |t|
  186
+          t.column :url, :string
  187
+        end          
  188
+        ActiveRecord::Base.connection.rename_table :octopuses, :octopi
  189
+        
  190
+        assert_nothing_raised do
  191
+          ActiveRecord::Base.connection.execute "INSERT INTO octopi (url) VALUES ('http://www.foreverflying.com/octopus-black7.jpg')"
  192
+        end
  193
+
  194
+        assert_equal 'http://www.foreverflying.com/octopus-black7.jpg', ActiveRecord::Base.connection.select_value("SELECT url FROM octopi WHERE id=1")
  195
+      
  196
+      ensure
  197
+        ActiveRecord::Base.connection.drop_table :octopuses rescue nil
  198
+        ActiveRecord::Base.connection.drop_table :octopi rescue nil
  199
+      end
  200
+    end
  201
+    
183 202
     def test_change_column
184 203
       Person.connection.add_column "people", "bio", :string
185 204
       assert_nothing_raised { Person.connection.change_column "people", "bio", :text }

0 notes on commit ebfddf3

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