Last active
October 18, 2017 15:26
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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