-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[8.x] Allow anonymous and class based migration coexisting #37006
Conversation
fedb31e
to
ee78372
Compare
Can you explain more what you mean? |
If there is a named migration, lets say The reason for this that only the class existence was checked but not that the class is the same as the wanted one. With this change only the first one is resolved to the named migration and the second one will use the anonymous one. |
This doesn't seems right to me. I'd most definitely expect the output from the current tests, not the one that was modified by this PR. |
@driesvints The actual error is injected in the fixture, the modified test is just to handle the modified fixture correctly. Without the fix the The problem is that the migrator will try to execute the first migration twice and that causes an SQL error. |
Probably best to let @thiagorb review this one |
So, @thiagorb's PR did not solve the entire problem it was intended to solve in the first place? |
In this case you can just modify both migrations to be anonymous. |
It works perfectly when there is two or more anonymous class based migration with same name. But when there is one named class based migration and one or more anonymous class the named one will be executed for all. |
@thiagorb But is there any reason to not support backward compatibility? It is only a comparison. The other main reason in my opinion to support this is that this only shows up when a full migration happens (because migrations are not autoloaded), so if someone does not have tests that are running migrations this can cause nasty surprises. |
The thing is, even if we add this extra condition, it only solves the problem for one named class. If you have 3 migrations, one being anonymous and the other 2 named there will still be an error. The benefit of anonymous migrations is that they make it possible to fix this situation easily, by just converting a named migration into anonymous. |
A somewhat realistic scenario currently it is possible to have a migration:
class IncreaseUrlSize extends ...
$table->string('url', 1024)->change();
return new class extends ...
$table->string('url', 2048)->change(); This wont even generate a failure but on an existing db the url field will be 2048 byte and on a fresh install it will be 1024 byte. Try to debug this months later. It is almost free to prevent this in the first place. This PR is to support existing codebases, in my opinion stubs should be updated to the new format rather sooner then later. Most people won't update existing migrations just because it can be updated. |
Basically this PR adds support for having one named "legacy" migration and one or more new anonymous migrations. Having multiple named ones was big no-no, so the only target of this PR is to NOT force peoples to update like 120 existing migrations just to use this new feature. If we update the stubs the new format the chance of someone creating a new named class is almost zero But having existing classes which he wont update just because he can is pretty high as most people wont even notice the change. They will just fill out the up and down method like always. And having silent errors like the example above is one of the worst since probably most people won't test the length of a field. |
The changes to the tests make sense, cause one of the test migrations was converted from anonymous to named, so the output of |
If required I can add a new test migration instead of changing the existing one if that's a problem that way there will be multiple anonymous ones too. |
Why was this merged and why in 8.x ? This was a breaking change The following doesn't work anymore 2020_some_migration_file.php <?php
require_once 'some_class_with_migration.php'; When running the migration this results in
I really don't understand the purpose of this PR, it was working fine as it was no? |
@Tofandel why are you using migrations like that? Migrations aren't loaded like that in the framework. |
I don't really think this is the way migrations should be used so I wouldn't consider this a breaking change, but could you post some example code for better understanding? |
Because I have tenants that sometimes copy some root migrations, so instead of copying the content of the file and getting a duplicate class error I just require the original migration |
Consider this structure The migrations of the admin site |
I'm sorry but that's a use case we really can't account for. We don't support migrations in sub directories. |
Except it was working before and that's the only way I can get it to work without copying the content of the file, renaming the migration and renaming the class as well, which leads to a lot of code duplication and a lot of work (I have 30 migrations like this that I would need to manually rename because of this 1 line of code) |
We can't account for every single way people use code in ways it wasn't intended to be used, sorry... |
@netpok Anything that breaks existing code is a breaking change, this definitely applies here even if you don't consider it one This should go to 9.x |
This never came up as this was never an intended way, but I'm opening a PR right now which fixes this. |
Thank you, and sorry for the trouble |
#36906 introduces anonymous migrations but it fails when there is a class based migration too with the same name.
This PR fixes this by validating the class based migration's path against the wanted migration. Test are also updated to fail if problem is not fixed.