Skip to content

Instantly share code, notes, and snippets.

@goulvench
Created February 12, 2024 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goulvench/c035bc90ed04c33be2212da43fa35146 to your computer and use it in GitHub Desktop.
Save goulvench/c035bc90ed04c33be2212da43fa35146 to your computer and use it in GitHub Desktop.
When migrating a Rails 6.1 app to Rails 7, all calculated SGID change, which causes ActionText attachments to error out. SGIDs are generated using `Rails.application.key_generator.generate_key('signed_global_ids')`, which uses different settings.
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
#gem "rails", "6.1"
gem "rails", github: "rails/rails", branch: :main
gem "sqlite3"
end
require "active_record/railtie"
require "active_storage/engine"
require "action_text/engine"
require "tmpdir"
class TestApp < Rails::Application
config.load_defaults 6.1
config.root = __dir__
config.hosts << "example.org"
config.eager_load = false
config.session_store :cookie_store, key: "cookie_store_key"
config.secret_key_base = "secret_key_base"
config.active_storage.service = :local
config.active_storage.service_configurations = {
local: {
root: Dir.tmpdir,
service: "Disk"
}
}
# Disable logging for a cleaner output
config.logger = Logger.new(nil)
end
ENV["DATABASE_URL"] = "sqlite3::memory:"
Rails.application.initialize!
require ActiveStorage::Engine.root.join("db/migrate/20170806125915_create_active_storage_tables.rb").to_s
# Disable migration logging for a cleaner output
ActiveRecord::Migration.verbose = false
ActiveRecord::Schema.define do
CreateActiveStorageTables.new.change
create_table :users, force: true
end
class User < ActiveRecord::Base
has_one_attached :profile
end
require "minitest/autorun"
class BugTest < Minitest::Test
def test_blob_sgid_after_rails_upgrade
user = User.create!(
profile: {
content_type: "text/plain",
filename: "dummy.txt",
io: ::StringIO.new("dummy"),
}
)
puts "Rails version: #{Rails::VERSION::STRING}"
rails_6_sgid = "eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaEpJalJuYVdRNkx5OTBaWE4wTFdGd2NDOUJZM1JwZG1WVGRHOXlZV2RsT2pwQ2JHOWlMekUvWlhod2FYSmxjMTlwYmdZNkJrVlUiLCJleHAiOm51bGwsInB1ciI6ImF0dGFjaGFibGUifX0=--aa099340e7af7ec6ce1af3866ade911ea22c7834"
assert_equal rails_6_sgid, user.profile.blob.attachable_sgid
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment