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

[4.x] Support database cache store tenancy #1290

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/Bootstrappers/DatabaseCacheBootstrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Stancl\Tenancy\Bootstrappers;

use Illuminate\Cache\CacheManager;
use Illuminate\Config\Repository;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant;

class DatabaseCacheBootstrapper implements TenancyBootstrapper
{
public function __construct(
protected Repository $config,
protected CacheManager $cache,
protected string|null $originalConnection = null,
) {}

public function bootstrap(Tenant $tenant): void
{
$this->originalConnection = $this->config->get('cache.stores.database.connection');

$this->config->set('cache.stores.database.connection', 'tenant');

$this->cache->purge('database');
}

public function revert(): void
{
$this->config->set('cache.stores.database.connection', $this->originalConnection);

$this->cache->purge('database');
}
}
77 changes: 77 additions & 0 deletions tests/DbCacheBootstrapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

use Stancl\Tenancy\Bootstrappers\DatabaseCacheBootstrapper;
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Events\TenantCreated;
use Stancl\JobPipeline\JobPipeline;
use Stancl\Tenancy\Jobs\CreateDatabase;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Illuminate\Support\Facades\DB;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Events\TenancyEnded;
use Stancl\Tenancy\Listeners\RevertToCentralContext;

beforeEach(function () {
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
return $event->tenant;
})->toListener());

Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
Event::listen(TenancyEnded::class, RevertToCentralContext::class);

config([
'cache.default' => 'database',
'tenancy.bootstrappers' => [
DatabaseTenancyBootstrapper::class,
DatabaseCacheBootstrapper::class, // Used instead of CacheTenancyBootstrapper
],
]);
});

test('DatabaseCacheBootstrapper uses tenant connection for saving cache', function() {
pest()->artisan('migrate', [
'--path' => __DIR__ . '/Etc/cache_migrations',
'--realpath' => true,
])->assertExitCode(0);

$databaseCacheKey = config('cache.prefix') . 'foo';
$databaseCacheValue = fn () => DB::selectOne("SELECT * FROM `cache` WHERE `key` = '{$databaseCacheKey}'")?->value;

$tenant = Tenant::create();
$tenant2 = Tenant::create();

pest()->artisan('tenants:migrate', [
'--path' => __DIR__ . '/Etc/cache_migrations',
'--realpath' => true,
])->assertExitCode(0);

// Write to cache in central context
cache(['foo' => 'CENTRAL']);

tenancy()->initialize($tenant);

// Write to cache in tenant context
cache(['foo' => 'TENANT']);

tenancy()->end();

// The 'foo' cache value in the central database should be 'CENTRAL'
expect(cache('foo'))->toBe('CENTRAL');
expect(str($databaseCacheValue())->contains('CENTRAL'))->toBeTrue();

tenancy()->initialize($tenant);

// The 'foo' cache value in the tenant database should be 'TENANT'
expect(cache('foo'))->toBe('TENANT');
expect(str($databaseCacheValue())->contains('TENANT'))->toBeTrue();

tenancy()->initialize($tenant2);

// The 'foo' cache value in another tenant's database should be null
expect(cache('foo'))->toBeNull();
expect(DB::select('select * from cache'))->toHaveCount(0);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('cache', function (Blueprint $table) {
$table->string('key')->primary();
$table->mediumText('value');
$table->integer('expiration');
});

Schema::create('cache_locks', function (Blueprint $table) {
$table->string('key')->primary();
$table->string('owner');
$table->integer('expiration');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('cache');
Schema::dropIfExists('cache_locks');
}
};
Loading