This document describes the comprehensive UUIDv7 implementation across the Powernode platform.
Powernode uses UUIDv7 (UUID Version 7) as the primary key format for all database models. UUIDv7 provides the benefits of traditional UUIDs while maintaining chronological ordering, making them ideal for database primary keys and distributed systems.
Location: app/models/concerns/uuid_generator.rb
The UuidGenerator concern is the core component that provides UUIDv7 generation functionality:
module UuidGenerator
extend ActiveSupport::Concern
included do
self.primary_key = 'id'
# Override the default UUID generation to use UUIDv7
before_create :generate_uuid_v7, if: -> { id.blank? }
end
private
def generate_uuid_v7
self.id = UUID7.generate if id.blank?
end
endKey Features:
- Automatically generates UUIDv7 for all new records
- Only generates if ID is blank (allows manual ID assignment)
- Uses the UUID7 gem for proper v7 format generation
- Chronologically sortable UUIDs with millisecond precision
Location: app/models/application_record.rb
All models inherit UUIDv7 generation by default:
class ApplicationRecord < ActiveRecord::Base
primary_abstract_class
# Include UuidGenerator by default for all models
# This ensures all models use UUIDv7 format for primary keys
include UuidGenerator
endBenefits:
- Default Behavior: All new models automatically use UUIDv7
- Consistency: No need to remember to add UUID generation to new models
- Platform-wide Standard: Ensures uniform ID format across all tables
ID Column Configuration:
create_table :example_table, id: :uuid do |t|
# PostgreSQL native UUID type
# No default value - Rails handles generation
t.timestamps
endForeign Key Configuration:
t.references :parent, null: false, foreign_key: true, type: :uuidKey Points:
- Uses PostgreSQL native
uuidtype (notstring) - No database-level default UUID generation
- Rails application layer controls UUID generation
- Foreign keys properly typed as UUID
UUIDv7: 0198ebd9-6018-7c94-ad91-9eb1cf7745d5
└─timestamp─┘ └ver─┘ └─────random─────┘
- Timestamp (48 bits): Unix timestamp with millisecond precision
- Version (4 bits): Always "7" for UUIDv7
- Random (74 bits): Cryptographically random data
- Chronological Ordering: Natural database index ordering
- Global Uniqueness: Collision-resistant across distributed systems
- Database Performance: Better B-tree index performance than UUIDv4
- Sortability: Can sort by creation time using string comparison
Gemfile:
gem 'uuid7', '~> 0.1' # UUIDv7 generationAll models in the system include UUIDv7 by default through ApplicationRecord inheritance:
class ExampleModel < ApplicationRecord
# UuidGenerator included automatically
# No additional configuration needed
endclass SpecialModel < ApplicationRecord
# Override the default behavior if needed
before_create :custom_id_generation
private
def custom_id_generation
# Custom logic here
end
end- Schema Update: Convert all ID columns from
stringtouuidtype - Data Migration: Convert existing string UUIDs to proper UUID format
- Application Update: Add UuidGenerator to all models
- Verification: Ensure all new records use UUIDv7 format
Creating new table:
class CreateNewTable < ActiveRecord::Migration[8.0]
def change
create_table :new_table, id: :uuid do |t|
t.string :name, null: false
t.references :parent, null: false, foreign_key: true, type: :uuid
t.timestamps
end
end
endConverting existing table:
class ConvertTableToUuid < ActiveRecord::Migration[8.0]
def up
# Drop existing table and recreate with UUID
drop_table :example_table
create_table :example_table, id: :uuid do |t|
# Recreate columns
end
end
end- Index Performance: UUIDv7 provides better B-tree index performance than UUIDv4
- Storage Size: 16 bytes per UUID (same as UUIDv4)
- Insert Performance: Chronological ordering reduces index fragmentation
- Generation Speed: Fast UUID generation using optimized UUID7 gem
- Memory Usage: Minimal overhead from UuidGenerator concern
- Startup Time: No impact on application startup
# Verify UUIDv7 format
def check_uuid_format(uuid_string)
version = uuid_string.split('-')[2][0].to_i(16)
case version
when 7 then 'UUIDv7'
else "Unknown (v#{version})"
end
end# Check all models use UUIDv7
models_with_non_v7 = []
ApplicationRecord.descendants.each do |model|
next unless model.table_exists?
sample = model.first
next unless sample
version = sample.id.split('-')[2][0].to_i(16)
models_with_non_v7 << model.name unless version == 7
end- Inherit from ApplicationRecord: Automatic UUIDv7 inclusion
- Don't override ID generation: Unless specifically needed
- Use UUID foreign keys: Maintain consistency across relationships
- Test UUID format: Verify v7 format in critical tests
- Use native UUID type: Not string(36)
- Proper foreign key types: Always specify
type: :uuid - Index considerations: UUIDv7 works well with B-tree indexes
- Backup considerations: UUIDs are universally unique across environments
- New models: No special UUID configuration needed
- Existing models: Already updated with UuidGenerator
- Testing: Use factories that leverage automatic UUID generation
- Debugging: UUID format indicates creation order
Issue: Model not generating UUIDv7 Solution: Verify it inherits from ApplicationRecord and has UUID ID column
Issue: Foreign key type mismatch
Solution: Ensure foreign key references use type: :uuid
Issue: Existing data with wrong UUID format Solution: Run data migration to regenerate UUIDs for affected records
# Check model UUID configuration
Model.new.tap { |m| puts m.class.primary_key } # Should be 'id'
Model.create.id.split('-')[2][0] # Should be '7'
# Verify database schema
ActiveRecord::Base.connection.columns('table_name').find { |c| c.name == 'id' }.sql_type
# Should be 'uuid'- User, Account, Plan, Role, Permission, Subscription
- Knowledge Base models (Article, Category, Tag, etc.)
- All remaining 50+ models updated
- ApplicationRecord integration for future models
- Documentation and guidelines created
- Track UUID generation performance
- Monitor database index performance
- Verify UUID format compliance
- Consider custom UUID prefixes for different model types
- Implement UUID validation helpers
- Add metrics for UUID generation patterns
Implementation Status: ✅ Complete
Total Models with UUIDv7: 64/64
Database Schema: Native PostgreSQL UUID type
Default Behavior: All new models inherit UUIDv7 generation