Skip to content

Commit

Permalink
Fixing tests and re-locating error checking.
Browse files Browse the repository at this point in the history
  • Loading branch information
schneems committed Jan 8, 2016
1 parent f6628ad commit d70c68d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
15 changes: 8 additions & 7 deletions activerecord/lib/active_record/migration.rb
Expand Up @@ -164,7 +164,7 @@ def initialize(env = "production")
end

class EnvironmentMismatchError < ActiveRecordError
def initialize(current: , stored: )
def initialize(current: nil, stored: nil)
msg = "You are attempting to modify a database that was last run in #{ stored } environment.\n"
msg << "You are running in #{ current } environment."
msg << "if you are sure you want to continue, run the same command with the environment variable\n"
Expand Down Expand Up @@ -1238,19 +1238,20 @@ def record_version_state_after_migrating(version)
end

def self.last_stored_environment
ActiveRecord::InternalMetadata[:environment]
return nil if current_version == 0
raise NoEnvironmentInSchemaError unless ActiveRecord::InternalMetadata.table_exists?

environment = ActiveRecord::InternalMetadata[:environment]
raise NoEnvironmentInSchemaError unless environment
environment
end

def self.current_environment
ActiveRecord::ConnectionHandling::DEFAULT_ENV.call
end

def self.protected_environment?
return false if current_version == 0
raise NoEnvironmentInSchemaError unless ActiveRecord::InternalMetadata.table_exists?

raise NoEnvironmentInSchemaError unless last_stored_environment
ActiveRecord::Base.protected_environments.include?(last_stored_environment)
ActiveRecord::Base.protected_environments.include?(last_stored_environment) if last_stored_environment
end

def up?
Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/tasks/database_tasks.rb
Expand Up @@ -51,8 +51,8 @@ def check_protected_environments!
raise ActiveRecord::ProtectedEnvironmentError.new(stored)
end

if current != stored
raise EnvironmentMismatchError.new(current: current, stored: stored)
if stored && stored != current
raise ActiveRecord::EnvironmentMismatchError.new(current: current, stored: stored)
end
end
rescue ActiveRecord::NoDatabaseError
Expand Down
8 changes: 4 additions & 4 deletions activerecord/test/cases/schema_dumper_test.rb
Expand Up @@ -38,7 +38,7 @@ def test_schema_dump
assert_match %r{create_table "accounts"}, output
assert_match %r{create_table "authors"}, output
assert_no_match %r{create_table "schema_migrations"}, output
assert_no_match %r{create_table "internal_metadatas"}, output
assert_no_match %r{create_table "active_record_internal_metadatas"}, output
end

def test_schema_dump_uses_force_cascade_on_create_table
Expand Down Expand Up @@ -159,15 +159,15 @@ def test_schema_dump_with_string_ignored_table
assert_no_match %r{create_table "accounts"}, output
assert_match %r{create_table "authors"}, output
assert_no_match %r{create_table "schema_migrations"}, output
assert_no_match %r{create_table "internal_metadatas"}, output
assert_no_match %r{create_table "active_record_internal_metadatas"}, output
end

def test_schema_dump_with_regexp_ignored_table
output = dump_all_table_schema([/^account/])
assert_no_match %r{create_table "accounts"}, output
assert_match %r{create_table "authors"}, output
assert_no_match %r{create_table "schema_migrations"}, output
assert_no_match %r{create_table "internal_metadatas"}, output
assert_no_match %r{create_table "active_record_internal_metadatas"}, output
end

def test_schema_dumps_index_columns_in_right_order
Expand Down Expand Up @@ -345,7 +345,7 @@ def test_schema_dump_with_table_name_prefix_and_suffix
assert_no_match %r{create_table "foo_.+_bar"}, output
assert_no_match %r{add_index "foo_.+_bar"}, output
assert_no_match %r{create_table "schema_migrations"}, output
assert_no_match %r{create_table "internal_metadatas"}, output
assert_no_match %r{create_table "active_record_internal_metadatas"}, output

if ActiveRecord::Base.connection.supports_foreign_keys?
assert_no_match %r{add_foreign_key "foo_.+_bar"}, output
Expand Down
2 changes: 1 addition & 1 deletion railties/test/application/bin_setup_test.rb
Expand Up @@ -28,7 +28,7 @@ def test_bin_setup
assert_not File.exist?("tmp/restart.txt")
`bin/setup 2>&1`
assert_equal 0, File.size("log/my.log")
assert_equal '["articles", "schema_migrations", "internal_metadatas"]', list_tables.call
assert_equal '["articles", "schema_migrations", "active_record_internal_metadatas"]', list_tables.call
assert File.exist?("tmp/restart.txt")
end
end
Expand Down
4 changes: 2 additions & 2 deletions railties/test/application/rake/dbs_test.rb
Expand Up @@ -222,14 +222,14 @@ def db_structure_dump_and_load(expected_database)

assert_equal '["posts"]', list_tables[]
`bin/rake db:schema:load`
assert_equal '["posts", "comments", "schema_migrations", "internal_metadatas"]', list_tables[]
assert_equal '["posts", "comments", "schema_migrations", "active_record_internal_metadatas"]', list_tables[]

app_file 'db/structure.sql', <<-SQL
CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255));
SQL

`bin/rake db:structure:load`
assert_equal '["posts", "comments", "schema_migrations", "internal_metadatas", "users"]', list_tables[]
assert_equal '["posts", "comments", "schema_migrations", "active_record_internal_metadatas", "users"]', list_tables[]
end
end

Expand Down

0 comments on commit d70c68d

Please sign in to comment.