rails/rails — pull request #18082 by senny (2014-12-18 09:18:35+00:00)
Title: `db:schema:load` and `db:structure:load` do not purge the database

Closes #17945

There are environments (like Heroku) where it's not possible to purge a database. This rendered `db:schema:load` and `db:structure:load` useless in such circumstances.

The original behavior (until `Rails 4.0`) was as follows:
* `db/schema.rb` used `create_table force: true` to make it possible to run against an already populated database. It would basically recreate every table defined in the schema and leave the rest alone.
* With `db:structure:load` it was up to the user to create an acceptable database state before loading `structure.sql`.

The introduction of foreign keys complicates things a bit. Simply using `force: true` won't be enough. The database will reject dropping a table, which is referenced by a foreign key. This patch solves that problem by introducing `force: :cascade`, which drops dependent objects as well. New `db/schema.rb` files will be created with `force: :cascade` instead of `force: true`.

ToDo:

- [ ] Sadly [MySQL does no longer support `CASCADE`](http://dev.mysql.com/doc/refman/5.5/en/drop-table.html):

> RESTRICT and CASCADE are permitted to make porting easier. In MySQL 5.5, they do nothing.

While it is possible to `disable_referential_integrity` and drop the tables, this will only remove the table and not dependent foreign keys. Resulting in a later error when `db/schema.rb` tries to recreate the foreign key.