I didn't have much luck with doing the typical drop, create, migrate in run-time, so I used the following approach (suggested by @David Aldridge):
connection ||= ActiveRecord::Base.connection
connection.tables.each do |t|
connection.execute("TRUNCATE \"#{t}\" RESTART IDENTITY CASCADE") unless t == 'schema_migrations'
end
I should first mention that I'm using postgres.
The problem I was running into previously was that I was getting blocked by index constraints in the db, but using the CASCADE
argument solved that.
CASCADE
allows for associated tables to also be truncated -- essentially bypassing constraints
RESTART IDENTITY
allows for resetting the table increment to zero.