Skip to content

Commit 0e407a9

Browse files
committed
Merge pull request rails#1163 from amatsuda/sexier_migration_31
Sexier migrations
2 parents 533a9f8 + 696c2ea commit 0e407a9

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,11 @@ def column_exists?(table_name, column_name, type = nil, options = {})
154154
# )
155155
#
156156
# See also TableDefinition#column for details on how to create columns.
157-
def create_table(table_name, options = {})
157+
def create_table(table_name, options = {}, &blk)
158158
td = table_definition
159159
td.primary_key(options[:primary_key] || Base.get_primary_key(table_name.to_s.singularize)) unless options[:id] == false
160160

161-
yield td if block_given?
161+
td.instance_eval(&blk) if blk
162162

163163
if options[:force] && table_exists?(table_name)
164164
drop_table(table_name)

activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -457,28 +457,30 @@ def move_table(from, to, options = {}, &block) #:nodoc:
457457
drop_table(from)
458458
end
459459

460-
def copy_table(from, to, options = {}) #:nodoc:
461-
options = options.merge(:id => (!columns(from).detect{|c| c.name == 'id'}.nil? && 'id' == primary_key(from).to_s))
460+
def copy_table(from, to, options = {}, &block) #:nodoc:
461+
from_columns, from_primary_key = columns(from), primary_key(from)
462+
options = options.merge(:id => (!from_columns.detect {|c| c.name == 'id'}.nil? && 'id' == primary_key(from).to_s))
463+
table_definition = nil
462464
create_table(to, options) do |definition|
463-
@definition = definition
464-
columns(from).each do |column|
465+
table_definition = definition
466+
from_columns.each do |column|
465467
column_name = options[:rename] ?
466468
(options[:rename][column.name] ||
467469
options[:rename][column.name.to_sym] ||
468470
column.name) : column.name
469471

470-
@definition.column(column_name, column.type,
472+
table_definition.column(column_name, column.type,
471473
:limit => column.limit, :default => column.default,
472474
:precision => column.precision, :scale => column.scale,
473475
:null => column.null)
474476
end
475-
@definition.primary_key(primary_key(from)) if primary_key(from)
476-
yield @definition if block_given?
477+
table_definition.primary_key from_primary_key if from_primary_key
478+
table_definition.instance_eval(&block) if block
477479
end
478480

479481
copy_table_indexes(from, to, options[:rename] || {})
480482
copy_table_contents(from, to,
481-
@definition.columns.map {|column| column.name},
483+
table_definition.columns.map {|column| column.name},
482484
options[:rename] || {})
483485
end
484486

activerecord/lib/active_record/session_store.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,13 @@ def drop_table!
6464
end
6565

6666
def create_table!
67+
id_col_name, data_col_name = session_id_column, data_column_name
6768
connection_pool.clear_table_cache!(table_name)
6869
connection.create_table(table_name) do |t|
69-
t.string session_id_column, :limit => 255
70-
t.text data_column_name
70+
t.string id_col_name, :limit => 255
71+
t.text data_col_name
7172
end
72-
connection.add_index table_name, session_id_column, :unique => true
73+
connection.add_index table_name, id_col_name, :unique => true
7374
end
7475
end
7576

activerecord/test/cases/migration_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,6 +1712,26 @@ def with_new_table
17121712

17131713
end # SexyMigrationsTest
17141714

1715+
class SexierMigrationsTest < ActiveRecord::TestCase
1716+
def test_create_table_with_column_without_block_parameter
1717+
Person.connection.create_table :testings, :force => true do
1718+
column :foo, :string
1719+
end
1720+
assert Person.connection.column_exists?(:testings, :foo, :string)
1721+
ensure
1722+
Person.connection.drop_table :testings rescue nil
1723+
end
1724+
1725+
def test_create_table_with_sexy_column_without_block_parameter
1726+
Person.connection.create_table :testings, :force => true do
1727+
integer :bar
1728+
end
1729+
assert Person.connection.column_exists?(:testings, :bar, :integer)
1730+
ensure
1731+
Person.connection.drop_table :testings rescue nil
1732+
end
1733+
end # SexierMigrationsTest
1734+
17151735
class MigrationLoggerTest < ActiveRecord::TestCase
17161736
def test_migration_should_be_run_without_logger
17171737
previous_logger = ActiveRecord::Base.logger

0 commit comments

Comments
 (0)