Skip to content
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

Merged
merged 2 commits into from
Dec 20, 2017
Merged

Conversation

norkus256
Copy link

@norkus256 norkus256 commented Nov 26, 2017

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:

public function getDependencies()
{
    return [
        'UserSeeder',
        'GSeeder',
    ];
}

/**
* Get seed dependencies instances from seed dependency array
*
* @param OrderedSeedInterface $seed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing parameter comment

}
}
}
return $dependenciesInstances;
Copy link
Contributor

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

/**
* Order seeds by dependencies
*
* @param AbstractSeed[] $seeds
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing parameter comment

Copy link
Member

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.

$orderedSeeds[$key] = $seed;
}
}
return $orderedSeeds;
Copy link
Contributor

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-io
Copy link

codecov-io commented Nov 26, 2017

Codecov Report

Merging #1244 into master will increase coverage by 0.12%.
The diff coverage is 100%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
src/Phinx/Seed/AbstractSeed.php 48.83% <100%> (+2.49%) ⬆️
src/Phinx/Migration/Manager.php 90.54% <100%> (+0.52%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7072a54...00ffb18. Read the comment docs.

$dependencies = $seed->getDependencies();
foreach ($dependencies as $dependency) {
foreach ($this->seeds as $seed) {
if (get_class($seed) === $dependency) {
Copy link
Contributor

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().

Copy link
Author

@norkus256 norkus256 Dec 10, 2017

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...

Copy link
Author

@norkus256 norkus256 Dec 10, 2017

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.

Copy link
Contributor

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.

Copy link
Author

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...).

Copy link
Contributor

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();
Copy link
Contributor

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.

Copy link
Author

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.

@norkus256 norkus256 force-pushed the feature-ordered-seeds branch 3 times, most recently from 1b0f373 to d338f31 Compare December 14, 2017 06:04
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
);
);
Copy link
Contributor

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

@norkus256 norkus256 force-pushed the feature-ordered-seeds branch 6 times, most recently from 357c942 to 6543ca5 Compare December 17, 2017 11:10
@norkus256
Copy link
Author

@chinpei215

Copy link
Contributor

@chinpei215 chinpei215 left a 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.

*
* @return AbstractSeed[]
*/
protected function getSeedDependenciesInstances(AbstractSeed $seed)
Copy link
Contributor

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.

{
if (empty($this->seeds)) {
return [];
}
Copy link
Contributor

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.

*
* @return AbstractSeed[]
*/
protected function orderSeedsByDependencies(array $seeds)
Copy link
Contributor

@chinpei215 chinpei215 Dec 20, 2017

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.

@norkus256 norkus256 force-pushed the feature-ordered-seeds branch from 6543ca5 to 00ffb18 Compare December 20, 2017 08:14
@chinpei215
Copy link
Contributor

Thank you. Looks good to me.

@norkus256
Copy link
Author

@chinpei215 if everything is ok, could you merge now those changes?

@chinpei215 chinpei215 merged commit f0864db into cakephp:master Dec 20, 2017
@chinpei215
Copy link
Contributor

@Emptyhand Merged. Thank you for your contribution.

@Shardj
Copy link

Shardj commented Jul 27, 2018

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

@Shardj
Copy link

Shardj commented Jul 27, 2018

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.

@norkus256
Copy link
Author

Yes it works for me well. You have to run just console command php vendor/bin/phinx seed:run
without adding any seeds, then dependencies will be executed automatically

@Shardj
Copy link

Shardj commented Jul 27, 2018

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).

@Shardj
Copy link

Shardj commented Jul 30, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants