Your rails app comes with 2 tasks to clone your database structure into the test database: db:test:clone, which does everything through ruby, and db:test:clone_structure, which dumps the SQL needed to recreate the database structure, then reloads it, in a DB-specific fashion. You can switch which version is run by the db:test:prepare task in environment.rb.
Find this bit:
# Use Active Record's schema dumper instead of SQL when creating the test database
# (enables use of different database adapters for development and test environments)
# config.active_record.schema_format = :ruby
Uncomment the last line shown above, and replace :ruby with :sql - that’s it!
If, like me, you’re using postgres databases, this does the trick - now db:test:prepare dumps the structure of your dev/production database using pg_dump -s … and loads it into the test database using psql.
A couple of caveats:
- If you’re using functions written in a language that needs to be created after a new database is created, you must connect to your test database as a superuser, as only superusers can add languages.
- If you’re using views in MySQL, the views are carefully removed from the dumped structure, so won’t be in your test database.