aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb4
-rw-r--r--activerecord/lib/active_record/migration.rb1
-rw-r--r--activerecord/lib/active_record/schema.rb17
-rw-r--r--activerecord/lib/active_record/schema_dumper.rb10
-rw-r--r--activerecord/test/cases/ar_schema_test.rb23
-rw-r--r--activerecord/test/cases/migration_test.rb20
-rw-r--r--activerecord/test/cases/schema_dumper_test.rb15
7 files changed, 78 insertions, 12 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 730b1c7425..ccb6fe3be2 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ -429,9 +429,13 @@ module ActiveRecord
update "UPDATE #{quote_table_name(sm_table)} SET migrated_at = '#{quoted_date(Time.now)}' WHERE migrated_at IS NULL"
change_column sm_table, :migrated_at, :datetime, :null => false
end
+ unless cols.include?("name")
+ add_column sm_table, :name, :string, :null => false, :default => ""
+ end
else
create_table(sm_table, :id => false) do |schema_migrations_table|
schema_migrations_table.column :version, :string, :null => false
+ schema_migrations_table.column :name, :string, :null => false, :default => ""
schema_migrations_table.column :migrated_at, :datetime, :null => false
end
add_index sm_table, :version, :unique => true,
diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb
index 2cf2f4acd0..d2ebf656cb 100644
--- a/activerecord/lib/active_record/migration.rb
+++ b/activerecord/lib/active_record/migration.rb
@@ -701,6 +701,7 @@ module ActiveRecord
@migrated_versions.push(target.version).sort!
table.insert(
table["version"] => target.version.to_s,
+ table["name"] => File.basename(target.filename,'.rb').gsub(/^\d+_/,''),
table["migrated_at"] => Time.now
)
end
diff --git a/activerecord/lib/active_record/schema.rb b/activerecord/lib/active_record/schema.rb
index c6bb5c1961..c9b5539e4f 100644
--- a/activerecord/lib/active_record/schema.rb
+++ b/activerecord/lib/active_record/schema.rb
@@ -49,10 +49,19 @@ module ActiveRecord
schema = new
schema.instance_eval(&block)
- unless info[:version].blank?
- initialize_schema_migrations_table
- assume_migrated_upto_version(info[:version], schema.migrations_path)
- end
+ initialize_schema_migrations_table
+ assume_migrated_upto_version(info[:version], schema.migrations_path) unless info[:version].blank?
+ end
+
+ def self.migration(version, name="", options={})
+ name, options = "", name if name.is_a?(Hash)
+
+ table = Arel::Table.new(ActiveRecord::Migrator.schema_migrations_table_name)
+ table.insert(
+ table["version"] => version,
+ table["name"] => name,
+ table["migrated_at"] => Time.now
+ )
end
end
end
diff --git a/activerecord/lib/active_record/schema_dumper.rb b/activerecord/lib/active_record/schema_dumper.rb
index e30b481fe1..a53dce0cf4 100644
--- a/activerecord/lib/active_record/schema_dumper.rb
+++ b/activerecord/lib/active_record/schema_dumper.rb
@@ -24,6 +24,7 @@ module ActiveRecord
def dump(stream)
header(stream)
+ migrations(stream)
tables(stream)
trailer(stream)
stream
@@ -62,6 +63,15 @@ HEADER
stream.puts "end"
end
+ def migrations(stream)
+ rows = @connection.select_all("SELECT * FROM #{@connection.quote_table_name(ActiveRecord::Migrator.schema_migrations_table_name)}")
+ rows.each do |migration|
+ line = %Q(migration "#{migration['version']}")
+ line << %Q(, "#{migration['name']}") unless migration['name'].blank?
+ stream.puts line
+ end
+ end
+
def tables(stream)
@connection.tables.sort.each do |tbl|
next if ['schema_migrations', ignore_tables].flatten.any? do |ignored|
diff --git a/activerecord/test/cases/ar_schema_test.rb b/activerecord/test/cases/ar_schema_test.rb
index 588adc38e3..e7f3907ff5 100644
--- a/activerecord/test/cases/ar_schema_test.rb
+++ b/activerecord/test/cases/ar_schema_test.rb
@@ -39,4 +39,27 @@ if ActiveRecord::Base.connection.supports_migrations?
end
end
+ class ActiveRecordSchemaMigrationsTest < ActiveRecordSchemaTest
+ def setup
+ super
+ @sm_table = ActiveRecord::Migrator.schema_migrations_table_name
+ @connection.execute "DELETE FROM #{@connection.quote_table_name(@sm_table)}"
+ end
+
+ def test_migration_adds_row_to_migrations_table
+ ActiveRecord::Schema.migration("123001")
+ ActiveRecord::Schema.migration("123002", "add_magic_power_to_unicorns")
+ rows = @connection.select_all("SELECT * FROM #{@connection.quote_table_name(@sm_table)}")
+ assert_equal 2, rows.length
+
+ assert_equal "123001", rows[0]["version"]
+ assert_equal "", rows[0]["name"]
+ assert_match /^2\d\d\d-/, rows[0]["migrated_at"]
+
+ assert_equal "123002", rows[1]["version"]
+ assert_equal "add_magic_power_to_unicorns", rows[1]["name"]
+ assert_match /^2\d\d\d-/, rows[1]["migrated_at"]
+ end
+ end
+
end
diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb
index e76097d42d..497391e713 100644
--- a/activerecord/test/cases/migration_test.rb
+++ b/activerecord/test/cases/migration_test.rb
@@ -50,10 +50,10 @@ if ActiveRecord::Base.connection.supports_migrations?
@conn.initialize_schema_migrations_table
columns = @conn.columns(ActiveRecord::Migrator.schema_migrations_table_name).collect(&:name)
- %w[version migrated_at].each { |col| assert columns.include?(col) }
+ %w[version name migrated_at].each { |col| assert columns.include?(col) }
end
- def test_add_migrated_at_to_exisiting_schema_migrations
+ def test_add_name_and_migrated_at_to_exisiting_schema_migrations
sm_table = ActiveRecord::Migrator.schema_migrations_table_name
@conn.create_table(sm_table, :id => false) do |schema_migrations_table|
schema_migrations_table.column :version, :string, :null => false
@@ -63,9 +63,9 @@ if ActiveRecord::Base.connection.supports_migrations?
@conn.initialize_schema_migrations_table
- m_ats = @conn.select_values("SELECT migrated_at FROM #{@conn.quote_table_name(sm_table)}")
- assert_equal 2, m_ats.length
- assert_equal 2, m_ats.compact.length
+ rows = @conn.select_all("SELECT * FROM #{@conn.quote_table_name(sm_table)}")
+ assert rows[0].has_key?("name")
+ assert rows[0].has_key?("migrated_at")
end
end
@@ -1499,15 +1499,19 @@ if ActiveRecord::Base.connection.supports_migrations?
ActiveRecord::Base.table_name_suffix = ""
end
- def test_migration_row_includes_timestamp
+ def test_migration_row_includes_name_and_timestamp
conn = ActiveRecord::Base.connection
sm_table = ActiveRecord::Migrator.schema_migrations_table_name
ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/valid")
- conn.select_all("SELECT * FROM #{conn.quote_table_name(sm_table)}").each do |row|
- assert_match /^2\d\d\d-/, row["migrated_at"], "missing migrated_at"
+ rows = conn.select_all("SELECT * FROM #{conn.quote_table_name(sm_table)}")
+ rows.each do |row|
+ assert_match( /^2\d\d\d-/, row["migrated_at"], "missing migrated_at" )
end
+ assert_equal "people_have_last_names", rows[0]["name"]
+ assert_equal "we_need_reminders", rows[1]["name"]
+ assert_equal "innocent_jointable", rows[2]["name"]
end
def test_proper_table_name
diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb
index 9b2c7c00df..fdcf1b5cd4 100644
--- a/activerecord/test/cases/schema_dumper_test.rb
+++ b/activerecord/test/cases/schema_dumper_test.rb
@@ -17,6 +17,21 @@ class SchemaDumperTest < ActiveRecord::TestCase
assert_no_match %r{create_table "schema_migrations"}, output
end
+ def test_schema_dump_includes_migrations
+ conn = ActiveRecord::Base.connection
+ sm_table = ActiveRecord::Migrator.schema_migrations_table_name
+ conn.execute "DELETE FROM #{conn.quote_table_name(sm_table)}"
+ conn.remove_column "people", "last_name" rescue nil
+ conn.drop_table "reminders" rescue nil
+ conn.drop_table "people_reminders" rescue nil
+ ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/valid")
+
+ output = standard_dump
+ assert_match %r{migration "1", "people_have_last_names"}, output
+ assert_match %r{migration "2", "we_need_reminders"}, output
+ assert_match %r{migration "3", "innocent_jointable"}, output
+ end
+
def test_schema_dump_excludes_sqlite_sequence
output = standard_dump
assert_no_match %r{create_table "sqlite_sequence"}, output