-
Notifications
You must be signed in to change notification settings - Fork 892
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
Add ordered seeds feature #1244
Conversation
src/Phinx/Migration/Manager.php
Outdated
/** | ||
* Get seed dependencies instances from seed dependency array | ||
* | ||
* @param OrderedSeedInterface $seed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing parameter comment
src/Phinx/Migration/Manager.php
Outdated
} | ||
} | ||
} | ||
return $dependenciesInstances; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing blank line before return statement
src/Phinx/Migration/Manager.php
Outdated
/** | ||
* Order seeds by dependencies | ||
* | ||
* @param AbstractSeed[] $seeds |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing parameter comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also FQCN please inside comments.
src/Phinx/Migration/Manager.php
Outdated
$orderedSeeds[$key] = $seed; | ||
} | ||
} | ||
return $orderedSeeds; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing blank line before return statement
Codecov Report
@@ Coverage Diff @@
## master #1244 +/- ##
==========================================
+ Coverage 74.64% 74.76% +0.12%
==========================================
Files 35 35
Lines 4741 4764 +23
==========================================
+ Hits 3539 3562 +23
Misses 1202 1202
Continue to review full report at Codecov.
|
43413cb
to
bc9fe48
Compare
src/Phinx/Migration/Manager.php
Outdated
$dependencies = $seed->getDependencies(); | ||
foreach ($dependencies as $dependency) { | ||
foreach ($this->seeds as $seed) { | ||
if (get_class($seed) === $dependency) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry if I am wrong, but if people make a typo in the etDependencies() method, it will be discarded silently. Couldn't it be implemented by using usort()
and the callback method? I think you want to sort seeds by a custom logic. In such case, I would use usort()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can add exception if there is typo dependency written in the function. About sorting seeds in custom logic, I can not imagine a case where it would be used. Why you want to order seeds in custom logic? And why write additional code if there is simple and suitable solution with dependencies...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also in php 5.3 you can write your class name like that "YourSeedClass::class" to avoid typos.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for my English. I didn't say I wanted to order seeds in custom logic. I meant that your implementation looks like that it sorts seeds by using two methods - orderSeedsByDependencies()
and getSeedDependenciesInstances()
. But if you have a method to compare seeds, you can do usort($seeds, [$this, 'compareSeeds'])
.
Also, people cannot use ::class
syntax to avoid typos. Try NonExisitentClass::class
. It would also returns NonExisitentClass
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could be implemented in many ways :). Please write me explicitly what should I fix, to merge this feature (I need it in production urgently...).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. Then please don't add additional interface. Other points are minor things. It could be changed later if someone interests.
/** | ||
* @return array | ||
*/ | ||
public function getDependencies(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need an additional interface? I think we can add some additional method to the AbstractSeed
class with a default implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of course we can add method to AbstractSeed. I did interface if some people do not want to order they seeds and then they keep them without this interface and without getDependencies() function.
If there is a need to move to AbstractSeed class I can do it.
1b0f373
to
d338f31
Compare
src/Phinx/Console/Command/Test.php
Outdated
The <info>test</info> command verifies the YAML configuration file and optionally an environment | ||
|
||
<info>phinx test</info> | ||
<info>phinx test -e development</info> | ||
|
||
EOT | ||
); | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line indented incorrectly; expected at least 8 spaces, found 0
357c942
to
6543ca5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for repeated requests. As I said above, I want to keep smaller interface. If we add new public/protected methods, we cannot changes those methods without breaking backward compatibility.
src/Phinx/Migration/Manager.php
Outdated
* | ||
* @return AbstractSeed[] | ||
*/ | ||
protected function getSeedDependenciesInstances(AbstractSeed $seed) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please change protected
to private
.
src/Phinx/Migration/Manager.php
Outdated
{ | ||
if (empty($this->seeds)) { | ||
return []; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check seems unnecessary. If $this->seeds
is an empty array, I think this method is not called.
src/Phinx/Migration/Manager.php
Outdated
* | ||
* @return AbstractSeed[] | ||
*/ | ||
protected function orderSeedsByDependencies(array $seeds) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please change protected
to private
.
6543ca5
to
00ffb18
Compare
Thank you. Looks good to me. |
@chinpei215 if everything is ok, could you merge now those changes? |
@Emptyhand Merged. Thank you for your contribution. |
Hey does this actually work? When I run my seeder with function getDependencies() it still immediately goes into run() and doesn't run the dependency seeders first |
I'm going to xdebug through on monday and see if I can find out why it isn't running these. Manager.php 's getSeedDependenciesInstances seems to correctly find them and return them. It does run twice though for some reason, first instance nothing is passed in, presumably it's using a non-extended instance of AbstractSeed then. |
Yes it works for me well. You have to run just console command |
Definitely not my experience so far, running the seeder with the dependencies specifically jumps straight to run, and running all seeders just executes them in alphanumeric order and when it reaches the file in question it ignores the dependencies. I'll try to find out why when I'm back at work on Monday, I'll make a pull request if it's a flaw and not something idiotic I did (I suspect namespaces and bad backslash encoding). |
No idea why but when I came in today it was working fine, I probably did something stupid, or it's standard PHP wizardry. Sorry for the trouble |
This feature is for ordering seeds execution by seeds dependencies. You can define other seeds which are dependencies for your seed and change seeds execution order automatically.
This feature is useful when you extend phinx manager (for example in unit tests to use in memory database) and do not have possibility execute seeds command from terminal.
Also if you have a lot of seeds it is not convenient to order seeds writing one by one in terminal to run ordered seeds.
Just run
phinx seeds:run
and your seeds will be executed in ordered way.Use this function to define dependencies: