mirror of
https://github.com/github/rails.git
synced 2026-01-26 14:58:11 -05:00
Add rename_table to mysql, sqlite and postgres adapters for use in migrations
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2477 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
@@ -90,6 +90,13 @@ module ActiveRecord
|
||||
create_sql << ") #{options[:options]}"
|
||||
execute create_sql
|
||||
end
|
||||
|
||||
# Renames a table.
|
||||
# ===== Example
|
||||
# rename_table('octopuses', 'octopi')
|
||||
def rename_table(name, new_name)
|
||||
raise NotImplementedError, "rename_table is not implemented"
|
||||
end
|
||||
|
||||
# Drops a table from the database.
|
||||
def drop_table(name)
|
||||
@@ -109,7 +116,7 @@ module ActiveRecord
|
||||
# remove_column(:suppliers, :qualification)
|
||||
def remove_column(table_name, column_name)
|
||||
execute "ALTER TABLE #{table_name} DROP #{column_name}"
|
||||
end
|
||||
end
|
||||
|
||||
# Changes the column's definition according to the new options.
|
||||
# See TableDefinition#column for details of the options you can use.
|
||||
|
||||
@@ -258,6 +258,10 @@ module ActiveRecord
|
||||
def create_table(name, options = {}) #:nodoc:
|
||||
super(name, {:options => "ENGINE=InnoDB"}.merge(options))
|
||||
end
|
||||
|
||||
def rename_table(name, new_name)
|
||||
execute "RENAME TABLE #{name} TO #{new_name}"
|
||||
end
|
||||
|
||||
def change_column_default(table_name, column_name, default) #:nodoc:
|
||||
current_type = select_one("SHOW COLUMNS FROM #{table_name} LIKE '#{column_name}'")["Type"]
|
||||
|
||||
@@ -198,6 +198,10 @@ module ActiveRecord
|
||||
def schema_search_path #:nodoc:
|
||||
@schema_search_path ||= query('SHOW search_path')[0][0]
|
||||
end
|
||||
|
||||
def rename_table(name, new_name)
|
||||
execute "ALTER TABLE #{name} RENAME TO #{new_name}"
|
||||
end
|
||||
|
||||
def add_column(table_name, column_name, type, options = {})
|
||||
native_type = native_database_types[type]
|
||||
|
||||
@@ -214,6 +214,10 @@ module ActiveRecord
|
||||
|
||||
execute "DROP INDEX #{index_name}"
|
||||
end
|
||||
|
||||
def rename_table(name, new_name)
|
||||
move_table(name, new_name)
|
||||
end
|
||||
|
||||
def add_column(table_name, column_name, type, options = {}) #:nodoc:
|
||||
alter_table(table_name) do |definition|
|
||||
|
||||
@@ -180,6 +180,25 @@ if ActiveRecord::Base.connection.supports_migrations?
|
||||
|
||||
end
|
||||
|
||||
def test_rename_table
|
||||
begin
|
||||
ActiveRecord::Base.connection.create_table :octopuses do |t|
|
||||
t.column :url, :string
|
||||
end
|
||||
ActiveRecord::Base.connection.rename_table :octopuses, :octopi
|
||||
|
||||
assert_nothing_raised do
|
||||
ActiveRecord::Base.connection.execute "INSERT INTO octopi (url) VALUES ('http://www.foreverflying.com/octopus-black7.jpg')"
|
||||
end
|
||||
|
||||
assert_equal 'http://www.foreverflying.com/octopus-black7.jpg', ActiveRecord::Base.connection.select_value("SELECT url FROM octopi WHERE id=1")
|
||||
|
||||
ensure
|
||||
ActiveRecord::Base.connection.drop_table :octopuses rescue nil
|
||||
ActiveRecord::Base.connection.drop_table :octopi rescue nil
|
||||
end
|
||||
end
|
||||
|
||||
def test_change_column
|
||||
Person.connection.add_column "people", "bio", :string
|
||||
assert_nothing_raised { Person.connection.change_column "people", "bio", :text }
|
||||
|
||||
Reference in New Issue
Block a user