Closed j-mcnally closed 10 months ago
Ok i figured out part 1.
The maglev migrations / active storage do not work with uuids
For clarity, ive modified all the maglev migrations copied over to use uuids. This works fine now, uploads are fixed.
It might be worth looking at the migrations and changing to use the orm id generator types like active storage does.
But still failing to serve the uploaded images so digging into that.
Ok well i had to patch Maglev::Asset to make this work, but theres probably a better way, would love some help.
Maglev::Asset.class_eval do
def self.find(friendly_name)
# Extract uuid from friendly_name using a regex
uuid = friendly_name.match(/(?<uuid>[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})/)['uuid']
super(uuid)
end
end
hey @j-mcnally. sorry for being late in the game. Let me read everything :-)
No worried @did appreciate you taking a peek for me. I have an unideal working solution, but feels like im hacking too much of the Maglev::Asset model, not really clear how its "supposed to work".
The failing error on asset load was
Started GET "/maglev/assets/17841ed1-bb76-4a79-bb56-90e95238da5b-fabian-centeno-uy60pjuhqoo-unsplash-1.jpg" for ::1 at 2023-12-08 11:14:34 -0500
Processing by Maglev::AssetsController#show as JPEG
Parameters: {"id"=>"17841ed1-bb76-4a79-bb56-90e95238da5b-fabian-centeno-uy60pjuhqoo-unsplash-1"}
Maglev::Asset Load (0.8ms) SELECT "maglev_assets".* FROM "maglev_assets" WHERE "maglev_assets"."id" = $1 LIMIT $2 [["id", nil], ["LIMIT", 1]]
Completed 404 Not Found in 4ms (ActiveRecord: 0.8ms | Allocations: 1704)
ActiveRecord::RecordNotFound (Couldn't find Maglev::Asset with 'id'=17841ed1-bb76-4a79-bb56-90e95238da5b-fabian-centeno-uy60pjuhqoo-unsplash-1):
Thanks for the clear explanation. I'm confused because Rails is supposed to take care of "friendly" urls by parsing ids (either integer based or UUID base). Hmm.. And writing a spec for this specific use case, it's not an easy task...
its likely some edge case with the "Friendly part" and "UUIDs" i noticed that if i pass anything other than a UUID to find, it auto nullls it out, likely something rails added for safety with UUIDs without considering how normal id lookups work. So unfortunately this feels like an upstream bug.
I never really knew it worked this way. But look at this http://tutorials.jumpstartlab.com/topics/controllers/friendly-urls.html it appears the friendly thing works because of the way .to_i works
Loading development environment (Rails 7.1.2)
3.2.2 :001 > "1-abc-123.jpg".to_i
=> 1
3.2.2 :002 >
which explains why this works ints and not uuids if the find call is having the slug have .to_i called on it.
My hottest take would be to use something other than find for the this one specific lookup use case and then allow it to be more safely overwritten with other id implementations.
This article explains how UUIDs or non valid uuids are converted to nil
I knew about the to_i
trick regarding friendly urls. And I also read Pawel's article!
Actually, we need to 2 things to make Maglev support uuids.
in the meantime, I'm going to play with a new Maglev site and uuids.
ActiveStorage handles this well i think, if you want to take a look
# This migration comes from active_storage (originally 20170806125915)
class CreateActiveStorageTables < ActiveRecord::Migration[7.0]
def change
# Use Active Record's configured type for primary and foreign keys
primary_key_type, foreign_key_type = primary_and_foreign_key_types
create_table :active_storage_blobs, id: primary_key_type do |t|
t.string :key, null: false
t.string :filename, null: false
t.string :content_type
t.text :metadata
t.string :service_name, null: false
t.bigint :byte_size, null: false
t.string :checksum
if connection.supports_datetime_with_precision?
t.datetime :created_at, precision: 6, null: false
else
t.datetime :created_at, null: false
end
t.index [ :key ], unique: true
end
create_table :active_storage_attachments, id: primary_key_type do |t|
t.string :name, null: false
t.references :record, null: false, polymorphic: true, index: false, type: foreign_key_type
t.references :blob, null: false, type: foreign_key_type
if connection.supports_datetime_with_precision?
t.datetime :created_at, precision: 6, null: false
else
t.datetime :created_at, null: false
end
t.index [ :record_type, :record_id, :name, :blob_id ], name: :index_active_storage_attachments_uniqueness, unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end
create_table :active_storage_variant_records, id: primary_key_type do |t|
t.belongs_to :blob, null: false, index: false, type: foreign_key_type
t.string :variation_digest, null: false
t.index [ :blob_id, :variation_digest ], name: :index_active_storage_variant_records_uniqueness, unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end
end
private
def primary_and_foreign_key_types
config = Rails.configuration.generators
setting = config.options[config.orm][:primary_key_type]
primary_key_type = setting || :primary_key
foreign_key_type = setting || :bigint
[primary_key_type, foreign_key_type]
end
end
@did i really do appreciate you digging into this, hopefully things will continue to work well for me atleast in the meantime. Maglev seems awesome so far.
@j-mcnally you're very welcome and thanks for taking the time to find a solution!
hey @j-mcnally! Thanks to this PR #72 (not yet merged), I successfully managed to create a Maglev site within an application which uses UUIDs. Let me know if you see any issues or if you've comments.
this is great, the comprehensive support across models for uuids will be a win win i think for users.
I've setup active_storage but uploads keep failing...