Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrations to remove Drupal gallery and integrate into node revision #11371

Merged
merged 4 commits into from
Sep 5, 2022

Conversation

anirudhprabhakaran3
Copy link
Member

@anirudhprabhakaran3 anirudhprabhakaran3 commented Aug 28, 2022

Fixes #4074

Make sure these boxes are checked before your pull request (PR) is ready to be reviewed and merged. Thanks!

  • PR is descriptively titled 📑 and links the original issue above 🔗
  • tests pass -- look for a green checkbox ✔️ a few minutes after opening your PR -- or run tests locally with rake test
  • code is in uniquely-named feature branch and has no merge conflicts 📁
  • screenshots/GIFs are attached 📎 in case of UI updation
  • ask @publiclab/reviewers for help, in a comment below

This PR for now only makes the migration from Drupal Galleries to Node revisions. In another commit, I'll add the migration for the removal of the table itself.

CC: #11185

@anirudhprabhakaran3 anirudhprabhakaran3 requested a review from a team as a code owner August 28, 2022 14:42
@gitpod-io
Copy link

gitpod-io bot commented Aug 28, 2022

Copy link
Member

@jywarren jywarren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good! Have you tested it out, did you need to test it in unstable or anything?

@jywarren
Copy link
Member

Restarted the system test which was a random container crash.

@github-actions
Copy link

This pull request generated screenshots of many common pages in the running app. You should be able to download and view them here:
https://github.com/publiclab/plots2/suites/8018661114/artifacts/345255232

@jywarren
Copy link
Member

This is ready to merge if @anirudhprabhakaran3 gives a +1 on the testing/stable question!

@anirudhprabhakaran3
Copy link
Member Author

Yeah, I'll test it on unstable in a few minutes. I was also wondering if I should remove the _gallery.html.erb (and references to it) and make another commit. That way, all dependencies will be wrapped up in a single PR

@jywarren
Copy link
Member

If you like, go ahead, but I think 2 PRs is fine too!

@jywarren
Copy link
Member

Just let me know what you decide and we can merge this whenever you're ready. Great work!

@codecov
Copy link

codecov bot commented Sep 4, 2022

Codecov Report

Merging #11371 (98fcb49) into main (5aa8720) will decrease coverage by 0.05%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #11371      +/-   ##
==========================================
- Coverage   82.46%   82.41%   -0.06%     
==========================================
  Files          98       97       -1     
  Lines        5999     5975      -24     
==========================================
- Hits         4947     4924      -23     
+ Misses       1052     1051       -1     
Impacted Files Coverage Δ
app/controllers/notes_controller.rb 85.66% <ø> (+0.57%) ⬆️
app/models/node.rb 91.20% <ø> (+0.07%) ⬆️
app/services/execute_search.rb 88.88% <0.00%> (-5.56%) ⬇️
app/api/srch/search.rb 66.66% <0.00%> (-3.85%) ⬇️

@github-actions
Copy link

github-actions bot commented Sep 4, 2022

This pull request generated screenshots of many common pages in the running app. You should be able to download and view them here:
https://github.com/publiclab/plots2/suites/8124298926/artifacts/351743698

@codeclimate
Copy link

codeclimate bot commented Sep 4, 2022

Code Climate has analyzed commit 98fcb49 and detected 0 issues on this pull request.

View more on Code Climate.

@github-actions
Copy link

github-actions bot commented Sep 4, 2022

This pull request generated screenshots of many common pages in the running app. You should be able to download and view them here:
https://github.com/publiclab/plots2/suites/8124356653/artifacts/351755330

@anirudhprabhakaran3
Copy link
Member Author

Hi! I think we can review, test and merge this MR now. I think I've removed all the related dependencies now. Sorry for the delay!

@jywarren
Copy link
Member

jywarren commented Sep 5, 2022

Hi @anirudhprabhakaran3 i'd like to try but i want to check on stable once we merge it. What is an example page of where on the live site we use the image gallery, so I can confirm it still displays the images after we merge this on stable?

@jywarren
Copy link
Member

jywarren commented Sep 5, 2022

@jywarren jywarren merged commit 916c1d2 into publiclab:main Sep 5, 2022
@jywarren
Copy link
Member

jywarren commented Sep 5, 2022

https://stable.publiclab.org/notes/eymund-diegel/10-26-2012/hello-kitty-watchdog-patrols-gowanus-canal-skies didn't work, strangely, even before stable began to build.

@jywarren
Copy link
Member

jywarren commented Sep 5, 2022

image

@jywarren
Copy link
Member

jywarren commented Sep 6, 2022

The error in stable is due to a migration:

== 20220828142008 RemoveDrupalGalleryFromNode: migrating ======================
rake aborted!
StandardError: An error has occurred, all later migrations canceled:

undefined method `gallery' for #<Node:0x000055cd4df241d8>

Do we need to run the migration first, then in a second PR, remove the 'gallery' method?

.order('field_image_gallery_fid')
end

def gallery
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, here, we are actually removing the method we need to use in the migration. We can use the definition itself to resolve this!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we'll have to be sure it runs all together -- this PRs commit, and the new changes, and gets built in a container all together. Because it will fail if they are done in 2 different builds.

jywarren added a commit that referenced this pull request Sep 6, 2022
Node.all.each do |node|
new_revision = node.revisions.first.dup
gallery_images = ""
node.gallery.each do |image|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I changed this and directly referenced it as node&.drupal_content_field_image_gallery.each do |image|

4e80ae2

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And noting the fixes @anirudhprabhakaran3 had added in this PR too: #11393

@jywarren
Copy link
Member

jywarren commented Sep 6, 2022

Oh darn, that didn't work.

Caused by:
NoMethodError: undefined method `drupal_content_field_image_gallery' for #<Node:0x00005575ad031970>

@@ -359,20 +359,6 @@ def scraped_image
match&.split('src="')&.last&.split('"')&.first
end

# was unable to set up this relationship properly with ActiveRecord associations
def drupal_content_field_image_gallery
DrupalContentFieldImageGallery.where(nid: nid)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah its because that was also not a built-in method - we deleted it too! OK, will adapt to this too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, here it is:

gallery = DrupalContentFieldImageGallery.where(nid: node.nid)
.order('field_image_gallery_fid')
gallery.each do |image|
html = "<a target='_blank' href='#{image&.image&.path(:original)}'><img rel='tooltip' data-title='#{image&.description}' style='margin-bottom:4px;' class='rounded' src='#{image&.image&.path(:thumb)}' /></a>"
gallery_images << html unless image.nil? || image.image.nil? || image.image.path.nil?
end

jywarren added a commit that referenced this pull request Sep 6, 2022
@jywarren
Copy link
Member

jywarren commented Sep 6, 2022

:-(

== 20220828142008 RemoveDrupalGalleryFromNode: migrating ======================
rake aborted!
StandardError: An error has occurred, all later migrations canceled:

uninitialized constant RemoveDrupalGalleryFromNode::DrupalContentFieldImageGallery
/app/db/migrate/20220828142008_remove_drupal_gallery_from_node.rb:6:in `block in change'

@jywarren
Copy link
Member

jywarren commented Sep 6, 2022

I'm not sure why that didn't work. I'll check the schema and see if the following migration somehow got run, which would have eliminated that class?

@@ -1,27 +0,0 @@
require 'php_serialize'

class DrupalContentFieldImageGallery < ApplicationRecord
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so I think it didn't work because we deleted this entire file. So we either need to re-add it, or we need to do raw SQL. I think I will re-add this file so that the migration can succeed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

phew, a lot of editing on main, but fingers X this works: cb073ae

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jywarren added a commit that referenced this pull request Sep 6, 2022
@jywarren
Copy link
Member

jywarren commented Sep 6, 2022

OK, we'll need a few more files.


== 20220828142008 RemoveDrupalGalleryFromNode: migrating ======================
/app/app/models/user.rb:10: warning: already initialized constant Authlogic::Regex::LOGIN
/usr/local/bundle/gems/authlogic-4.4.2/lib/authlogic/regex.rb:48: warning: previous definition of LOGIN was here
You have selected Sha512 as your authlogic crypto provider. This algorithm
does not have any practical known attacks against it. However, there are
better choices.

Authlogic has no plans yet to deprecate this crypto provider. However,
we recommend transitioning to a more secure, adaptive hashing algorithm,
like scrypt. Adaptive algorithms are designed to slow down brute force
attacks, and over time the iteration count can be increased to make it
slower, so it remains resistant to brute-force search attacks even in
the face of increasing computation power.

Use the transition_from_crypto_providers option to make the transition
painless for your users.
rake aborted!
StandardError: An error has occurred, all later migrations canceled:

Couldn't find DrupalFile without an ID
/usr/local/bundle/gems/activerecord-5.2.8.1/lib/active_record/relation/finder_methods.rb:433:in `find_with_ids'
/usr/local/bundle/gems/activerecord-5.2.8.1/lib/active_record/relation/finder_methods.rb:69:in `find'
/usr/local/bundle/gems/activerecord-5.2.8.1/lib/active_record/querying.rb:5:in `find'
/usr/local/bundle/gems/composite_primary_keys-11.3.1/lib/composite_primary_keys/core.rb:34:in `find'
/app/app/models/drupal_content_field_image_gallery.rb:19:in `image'
/app/db/migrate/20220828142008_remove_drupal_gallery_from_node.rb:9:in `block (2 levels) in change'

jywarren added a commit that referenced this pull request Sep 6, 2022
@jywarren
Copy link
Member

jywarren commented Sep 6, 2022

Oof, ok, so i'm just going to check for existence of all necessary fields before:

ae016fe

Didn't need to re-add more files, just more nil checks.

@jywarren
Copy link
Member

jywarren commented Sep 6, 2022

Another error - this time, a validation error, which I think means we're close:

== 20220828142008 RemoveDrupalGalleryFromNode: migrating ======================
/app/app/models/user.rb:10: warning: already initialized constant Authlogic::Regex::LOGIN
/usr/local/bundle/gems/authlogic-4.4.2/lib/authlogic/regex.rb:48: warning: previous definition of LOGIN was here
You have selected Sha512 as your authlogic crypto provider. This algorithm
does not have any practical known attacks against it. However, there are
better choices.

Authlogic has no plans yet to deprecate this crypto provider. However,
we recommend transitioning to a more secure, adaptive hashing algorithm,
like scrypt. Adaptive algorithms are designed to slow down brute force
attacks, and over time the iteration count can be increased to make it
slower, so it remains resistant to brute-force search attacks even in
the face of increasing computation power.

Use the transition_from_crypto_providers option to make the transition
painless for your users.
rake aborted!
StandardError: An error has occurred, all later migrations canceled:

Validation failed: Body can't be blank
/usr/local/bundle/gems/activerecord-5.2.8.1/lib/active_record/validations.rb:80:in `raise_validation_error'
/usr/local/bundle/gems/activerecord-5.2.8.1/lib/active_record/validations.rb:52:in `save!'

@jywarren
Copy link
Member

jywarren commented Sep 6, 2022

a4a4c59

@jywarren
Copy link
Member

jywarren commented Sep 6, 2022

@jywarren
Copy link
Member

jywarren commented Sep 6, 2022

That's odd. It's pretty clear here:

new_revision.save! unless new_revision.body.empty?

@jywarren
Copy link
Member

jywarren commented Sep 6, 2022

So, I think what's happening is that the validation will not allow space characters... but a body of " " (one or more spaces) still reads as not .empty?

@jywarren
Copy link
Member

jywarren commented Sep 6, 2022

Hmm, no, that doesn't seem right -- I checked the output of all 21 records where body is "" (from the total nids here), and all are just empty. Anyways I tried just changing all to <br /> instead and restarted the stable build to just see if we can get past that. We could repeat this on production.

@jywarren
Copy link
Member

jywarren commented Sep 6, 2022

Strangely it didn't seem to try to run migrations: https://ci-j.publiclab.org/job/Plots-Stable/3315/console

I may need to trigger it with a new PR merge.

@jywarren
Copy link
Member

jywarren commented Sep 6, 2022

No, it did build, but no, it also didn't work. Same error. I'm going to insert a log comment in the migration. If that doesn't work, I will try manually running the migration in the Rails console before doing more.

@jywarren
Copy link
Member

jywarren commented Sep 6, 2022

OK, i believe I found it:

=> #<Node nid: 333, vid: 1277, type: "note", language: "", title: "National Geographic: LIDAR survey of tree canopy w...", u...
irb(main):014:0> n.latest.body
=> "\r\n"

So there, it's not empty, but it is whitespace.

OK, trying again, this time it got stuck on บอลเด็ด กีฬาประเภททีมฟุตบอล apologies, I can't read it, but we are now in territory where we're tripping validations for old nodes which never had to pass them, and it's unrelated to image galleries. I'm going to restrict the migration to not try saving unless there was something new added, so only galleries, and then if that fails I will narrow the nids we are dealing with to ONLY the range which actually have galleries, from https://gist.github.com/jywarren/3ab414921b40ef6d8b4df68679610d93

@jywarren
Copy link
Member

jywarren commented Sep 6, 2022

@jywarren
Copy link
Member

jywarren commented Sep 6, 2022

Error:

[d18ef52f-4de3-4fd2-9dff-12d2896cb0d4] Started GET "/notes/juan/6-5-2012/fabaoutfit-vertical-kap-and-bap" for 96.238.50.169 at 2022-09-06 19:58:39 +0000
[d18ef52f-4de3-4fd2-9dff-12d2896cb0d4] Processing by NotesController#show as HTML
[d18ef52f-4de3-4fd2-9dff-12d2896cb0d4]   Parameters: {"author"=>"juan", "date"=>"6-5-2012", "id"=>"fabaoutfit-vertical-kap-and-bap"}
[d18ef52f-4de3-4fd2-9dff-12d2896cb0d4] Completed 500 Internal Server Error in 727ms (ActiveRecord: 695.0ms)
[d18ef52f-4de3-4fd2-9dff-12d2896cb0d4]   
[d18ef52f-4de3-4fd2-9dff-12d2896cb0d4] Psych::DisallowedClass (Tried to load unspecified class: ActiveSupport::HashWithIndifferentAccess):
[d18ef52f-4de3-4fd2-9dff-12d2896cb0d4]   
[d18ef52f-4de3-4fd2-9dff-12d2896cb0d4] app/controllers/notes_controller.rb:59:in `show'

@jywarren
Copy link
Member

jywarren commented Sep 6, 2022

Tracking that here: #11355 (comment)

@jywarren
Copy link
Member

jywarren commented Sep 6, 2022

OK, that's resolved. Phew. Sorry @anirudhprabhakaran3 that was a really involved process but it highlights the challenges of removing infrastructure in the correct order. I'm glad we finally got that done, and apologies for making several commits to main, that is really not typical of the process we want, but I kept thinking we were just about to solve it. No harm done, and next time we should more thoroughly test the process on unstable. Thanks for your patience!

@jywarren
Copy link
Member

jywarren commented Sep 6, 2022

Noting the images got added twice, i believe because I ran the migration twice for many pages. However this won't happen on production:

image

@jywarren
Copy link
Member

jywarren commented Sep 6, 2022

Also i had to add the absolute hostnames to the page in the dev console to see them. They had all pointed back at stable.publiclab.org instead of publiclab.org, but when i changed it, they all appeared. Great!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor legacy gallery code into node revision body
2 participants