diff --git a/CHANGELOG.md b/CHANGELOG.md index 1051b7df..694999f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ Added - Added deprecation warnings to `Connection::runDdl` and `Connection::runDdls` (#98) - Added `ManagesMutations::insertOrUpdateUsingMutation` and `UsesMutations::insertOrUpdateUsingMutation` to do upserts (#109) - Added Support for `Schema\Builder::dropIfExists()` (#115) +- Added support for adding row deletion policy when modifying table (#124) Changed - `Connection::waitForOperation` and `Connection::isDoneOperation` has been removed. (#99) diff --git a/src/Schema/Blueprint.php b/src/Schema/Blueprint.php index 2c158a30..52699f6c 100644 --- a/src/Schema/Blueprint.php +++ b/src/Schema/Blueprint.php @@ -217,6 +217,20 @@ public function deleteRowsOlderThan(string $column, int $days) ]); } + /** + * @param string $column + * @param int $days + * @return Fluent + */ + public function addRowDeletionPolicy(string $column, int $days): Fluent + { + return $this->addCommand('addRowDeletionPolicy', [ + 'policy' => 'olderThan', + 'column' => $column, + 'days' => $days, + ]); + } + /** * @param string $column * @param int $days diff --git a/src/Schema/Grammar.php b/src/Schema/Grammar.php index 5da84605..78fd9e1f 100644 --- a/src/Schema/Grammar.php +++ b/src/Schema/Grammar.php @@ -134,6 +134,21 @@ public function compileDropColumn(Blueprint $blueprint, Fluent $command) return 'alter table '.$this->wrapTable($blueprint).' '.implode(', ', $columns); } + /** + * @param Blueprint $blueprint + * @param Fluent $command + * @return string + */ + public function compileAddRowDeletionPolicy(Blueprint $blueprint, Fluent $command) + { + if ($command->policy !== 'olderThan') { + throw new RuntimeException('Unknown deletion policy:'.$command->policy); + } + $table = $this->wrapTable($blueprint); + $column = $this->wrap($command->column); + return "alter table {$table} add row deletion policy (older_than({$column}, interval {$command->days} day))"; + } + /** * @param Blueprint $blueprint * @param Fluent $command diff --git a/tests/Schema/BlueprintTest.php b/tests/Schema/BlueprintTest.php index 1d486cc4..003c385f 100644 --- a/tests/Schema/BlueprintTest.php +++ b/tests/Schema/BlueprintTest.php @@ -306,6 +306,34 @@ public function test_create_with_row_deletion_policy(): void ); } + public function test_add_row_deletion_policy(): void + { + $conn = $this->getDefaultConnection(); + $conn->useDefaultSchemaGrammar(); + $grammar = $conn->getSchemaGrammar(); + $table = 'Test_' . Str::random(); + + $blueprint1 = new Blueprint($table, function (Blueprint $table) { + $table->create(); + $table->uuid('id'); + $table->primary('id'); + $table->dateTime('t')->nullable(); + }); + $blueprint1->build($conn, $grammar); + + $blueprint2 = new Blueprint($table, function (Blueprint $table) { + $table->addRowDeletionPolicy('t', 200); + }); + $blueprint2->build($conn, $grammar); + + $statement = $blueprint2->toSql($conn, $grammar)[0]; + + self::assertEquals( + "alter table `{$table}` add row deletion policy (older_than(`t`, interval 200 day))", + $statement, + ); + } + public function test_replace_row_deletion_policy(): void { $conn = $this->getDefaultConnection();