From bc6ab9c7ba14c4677fc8e3ebee1987b6482730e5 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Fri, 10 Jan 2025 21:14:38 +0200 Subject: [PATCH 1/4] Introduce truncate method --- src/Schema/Tables/Contracts/Table.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/Schema/Tables/Contracts/Table.php b/src/Schema/Tables/Contracts/Table.php index f2a586f..19ca217 100644 --- a/src/Schema/Tables/Contracts/Table.php +++ b/src/Schema/Tables/Contracts/Table.php @@ -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 TBD + * + * @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" ); From fba6dc43e364b0212279ba13b6a2ee6fa1b09c75 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Fri, 10 Jan 2025 21:14:51 +0200 Subject: [PATCH 2/4] Prevent multiple "check" queries --- src/Schema/Builder.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index 0e74117..21a1d90 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -256,11 +256,8 @@ public function register_custom_tables_names() { * @return array 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. - return []; + if ( ! is_blog_installed() || wp_installing() ) { + return; } $results = []; From fc54a5aabb33dddc9680bcee7e8ad889522cd9b9 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Fri, 10 Jan 2025 21:17:52 +0200 Subject: [PATCH 3/4] fix static analysis --- src/Schema/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index 21a1d90..1d8a987 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -257,7 +257,7 @@ public function register_custom_tables_names() { */ public function up( $force = false ) { if ( ! is_blog_installed() || wp_installing() ) { - return; + return []; } $results = []; From ed6f8e1bebcff3bb21b2407298876672cf0ff362 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Fri, 10 Jan 2025 21:38:37 +0200 Subject: [PATCH 4/4] Added docs --- CHANGELOG.md | 4 ++++ src/Schema/Builder.php | 1 + src/Schema/Tables/Contracts/Table.php | 4 ++-- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 82901fd..87d7abb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index 1d8a987..74996cd 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -250,6 +250,7 @@ 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. * diff --git a/src/Schema/Tables/Contracts/Table.php b/src/Schema/Tables/Contracts/Table.php index 19ca217..27e1b7a 100644 --- a/src/Schema/Tables/Contracts/Table.php +++ b/src/Schema/Tables/Contracts/Table.php @@ -230,7 +230,7 @@ public function empty_table() { /** * Truncates the custom table. * - * @since TBD + * @since 1.1.8 * * @return int|false The number of removed rows, or `false` to indicate a failure. */ @@ -557,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.