diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2019-03-17 06:06:21 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2019-03-17 09:37:08 +0900 |
commit | 0ad70eb2d063cab577a559f6c3d28e787ca1dca8 (patch) | |
tree | da8193ff9d12a38c826250a50fc98844bf0eb0e0 /activerecord/lib/active_record/connection_adapters/mysql | |
parent | 8ed636511779ed1472f4f88362ded34f61005f4a (diff) | |
download | rails-0ad70eb2d063cab577a559f6c3d28e787ca1dca8.tar.gz rails-0ad70eb2d063cab577a559f6c3d28e787ca1dca8.tar.bz2 rails-0ad70eb2d063cab577a559f6c3d28e787ca1dca8.zip |
Make `truncate_tables` to bulk statements
Before:
```
(16.4ms) TRUNCATE TABLE `author_addresses`
(20.5ms) TRUNCATE TABLE `authors`
(19.4ms) TRUNCATE TABLE `posts`
```
After:
```
Truncate Tables (19.5ms) TRUNCATE TABLE `author_addresses`;
TRUNCATE TABLE `authors`;
TRUNCATE TABLE `posts`
```
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/mysql')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb b/activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb index 6adcc14545..421afc34bc 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb @@ -68,6 +68,14 @@ module ActiveRecord end alias :exec_update :exec_delete + def insert_fixtures_set(fixture_set, tables_to_delete = []) # :nodoc: + super { discard_remaining_results } + end + + def truncate_tables(*table_names) # :nodoc: + super { discard_remaining_results } + end + private def default_insert_value(column) Arel.sql("DEFAULT") unless column.auto_increment? @@ -85,6 +93,14 @@ module ActiveRecord @connection.respond_to?(:set_server_option) end + def build_truncate_statements(*table_names) + if table_names.size == 1 + super.first + else + super + end + end + def multi_statements_enabled?(flags) if flags.is_a?(Array) flags.include?("MULTI_STATEMENTS") @@ -117,6 +133,36 @@ module ActiveRecord end end + def combine_multi_statements(total_sql) + total_sql.each_with_object([]) do |sql, total_sql_chunks| + previous_packet = total_sql_chunks.last + if max_allowed_packet_reached?(sql, previous_packet) + total_sql_chunks << +sql + else + previous_packet << ";\n" + previous_packet << sql + end + end + end + + def max_allowed_packet_reached?(current_packet, previous_packet) + if current_packet.bytesize > max_allowed_packet + raise ActiveRecordError, + "Fixtures set is too large #{current_packet.bytesize}. Consider increasing the max_allowed_packet variable." + elsif previous_packet.nil? + true + else + (current_packet.bytesize + previous_packet.bytesize) > max_allowed_packet + end + end + + def max_allowed_packet + @max_allowed_packet ||= begin + bytes_margin = 2 + show_variable("max_allowed_packet") - bytes_margin + end + end + def exec_stmt_and_free(sql, name, binds, cache_stmt: false) if preventing_writes? && write_query?(sql) raise ActiveRecord::ReadOnlyError, "Write query attempted while in readonly mode: #{sql}" |