Skip to content

[5.2] Improve behavior of MySQL flag, add modes config #12030

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

Merged
merged 1 commit into from
Jan 26, 2016
Merged

[5.2] Improve behavior of MySQL flag, add modes config #12030

merged 1 commit into from
Jan 26, 2016

Conversation

adamwathan
Copy link
Contributor

Re-opening #12012 against 5.2 branch

This PR improves the behavior of the strict flag for the MySQL connector and adds a new modes config section for users who need more control than the strict flag allows.

Quick example:

'mysql' => [
    'database'  => env('DB_DATABASE', 'forge'),
    'username'  => env('DB_USERNAME', 'forge'),
    'password'  => env('DB_PASSWORD', ''),
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',

    // Behave like MySQL 5.6
    'strict'    => false,

    // Behave like MySQL 5.7
    'strict'    => true,

    // Ignore this key and rely on the `strict` key
    'modes'     => null,

    // Explicitly disable all modes, overrides `strict` setting
    'modes'     => [],

    // Explicitly enable specific modes, overrides `strict` setting
    'modes'     => [
        'STRICT_TRANS_TABLES',
        'ONLY_FULL_GROUP_BY',
    ],
],
  • Setting strict to true now enables all of the default MySQL 5.7 modes rather than just STRICT_TRANS_TABLES.

    Reasoning here is that MySQL 5.7 being "strict" isn't just defined by one mode, and is really a combination of many. The strict config setting in this case is being treated as a convenience toggle between MySQL 5.6's "loose" behavior and MySQL 5.7's "strict" behavior.

    In 5.7, STRICT_TRANS_TABLES does include many of these other strict settings by default, like NO_ZERO_IN_DATE for example, but to replicate that behavior in 5.6 and below requires enabling those modes explicitly.

    Read this long ass confusing page for more info: http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html

  • Setting strict to false now enables juset the default MySQL 5.6 modes.

    Prior to this PR, setting strict to false actually removed all modes that were enabled by the database, as you can see here:

    $connection->prepare("set session sql_mode=''")->execute();

    This PR just sets the mode to NO_ENGINE_SUBSTITION which was the default prior to 5.7.

  • A new modes config key has been added

    This key is an array of the modes you would like to enable.

    • If it is not set or null, it has no effect.
    • If it is an empty array, it overrides the strict flag, and will explicitly set your sql_mode to "".
    • If it contains modes, those are the modes that will be enabled for the connection.

@adamwathan
Copy link
Contributor Author

Here are some references for the default SQL mode settings:

For MySQL 5.6, from here:

As of MySQL 5.6.6, the default SQL mode is NO_ENGINE_SUBSTITUTION. Before 5.6.6, the default mode was empty (no modes enabled).

For MySQL 5.7, from here:

The default SQL mode in MySQL 5.7 includes these modes: ONLY_FULL_GROUP_BY, STRICT_TRANS_TABLES, NO_ZERO_IN_DATE, NO_ZERO_DATE, ERROR_FOR_DIVISION_BY_ZERO, NO_AUTO_CREATE_USER, and NO_ENGINE_SUBSTITUTION.

The ONLY_FULL_GROUP_BY and STRICT_TRANS_TABLES modes were added in MySQL 5.7.5. The NO_AUTO_CREATE_USER mode was added in MySQL 5.7.7. The ERROR_FOR_DIVISION_BY_ZERO, NO_ZERO_DATE, and NO_ZERO_IN_DATE modes were added in MySQL 5.7.8. For additional discussion regarding these changes to the default SQL mode value, see SQL Mode Changes in MySQL 5.7.

@taylorotwell
Copy link
Member

Thanks <3

@GrahamCampbell GrahamCampbell changed the title Improve behavior of MySQL flag, add modes config [5.2] Improve behavior of MySQL flag, add modes config Jan 26, 2016
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.

2 participants