Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sexier migrations #1163

Merged
merged 2 commits into from
Nov 4, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,11 @@ def column_exists?(table_name, column_name, type = nil, options = {})
# )
#
# See also TableDefinition#column for details on how to create columns.
def create_table(table_name, options = {})
def create_table(table_name, options = {}, &blk)
td = table_definition
td.primary_key(options[:primary_key] || Base.get_primary_key(table_name.to_s.singularize)) unless options[:id] == false

yield td if block_given?
td.instance_eval(&blk) if blk

if options[:force] && table_exists?(table_name)
drop_table(table_name, options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,27 +394,29 @@ def move_table(from, to, options = {}, &block) #:nodoc:
drop_table(from)
end

def copy_table(from, to, options = {}) #:nodoc:
options = options.merge(:id => (!columns(from).detect{|c| c.name == 'id'}.nil? && 'id' == primary_key(from).to_s))
def copy_table(from, to, options = {}, &block) #:nodoc:
from_columns, from_primary_key = columns(from), primary_key(from)
options = options.merge(:id => (!from_columns.detect {|c| c.name == 'id'}.nil? && 'id' == primary_key(from).to_s))
table_definition = nil
create_table(to, options) do |definition|
@definition = definition
columns(from).each do |column|
table_definition = definition
from_columns.each do |column|
column_name = options[:rename] ?
(options[:rename][column.name] ||
options[:rename][column.name.to_sym] ||
column.name) : column.name

@definition.column(column_name, column.type,
table_definition.column(column_name, column.type,
:limit => column.limit, :default => column.default,
:null => column.null)
end
@definition.primary_key(primary_key(from)) if primary_key(from)
yield @definition if block_given?
table_definition.primary_key from_primary_key if from_primary_key
table_definition.instance_eval(&block) if block
end

copy_table_indexes(from, to, options[:rename] || {})
copy_table_contents(from, to,
@definition.columns.map {|column| column.name},
table_definition.columns.map {|column| column.name},
options[:rename] || {})
end

Expand Down
7 changes: 4 additions & 3 deletions activerecord/lib/active_record/session_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ def drop_table!
end

def create_table!
id_col_name, data_col_name = session_id_column, data_column_name
connection_pool.clear_table_cache!(table_name)
connection.create_table(table_name) do |t|
t.string session_id_column, :limit => 255
t.text data_column_name
t.string id_col_name, :limit => 255
t.text data_col_name
end
connection.add_index table_name, session_id_column, :unique => true
connection.add_index table_name, id_col_name, :unique => true
end
end

Expand Down
20 changes: 20 additions & 0 deletions activerecord/test/cases/migration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1653,6 +1653,26 @@ def with_new_table

end # SexyMigrationsTest

class SexierMigrationsTest < ActiveRecord::TestCase
def test_create_table_with_column_without_block_parameter
Person.connection.create_table :testings, :force => true do
column :foo, :string
end
assert Person.connection.column_exists?(:testings, :foo, :string)
ensure
Person.connection.drop_table :testings rescue nil
end

def test_create_table_with_sexy_column_without_block_parameter
Person.connection.create_table :testings, :force => true do
integer :bar
end
assert Person.connection.column_exists?(:testings, :bar, :integer)
ensure
Person.connection.drop_table :testings rescue nil
end
end # SexierMigrationsTest

class MigrationLoggerTest < ActiveRecord::TestCase
def test_migration_should_be_run_without_logger
previous_logger = ActiveRecord::Base.logger
Expand Down