Using K-Sortable Unique IDentifiers in ActiveRecord and wish there was an easier way to do it? Look no further! activerecord-ksuid
adds support for the data type to your application with zero friction.
Add this line to your application's Gemfile:
gem 'activerecord-ksuid'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install activerecord-ksuid
Whether you are using ActiveRecord inside an existing project or in a new project, usage is simple. Additionally, you can use it with or without Rails.
Within a Rails project, it is very easy to get started using KSUIDs within your models. You can use the ksuid
column type in a Rails migration to add a column to an existing model:
rails generate migration add_ksuid_to_events ksuid:ksuid
This will generate a migration like the following:
class AddKsuidToEvents < ActiveRecord::Migration[5.2]
def change
add_column :events, :unique_id, :ksuid
end
end
Then, to add proper handling to the field, you will want to mix a module into the model:
class Event < ApplicationRecord
include ActiveRecord::KSUID[:unique_id]
end
To create a new model with a ksuid
field that is stored as a KSUID, use the ksuid
column type. Using the Rails generators, this looks like:
rails generate model Event my_field_name:ksuid
If you would like to add a KSUID to an existing model, you can do so with the following:
class AddKsuidToEvents < ActiveRecord::Migration[5.2]
change_table :events do |table|
table.ksuid :my_field_name
end
end
Once you have generated the table that you will use for your model, you will need to include a module into the model class, as follows:
class Event < ApplicationRecord
include ActiveRecord::KSUID[:my_field_name]
end
You can also use a KSUID as the primary key on a table, much like you can use a UUID in vanilla Rails. To do so requires a little more finagling than you can manage through the generators. When hand-writing the migration, it will look like this:
class CreateEvents < ActiveRecord::Migration[5.2]
create_table :events, id: false do |table|
table.ksuid :id, primary_key: true
end
end
You will need to mix in the module into your model as well:
class Event < ApplicationRecord
include ActiveRecord::KSUID[:id]
end
Outside of Rails, you cannot rely on the Railtie to load the appropriate files for you automatically. Toward the start of your application's boot process, you will want to require the following:
require 'active_record/ksuid'
# If you will be using the ksuid column type in a migration
require 'active_record/ksuid/table_definition'
Once you have required the file(s) that you need, everything else will work as it does above.
These examples all store your identifier as a string-based KSUID. If you would like to use binary KSUIDs instead, use the ksuid_binary
column type. Unless you need to be super-efficient with your database, we recommend using string-based KSUIDs because it makes looking at the data while in the database a little easier to understand.
When you include the KSUID module into your model, you will want to pass the :binary
option as well:
class Event < ApplicationRecord
include ActiveRecord::KSUID[:my_field_name, binary: true]
end
Since KSUIDs include a timestamp as well, you can infer the #created_at
timestamp from the KSUID when it is being used as your primary key. The module builder enables that option automatically with the :created_at
option, like so:
class Event < ApplicationRecord
include ActiveRecord::KSUID[:my_field_name, created_at: true]
end
This allows you to be efficient in your database design if that is a constraint you need to satisfy.
So you’re interested in contributing to KSUID for ActiveRecord? Check out our contributing guidelines for more information on how to do that.
This library aims to support and is tested against the following Ruby versions:
- Ruby 2.5
- Ruby 2.6
- Ruby 2.7
- JRuby 9.2
If something doesn't work on one of these versions, it's a bug.
This library may inadvertently work (or seem to work) on other Ruby versions, however support will only be provided for the versions listed above.
If you would like this library to support another Ruby version or implementation, you may volunteer to be a maintainer. Being a maintainer entails making sure all tests run and pass on that implementation. When something breaks on your implementation, you will be responsible for providing patches in a timely fashion. If critical issues for a particular implementation exist at the time of a major release, support for that Ruby version may be dropped.
This library aims to adhere to Semantic Versioning 2.0.0. Violations of this scheme should be reported as bugs. Specifically, if a minor or patch version is released that breaks backward compatibility, that version should be immediately yanked and/or a new version should be immediately released that restores compatibility. Breaking changes to the public API will only be introduced with new major versions. As a result of this policy, you can (and should) specify a dependency on this gem using the Pessimistic Version Constraint with two digits of precision. For example:
spec.add_dependency "activerecord-ksuid", "~> 0.1"
The gem is available as open source under the terms of the MIT License.