tripal / TripalTestSuite

Handle drupal bootstrapping, load environment variables, and easily wrap your tests in DB transactions.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

default seeder has no down method, breaks second run of db:seed

bradfordcondon opened this issue · comments

vendor/bin/tripaltest db:seed
Ran Tests\DatabaseSeeders\DevSeedSeeder successfully!

In database.inc line 2227:

  SQLSTATE[23505]: Unique violation: 7 ERROR:  duplicate key value violates unique constraint "users_name_key"
  DETAIL:  Key (name)=(test user) already exists.


db:seed [<name>]

fix by adding down method or commenting out seed

We agreed that seeders should provide permanent data and that running them in transactions should suffice. However, we can allow for a down method in the seeder than can be run when the user runs db:seed --down. Not a required method but can be there if needed.

How would the down method know what data needs to be deleted?

Example:
Seeder used factories to create a CV and attach it to 100 features. In order for the seeder to be able to delete only the data it created, it needs access to the ids of those features and CVs so that it can run a delete query. But, there is no way we can provide that at this state.

Solution:
Ship tripal test suite with an SQLite database (simple file that can be queried using sql). Then by the end of each up run, store a serialized object of the instance of the seeder. When the --down is specified, we can then run deserialize the object and run the down method. This requires the seeder to do nothing more than to store anything necessary to identify the records in a class property.

Doable? Yes. But, should we do it?

Feels unnecessary

for anyone discovering this issue: we're open ti implementing but we have no need for it ourselves. If you wanted to contribute, we'd happily accept.