Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add method for adding row deletion policy (for modifying table) #124

Merged
merged 2 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions src/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,20 @@ public function deleteRowsOlderThan(string $column, int $days)
]);
}

/**
* @param string $column
* @param int $days
* @return Fluent<string, mixed>
*/
public function addRowDeletionPolicy(string $column, int $days): Fluent
{
return $this->addCommand('addRowDeletionPolicy', [
'policy' => 'olderThan',
'column' => $column,
'days' => $days,
]);
}

/**
* @param string $column
* @param int $days
Expand Down
15 changes: 15 additions & 0 deletions src/Schema/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,21 @@ public function compileDropColumn(Blueprint $blueprint, Fluent $command)
return 'alter table '.$this->wrapTable($blueprint).' '.implode(', ', $columns);
}

/**
* @param Blueprint $blueprint
* @param Fluent<string, mixed> $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<string, mixed> $command
Expand Down
28 changes: 28 additions & 0 deletions tests/Schema/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down