data-fixtures icon indicating copy to clipboard operation
data-fixtures copied to clipboard

Only Purge tables that have a fixture class

Open BladeRavinger opened this issue 5 years ago • 12 comments

So im obviously working on my website and for back end trial and error junk i require user logins to get at admin sections for reasons. it would be nice if instead of a database purge, wiping my 3 users of various permission levels, it would only drop/create tables that fixtures will be working on, as at present i am not creating dummy user data and its a pain to recreate the users each time i reload fixtures.

BladeRavinger avatar Jun 13 '19 23:06 BladeRavinger

Hi, can you please help to clearify what kind of feature you're proposing in this issue? I don't really understand your description.

As far as I can read, it is about not purging tables, but only for some table rows and not the whole table data?

SenseException avatar Jun 14 '19 12:06 SenseException

Sorry i was not clear.

I propose that only tables that the developer has created fixtures for be purged instead of the whole database.

Example. I have database with 5 tables, 2 of those table i manage manually for User/Role management. the other 3 tables are for Posts/Comments/Tags.

I have created a fixtures file and im making changes to Post/Comments/Tags Fixtures knows this as it has the use statements for these entity's. Fixtures should read that i only want to make changes to these 3 tables. purge them and leave User/Roles unaffected.

i use fixtures often, every time i generate a new migration that adds a field to my tables i update fixtures and load it. in the current state this means i have to either dump the Users table and then import it after the load. or recreate the user each time. i know i could just create a user fixture and insert the same user every time. but that not ideal.

BladeRavinger avatar Jun 14 '19 13:06 BladeRavinger

My point is also here. If I use --group={SOME_GROUP} and --purge-with-truncate options at the same time, I would like only tables in specified SOME_GROUP to be truncated.

BTW, class Doctrine\Common\DataFixtures\Purger\ORMPurger has 2nd parameter as array with excluded tables.

thewind1984 avatar Jun 19 '19 14:06 thewind1984

My point is also here. If I use --group={SOME_GROUP} and --purge-with-truncate options at the same time, I would like only tables in specified SOME_GROUP to be truncated.

i did not realize this was an option, i will give it a try. either my understanding of the documentation is lacking or more details could be added?

sorry im a little retarded so i have great difficulty interpreting most documentation

BladeRavinger avatar Jun 19 '19 14:06 BladeRavinger

My point is also here. If I use --group={SOME_GROUP} and --purge-with-truncate options at the same time, I would like only tables in specified SOME_GROUP to be truncated.

i did not realize this was an option, i will give it a try. either my understanding of the documentation is lacking or more details could be added?

sorry im a little retarded so i have great difficulty interpreting most documentation

there is an option to separate fixtures by groups, but if you use purge option, all the tables will be truncated. I just wrote an suggestion.

thewind1984 avatar Jun 19 '19 14:06 thewind1984

i saw thank you. this will allow me to do what i want. i appreciate your input

BladeRavinger avatar Jun 19 '19 15:06 BladeRavinger

Purging only tables based on the fixture classes is going to be difficult: there’s nothing stopping you from creating multiple different entities in a single fixture class, so the library can’t really know what to clear based on the class.

I’m moving this issue to the data-fixtures project, as this is nothing that should be solved in the bundle, but rather in the underlying library.

alcaeus avatar Jun 19 '19 17:06 alcaeus

Purging only tables based on the fixture classes is going to be difficult: there’s nothing stopping you from creating multiple different entities in a single fixture class, so the library can’t really know what to clear based on the class.

I’m moving this issue to the data-fixtures project, as this is nothing that should be solved in the bundle, but rather in the underlying library.

Maybe the following idea could be fixable - to add new option -exclude="table1, table2, ..." to prevent some tables to be truncated...

thewind1984 avatar Jun 19 '19 17:06 thewind1984

Perhaps the fact that in the fixture class we use App\Entity\Foo; don't affect tables that are not used?

obviously if you try to make changes without a use statement you get errors so as its a thing that will exist, and must exist, can we not look for use App/Entity/* and drop or truncate these automatically. i havent a clue if this is even possible, but as a default behavior that in itself makes sense.

thats just my 10cent.

i thank you all for the help.

BladeRavinger avatar Jun 19 '19 17:06 BladeRavinger

Note that currently the purger has no idea of what you’re doing in your fixtures. I’m not saying it’s impossible, but it’s also not something that we can just fix. It definitely requires some thinking as to how to accomplish it.

alcaeus avatar Jun 19 '19 17:06 alcaeus

Note that currently the purger has no idea of what you’re doing in your fixtures. I’m not saying it’s impossible, but it’s also not something that we can just fix. It definitely requires some thinking as to how to accomplish it.

of course, being a sub par coder myself i understand this, so the idea is here if you guys ever choose to work on it. im not pushing you to go make the change. Thewind1984 gave me a solution that will work for what i want, but the suggestion still stands if you guys choose to incorporate it in the future

BladeRavinger avatar Jun 19 '19 18:06 BladeRavinger

This is not the first time I would like to have a feature like the one describe here.

A common use, is when you have a table for statuses of another table (like "Project" and "ProjectStatus"). In my case, ProjectStatus can't be added using the app. So to manage those statuses, I use Fixtures.

When pushing in prod, I need to update the prod ProjectStatus in database values but I just can't load the ProjectStatusFixtures beacause the only two options are :

  • append data which results in duplicated values
  • purge all the data with data loss in other tables

A solution could be an interface Doctrine\DataFixtures\TableRelatedFixtureInterface with a method public static function getTables(): array. When implementing the interface, the developer needs to define that method to give the information of which tables are affected by the fixture (and should be purged) And option like --purge-only-related-tables will make the purger to check the getTables() of each Fixtures file.

vincentjdc avatar Feb 05 '21 09:02 vincentjdc