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

Explicit connection collation #183

Merged
merged 4 commits into from
Feb 6, 2020
Merged

Explicit connection collation #183

merged 4 commits into from
Feb 6, 2020

Conversation

g105b
Copy link
Member

@g105b g105b commented Jan 29, 2020

When PDO binds data, it asks the schema for what type of data the binding should be done, so everything is done in a type-safe way. This is also the case for character sets and collations.

There are occasions when bindings are possible out-of-schema, such as when binding a value to a variable, e.g. select @example_variable := ?. Because this variable does not exist in the schema, PDO will infer the character set and collation from its connection information.

A bug arises when the connection is made in a different collation than the schema was created in. By default all schemas are created in utf8_general_ci, as this is loaded from the DefaultSettings object. However, when creating the PDO connection, the collation is never specified, so will default to whatever the OS deems as a correct default.

A recent update to MySQL/MariaDB has changed this default to move from utf8_general_ci to utf8_unicode_ci. This causes the issue in question when binding to out-of-schema values. When there is a mismatch of collations, MySQL throws an “Illegal mix of collations” error.

This can be solved by specifying the connection's collation using the MYSQL_ATTR_INIT_COMMAND attribute and setting to a value of SET SESSION collation_connection='$collation', where $collation can be loaded from the same consistent place that the migration script does.

@g105b g105b merged commit dc7c9cf into master Feb 6, 2020
@g105b g105b deleted the 182-collations branch February 6, 2020 13:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant