diff --git a/includes/models/class-test-run.php b/includes/models/class-test-run.php index f4ff3c3..85297ff 100644 --- a/includes/models/class-test-run.php +++ b/includes/models/class-test-run.php @@ -256,6 +256,23 @@ public static function save( $args = [], $row_id = null ) { } } + /** + * Delete duplicate test runs by service_test_run_id from database. + * + * @return void + */ + public static function delete_duplicates() { + global $wpdb; + + $test_runs_table = Test_Runs_Table::get_table_name(); + + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- It's ok. + $wpdb->query( + // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- It's ok. + "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" + ); + } + /** * Insert multiple test data * diff --git a/includes/services/class-test-run-service.php b/includes/services/class-test-run-service.php index 0b35581..8269327 100644 --- a/includes/services/class-test-run-service.php +++ b/includes/services/class-test-run-service.php @@ -102,9 +102,11 @@ public function create_test_run( $service_test_run_id, $data, $update = false ) if ( $test_run && ! $update ) { return false; } - return Test_Run::save(array_merge( $data, [ + $test_run_id = Test_Run::save(array_merge( $data, [ 'service_test_run_id' => $service_test_run_id, ]), $test_run->id ?? null); + Test_Run::delete_duplicates(); + return $test_run_id; } /**