diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2011-11-04 03:29:59 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-11-04 03:29:59 -0700 |
commit | 0e407a90413d8a19002b85508d811ccdf2190783 (patch) | |
tree | 92c006a22d094276225bcfc497a9941eb56e85f8 /activerecord | |
parent | 533a9f84b035756eedf9fdccf0c494dc9701ba72 (diff) | |
parent | 696c2ea68d7cdbf7eb15f7dce2d83498ef659eb7 (diff) | |
download | rails-0e407a90413d8a19002b85508d811ccdf2190783.tar.gz rails-0e407a90413d8a19002b85508d811ccdf2190783.tar.bz2 rails-0e407a90413d8a19002b85508d811ccdf2190783.zip |
Merge pull request #1163 from amatsuda/sexier_migration_31
Sexier migrations
Diffstat (limited to 'activerecord')
4 files changed, 36 insertions, 13 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index 910ef3efce..7226069ebf 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -154,11 +154,11 @@ module ActiveRecord # ) # # 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) diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb index e0e957a12c..f74f3e6ec8 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb @@ -457,28 +457,30 @@ module ActiveRecord 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, :precision => column.precision, :scale => column.scale, :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 diff --git a/activerecord/lib/active_record/session_store.rb b/activerecord/lib/active_record/session_store.rb index 76c37cc367..92550c7efc 100644 --- a/activerecord/lib/active_record/session_store.rb +++ b/activerecord/lib/active_record/session_store.rb @@ -64,12 +64,13 @@ module ActiveRecord 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 diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index f61d0315d0..5c47a8ad33 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -1712,6 +1712,26 @@ if ActiveRecord::Base.connection.supports_migrations? 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 |