clock->now() - self::RETENTION_SECONDS; $removed_links = $this->delete_old_links( $oldest_kept_time ); $removed_fixes = $this->delete_old_fixes( $oldest_kept_time ); $removed_rows = $removed_links + $removed_fixes; if ( 0 < $removed_rows ) { $this->data_version->bump(); } return $removed_rows; } private function delete_old_links( int $oldest_kept_time ): int { $links = $this->tables->links(); return $this->query_runner->execute( $this->query_runner->prepare( 'DELETE FROM ' . $links . ' WHERE outcome IN (%s, %s)' . ' AND NOT EXISTS (SELECT 1 FROM ' . $this->tables->uses() . ' uses' . ' WHERE uses.link_id = ' . $links . '.id)' . ' AND fixed_by IN (SELECT fixes.id FROM ' . $this->tables->fixes() . ' fixes' . ' WHERE fixes.created_at < %d)', Outcome::Resolved->value, Outcome::Removed->value, $oldest_kept_time ) ); } private function delete_old_fixes( int $oldest_kept_time ): int { return $this->query_runner->execute( $this->query_runner->prepare( 'DELETE FROM ' . $this->tables->fixes() . ' WHERE state IN (%s, %s) AND created_at < %d', self::FIX_DONE, self::FIX_UNDONE, $oldest_kept_time ) ); } }