mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 12:24:04 +00:00
Fix transactions
This commit is contained in:
parent
46609c5b0d
commit
c475e7a43d
11 changed files with 47 additions and 27 deletions
|
|
@ -56,7 +56,7 @@ class Migrate extends MigrateCommand
|
|||
// See Illuminate\Database\Migrations\DatabaseMigrationRepository::getConnection.
|
||||
// Database connections are cached by Illuminate\Database\ConnectionResolver.
|
||||
$this->input->setOption('database', 'tenant');
|
||||
tenancy()->initialize($tenant); // todo3 test that this works with multiple tenants with MySQL
|
||||
tenancy()->initialize($tenant); // todo2 test that this works with multiple tenants with MySQL
|
||||
|
||||
// Migrate
|
||||
parent::handle();
|
||||
|
|
|
|||
|
|
@ -4,8 +4,10 @@ declare(strict_types=1);
|
|||
|
||||
namespace Stancl\Tenancy;
|
||||
|
||||
use Illuminate\Database\Connection;
|
||||
use Illuminate\Database\DatabaseManager as BaseDatabaseManager;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
|
||||
use Stancl\Tenancy\Exceptions\DatabaseManagerNotRegisteredException;
|
||||
use Stancl\Tenancy\Exceptions\TenantDatabaseAlreadyExistsException;
|
||||
|
|
@ -176,4 +178,19 @@ class DatabaseManager
|
|||
|
||||
return $this->app[$databaseManagers[$driver]];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the central database connection.
|
||||
*
|
||||
* @return \Illuminate\Database\Connection
|
||||
*/
|
||||
public function getCentralConnection(): \Illuminate\Database\Connection
|
||||
{
|
||||
return DB::connection($this->getCentralConnectionName());
|
||||
}
|
||||
|
||||
public function getCentralConnectionName(): string
|
||||
{
|
||||
return $this->app['config']['tenancy.storage.db.connection'] ?? $this->originalDefaultConnectionName;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,7 +26,6 @@ class TelescopeTags implements Feature
|
|||
if (in_array('tenancy', optional(request()->route())->middleware() ?? [])) {
|
||||
$tags = array_merge($tags, [
|
||||
'tenant:' . tenant('id'),
|
||||
// todo3 domain?
|
||||
]);
|
||||
}
|
||||
|
||||
|
|
|
|||
13
src/StorageDrivers/Database/CentralConnection.php
Normal file
13
src/StorageDrivers/Database/CentralConnection.php
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\StorageDrivers\Database;
|
||||
|
||||
use Stancl\Tenancy\DatabaseManager;
|
||||
|
||||
trait CentralConnection
|
||||
{
|
||||
public function getConnectionName()
|
||||
{
|
||||
return app(DatabaseManager::class)->getCentralConnectionName();
|
||||
}
|
||||
}
|
||||
|
|
@ -5,8 +5,8 @@ declare(strict_types=1);
|
|||
namespace Stancl\Tenancy\StorageDrivers\Database;
|
||||
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Stancl\Tenancy\Contracts\StorageDriver;
|
||||
use Stancl\Tenancy\DatabaseManager;
|
||||
use Stancl\Tenancy\Exceptions\DomainsOccupiedByOtherTenantException;
|
||||
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
|
||||
use Stancl\Tenancy\Exceptions\TenantWithThisIdAlreadyExistsException;
|
||||
|
|
@ -16,17 +16,19 @@ use Stancl\Tenancy\Tenant;
|
|||
|
||||
class DatabaseStorageDriver implements StorageDriver
|
||||
{
|
||||
// todo4 write tests verifying that data is decoded and added to the array
|
||||
|
||||
/** @var Application */
|
||||
protected $app;
|
||||
|
||||
/** @var \Illuminate\Database\Connection */
|
||||
protected $centralDatabase;
|
||||
|
||||
/** @var Tenant The default tenant. */
|
||||
protected $tenant;
|
||||
|
||||
public function __construct(Application $app)
|
||||
{
|
||||
$this->app = $app;
|
||||
$this->centralDatabase = $app->make(DatabaseManager::class)->getCentralConnection();
|
||||
}
|
||||
|
||||
public function findByDomain(string $domain): Tenant
|
||||
|
|
@ -77,7 +79,7 @@ class DatabaseStorageDriver implements StorageDriver
|
|||
|
||||
public function createTenant(Tenant $tenant): void
|
||||
{
|
||||
DB::transaction(function () use ($tenant) {
|
||||
$this->centralDatabase->transaction(function () use ($tenant) {
|
||||
Tenants::create(['id' => $tenant->id, 'data' => '{}'])->toArray();
|
||||
|
||||
$domainData = [];
|
||||
|
|
@ -90,7 +92,7 @@ class DatabaseStorageDriver implements StorageDriver
|
|||
|
||||
public function updateTenant(Tenant $tenant): void
|
||||
{
|
||||
DB::transaction(function () use ($tenant) {
|
||||
$this->centralDatabase->transaction(function () use ($tenant) {
|
||||
Tenants::find($tenant->id)->putMany($tenant->data);
|
||||
|
||||
$original_domains = Domains::where('tenant_id', $tenant->id)->get()->map(function ($model) {
|
||||
|
|
@ -111,7 +113,7 @@ class DatabaseStorageDriver implements StorageDriver
|
|||
|
||||
public function deleteTenant(Tenant $tenant): void
|
||||
{
|
||||
DB::transacton(function () use ($tenant) {
|
||||
$this->centralDatabase->transaction(function () use ($tenant) {
|
||||
Tenants::find($tenant->id)->delete();
|
||||
Domains::where('tenant_id', $tenant->id)->delete();
|
||||
});
|
||||
|
|
|
|||
|
|
@ -5,13 +5,14 @@ declare(strict_types=1);
|
|||
namespace Stancl\Tenancy\StorageDrivers\Database;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Stancl\Tenancy\DatabaseManager;
|
||||
|
||||
/**
|
||||
* @internal Class is subject to breaking changes in minor and patch versions.
|
||||
*/
|
||||
class DomainModel extends Model
|
||||
{
|
||||
use CentralConnection;
|
||||
|
||||
protected $guarded = [];
|
||||
protected $primaryKey = 'id';
|
||||
public $incrementing = false;
|
||||
|
|
@ -21,9 +22,4 @@ class DomainModel extends Model
|
|||
{
|
||||
return config('tenancy.storage.db.table_names.DomainModel', 'domains');
|
||||
}
|
||||
|
||||
public function getConnectionName()
|
||||
{
|
||||
return config('tenancy.storage.db.connection') ?? app(DatabaseManager::class)->originalDefaultConnectionName;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,13 +5,14 @@ declare(strict_types=1);
|
|||
namespace Stancl\Tenancy\StorageDrivers\Database;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Stancl\Tenancy\DatabaseManager;
|
||||
|
||||
/**
|
||||
* @internal Class is subject to breaking changes in minor and patch versions.
|
||||
*/
|
||||
class TenantModel extends Model
|
||||
{
|
||||
use CentralConnection;
|
||||
|
||||
protected $guarded = [];
|
||||
protected $primaryKey = 'id';
|
||||
public $incrementing = false;
|
||||
|
|
@ -32,11 +33,6 @@ class TenantModel extends Model
|
|||
return config('tenancy.storage.db.custom_columns', []);
|
||||
}
|
||||
|
||||
public function getConnectionName()
|
||||
{
|
||||
return config('tenancy.storage.db.connection') ?? app(DatabaseManager::class)->originalDefaultConnectionName;
|
||||
}
|
||||
|
||||
public static function getAllTenants(array $ids)
|
||||
{
|
||||
$tenants = $ids ? static::findMany($ids) : static::all();
|
||||
|
|
|
|||
|
|
@ -50,7 +50,6 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper
|
|||
'tenant_id' => $id,
|
||||
'tags' => [
|
||||
"tenant:$id",
|
||||
// todo3 domain
|
||||
],
|
||||
];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ class QueueTest extends TestCase
|
|||
/** @test */
|
||||
public function queues_use_non_tenant_db_connection()
|
||||
{
|
||||
// todo2 finish this test. requires using the db driver
|
||||
// requires using the db driver
|
||||
$this->markTestIncomplete();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -15,14 +15,13 @@ class TenantAssetTest extends TestCase
|
|||
|
||||
// response()->file() returns BinaryFileResponse whose content is
|
||||
// inaccessible via getContent, so ->assertSee() can't be used
|
||||
// $this->get(tenant_asset($filename))->assertSuccessful(); // todo2 commented assertions
|
||||
// $this->assertFileExists($path); // todo2 commented assertions
|
||||
$this->get(tenant_asset($filename))->assertSuccessful();
|
||||
$this->assertFileExists($path);
|
||||
|
||||
$f = fopen($path, 'r');
|
||||
$content = fread($f, filesize($path));
|
||||
fclose($f);
|
||||
|
||||
// $this->assertSame('bar', $content); // todo2 commented assertions
|
||||
$this->assertTrue(true);
|
||||
$this->assertSame('bar', $content);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@ class TenantClassTest extends TestCase
|
|||
/** @test */
|
||||
public function data_cache_works_properly()
|
||||
{
|
||||
// todo constructor dependencies
|
||||
// $spy = Mockery::spy(config('tenancy.storage_driver'))->makePartial();
|
||||
// $this->instance(StorageDriver::class, $spy);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue