Skip to content

Conversation

@Zylius
Copy link

@Zylius Zylius commented Mar 6, 2017

Changes proposed in this pull request:

  • Add support for master slave configuration.

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

@Zylius Zylius force-pushed the 1.3_master_slave_connection branch 2 times, most recently from d159be3 to 8d636e2 Compare March 6, 2017 13:19
@patrickbrouwers
Copy link
Contributor

Can you add some more information to the PR?
E.g. does it have breaking changes for using the current state of the config file?

throw new \InvalidArgumentException("Parameter 'read' must be an array containing multiple arrays.");
}

if (($key = array_search(0, array_map('count', $slaves))) && $key !== false) {
Copy link
Contributor

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?

Copy link
Contributor

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.

Copy link
Author

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

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?

use LaravelDoctrine\ORM\EntityManagerFactory;
use LaravelDoctrine\ORM\Loggers\Logger;
use LaravelDoctrine\ORM\Resolvers\EntityListenerResolver as LaravelDoctrineEntityListenerResolver;

Copy link
Contributor

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

Choose a reason for hiding this comment

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

Same as comment ☝️

Copy link
Contributor

@maxbrokman maxbrokman left a 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 ?

throw new \InvalidArgumentException("Parameter 'read' must be an array containing multiple arrays.");
}

if (($key = array_search(0, array_map('count', $slaves))) && $key !== false) {
Copy link
Contributor

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."
Copy link
Contributor

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

@Zylius Zylius force-pushed the 1.3_master_slave_connection branch from a7b463c to ee5a1ee Compare March 7, 2017 13:30
@Zylius
Copy link
Author

Zylius commented Mar 7, 2017

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.

@patrickbrouwers
Copy link
Contributor

Can you add an example config to /config. This doesn't have to get published, but would be a good reference point for anybody wanting to use master-slave. Also a PR to our Docs repo would be very helpful.

@Zylius
Copy link
Author

Zylius commented Mar 29, 2017

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.

@Zylius
Copy link
Author

Zylius commented Mar 29, 2017

And here is the docs PR laravel-doctrine/docs#67.

@kpicaza
Copy link

kpicaza commented Mar 29, 2017

Sorry, this pull request will be merged in future versions of package?

It can be very powerful feature for most of big projects.

Thanks

@patrickbrouwers
Copy link
Contributor

@kpicaza yes, if it passes my functional tests I'll merge it.

@kpicaza
Copy link

kpicaza commented Apr 11, 2017

Can i help you on this issue improving tests or doing something that is needed?

@Zylius
Copy link
Author

Zylius commented Apr 12, 2017

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.

@guiwoda
Copy link
Contributor

guiwoda commented Apr 12, 2017

This PR was opened a month ago, not six. And we've shown interest in it.
Mind your attitude, please. We appreciate your help, but just as you, we have other responsibilities outside open source. This will be reviewed and merged when we can spare the time to do so, in the meantime, people can force their dependencies to the fork if they really need it, or implement master / slave outside app, which can also be done.

@Zylius
Copy link
Author

Zylius commented Apr 12, 2017

There must've been some misunderstanding, I was talking about the older PRs being open for six months and being closed.
Yeah, I've done the master/slave inside app thing. Just trying to help out the community.
Anyways, I appreciate your efforts in trying to make laravel a better framework!

@guiwoda
Copy link
Contributor

guiwoda commented Apr 12, 2017

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.

@Zylius Zylius force-pushed the 1.3_master_slave_connection branch from 503d835 to d678898 Compare April 12, 2017 13:25
@Zylius
Copy link
Author

Zylius commented Apr 12, 2017

Done ;)

Copy link
Contributor

@guiwoda guiwoda left a 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'),
Copy link
Contributor

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

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.

Copy link
Contributor

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 😄

Copy link
Contributor

@patrickbrouwers patrickbrouwers Apr 12, 2017

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.

$driver
);

if ($this->isMasterSlaveConfigured($driver) && $this->hasValidMasterSlaveConfig($driver)) {
Copy link
Contributor

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

throw new \InvalidArgumentException("Parameter 'read' config no. {$key} is empty.");
}

return true;
Copy link
Contributor

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

@Zylius Zylius force-pushed the 1.3_master_slave_connection branch from d678898 to 3f8f34f Compare April 13, 2017 06:15
@Zylius Zylius force-pushed the 1.3_master_slave_connection branch from 3f8f34f to cddf5b3 Compare April 13, 2017 06:16
@Zylius
Copy link
Author

Zylius commented Apr 13, 2017

Done.

@guiwoda
Copy link
Contributor

guiwoda commented Apr 13, 2017

I'm 👍 with this!
Shoud we merge, @patrickbrouwers ?

@patrickbrouwers patrickbrouwers merged commit 5f7523c into laravel-doctrine:1.3 Apr 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants