1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Extract truncate and truncate_tables into database statements

This is to easier make `truncate_tables` to bulk statements.
This commit is contained in:
Ryuta Kamizono 2019-03-10 01:53:48 +09:00
parent 1db0506543
commit fdac932707
13 changed files with 42 additions and 84 deletions

View file

@ -144,7 +144,11 @@ module ActiveRecord
# Executes the truncate statement.
def truncate(table_name, name = nil)
raise NotImplementedError
execute "TRUNCATE TABLE #{quote_table_name(table_name)}", name
end
def truncate_tables(*table_names) # :nodoc:
table_names.each { |table_name| truncate(table_name) }
end
# Executes update +sql+ statement in the context of this connection using

View file

@ -274,10 +274,6 @@ module ActiveRecord
show_variable "collation_database"
end
def truncate(table_name, name = nil)
execute "TRUNCATE TABLE #{quote_table_name(table_name)}", name
end
def table_comment(table_name) # :nodoc:
scope = quoted_scope(table_name)

View file

@ -143,6 +143,12 @@ module ActiveRecord
end
end
def truncate_tables(*table_names) # :nodoc:
unless table_names.empty?
execute "TRUNCATE TABLE #{table_names.map(&method(:quote_table_name)).join(", ")}"
end
end
# Begins a transaction.
def begin_db_transaction
execute "BEGIN"

View file

@ -259,10 +259,6 @@ module ActiveRecord
@use_insert_returning = @config.key?(:insert_returning) ? self.class.type_cast_config_to_boolean(@config[:insert_returning]) : true
end
def truncate(table_name, name = nil)
exec_query "TRUNCATE TABLE #{quote_table_name(table_name)}", name, []
end
# Is this connection alive and ready for queries?
def active?
@lock.synchronize do

View file

@ -155,10 +155,6 @@ module ActiveRecord
@connection.close rescue nil
end
def truncate(table_name, name = nil)
execute "DELETE FROM #{quote_table_name(table_name)}", name
end
def supports_index_sort_order?
true
end
@ -279,6 +275,10 @@ module ActiveRecord
end
end
def truncate(table_name, name = nil) # :nodoc:
execute "DELETE FROM #{quote_table_name(table_name)}", name
end
def begin_db_transaction #:nodoc:
log("begin transaction", nil) { @connection.transaction }
end

View file

@ -185,14 +185,17 @@ module ActiveRecord
def truncate_tables(configuration)
ActiveRecord::Base.connected_to(database: { truncation: configuration }) do
table_names = ActiveRecord::Base.connection.tables
internal_table_names = [
table_names -= [
ActiveRecord::Base.schema_migrations_table_name,
ActiveRecord::Base.internal_metadata_table_name
]
class_for_adapter(configuration["adapter"]).new(configuration).truncate_tables(*table_names.without(*internal_table_names))
ActiveRecord::Base.connection.disable_referential_integrity do
ActiveRecord::Base.connection.truncate_tables(*table_names)
end unless table_names.empty?
end
end
private :truncate_tables
def truncate_all(environment = env)
ActiveRecord::Base.configurations.configs_for(env_name: environment).each do |db_config|

View file

@ -31,16 +31,6 @@ module ActiveRecord
connection.recreate_database configuration["database"], creation_options
end
def truncate_tables(*table_names)
return if table_names.empty?
ActiveRecord::Base.connection.disable_referential_integrity do
table_names.each do |table_name|
ActiveRecord::Base.connection.truncate(table_name)
end
end
end
def charset
connection.charset
end

View file

@ -48,18 +48,6 @@ module ActiveRecord
create true
end
def truncate_tables(*table_names)
return if table_names.empty?
ActiveRecord::Base.connection.disable_referential_integrity do
quoted_table_names = table_names.map do |table_name|
ActiveRecord::Base.connection.quote_table_name(table_name)
end
ActiveRecord::Base.connection.execute "TRUNCATE TABLE #{quoted_table_names.join(", ")}"
end
end
def structure_dump(filename, extra_flags)
set_psql_env

View file

@ -33,16 +33,6 @@ module ActiveRecord
create
end
def truncate_tables(*table_names)
return if table_names.empty?
ActiveRecord::Base.connection.disable_referential_integrity do
table_names.each do |table_name|
ActiveRecord::Base.connection.truncate(table_name)
end
end
end
def charset
connection.encoding
end

View file

@ -452,6 +452,8 @@ module ActiveRecord
class AdapterTestWithoutTransaction < ActiveRecord::TestCase
self.use_transactional_tests = false
fixtures :posts, :authors, :author_addresses
def setup
@connection = ActiveRecord::Base.connection
end
@ -482,6 +484,26 @@ module ActiveRecord
end
end
def test_truncate
assert_operator @connection.query_value("SELECT COUNT(*) FROM posts"), :>, 0
@connection.truncate("posts")
assert_equal 0, @connection.query_value("SELECT COUNT(*) FROM posts")
end
def test_truncate_tables
assert_operator @connection.query_value("SELECT COUNT(*) FROM authors"), :>, 0
assert_operator @connection.query_value("SELECT COUNT(*) FROM author_addresses"), :>, 0
@connection.disable_referential_integrity do
@connection.truncate_tables("author_addresses", "authors")
end
assert_equal 0, @connection.query_value("SELECT COUNT(*) FROM authors")
assert_equal 0, @connection.query_value("SELECT COUNT(*) FROM author_addresses")
end
# test resetting sequences in odd tables in PostgreSQL
if ActiveRecord::Base.connection.respond_to?(:reset_pk_sequence!)
require "models/movie"

View file

@ -28,17 +28,6 @@ class Mysql2ConnectionTest < ActiveRecord::Mysql2TestCase
end
end
def test_truncate
rows = ActiveRecord::Base.connection.exec_query("select count(*) from comments")
count = rows.first.values.first
assert_operator count, :>, 0
ActiveRecord::Base.connection.truncate("comments")
rows = ActiveRecord::Base.connection.exec_query("select count(*) from comments")
count = rows.first.values.first
assert_equal 0, count
end
def test_no_automatic_reconnection_after_timeout
assert_predicate @connection, :active?
@connection.update("set @@wait_timeout=1")

View file

@ -25,14 +25,6 @@ module ActiveRecord
super
end
def test_truncate
count = ActiveRecord::Base.connection.execute("select count(*) from comments").first["count"].to_i
assert_operator count, :>, 0
ActiveRecord::Base.connection.truncate("comments")
count = ActiveRecord::Base.connection.execute("select count(*) from comments").first["count"].to_i
assert_equal 0, count
end
def test_encoding
assert_queries(1) do
assert_not_nil @connection.encoding

View file

@ -1,18 +0,0 @@
# frozen_string_literal: true
require "cases/helper"
class SQLite3ConnectionTest < ActiveRecord::SQLite3TestCase
fixtures :comments
def test_truncate
rows = ActiveRecord::Base.connection.exec_query("select count(*) from comments")
count = rows.first.values.first
assert_operator count, :>, 0
ActiveRecord::Base.connection.truncate("comments")
rows = ActiveRecord::Base.connection.exec_query("select count(*) from comments")
count = rows.first.values.first
assert_equal 0, count
end
end