Skip to content

Instantly share code, notes, and snippets.

@AlyBadawy
Created October 20, 2025 13:37
Show Gist options
  • Select an option

  • Save AlyBadawy/3fb3f0f70d16226b4f931821927b699b to your computer and use it in GitHub Desktop.

Select an option

Save AlyBadawy/3fb3f0f70d16226b4f931821927b699b to your computer and use it in GitHub Desktop.
Rails/app/config/initializers/uuid_v7.rb
# config/initializers/uuidv7_default.rb
# Ensure generators use UUID columns for PKs and references
Rails.application.config.generators do |g|
g.orm :active_record, primary_key_type: :uuid
end
# When migrations call create_table with id: :uuid, make the DB default uuidv7()
module UuidV7PrimaryKeyDefault
def primary_key(name, type = :primary_key, **options)
if type == :uuid
options[:default] ||= -> { "uuidv7()" } # PG 18 builtin
end
super
end
end
# Patch the PostgreSQL table definition class once ActiveRecord (and the
# adapter) have been loaded. Doing this at boot time caused NameError when
# ActiveRecord constants were not yet defined.
ActiveSupport.on_load(:active_record) do
begin
if defined?(ActiveRecord::ConnectionAdapters::PostgreSQL::TableDefinition)
ActiveRecord::ConnectionAdapters::PostgreSQL::TableDefinition.prepend(UuidV7PrimaryKeyDefault)
end
rescue NameError
# Adapter not available; skip patching.
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment