Lessons from Fizzy - Fixtures
Why are IDs specified via <%= ActiveRecord::FixtureSet.identify("david", :uuid) %>
Taken from Fizzy (publicly available).
# users.yml
david:
id: <%= ActiveRecord::FixtureSet.identify("david", :uuid) %>
name: David
role: member
identity: david
account: 37s_uuid # <------ notice this here
verified_at: <%= Time.current.to_fs(:db) %>
Source: https://github.com/basecamp/fizzy/blob/main/test/fixtures/users.yml
Notice the key-value entry for account?
# Let's review the accounts.yml file
37s:
id: <%= ActiveRecord::FixtureSet.identify("37s", :uuid) %> # < ---- check this out
name: 37signals
external_account_id: <%= ActiveRecord::FixtureSet.identify("37signals") %>
cards_count: 5
Source: https://github.com/basecamp/fizzy/blob/main/test/fixtures/accounts.yml
Notice how you can reference IDs via name e.g. via 37s_uuid without using a label?
Label Names
Checkout out the section: “Stable, Autogenerated IDs” in the following link: https://api.rubyonrails.org/v8.0.1/classes/ActiveRecord/FixtureSet.html
You can definitely use label names: instead of specifying IDs like this: <%= ActiveRecord::FixtureSet.identify("37s", :uuid) %> - but if you want a UUID - something that is absolutely unique no matter what, then you will have to use the ruby code just mentioned. That will give you a compliant UUID, whereas a label will not give you that. If you are ok with labels, the following will be fine:
From the docs:
Here, have a monkey fixture:
george:
id: 1
name: George the Monkey
reginald:
id: 2
name: Reginald the Pirate
Each of these fixtures has two unique identifiers: one for the database and one for the humans. Why don’t we generate the primary key instead? Hashing each fixture’s label yields a consistent ID:
george: # generated id: 503576764
name: George the Monkey
reginald: # generated id: 324201669
name: Reginald the Pirate