Skip to content

Commit 979344c

Browse files
authored
Merge pull request #35 from stellarwp/feat/introduce-truncate-method
Introduce truncate method
2 parents 4dc4939 + ed6f8e1 commit 979344c

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. This projec
44

55
## [unreleased] Unreleased
66

7+
## [1.1.8] 2025-01-10
8+
9+
* 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.
10+
* Tweak - Decide if we can create/update during this requests based on blog's status, preventing multiple "check" queries.
711

812
## [1.1.7] 2024-06-05
913

src/Schema/Builder.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,16 +250,14 @@ public function register_custom_tables_names() {
250250
* Creates or updates the custom tables the plugin will use.
251251
*
252252
* @since 1.0.0
253+
* @since 1.1.8 Decided if we can perform the queries based on blog's status.
253254
*
254255
* @param bool $force Whether to force the creation or update of the tables or not.
255256
*
256257
* @return array<mixed> A list of each creation or update result.
257258
*/
258259
public function up( $force = false ) {
259-
try {
260-
$this->db::table( 'posts' )->select ( 1 )->limit( 1 )->get();
261-
} catch ( \Exception $e ) {
262-
// Let's not try to create the tables on a blog that's missing the basic ones.
260+
if ( ! is_blog_installed() || wp_installing() ) {
263261
return [];
264262
}
265263

src/Schema/Tables/Contracts/Table.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,28 @@ public function empty_table() {
220220

221221
$this_table = static::table_name( true );
222222

223+
$this->db::query( "SET foreign_key_checks = 0" );
224+
$result = $this->db::query( "DELETE FROM {$this_table}" );
225+
$this->db::query( "SET foreign_key_checks = 1" );
226+
227+
return $result;
228+
}
229+
230+
/**
231+
* Truncates the custom table.
232+
*
233+
* @since 1.1.8
234+
*
235+
* @return int|false The number of removed rows, or `false` to indicate a failure.
236+
*/
237+
public function truncate() {
238+
if ( ! $this->exists() ) {
239+
// There is really nothing to empty here.
240+
return 0;
241+
}
242+
243+
$this_table = static::table_name( true );
244+
223245
$this->db::query( "SET foreign_key_checks = 0" );
224246
$result = $this->db::query( "TRUNCATE {$this_table}" );
225247
$this->db::query( "SET foreign_key_checks = 1" );
@@ -535,7 +557,7 @@ public function update() {
535557
/**
536558
* Checks if a foreign key exists on a table.
537559
*
538-
* @since TBD
560+
* @since 1.1.3
539561
*
540562
* @param string $foreign_key The foreign key to check for.
541563
* @param string|null $table_name The table name to check. Defaults to the current table.

0 commit comments

Comments
 (0)