Skip to content

Commit 8f6dd70

Browse files
authored
πŸ› Fix importers sorting for last run and next run (#977)
* πŸ› Fix importers sorting for last run and next run This commit will fix the sorting error that happens when the user is on the importers index and attempts to sort by the last run or next run. We add some migrations to add fields that the datatables can use so it can sort properly. Previously, this was not working because the last_imported_at and next_import_at fields were actually methods on the importer_run object and not the importer object. We are adding a few callbacks to the importer and importer_run models to ensure that the fields are properly set when they are called from either the web or the worker. Ref: - #956 * πŸ€– Update upload-artifact and download-artifact CI was getting errors because the versions we were previously using were deprecated. This commit updates the actions to the latest versions. * πŸ› Remove Downloadable Files sorting The downloadable files sorting was broken plus, it's not clear now a downloadable file should be sorted. * βš™οΈ Add guard for new migrations This commit will add a guard to the new migrations to ensure that they do not run if the columns already exist in the database. * πŸ€– Add rake task to re save importers This rake task will allow users to re save all their importers. It accounts for tenants if it is a Hyku application. ```sh bundle exec rake bulkrax:resave_importers ```
1 parent b511db6 commit 8f6dd70

File tree

7 files changed

+72
-4
lines changed

7 files changed

+72
-4
lines changed

β€Ž.github/workflows/test.ymlβ€Ž

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ jobs:
4343
run: bundle exec rake spec
4444

4545
- name: Upload coverage results
46-
uses: actions/upload-artifact@v2
46+
uses: actions/upload-artifact@v4.4.0
4747
with:
4848
name: coverage-report-${{ matrix.ruby }}
49-
path: coverage
49+
path: coverage/**
50+
include-hidden-files: true
5051

5152
coverage:
5253
runs-on: ubuntu-latest
@@ -56,7 +57,7 @@ jobs:
5657

5758
steps:
5859
- name: Download coverage report
59-
uses: actions/download-artifact@v2
60+
uses: actions/download-artifact@v4.1.8
6061
with:
6162
name: coverage-report-2.7
6263
path: coverage

β€Žapp/assets/javascripts/bulkrax/datatables.jsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Blacklight.onLoad(function() {
6767
{ "data": "name" },
6868
{ "data": "status_message" },
6969
{ "data": "created_at" },
70-
{ "data": "download" },
70+
{ "data": "download", "orderable": false },
7171
{ "data": "actions", "orderable": false }
7272
],
7373
initComplete: function () {

β€Žapp/models/bulkrax/importer.rbβ€Ž

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class Importer < ApplicationRecord # rubocop:disable Metrics/ClassLength
1818

1919
delegate :create_parent_child_relationships, :valid_import?, :write_errored_entries_file, :visibility, to: :parser
2020

21+
after_save :set_last_imported_at_from_importer_run
22+
after_save :set_next_import_at_from_importer_run
23+
2124
attr_accessor :only_updates, :file_style, :file
2225
attr_writer :current_run
2326

@@ -247,5 +250,27 @@ def path_string
247250
rescue
248251
"#{self.id}_#{self.created_at.strftime('%Y%m%d%H%M%S')}"
249252
end
253+
254+
private
255+
256+
# Adding this here since we can update the importer without running the importer.
257+
# When we simply save the importer (as in just updating the importer from the options),
258+
# it does not trigger the after_save callback in the importer_run.
259+
def set_last_imported_at_from_importer_run
260+
return if @skip_set_last_imported_at # Prevent infinite loop
261+
262+
@skip_set_last_imported_at = true
263+
importer_runs.last&.set_last_imported_at
264+
@skip_set_last_imported_at = false
265+
end
266+
267+
# @see #set_last_imported_at_from_importer_run
268+
def set_next_import_at_from_importer_run
269+
return if @skip_set_next_import_at # Prevent infinite loop
270+
271+
@skip_set_next_import_at = true
272+
importer_runs.last&.set_next_import_at
273+
@skip_set_next_import_at = false
274+
end
250275
end
251276
end

β€Žapp/models/bulkrax/importer_run.rbβ€Ž

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ class ImporterRun < ApplicationRecord
66
has_many :statuses, as: :runnable, dependent: :destroy
77
has_many :pending_relationships, dependent: :destroy
88

9+
after_save :set_last_imported_at
10+
after_save :set_next_import_at
11+
912
def parents
1013
pending_relationships.pluck(:parent_id).uniq
1114
end
@@ -15,5 +18,13 @@ def user
1518
# fallback to the configured user.
1619
importer.user || Bulkrax.fallback_user_for_importer_exporter_processing
1720
end
21+
22+
def set_last_imported_at
23+
importer.update(last_imported_at: importer.last_imported_at)
24+
end
25+
26+
def set_next_import_at
27+
importer.update(next_import_at: importer.next_import_at)
28+
end
1829
end
1930
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddLastImportedAtToBulkraxImporters < ActiveRecord::Migration[5.1]
2+
def change
3+
add_column :bulkrax_importers, :last_imported_at, :datetime unless column_exists?(:bulkrax_importers, :last_imported_at)
4+
end
5+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddNextImportAtToBulkraxImporters < ActiveRecord::Migration[5.1]
2+
def change
3+
add_column :bulkrax_importers, :next_import_at, :datetime unless column_exists?(:bulkrax_importers, :next_import_at)
4+
end
5+
end

β€Žlib/tasks/bulkrax_tasks.rakeβ€Ž

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,25 @@ namespace :bulkrax do
141141
rescue => e
142142
puts "(#{e.message})"
143143
end
144+
145+
desc "Resave importers"
146+
task resave_importers: :environment do
147+
if defined?(::Hyku)
148+
Account.find_each do |account|
149+
next if account.name == "search"
150+
switch!(account)
151+
puts "=============== updating #{account.name} ============"
152+
153+
resave_importers
154+
155+
puts "=============== finished updating #{account.name} ============"
156+
end
157+
else
158+
resave_importers
159+
end
160+
end
161+
162+
def resave_importers
163+
Bulkrax::Importer.find_each(&:save!)
164+
end
144165
end

0 commit comments

Comments
Β (0)