Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support uuid type for MariaDB 10.7.0+ #49842

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

fatkodima
Copy link
Member

@fatkodima fatkodima commented Oct 30, 2023

Fixes #49752.

MariaDB since version 10.7 natively supports uuid data type. Also, since 10.5 supports INSERT ... RETURNING.

This PR adds support for uuid type for MariaDB only.

MySQL, unfortunately, as usual does not support anything. There are some hacks on how people manually implement "uuid" data type. For example, using a BINARY(16) column with a UUID_TO_BIN(UUID()) as a default or a VARCHAR(36) with UUID() as a default. See https://blogs.oracle.com/mysql/post/mysql-uuids or https://dev.mysql.com/blog-archive/mysql-8-0-uuid-support/ for detailed examples. But since MySQL does not support RETURNING, there is no way we can retrieve these values during the INSERT without the record.reload. Or, differently, we must manually set the uuid before creating a record.

So, the question is: Should we add also support for MySQL, expecting that people will manually set the default?
Something like:

attribute :guid, :uuid, default: -> { SomeUuidGenerator.generate }

The added test cases are basically adopted tests for PostgreSQL uuid (https://github.com/rails/rails/blob/main/activerecord/test/cases/adapters/postgresql/uuid_test.rb).

Before merging this PR, #49840 should be merged, because these have overlapping code regarding RETURNING for MariaDB. (done)

@john-999
Copy link

So, the question is: Should we add also support for MySQL, expecting that people will manually set the default?

Doesn't it make perfect sense to expect ALL people (incl. MariaDB users) to always set the default value for uuid manually, because that's how they can ensure uniqueness before the INSERT of the record (since uuid by definition is not auto-incremented like int or bigint)?

@fatkodima
Copy link
Member Author

No, because MariaDB, for example, has a function that can be used for default generation inside the database. PostgreSQL too, which is already used as a default. See

def primary_key(name, type = :primary_key, **options)
if type == :uuid
options[:default] = options.fetch(:default, "gen_random_uuid()")
end
super
end

By not enforcing to set manually uuids, we can avoid one extra step for people to use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants