Giter Club home page Giter Club logo

Comments (17)

j-mcnally avatar j-mcnally commented on August 19, 2024 1

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.

from maglev-core.

j-mcnally avatar j-mcnally commented on August 19, 2024 1

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

from maglev-core.

j-mcnally avatar j-mcnally commented on August 19, 2024 1

@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.

from maglev-core.

j-mcnally avatar j-mcnally commented on August 19, 2024

But still failing to serve the uploaded images so digging into that.

from maglev-core.

j-mcnally avatar j-mcnally commented on August 19, 2024

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

from maglev-core.

did avatar did commented on August 19, 2024

hey @j-mcnally. sorry for being late in the game. Let me read everything :-)

from maglev-core.

j-mcnally avatar j-mcnally commented on August 19, 2024

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".

from maglev-core.

j-mcnally avatar j-mcnally commented on August 19, 2024

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):

from maglev-core.

did avatar did commented on August 19, 2024

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...

from maglev-core.

j-mcnally avatar j-mcnally commented on August 19, 2024

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.

from maglev-core.

j-mcnally avatar j-mcnally commented on August 19, 2024

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.

from maglev-core.

j-mcnally avatar j-mcnally commented on August 19, 2024

This article explains how UUIDs or non valid uuids are converted to nil

https://pawelurbanek.com/uuid-order-rails

from maglev-core.

did avatar did commented on August 19, 2024

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.

from maglev-core.

did avatar did commented on August 19, 2024

in the meantime, I'm going to play with a new Maglev site and uuids.

from maglev-core.

did avatar did commented on August 19, 2024

@j-mcnally you're very welcome and thanks for taking the time to find a solution!

from maglev-core.

did avatar did commented on August 19, 2024

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.

from maglev-core.

j-mcnally avatar j-mcnally commented on August 19, 2024

this is great, the comprehensive support across models for uuids will be a win win i think for users.

from maglev-core.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.