Skip to content

Commit 3aebb60

Browse files
committed
feat(TestRuns): autodelete duplicates
1 parent d317c5c commit 3aebb60

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

includes/models/class-test-run.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,23 @@ public static function save( $args = [], $row_id = null ) {
256256
}
257257
}
258258

259+
/**
260+
* Delete duplicate test runs by service_test_run_id from database.
261+
*
262+
* @return void
263+
*/
264+
public static function delete_duplicates() {
265+
global $wpdb;
266+
267+
$test_runs_table = Test_Runs_Table::get_table_name();
268+
269+
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- It's ok.
270+
$wpdb->query(
271+
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- It's ok.
272+
"DELETE t1 FROM $test_runs_table t1 INNER JOIN $test_runs_table t2 WHERE t1.id > t2.id AND t1.service_test_run_id = t2.service_test_run_id"
273+
);
274+
}
275+
259276
/**
260277
* Insert multiple test data
261278
*

includes/services/class-test-run-service.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,11 @@ public function create_test_run( $service_test_run_id, $data, $update = false )
102102
if ( $test_run && ! $update ) {
103103
return false;
104104
}
105-
return Test_Run::save(array_merge( $data, [
105+
$test_run_id = Test_Run::save(array_merge( $data, [
106106
'service_test_run_id' => $service_test_run_id,
107107
]), $test_run->id ?? null);
108+
Test_Run::delete_duplicates();
109+
return $test_run_id;
108110
}
109111

110112
/**

0 commit comments

Comments
 (0)