Fixtures: Delete ALL fixtures before load any

If the one is using “ON DELETE CASCADE” SQL feature then there is a very possible situation that later DELETE cause deletion of related rows which were already loaded earlier.

A bit pseudo-code (PostgreSQL-like):

CREATE TABLE users (

id integer NOT NULL,

name character varying NOT NULL

);

CREATE TABLE comments (

id integer NOT NULL,

user_id integer,

content text

);

ALTER TABLE comments

ADD CONSTRAINT fk_comments_to_users

FOREIGN KEY (user_id)

REFERENCES users(id)

ON DELETE CASCADE;

Fixtures loaded in arbitrary order:

Fixture Delete (2.0ms) DELETE FROM “comments”

Fixture Insert (0.3ms) INSERT INTO “comments” …

Fixture Insert (0.8ms) INSERT INTO “comments” …

Fixture Insert (0.2ms) INSERT INTO “comments” …

Fixture Delete (2.0ms) DELETE FROM “users” # All the comments are deleted there

Fixture Insert (0.3ms) INSERT INTO “users” …

Result: no comments in tests at all.

The more suitable way is to delete all the fixtures first and then load fresh ones.

It should not break existing behaviour in any way but solves some problems like this one.