From b0e5564197b29649c0597a8356fb1dcd7b180f84 Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Wed, 5 Jan 2022 12:59:54 -0500 Subject: [PATCH 1/5] Use delete instead of truncate https://github.com/babenkoivan/elastic-migrations/issues/34 --- src/Repositories/MigrationRepository.php | 2 +- .../Integration/Repositories/MigrationRepositoryTest.php | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Repositories/MigrationRepository.php b/src/Repositories/MigrationRepository.php index 5876383..59157db 100644 --- a/src/Repositories/MigrationRepository.php +++ b/src/Repositories/MigrationRepository.php @@ -50,7 +50,7 @@ public function delete(string $fileName): bool public function truncate(): void { - $this->table()->truncate(); + $this->table()->delete(); } public function getLastBatchNumber(): ?int diff --git a/tests/Integration/Repositories/MigrationRepositoryTest.php b/tests/Integration/Repositories/MigrationRepositoryTest.php index 6792351..2179e81 100644 --- a/tests/Integration/Repositories/MigrationRepositoryTest.php +++ b/tests/Integration/Repositories/MigrationRepositoryTest.php @@ -105,4 +105,13 @@ public function test_repository_is_not_ready_when_table_does_not_exist(): void $this->assertFalse($this->migrationRepository->isReady()); } + + public function test_repository_can_truncate_all_records(): void + { + $this->assertDatabaseCount($this->table, 2); + + $this->migrationRepository->truncate(); + + $this->assertDatabaseCount($this->table, 0); + } } From 54fad178b0ede818556563829e8ab9506cf6a378 Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Wed, 5 Jan 2022 13:04:59 -0500 Subject: [PATCH 2/5] Use assert count due to undefined assertions in lower PHP versions --- tests/Integration/Repositories/MigrationRepositoryTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Integration/Repositories/MigrationRepositoryTest.php b/tests/Integration/Repositories/MigrationRepositoryTest.php index 2179e81..f002a58 100644 --- a/tests/Integration/Repositories/MigrationRepositoryTest.php +++ b/tests/Integration/Repositories/MigrationRepositoryTest.php @@ -108,10 +108,10 @@ public function test_repository_is_not_ready_when_table_does_not_exist(): void public function test_repository_can_truncate_all_records(): void { - $this->assertDatabaseCount($this->table, 2); + $this->assertCount(2, $this->migrationRepository->getAll()); $this->migrationRepository->truncate(); - $this->assertDatabaseCount($this->table, 0); + $this->assertCount(0, $this->migrationRepository->getAll()); } } From 9d86c816c31debb73cfd9a959b5e042259862691 Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Wed, 5 Jan 2022 14:46:08 -0500 Subject: [PATCH 3/5] Add `deleteAll()` method and deprecate `truncate()` method --- src/Console/FreshCommand.php | 2 +- src/Repositories/MigrationRepository.php | 10 +++++++++- .../Repositories/MigrationRepositoryTest.php | 4 ++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Console/FreshCommand.php b/src/Console/FreshCommand.php index 2ed3177..db29fe4 100644 --- a/src/Console/FreshCommand.php +++ b/src/Console/FreshCommand.php @@ -35,7 +35,7 @@ public function handle( $indexManager->drop('*'); - $migrationRepository->truncate(); + $migrationRepository->deleteAll(); $migrator->migrateAll(); diff --git a/src/Repositories/MigrationRepository.php b/src/Repositories/MigrationRepository.php index 59157db..bb33e15 100644 --- a/src/Repositories/MigrationRepository.php +++ b/src/Repositories/MigrationRepository.php @@ -48,11 +48,19 @@ public function delete(string $fileName): bool ->delete(); } - public function truncate(): void + public function deleteAll(): void { $this->table()->delete(); } + /** + * @deprecated + */ + public function truncate(): void + { + $this->deleteAll(); + } + public function getLastBatchNumber(): ?int { /** @var stdClass|null $record */ diff --git a/tests/Integration/Repositories/MigrationRepositoryTest.php b/tests/Integration/Repositories/MigrationRepositoryTest.php index f002a58..ba43086 100644 --- a/tests/Integration/Repositories/MigrationRepositoryTest.php +++ b/tests/Integration/Repositories/MigrationRepositoryTest.php @@ -106,11 +106,11 @@ public function test_repository_is_not_ready_when_table_does_not_exist(): void $this->assertFalse($this->migrationRepository->isReady()); } - public function test_repository_can_truncate_all_records(): void + public function test_repository_can_delete_all_records(): void { $this->assertCount(2, $this->migrationRepository->getAll()); - $this->migrationRepository->truncate(); + $this->migrationRepository->deleteAll(); $this->assertCount(0, $this->migrationRepository->getAll()); } From 388885bc81e8685566299ad371c9da375262e090 Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Wed, 5 Jan 2022 14:47:39 -0500 Subject: [PATCH 4/5] Fix tests --- tests/Integration/Console/FreshCommandTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Integration/Console/FreshCommandTest.php b/tests/Integration/Console/FreshCommandTest.php index c933b52..88defec 100644 --- a/tests/Integration/Console/FreshCommandTest.php +++ b/tests/Integration/Console/FreshCommandTest.php @@ -63,7 +63,7 @@ public function test_does_nothing_if_migrator_is_not_ready(): void $this->migrationRepository ->expects($this->never()) - ->method('truncate'); + ->method('deleteAll'); $this->migrator ->expects($this->never()) @@ -91,7 +91,7 @@ public function test_drops_indicies_and_migration(): void $this->migrationRepository ->expects($this->once()) - ->method('truncate'); + ->method('deleteAll'); $this->migrator ->expects($this->once()) From 6afbcb5eb9fa13cd242499bf8a0327b93b819bfe Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Wed, 12 Jan 2022 08:54:49 -0500 Subject: [PATCH 5/5] Truncate table to maintain BC --- src/Repositories/MigrationRepository.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Repositories/MigrationRepository.php b/src/Repositories/MigrationRepository.php index bb33e15..c13ac41 100644 --- a/src/Repositories/MigrationRepository.php +++ b/src/Repositories/MigrationRepository.php @@ -58,7 +58,7 @@ public function deleteAll(): void */ public function truncate(): void { - $this->deleteAll(); + $this->table()->truncate(); } public function getLastBatchNumber(): ?int