Hi all
I have an existing project that I need to add soft deletes to. That's as simple as adding new migrations to add the db columns, and adding the SoftDeletes trait to the models.
But in some of my older migrations, there are queries using models: eg SomeModel::where('thing', '=', 'foo')->first().
In prod that's fine, cause those migrations have already run and will never run again. But in dev and automated tests, the db is being torn down and migrated from scratch a lot. And when those "old" migrations are run they fail because the query automatically now adds the deleted_at column check. It all makes sense as to why, I just don't know what the best fix is.
As I can see it, there are a number of options:
- Update the old migrations to add the soft delete columns when the table was first created (in addtion to the new migration for prod). Seems a bit sketch updating old migrations like that?
- Update the queries in the old migrations to use
DB and not models directly. But then I lose nice model helpers like firstOrCreate()
- Add
withTrashed() to the old migration queries so they don't try add the deleted_at column
- "Squash" the migrations so the old migrations don't run even in dev or test
Other than squashing, these options involve editing old migrations which is obviously not ideal. Is there a more conventional or recommended way of dealing with this situation? (there probably shouldn't be eloquent queries in the migrations to be fair, but I can't fix the past 😊)