{"id":22282020,"url":"https://github.com/nedap/mysql-binuuid-rails","last_synced_at":"2025-05-16T19:07:32.034Z","repository":{"id":23202126,"uuid":"98407574","full_name":"nedap/mysql-binuuid-rails","owner":"nedap","description":"Store UUIDs in binary MySQL database columns. Saves storage, and increases performance.","archived":false,"fork":false,"pushed_at":"2025-01-07T12:06:15.000Z","size":73,"stargazers_count":57,"open_issues_count":7,"forks_count":10,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-05-15T06:07:09.807Z","etag":null,"topics":["gem","mysql","nedap","rails","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nedap.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-07-26T09:49:59.000Z","updated_at":"2025-01-18T14:17:07.000Z","dependencies_parsed_at":"2024-06-19T04:08:19.270Z","dependency_job_id":"b1202b60-573c-4d8f-bcd7-e3a981fb8e0f","html_url":"https://github.com/nedap/mysql-binuuid-rails","commit_stats":{"total_commits":56,"total_committers":6,"mean_commits":9.333333333333334,"dds":0.3928571428571429,"last_synced_commit":"abd7e928c9b3fd54757224aa7219c4ed7a630fc2"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nedap%2Fmysql-binuuid-rails","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nedap%2Fmysql-binuuid-rails/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nedap%2Fmysql-binuuid-rails/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nedap%2Fmysql-binuuid-rails/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nedap","download_url":"https://codeload.github.com/nedap/mysql-binuuid-rails/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254592395,"owners_count":22097013,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gem","mysql","nedap","rails","ruby"],"created_at":"2024-12-03T16:24:36.816Z","updated_at":"2025-05-16T19:07:31.997Z","avatar_url":"https://github.com/nedap.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![CI](https://github.com/nedap/mysql-binuuid-rails/actions/workflows/ci.yml/badge.svg)](https://github.com/nedap/mysql-binuuid-rails/actions/workflows/ci.yml) [![Maintainability](https://api.codeclimate.com/v1/badges/7bcb6538e7666bc37f9a/maintainability)](https://codeclimate.com/github/nedap/mysql-binuuid-rails/maintainability)\n\n\n# mysql-binuuid-rails\n`mysql-binuuid-rails` lets you define attributes of a UUID type on your models\nby leveraging the Attributes API that has been available since Rails 5. By doing\nso, you can store your UUIDs as binary values in your database, and still be\nable to query using the string representations since the database will take care\nof the type conversion.\n\nAs the name suggests, it only supports MySQL. If you're on PostgreSQL, you\ncan use UUIDs the proper way already.\n\nIf you were to store a UUID of 32 characters (without the dashes) as text in\nyour database, it would cost you at least 32 bytes. And that only counts if\nevery character only requires 1 byte. But that completely depends on your\nencoding. If every character requires 2 bytes, storing it would already cost\nyou 64 bytes. And that's a lot, if you think about the fact that a UUID is\nonly 128 bits.\n\nBeing 128 bits, a UUID fits precisely in a column of 16 bytes. Though it won't\nbe really readable it sure saves up a lot of space and it's only 4x bigger\nthan a 32-bit integer, or 2x bigger than a 64-bit integer.\n\nNot to mention the space you'll be saving when you create an index on the\ncolumn holding your UUID.\n\n# Installation\nYou know the drill, add this line to your gemfile:\n\n```\ngem 'mysql-binuuid-rails'\n```\n\n\n# Usage\nUsing binary columns for UUIDs is very easy. There's only two steps you need to\nperform which are described here.\n\n## Adding the column to store your UUID\nSuppose you have a model called `Book` to which you want to add a unique\nidentifier in the form of a UUID. First, make sure your database is able to\nhold this attribute. So let's create a migration.\n\n```\n$ rails g migration AddUuidColumnToBooks\n```\n\nOpen up the migration file and change it as you'd like:\n\n```ruby\nclass AddUuidColumnToBooks \u003c ActiveRecord::Migration[5.1]\n  def change\n    # 'uuid' is the column name, and 'binary' is the column type. You have to\n    # specify it as a binary column yourself. And because we know that a UUID\n    # takes up 16 bytes, we set can specify its limit.\n    add_column :books, :uuid, :binary, limit: 16\n  end\nend\n```\n\nPerform the migration:\n\n```\nrails db:migrate\n```\n\n## Tell your model how to handle the binary UUID column\nAll you have to do now, is specify in your `Book` model how Rails should handle\nthe `uuid` column. Open up `app/models/book.rb` and simply add the following\nsingle line:\n\n```ruby\nclass Book \u003c ApplicationRecord\n  attribute :uuid, MySQLBinUUID::Type.new\nend\n```\n\n\n# Migrating from ActiveUUID\nThere's a couple of things you need to take into consideration when you're\nmigrating from ActiveUUID to `mysql-binuuid-rails`.\n\n## Replace `include ActiveUUID::UUID` in your models\nIn your models where you did `include ActiveUUID::UUID`, you now have to\nspecify the attribute which is a UUID instead:\n\n```ruby\nclass Book \u003c ApplicationRecord\n  attribute :uuid, MySQLBinUUID::Type.new\nend\n```\n\n## No `uuid` column in database migrations\nActiveUUID comes with a neat column type that you can use in migrations. Since\n`mysql-binuuid-rails` does not, you will have to change all migrations in which\nyou leveraged on that migration column if you want your migrations to keep\nworking for new setups.\n\nThe idea behind *not* providing a `uuid` type for columnns in migrations is\nthat you are aware of what the actual type of the column is you're creating,\nand that it is not hidden magic.\n\nIt's pretty simple:\n\n\n```ruby\n# Anywhere where you did this in your migrations...\n\ncreate_table :books do |t|\n  t.uuid :reference, ...\nend\n\n# ..you should change these kinds of lines into the kind described\n# below. It's what ActiveUUID  did for you, but what you now have\n# to do yourself.\n\ncreate_table :books do |t|\n  t.binary :reference, limit: 16, ...\nend\n```\n\n## No UUIDTools\nActiveUUID comes with [UUIDTools](https://github.com/sporkmonger/uuidtools).\n`mysql-binuuid-rails` does not. When you retrieve a UUID typed attribute from\na model when using ActiveUUID, the result is a `UUIDTools::UUID` object. When\nyou retrieve a UUID typed attribute from a model when using\n`mysql-binuuid-rails`, you just get a `String` of 36 characters (it includes\nthe dashes).\n\nMigrating shouldn't be that difficult though. `UUIDTools::UUID` implements\n`#to_s`, which returns precisely the same as `mysql-binuuid-rails` returns\nby default. But it's good to be aware of this in case you're running into\nweirdness.\n\n\n# Contributing\nTo start coding on `mysql-binuuid-rails`, fork the project, clone it locally\nand then run `bin/setup` to get up and running. If you want to fool around in\na console with the changes you made, run `bin/console`.\n\nBug reports and pull requests are welcome on GitHub at\nhttps://github.com/nedap/mysql-binuuid-rails\n\n## Testing\nFor the most recent major version of ActiveRecord, tests are run against the\nlatest patch level of all minor versions. For earlier major versions, tests are\nrun against the latest minor/patch.\n\nRun tests yourself to verify everything is still working:\n\n```\n$ bundle exec rake\n```\n\n## Contributors\nSee [CONTRIBUTORS.md](CONTRIBUTORS.md).\n\n\n# License\nThe gem is available as open source under the terms of the\n[MIT License](http://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnedap%2Fmysql-binuuid-rails","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnedap%2Fmysql-binuuid-rails","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnedap%2Fmysql-binuuid-rails/lists"}