提交 4f2bce95 编写于 作者: S Sean Griffin

Merge pull request #23025 from yahonda/shorten_internal_metadata_table_name_less_than_30_byte

Shorten ActiveRecord::InternalMetadata.table_name to ar_internal_metadata
......@@ -14,6 +14,10 @@ def table_name
"#{table_name_prefix}#{ActiveRecord::Base.internal_metadata_table_name}#{table_name_suffix}"
end
def original_table_name
"#{table_name_prefix}active_record_internal_metadatas#{table_name_suffix}"
end
def []=(key, value)
first_or_initialize(key: key).update_attributes!(value: value)
end
......@@ -26,8 +30,17 @@ def table_exists?
ActiveSupport::Deprecation.silence { connection.table_exists?(table_name) }
end
def original_table_exists?
# This method will be removed in Rails 5.1
# Since it is only necessary when `active_record_internal_metadatas` could exist
ActiveSupport::Deprecation.silence { connection.table_exists?(original_table_name) }
end
# Creates an internal metadata table with columns +key+ and +value+
def create_table
if original_table_exists?
connection.rename_table(original_table_name, table_name)
end
unless table_exists?
key_options = connection.internal_string_options_for_primary_key
......
......@@ -44,9 +44,9 @@ module ModelSchema
##
# :singleton-method:
# Accessor for the name of the internal metadata table. By default, the value is "active_record_internal_metadatas"
# Accessor for the name of the internal metadata table. By default, the value is "ar_internal_metadata"
class_attribute :internal_metadata_table_name, instance_accessor: false
self.internal_metadata_table_name = "active_record_internal_metadatas"
self.internal_metadata_table_name = "ar_internal_metadata"
##
# :singleton-method:
......
......@@ -357,14 +357,14 @@ def test_schema_migrations_table_name
def test_internal_metadata_table_name
original_internal_metadata_table_name = ActiveRecord::Base.internal_metadata_table_name
assert_equal "active_record_internal_metadatas", ActiveRecord::InternalMetadata.table_name
ActiveRecord::Base.table_name_prefix = "prefix_"
ActiveRecord::Base.table_name_suffix = "_suffix"
assert_equal "ar_internal_metadata", ActiveRecord::InternalMetadata.table_name
ActiveRecord::Base.table_name_prefix = "p_"
ActiveRecord::Base.table_name_suffix = "_s"
Reminder.reset_table_name
assert_equal "prefix_active_record_internal_metadatas_suffix", ActiveRecord::InternalMetadata.table_name
assert_equal "p_ar_internal_metadata_s", ActiveRecord::InternalMetadata.table_name
ActiveRecord::Base.internal_metadata_table_name = "changed"
Reminder.reset_table_name
assert_equal "prefix_changed_suffix", ActiveRecord::InternalMetadata.table_name
assert_equal "p_changed_s", ActiveRecord::InternalMetadata.table_name
ActiveRecord::Base.table_name_prefix = ""
ActiveRecord::Base.table_name_suffix = ""
Reminder.reset_table_name
......@@ -426,6 +426,21 @@ def test_migration_sets_internal_metadata_even_when_fully_migrated
ENV["RACK_ENV"] = original_rack_env
end
def test_rename_internal_metadata_table
original_internal_metadata_table_name = ActiveRecord::Base.internal_metadata_table_name
ActiveRecord::Base.internal_metadata_table_name = "active_record_internal_metadatas"
Reminder.reset_table_name
ActiveRecord::Base.internal_metadata_table_name = original_internal_metadata_table_name
Reminder.reset_table_name
assert_equal "ar_internal_metadata", ActiveRecord::InternalMetadata.table_name
ensure
ActiveRecord::Base.internal_metadata_table_name = original_internal_metadata_table_name
Reminder.reset_table_name
end
def test_proper_table_name_on_migration
reminder_class = new_isolated_reminder_class
migration = ActiveRecord::Migration.new
......
......@@ -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 "active_record_internal_metadatas"}, output
assert_no_match %r{create_table "ar_internal_metadata"}, output
end
def test_schema_dump_uses_force_cascade_on_create_table
......@@ -159,7 +159,7 @@ 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 "active_record_internal_metadatas"}, output
assert_no_match %r{create_table "ar_internal_metadata"}, output
end
def test_schema_dump_with_regexp_ignored_table
......@@ -167,7 +167,7 @@ def test_schema_dump_with_regexp_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 "active_record_internal_metadatas"}, output
assert_no_match %r{create_table "ar_internal_metadata"}, output
end
def test_schema_dumps_index_columns_in_right_order
......@@ -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 "active_record_internal_metadatas"}, output
assert_no_match %r{create_table "ar_internal_metadata"}, output
if ActiveRecord::Base.connection.supports_foreign_keys?
assert_no_match %r{add_foreign_key "foo_.+_bar"}, output
......
......@@ -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/test.log")
assert_equal '["articles", "schema_migrations", "active_record_internal_metadatas"]', list_tables.call
assert_equal '["articles", "schema_migrations", "ar_internal_metadata"]', list_tables.call
assert File.exist?("tmp/restart.txt")
end
end
......
......@@ -222,14 +222,14 @@ def db_structure_dump_and_load(expected_database)
assert_equal '["posts"]', list_tables[]
`bin/rails db:schema:load`
assert_equal '["posts", "comments", "schema_migrations", "active_record_internal_metadatas"]', list_tables[]
assert_equal '["posts", "comments", "schema_migrations", "ar_internal_metadata"]', list_tables[]
app_file 'db/structure.sql', <<-SQL
CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255));
SQL
`bin/rails db:structure:load`
assert_equal '["posts", "comments", "schema_migrations", "active_record_internal_metadatas", "users"]', list_tables[]
assert_equal '["posts", "comments", "schema_migrations", "ar_internal_metadata", "users"]', list_tables[]
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册