mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-13 03:34:04 +00:00
configure attributes for creating resource
This commit is contained in:
parent
db4a795c3e
commit
b90fb6ec77
4 changed files with 82 additions and 2 deletions
|
|
@ -15,4 +15,6 @@ interface Syncable
|
|||
public function getSyncedAttributeNames(): array;
|
||||
|
||||
public function triggerSyncEvent();
|
||||
|
||||
public function getCreateAttributeNames(): array;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -58,8 +58,7 @@ class UpdateSyncedResource extends QueueableListener
|
|||
event(new SyncedResourceChangedInForeignDatabase($event->model, null));
|
||||
} else {
|
||||
// If the resource doesn't exist at all in the central DB,we create
|
||||
// the record with all attributes, not just the synced ones.
|
||||
$centralModel = $event->model->getCentralModelName()::create($event->model->getAttributes());
|
||||
$centralModel = $event->model->getCentralModelName()::create($event->model->only($event->model->getCreateAttributeNames()));
|
||||
event(new SyncedResourceChangedInForeignDatabase($event->model, null));
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class TestCreateUsersWithExtraFieldTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('users', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('global_id')->unique();
|
||||
$table->string('name');
|
||||
$table->string('email');
|
||||
$table->string('password');
|
||||
|
||||
$table->string('role');
|
||||
$table->string('code');
|
||||
});
|
||||
}
|
||||
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('users');
|
||||
}
|
||||
}
|
||||
|
|
@ -126,6 +126,34 @@ test('only the synced columns are updated in the central db', function () {
|
|||
], ResourceUser::first()->getAttributes());
|
||||
});
|
||||
|
||||
test('creating the resource in tenant database creates it in central database and used custom attributes', function () {
|
||||
// Assert no user in central DB
|
||||
expect(ResourceUser::all())->toHaveCount(0);
|
||||
|
||||
$tenant = ResourceTenant::create();
|
||||
pest()->artisan('tenants:migrate', [
|
||||
'--path' => __DIR__ . '/Etc/synced_resource_migrations/custom',
|
||||
'--realpath' => true,
|
||||
])->assertExitCode(0);
|
||||
|
||||
tenancy()->initialize($tenant);
|
||||
|
||||
// Create the same user in tenant DB
|
||||
ResourceUser::create([
|
||||
'global_id' => 'acme',
|
||||
'name' => 'John Doe',
|
||||
'email' => 'john@localhost',
|
||||
'password' => 'secret',
|
||||
'role' => 'commenter', // unsynced
|
||||
'code' => 'bar' // extra column which does not exist in central users table
|
||||
]);
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
// Asset user was created
|
||||
expect(CentralUser::first()->global_id)->toBe('acme');
|
||||
});
|
||||
|
||||
test('creating the resource in tenant database creates it in central database and creates the mapping', function () {
|
||||
creatingResourceInTenantDatabaseCreatesAndMapInCentralDatabase();
|
||||
});
|
||||
|
|
@ -598,6 +626,11 @@ class CentralUser extends Model implements SyncMaster
|
|||
'email',
|
||||
];
|
||||
}
|
||||
|
||||
public function getCreateAttributeNames(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
class ResourceUser extends Model implements Syncable
|
||||
|
|
@ -633,4 +666,16 @@ class ResourceUser extends Model implements Syncable
|
|||
'email',
|
||||
];
|
||||
}
|
||||
|
||||
public function getCreateAttributeNames(): array
|
||||
{
|
||||
// attributes should be used when syncing resource from DB
|
||||
return [
|
||||
'global_id',
|
||||
'name',
|
||||
'password',
|
||||
'email',
|
||||
'role'
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue