module SupportUtils::Concerns::Utils::ClassMethods

Public Instance Methods

truncate!(confirm = false) click to toggle source
# File lib/support_utils/concerns/utils.rb, line 110
def truncate! confirm = false
  adapter = ActiveRecord::Base.configurations[Rails.env]["adapter"]
  adapter_method = :"truncate_#{adapter}!"
  if confirm
    if respond_to?(adapter_method, true)
      send(adapter_method)
    else
      ActiveRecord::Base.connection.execute("TRUNCATE TABLE #{table_name.to_s}")
    end
  end
end

Private Instance Methods

truncate_postgresql!() click to toggle source

The “RESTART IDENTITY” Option will restart the primary key

# File lib/support_utils/concerns/utils.rb, line 131
def truncate_postgresql!
  ActiveRecord::Base.connection.execute("TRUNCATE TABLE #{table_name.to_s} RESTART IDENTITY")
end
truncate_sqlite3!() click to toggle source

SQLite doesn't have the “TRUNCATE TABLE” command

# File lib/support_utils/concerns/utils.rb, line 125
def truncate_sqlite3!
  ActiveRecord::Base.connection.execute("DELETE FROM #{table_name.to_s}")
  ActiveRecord::Base.connection.execute("DELETE FROM sqlite_sequence WHERE name='#{table_name.to_s}'")
end