Description
I ran into some issues with the behavior of SerializationHelper::Load.truncate_table
when using yaml_db
on a PostgreSQL database that had referential integrity. In order to assist in resolving the referential integrity issues, I needed to add CASCADE
to the TRUNCATE
statement. In order to get the rescue
block to work within a transaction, I had to add SAVEPOINT
and ROLLBACK
statements. My monkey-patch only works for PostgreSQL, and may not work if you're not within a transaction, so getting this ready for inclusion into the actual code base would require adding database-specific support to yaml_db
as well as transaction detection logic. I'd be happy to work on a pull request, but before I do I'd like to see some consensus on the desirability of resolving this issue and some discussion of approaches (should I embed the database-specific code in truncate_table
or should I enable more generic support for database-specific code across the code base).
Here's my monkey-patch:
module SerializationHelper
class Load
def self.truncate_table(table)
begin
ActiveRecord::Base.connection.execute("SAVEPOINT before_truncation")
ActiveRecord::Base.connection.execute("TRUNCATE #{SerializationHelper::Utils.quote_table(table)} CASCADE")
rescue Exception
ActiveRecord::Base.connection.execute("ROLLBACK TO SAVEPOINT before_truncation")
ActiveRecord::Base.connection.execute("DELETE FROM #{SerializationHelper::Utils.quote_table(table)}")
end
end
end
end
Activity