Skip to content

Fix boolean columns triggering MySQL deprecation#54144

Merged
rafaelfranca merged 1 commit intorails:mainfrom
skipkayhil:hm-fix-mysql-deprecated-integer-width
Jan 7, 2025
Merged

Fix boolean columns triggering MySQL deprecation#54144
rafaelfranca merged 1 commit intorails:mainfrom
skipkayhil:hm-fix-mysql-deprecated-integer-width

Conversation

@skipkayhil
Copy link
Member

Motivation / Background

Previously, when creating a boolean column for a MySQL database

class AddSoldToProduct < ActiveRecord::Migration[8.0]
  def change
    add_column :products, :sold, :boolean
  end
end

Active Record would create a query like

ALTER TABLE `products` ADD `sold` tinyint(1)

which would trigger a deprecation warning in MySQL 8.0.17+

1681: Integer display width is deprecated and will be removed in a future release.

Additionally, starting in MySQL 8.0.19, integer display widths are no longer included in the output of SHOW CREATE TABLES with the exception of tinyint(1) because

MySQL Connectors make the assumption that TINYINT(1) columns
originated as BOOLEAN columns; this exception enables them to continue
to make that assumption.

Detail

This commit changes Active Record to use the boolean alias for MySQL (which still results in tinyint(1) columns) but does not emit the deprecation warning for using an integer display width.

ALTER TABLE `products` ADD `sold` boolean

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

Previously, when creating a boolean column for a MySQL database

```ruby
class AddSoldToProduct < ActiveRecord::Migration[8.0]
  def change
    add_column :products, :sold, :boolean
  end
end
```

Active Record would create a query like

```SQL
ALTER TABLE `products` ADD `sold` tinyint(1)
```

which would trigger a deprecation warning in MySQL 8.0.17+

```
1681: Integer display width is deprecated and will be removed in a future release.
```

Additionally, starting in MySQL 8.0.19, integer display widths are no
longer included in the output of `SHOW CREATE TABLES` with the exception
of `tinyint(1)` because

> MySQL Connectors make the assumption that TINYINT(1) columns
> originated as BOOLEAN columns; this exception enables them to continue
> to make that assumption.

This commit changes Active Record to use the `boolean` alias for MySQL
(which still results in `tinyint(1)` columns) but does not emit the
deprecation warning for using an integer display width.

```SQL
ALTER TABLE `products` ADD `sold` boolean
```
@rafaelfranca rafaelfranca merged commit cc0a09e into rails:main Jan 7, 2025
1 check passed
@skipkayhil skipkayhil deleted the hm-fix-mysql-deprecated-integer-width branch January 7, 2025 20:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants