提交 b86e313d 编写于 作者: P Pat Allan

Make ActiveRecord frozen string literal friendly.

上级 4ff30d9b
......@@ -197,8 +197,7 @@ def construct_tables!(parent, node)
def table_alias_for(reflection, parent, join)
name = "#{reflection.plural_name}_#{parent.table_name}"
name << "_join" if join
name
join ? "#{name}_join" : name
end
def walk(left, right)
......
......@@ -679,7 +679,7 @@ def checkout_for_exclusive_access(checkout_timeout)
# this block can't be easily moved into attempt_to_checkout_all_existing_connections's
# rescue block, because doing so would put it outside of synchronize section, without
# being in a critical section thread_report might become inaccurate
msg = "could not obtain ownership of all database connections in #{checkout_timeout} seconds"
msg = "could not obtain ownership of all database connections in #{checkout_timeout} seconds".dup
thread_report = []
@connections.each do |conn|
......
......@@ -22,7 +22,7 @@ def accept(o)
private
def visit_AlterTable(o)
sql = "ALTER TABLE #{quote_table_name(o.name)} "
sql = "ALTER TABLE #{quote_table_name(o.name)} ".dup
sql << o.adds.map { |col| accept col }.join(" ")
sql << o.foreign_key_adds.map { |fk| visit_AddForeignKey fk }.join(" ")
sql << o.foreign_key_drops.map { |fk| visit_DropForeignKey fk }.join(" ")
......@@ -30,17 +30,17 @@ def visit_AlterTable(o)
def visit_ColumnDefinition(o)
o.sql_type = type_to_sql(o.type, o.options)
column_sql = "#{quote_column_name(o.name)} #{o.sql_type}"
column_sql = "#{quote_column_name(o.name)} #{o.sql_type}".dup
add_column_options!(column_sql, column_options(o)) unless o.type == :primary_key
column_sql
end
def visit_AddColumnDefinition(o)
"ADD #{accept(o.column)}"
"ADD #{accept(o.column)}".dup
end
def visit_TableDefinition(o)
create_sql = "CREATE#{' TEMPORARY' if o.temporary} TABLE #{quote_table_name(o.name)} "
create_sql = "CREATE#{' TEMPORARY' if o.temporary} TABLE #{quote_table_name(o.name)} ".dup
statements = o.columns.map { |c| accept c }
statements << accept(o.primary_keys) if o.primary_keys
......
......@@ -1348,7 +1348,7 @@ def insert_versions_sql(versions)
sm_table = quote_table_name(ActiveRecord::SchemaMigration.table_name)
if versions.is_a?(Array)
sql = "INSERT INTO #{sm_table} (version) VALUES\n"
sql = "INSERT INTO #{sm_table} (version) VALUES\n".dup
sql << versions.map { |v| "(#{quote(v)})" }.join(",\n")
sql << ";\n\n"
sql
......
......@@ -147,7 +147,7 @@ def valid_type?(type) # :nodoc:
# this method must only be called while holding connection pool's mutex
def lease
if in_use?
msg = "Cannot lease connection, "
msg = "Cannot lease connection, ".dup
if @owner == Thread.current
msg << "it is already leased by the current thread."
else
......
......@@ -132,7 +132,7 @@ def native_database_types
end
def index_algorithms
{ default: "ALGORITHM = DEFAULT", copy: "ALGORITHM = COPY", inplace: "ALGORITHM = INPLACE" }
{ default: "ALGORITHM = DEFAULT".dup, copy: "ALGORITHM = COPY".dup, inplace: "ALGORITHM = INPLACE".dup }
end
# HELPER METHODS ===========================================
......@@ -379,7 +379,7 @@ def rename_column(table_name, column_name, new_column_name) #:nodoc:
def add_index(table_name, column_name, options = {}) #:nodoc:
index_name, index_type, index_columns, _, index_algorithm, index_using, comment = add_index_options(table_name, column_name, options)
sql = "CREATE #{index_type} INDEX #{quote_column_name(index_name)} #{index_using} ON #{quote_table_name(table_name)} (#{index_columns}) #{index_algorithm}"
sql = "CREATE #{index_type} INDEX #{quote_column_name(index_name)} #{index_using} ON #{quote_table_name(table_name)} (#{index_columns}) #{index_algorithm}".dup
execute add_sql_comment!(sql, comment)
end
......@@ -464,7 +464,7 @@ def type_to_sql(type, limit: nil, precision: nil, scale: nil, unsigned: nil, **)
super
end
sql << " unsigned" if unsigned && type != :primary_key
sql = "#{sql} unsigned" if unsigned && type != :primary_key
sql
end
......@@ -761,7 +761,7 @@ def configure_connection
# http://dev.mysql.com/doc/refman/5.7/en/set-statement.html#id944430
# (trailing comma because variable_assignments will always have content)
if @config[:encoding]
encoding = "NAMES #{@config[:encoding]}"
encoding = "NAMES #{@config[:encoding]}".dup
encoding << " COLLATE #{@config[:collation]}" if @config[:collation]
encoding << ", "
end
......
......@@ -16,7 +16,7 @@ def visit_AddColumnDefinition(o)
end
def visit_ChangeColumnDefinition(o)
change_column_sql = "CHANGE #{quote_column_name(o.name)} #{accept(o.column)}"
change_column_sql = "CHANGE #{quote_column_name(o.name)} #{accept(o.column)}".dup
add_column_position!(change_column_sql, column_options(o.column))
end
......@@ -63,7 +63,7 @@ def add_column_position!(sql, options)
def index_in_create(table_name, column_name, options)
index_name, index_type, index_columns, _, _, index_using, comment = @conn.add_index_options(table_name, column_name, options)
add_sql_comment!("#{index_type} INDEX #{quote_column_name(index_name)} #{index_using} (#{index_columns})", comment)
add_sql_comment!("#{index_type} INDEX #{quote_column_name(index_name)} #{index_using} (#{index_columns})".dup, comment)
end
end
end
......
......@@ -102,7 +102,7 @@ def extract_foreign_key_action(specifier)
def data_source_sql(name = nil, type: nil)
scope = quoted_scope(name, type: type)
sql = "SELECT table_name FROM information_schema.tables"
sql = "SELECT table_name FROM information_schema.tables".dup
sql << " WHERE table_schema = #{scope[:schema]}"
sql << " AND table_name = #{scope[:name]}" if scope[:name]
sql << " AND table_type = #{scope[:type]}" if scope[:type]
......
......@@ -408,7 +408,7 @@ def change_column(table_name, column_name, type, options = {}) #:nodoc:
quoted_table_name = quote_table_name(table_name)
quoted_column_name = quote_column_name(column_name)
sql_type = type_to_sql(type, options)
sql = "ALTER TABLE #{quoted_table_name} ALTER COLUMN #{quoted_column_name} TYPE #{sql_type}"
sql = "ALTER TABLE #{quoted_table_name} ALTER COLUMN #{quoted_column_name} TYPE #{sql_type}".dup
if options[:collation]
sql << " COLLATE \"#{options[:collation]}\""
end
......@@ -568,7 +568,7 @@ def type_to_sql(type, limit: nil, precision: nil, scale: nil, array: nil, **) #
super
end
sql << "[]" if array && type != :primary_key
sql = "#{sql}[]" if array && type != :primary_key
sql
end
......@@ -637,7 +637,7 @@ def data_source_sql(name = nil, type: nil)
scope = quoted_scope(name, type: type)
scope[:type] ||= "'r','v','m'" # (r)elation/table, (v)iew, (m)aterialized view
sql = "SELECT c.relname FROM pg_class c LEFT JOIN pg_namespace n ON n.oid = c.relnamespace"
sql = "SELECT c.relname FROM pg_class c LEFT JOIN pg_namespace n ON n.oid = c.relnamespace".dup
sql << " WHERE n.nspname = #{scope[:schema]}"
sql << " AND c.relname = #{scope[:name]}" if scope[:name]
sql << " AND c.relkind IN (#{scope[:type]})"
......
......@@ -67,7 +67,7 @@ def data_source_sql(name = nil, type: nil)
scope = quoted_scope(name, type: type)
scope[:type] ||= "'table','view'"
sql = "SELECT name FROM sqlite_master WHERE name <> 'sqlite_sequence'"
sql = "SELECT name FROM sqlite_master WHERE name <> 'sqlite_sequence'".dup
sql << " AND name = #{scope[:name]}" if scope[:name]
sql << " AND type IN (#{scope[:type]})"
sql
......
......@@ -16,7 +16,7 @@ def collecting_queries_for_explain # :nodoc:
# Returns a formatted string ready to be logged.
def exec_explain(queries) # :nodoc:
str = queries.map do |sql, binds|
msg = "EXPLAIN for: #{sql}"
msg = "EXPLAIN for: #{sql}".dup
unless binds.empty?
msg << " "
msg << binds.map { |attr| render_bind(attr) }.inspect
......
......@@ -157,7 +157,7 @@ def initialize
class ProtectedEnvironmentError < ActiveRecordError #:nodoc:
def initialize(env = "production")
msg = "You are attempting to run a destructive action against your '#{env}' database.\n"
msg = "You are attempting to run a destructive action against your '#{env}' database.\n".dup
msg << "If you are sure you want to continue, run the same command with the environment variable:\n"
msg << "DISABLE_DATABASE_ENVIRONMENT_CHECK=1"
super(msg)
......@@ -166,7 +166,7 @@ def initialize(env = "production")
class EnvironmentMismatchError < ActiveRecordError
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 attempting to modify a database that was last run in `#{ stored }` environment.\n".dup
msg << "You are running in `#{ current }` environment. "
msg << "If you are sure you want to continue, first set the environment using:\n\n"
msg << " bin/rails db:environment:set"
......@@ -1239,7 +1239,7 @@ def execute_migration_in_transaction(migration, direction)
record_version_state_after_migrating(migration.version)
end
rescue => e
msg = "An error has occurred, "
msg = "An error has occurred, ".dup
msg << "this and " if use_transaction?(migration)
msg << "all later migrations canceled:\n\n#{e}"
raise StandardError, msg, e.backtrace
......
......@@ -335,14 +335,14 @@ def raise_record_not_found_exception!(ids = nil, result_size = nil, expected_siz
name = @klass.name
if ids.nil?
error = "Couldn't find #{name}"
error = "Couldn't find #{name}".dup
error << " with#{conditions}" if conditions
raise RecordNotFound.new(error, name)
elsif Array(ids).size == 1
error = "Couldn't find #{name} with '#{key}'=#{ids}#{conditions}"
raise RecordNotFound.new(error, name, key, ids)
else
error = "Couldn't find all #{name.pluralize} with '#{key}': "
error = "Couldn't find all #{name.pluralize} with '#{key}': ".dup
error << "(#{ids.join(", ")})#{conditions} (found #{result_size} results, but was looking for #{expected_size})"
raise RecordNotFound.new(error, name, primary_key, ids)
......
......@@ -151,7 +151,7 @@ def run_cmd(cmd, args, action)
end
def run_cmd_error(cmd, args, action)
msg = "failed to execute: `#{cmd}`\n"
msg = "failed to execute: `#{cmd}`\n".dup
msg << "Please check the output above for any errors and make sure that `#{cmd}` is installed in your PATH and has proper permissions.\n\n"
msg
end
......
......@@ -47,7 +47,7 @@ def test_type_cast_binary_converts_the_encoding
end
def test_type_cast_binary_value
data = "\u001F\x8B".force_encoding("BINARY")
data = "\u001F\x8B".dup.force_encoding("BINARY")
assert_equal(data, @type.deserialize(data))
end
......
......@@ -47,10 +47,10 @@ def test_money_values
def test_money_type_cast
type = PostgresqlMoney.type_for_attribute("wealth")
assert_equal(12345678.12, type.cast("$12,345,678.12"))
assert_equal(12345678.12, type.cast("$12.345.678,12"))
assert_equal(-1.15, type.cast("-$1.15"))
assert_equal(-2.25, type.cast("($2.25)"))
assert_equal(12345678.12, type.cast("$12,345,678.12".dup))
assert_equal(12345678.12, type.cast("$12.345.678,12".dup))
assert_equal(-1.15, type.cast("-$1.15".dup))
assert_equal(-2.25, type.cast("($2.25)".dup))
end
def test_schema_dumping
......@@ -60,7 +60,7 @@ def test_schema_dumping
end
def test_create_and_update_money
money = PostgresqlMoney.create(wealth: "987.65")
money = PostgresqlMoney.create(wealth: "987.65".dup)
assert_equal 987.65, money.wealth
new_value = BigDecimal.new("123.45")
......
......@@ -165,7 +165,7 @@ def test_quote_binary_column_escapes_it
data binary
)
eosql
str = "\x80".force_encoding("ASCII-8BIT")
str = "\x80".dup.force_encoding("ASCII-8BIT")
binary = DualEncoding.new name: "いただきます!", data: str
binary.save!
assert_equal str, binary.data
......@@ -174,7 +174,7 @@ def test_quote_binary_column_escapes_it
end
def test_type_cast_should_not_mutate_encoding
name = "hello".force_encoding(Encoding::ASCII_8BIT)
name = "hello".dup.force_encoding(Encoding::ASCII_8BIT)
Owner.create(name: name)
assert_equal Encoding::ASCII_8BIT, name.encoding
ensure
......
......@@ -76,7 +76,7 @@ class AttributeTest < ActiveRecord::TestCase
end
test "duping dups the value" do
@type.expect(:deserialize, "type cast", ["a value"])
@type.expect(:deserialize, "type cast".dup, ["a value"])
attribute = Attribute.from_database(nil, "a value", @type)
value_from_orig = attribute.value
......@@ -244,7 +244,7 @@ def assert_valid_value(*)
end
test "with_type preserves mutations" do
attribute = Attribute.from_database(:foo, "", Type::Value.new)
attribute = Attribute.from_database(:foo, "".dup, Type::Value.new)
attribute.value << "1"
assert_equal 1, attribute.with_type(Type::Integer.new).value
......
......@@ -152,7 +152,7 @@ def test_find_in_batches_should_quote_batch_order
end
def test_find_in_batches_should_not_use_records_after_yielding_them_in_case_original_array_is_modified
not_a_post = "not a post"
not_a_post = "not a post".dup
def not_a_post.id; end
not_a_post.stub(:id, -> { raise StandardError.new("not_a_post had #id called on it") }) do
assert_nothing_raised do
......@@ -417,7 +417,7 @@ def test_in_batches_should_quote_batch_order
end
def test_in_batches_should_not_use_records_after_yielding_them_in_case_original_array_is_modified
not_a_post = "not a post"
not_a_post = "not a post".dup
def not_a_post.id
raise StandardError.new("not_a_post had #id called on it")
end
......
......@@ -10,7 +10,7 @@ class BinaryTest < ActiveRecord::TestCase
FIXTURES = %w(flowers.jpg example.log test.txt)
def test_mixed_encoding
str = "\x80"
str = "\x80".dup
str.force_encoding("ASCII-8BIT")
binary = Binary.new name: "いただきます!", data: str
......
......@@ -35,7 +35,7 @@ def test_inspect_class_without_table
def test_pretty_print_new
topic = Topic.new
actual = ""
actual = "".dup
PP.pp(topic, StringIO.new(actual))
expected = <<-PRETTY.strip_heredoc
#<Topic:0xXXXXXX
......@@ -64,7 +64,7 @@ def test_pretty_print_new
def test_pretty_print_persisted
topic = topics(:first)
actual = ""
actual = "".dup
PP.pp(topic, StringIO.new(actual))
expected = <<-PRETTY.strip_heredoc
#<Topic:0x\\w+
......@@ -92,7 +92,7 @@ def test_pretty_print_persisted
def test_pretty_print_uninitialized
topic = Topic.allocate
actual = ""
actual = "".dup
PP.pp(topic, StringIO.new(actual))
expected = "#<Topic:XXXXXX not initialized>\n"
assert actual.start_with?(expected.split("XXXXXX").first)
......@@ -105,7 +105,7 @@ def inspect
"inspecting topic"
end
end
actual = ""
actual = "".dup
PP.pp(subtopic.new, StringIO.new(actual))
assert_equal "inspecting topic\n", actual
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册