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

Introduce truncate method #35

Merged
merged 4 commits into from
Jan 10, 2025
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
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() {
Copy link
Contributor

@defunctl defunctl Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is going to be a public method, shouldn't this be added to the Schema_Interface and do a major version release as this would be a breaking change?

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
Loading