Skip to content

Commit

Permalink
Merge pull request #35 from stellarwp/feat/introduce-truncate-method
Browse files Browse the repository at this point in the history
Introduce truncate method
  • Loading branch information
dpanta94 authored Jan 10, 2025
2 parents 4dc4939 + ed6f8e1 commit 979344c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. This projec

## [unreleased] Unreleased

## [1.1.8] 2025-01-10

* Feature - Introduce truncate method which does what the empty_table method was doing. Update empty_table to actually empty the table instead of truncating it.
* Tweak - Decide if we can create/update during this requests based on blog's status, preventing multiple "check" queries.

## [1.1.7] 2024-06-05

Expand Down
6 changes: 2 additions & 4 deletions src/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,16 +250,14 @@ public function register_custom_tables_names() {
* Creates or updates the custom tables the plugin will use.
*
* @since 1.0.0
* @since 1.1.8 Decided if we can perform the queries based on blog's status.
*
* @param bool $force Whether to force the creation or update of the tables or not.
*
* @return array<mixed> A list of each creation or update result.
*/
public function up( $force = false ) {
try {
$this->db::table( 'posts' )->select ( 1 )->limit( 1 )->get();
} catch ( \Exception $e ) {
// Let's not try to create the tables on a blog that's missing the basic ones.
if ( ! is_blog_installed() || wp_installing() ) {
return [];
}

Expand Down
24 changes: 23 additions & 1 deletion src/Schema/Tables/Contracts/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,28 @@ public function empty_table() {

$this_table = static::table_name( true );

$this->db::query( "SET foreign_key_checks = 0" );
$result = $this->db::query( "DELETE FROM {$this_table}" );
$this->db::query( "SET foreign_key_checks = 1" );

return $result;
}

/**
* Truncates the custom table.
*
* @since 1.1.8
*
* @return int|false The number of removed rows, or `false` to indicate a failure.
*/
public function truncate() {
if ( ! $this->exists() ) {
// There is really nothing to empty here.
return 0;
}

$this_table = static::table_name( true );

$this->db::query( "SET foreign_key_checks = 0" );
$result = $this->db::query( "TRUNCATE {$this_table}" );
$this->db::query( "SET foreign_key_checks = 1" );
Expand Down Expand Up @@ -535,7 +557,7 @@ public function update() {
/**
* Checks if a foreign key exists on a table.
*
* @since TBD
* @since 1.1.3
*
* @param string $foreign_key The foreign key to check for.
* @param string|null $table_name The table name to check. Defaults to the current table.
Expand Down

0 comments on commit 979344c

Please sign in to comment.