Skip to content

Instantly share code, notes, and snippets.

@tiagoamaro
Last active October 18, 2017 15:26
Show Gist options
  • Save tiagoamaro/58f0d8b9b1d3f14a6562ce2c8c7ef164 to your computer and use it in GitHub Desktop.
Save tiagoamaro/58f0d8b9b1d3f14a6562ce2c8c7ef164 to your computer and use it in GitHub Desktop.
Clean up spec database tables without database_cleaner or database_rewinder (plus usage with Apartment gem)
class CleanupAllApartmentData
def self.call(schema_ary = [])
ar_conn = ActiveRecord::Base.connection
schema_ary.each do |schema|
Apartment::Tenant.switch!(schema)
ar_conn.disable_referential_integrity do
ar_conn
.tables
.map { |table| "DELETE FROM #{ar_conn.quote_table_name(table)}" }
.join(';')
.tap { |delete_tables_sql| ar_conn.execute delete_tables_sql }
end
end
end
end
class CleanupAllData
def self.call
ar_conn = ActiveRecord::Base.connection
ar_conn.disable_referential_integrity do
ar_conn
.tables
.map { |table| "DELETE FROM #{ar_conn.quote_table_name(table)}" }
.join(';')
.tap { |delete_tables_sql| ar_conn.execute delete_tables_sql }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment