-
Notifications
You must be signed in to change notification settings - Fork 179
[FEATURE] Master/slave connection support. #214
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
[FEATURE] Master/slave connection support. #214
Conversation
d159be3 to
8d636e2
Compare
|
Can you add some more information to the PR? |
src/EntityManagerFactory.php
Outdated
| throw new \InvalidArgumentException("Parameter 'read' must be an array containing multiple arrays."); | ||
| } | ||
|
|
||
| if (($key = array_search(0, array_map('count', $slaves))) && $key !== false) { |
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.
isn't $key !== false redundant here? if its false, first condition fails.
Search could return position 0, which would be false-ish here as well, right?
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.
yeah this seem like it's trying to be too clever by half and will fail if the first config (position 0) is empty, poc here https://3v4l.org/N8gnk There doesn't seem to be any tests for this?
A lot of this could be replaced with
array_map(function($config){
if (!is_array($config)) {
//...
}
if (!count($config)) {
//...
}
}, $slaves);
only one loop, probably less buggy.
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.
That's correct, it's faulty. Will remove the second check and add a test case for it.
| */ | ||
| public function testMasterSlaveConnection(array $resolvedBaseSettings, array $settings, array $expectedOutput) | ||
| { | ||
| $this->assertEquals( |
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 like how you used dataProvider to reuse this case multiple times, but I find it a bit hard to maintain in this current form.
I wouldn't know where to modify each expectation for each setting.
Maybe refactor each case into its own method, then do something like:
public function getMasterSlaveConnectionData()
{
return [
$this->dummyBaseSettings(), $this->dummyConfig(), $this->dummyExpectations(),
$this->oracleBaseSettings(), $this->oracleConfig(), $this->oracleExpectations(),
}
Or something along those lines?
tests/EntityManagerFactoryTest.php
Outdated
| use LaravelDoctrine\ORM\EntityManagerFactory; | ||
| use LaravelDoctrine\ORM\Loggers\Logger; | ||
| use LaravelDoctrine\ORM\Resolvers\EntityListenerResolver as LaravelDoctrineEntityListenerResolver; | ||
|
|
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.
Remove empty line between use statements.
| * | ||
| * @return array | ||
| */ | ||
| public function getTestMasterSlaveConnectionData() |
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.
Same as comment ☝️
maxbrokman
left a 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.
Is a duplicate of #157 ?
src/EntityManagerFactory.php
Outdated
| throw new \InvalidArgumentException("Parameter 'read' must be an array containing multiple arrays."); | ||
| } | ||
|
|
||
| if (($key = array_search(0, array_map('count', $slaves))) && $key !== false) { |
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.
yeah this seem like it's trying to be too clever by half and will fail if the first config (position 0) is empty, poc here https://3v4l.org/N8gnk There doesn't seem to be any tests for this?
A lot of this could be replaced with
array_map(function($config){
if (!is_array($config)) {
//...
}
if (!count($config)) {
//...
}
}, $slaves);
only one loop, probably less buggy.
| $out[] = [ | ||
| $inputConfig, | ||
| \InvalidArgumentException::class, | ||
| "Parameter 'read' config no. 2 is empty." |
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.
consider adding a test for the bug when read config 0 is empty
a7b463c to
ee5a1ee
Compare
|
Updated it according to comments. I'm fully aware this is a duplicate of two other PRs but hopefully since there are so many PRs for this issue we'll get one in laravel doctrine and this PR will be the one getting merged. |
|
Can you add an example config to |
8a8fcd9 to
503d835
Compare
|
I don't know why you guys want to add sample documentation for laravels configuration file in your ORM repo but I've done it. Also, you can find actual documentation for this on the laravel web site. |
|
And here is the docs PR laravel-doctrine/docs#67. |
|
Sorry, this pull request will be merged in future versions of package? It can be very powerful feature for most of big projects. Thanks |
|
@kpicaza yes, if it passes my functional tests I'll merge it. |
|
Can i help you on this issue improving tests or doing something that is needed? |
|
So are you guys actually planning to merge this or just ignore it for the next six months and close it like the other PRs? I don't really care, but it seems like the community really wants it, and it doesn't look like anything is failing because of this new feature. |
|
This PR was opened a month ago, not six. And we've shown interest in it. |
|
There must've been some misunderstanding, I was talking about the older PRs being open for six months and being closed. |
|
I think you may be confusing projects: we don't have any history of closing six-month-old PRs. I've fixed Travis configuration, now 1.3 is running (and passing) all tests. Please rebase your fork and check that tests also pass in it. |
503d835 to
d678898
Compare
|
Done ;) |
guiwoda
left a 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.
Some more suggestions, I think it looks pretty good overall. Would merge as soon as this is ready!
| | | ||
| */ | ||
|
|
||
| 'default' => env('DB_CONNECTION', 'auth'), |
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.
Having an invalid default here may be confusing for users.
| @@ -0,0 +1,105 @@ | |||
| <?php | |||
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.
Do we want a sample in code? I believe having updated docs with sample in docs will have better visibility.
Also, if syntax matches what Laravel documents, then we don't really need this at all.
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 question is meant for @patrickbrouwers actually, as he suggested adding it 😄
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.
Yes can be removed, I didn't understand at first it was the same syntax as the laravel config.
src/EntityManagerFactory.php
Outdated
| $driver | ||
| ); | ||
|
|
||
| if ($this->isMasterSlaveConfigured($driver) && $this->hasValidMasterSlaveConfig($driver)) { |
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.
Refactor into:
if ($this->isMasterSlaveConfigured()) {
$this->validateMasterSlaveConfig($driver);
$connection = (new ...);
}
src/EntityManagerFactory.php
Outdated
| throw new \InvalidArgumentException("Parameter 'read' config no. {$key} is empty."); | ||
| } | ||
|
|
||
| return true; |
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.
By suggested refactor, this method does not need a return value
d678898 to
3f8f34f
Compare
3f8f34f to
cddf5b3
Compare
|
Done. |
|
I'm 👍 with this! |
Changes proposed in this pull request:
This PR doesn't have breaking changes so long as users don't have random read or write keys in their connection configurations.
Example master/slave connection configuration:
[ 'driver' => 'mysql', 'host' => 'localhost', 'port' => '3306', 'database' => 'test', 'username' => 'homestead', 'password' => 'secret', 'write' => [ 'port' => 3307, 'user' => 'homestead1', 'password' => 'secret1', ], 'read' => [ [ 'port' => 3308, 'database' => 'test2', ], [ 'host' => 'localhost2', 'port' => 3309 ], ], ];Settings can be specified different for each read/write configuration or you can just specify your base settings and override them in a specific read/write configuration (as in example).