Skip to content

Conversation

@Sti3bas
Copy link
Contributor

@Sti3bas Sti3bas commented Nov 6, 2018

Current implementation of tenancy:migrate and tenancy:seed commands purges the database connection no matter of how many tenants are migrated or seeded. It means that it can’t be used with SQLite in-memory databases and that makes testing more complex.

This PR updates tenancy:migrate and tenancy:seed commands to only purge the database connection when multiple tenants are migrated or seeded, which makes TenantAwareTestCase more simple and elegant compared to the older version (#627 (comment)):

class TenantAwareTestCase extends TestCase
{
    use RefreshDatabase;

    protected $hostname = 'test.test.test';

    protected function setUp()
    {
        parent::setUp();

        app('tenancy.db.drivers')->put('sqlite', SqliteDriver::class);

        config(['tenancy.db.tenant-division-mode' => 'bypass']);

        $website = new Website;        
        app(WebsiteRepository::class)->create($website);
        
        $hostname = new Hostname;
        $hostname->fqdn = $this->hostname;
        $hostname = app(HostnameRepository::class)->create($hostname);
        app(HostnameRepository::class)->attach($hostname, $website);
        
        app()->singleton(CurrentHostname::class, function () use ($hostname) {
            return $hostname;
        });

        (new RouteProvider(app()))->boot();
    }
}

@bkintanar
Copy link
Member

Thank you for your time in writing this PR. I will try to have time to check on this. But all seems to be green and ready to go. Just need to double check. 👍

Cheers!

$this->input->setOption('database', $this->connection->tenantName());

$this->processHandle(function ($website) {
$this->processHandle(function (Website $website, Collection $websites) {
Copy link
Member

Choose a reason for hiding this comment

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

Instead of passing the full collection (thus creating a full copy in memory), why don't we make it a boolean?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@fletch3555 I think making it a boolean would make processHandle method too much concentrated to this issue. Maybe passing chunkSize integer would be a better idea? What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

I mean, its already specific to this issue, right? All it's checking is if the count > 1 then purging. Would make sense to simply pass in $shouldPurge as a boolean and leave the decision on why elsewhere

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I mean processHandle method is not responsible for managing the connection and it should know nothing about it, so adding connection specific logic seems not right to me. All it does is running the provided callback on the chunks of tenants. So passing the information about the chunk seems more clean to me:

$this->processHandle(function (Website $website, $chunkSize) {
    $this->connection->set($website);

    parent::handle();

    if ($chunkSize > 1) {
        $this->connection->purge();
    }
});

Copy link
Contributor

Choose a reason for hiding this comment

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

Fletch is right though. You're sending an argument into a method that will possibly increase the memory footprint. Either we move the purge out of the processHandle or we use a boolean argument (bool $purge = true).

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@luceos I agree with Fletch on memory footprint issue, but I don't agree with his proposed solution. Moving connection handling out of closure seems like a better way to solve this. I've just updated the code. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @luceos and @fletch3555, are there still any problems with this PR?

Copy link
Contributor

@luceos luceos left a comment

Choose a reason for hiding this comment

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

This is okay as is, but please fix the use of $websiteB in the tests, there's no reason to use a variable like that in the scope of the test functions, $website will suffice.

Thanks for your patience, I love your parent::handle() fallback, great idea! 👏

@luceos luceos merged commit 7c8865c into tenancy:5.x Nov 19, 2018
@luceos
Copy link
Contributor

luceos commented Nov 19, 2018

🎆 thank you! Time to tag a new version soon 👍

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.

4 participants