From b90fb6ec77e02228c3dc0f2fd2e3bcb4216708e0 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Mon, 8 Aug 2022 16:55:27 +0500 Subject: [PATCH] configure attributes for creating resource --- src/Contracts/Syncable.php | 2 + src/Listeners/UpdateSyncedResource.php | 3 +- ...st_create_users_with_extra_field_table.php | 34 ++++++++++++++ tests/ResourceSyncingTest.php | 45 +++++++++++++++++++ 4 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 tests/Etc/synced_resource_migrations/custom/2020_05_11_000001_test_create_users_with_extra_field_table.php diff --git a/src/Contracts/Syncable.php b/src/Contracts/Syncable.php index 1f6e36e7..5d978db4 100644 --- a/src/Contracts/Syncable.php +++ b/src/Contracts/Syncable.php @@ -15,4 +15,6 @@ interface Syncable public function getSyncedAttributeNames(): array; public function triggerSyncEvent(); + + public function getCreateAttributeNames(): array; } diff --git a/src/Listeners/UpdateSyncedResource.php b/src/Listeners/UpdateSyncedResource.php index 9be290f0..201c7c3b 100644 --- a/src/Listeners/UpdateSyncedResource.php +++ b/src/Listeners/UpdateSyncedResource.php @@ -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)); } }); diff --git a/tests/Etc/synced_resource_migrations/custom/2020_05_11_000001_test_create_users_with_extra_field_table.php b/tests/Etc/synced_resource_migrations/custom/2020_05_11_000001_test_create_users_with_extra_field_table.php new file mode 100644 index 00000000..c0a0ccf2 --- /dev/null +++ b/tests/Etc/synced_resource_migrations/custom/2020_05_11_000001_test_create_users_with_extra_field_table.php @@ -0,0 +1,34 @@ +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'); + } +} diff --git a/tests/ResourceSyncingTest.php b/tests/ResourceSyncingTest.php index 806e8706..80a0f96b 100644 --- a/tests/ResourceSyncingTest.php +++ b/tests/ResourceSyncingTest.php @@ -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' + ]; + } }