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

Field column size improvements #13025

Merged

Conversation

brandonkelly
Copy link
Member

@brandonkelly brandonkelly commented Apr 2, 2023

Description

Makes the following improvements for custom field content table columns:

  • When adding or renaming a column, if a column with the same name already exists, a new table will be created (content_<columnName>_bak_<timestamp> to store the existing column’s non-null values, along with the rows’ id, elementId, and siteId values, and then the original column will be deleted from the content table. (Previously, the column was just renamed to <columnName>_old, adding to the table’s row size.)
  • When changing a custom field’s column type, if a SQL error occurs due to the existing data being too long, the existing column data will be backed up in a new table, and then any existing values that would overflow the new column size will be set to null, then the alter table command will be attempted once again. (Previously, the column was just renamed to <columnName>_old and a new column was created, effectively wiping out the field’s existing values until manually reconstructed.)
  • Dropdown and Radio Buttons fields now use smart column lengths, based on their longest option value’s length. (Note this doesn’t come into effect until the fields are re-saved.)

Example

If you have a Dropdown field with yes, no, and maybe options, its column type will be varchar(5) (instead of varchar(255) as before).

If its maybe option is removed later on, its column type will be changed to varchar(3). If any entries exist that had maybe selected, a new content_<columnName>_bak_<timestamp> table will be created, which will contain all of the non-null Dropdown field values, and then all maybe options will be set to null in the content table.

Related issues

@brandonkelly brandonkelly requested a review from a team as a code owner April 2, 2023 01:18
@linear
Copy link

linear bot commented Apr 2, 2023

[ci skip]
@brandonkelly brandonkelly merged commit 552047e into 4.5 Apr 2, 2023
@brandonkelly brandonkelly deleted the feature/dev-1103-smart-dropdownradio-button-column branch April 2, 2023 01:34
@Mosnar
Copy link
Contributor

Mosnar commented Apr 5, 2023

@brandonkelly Is there any risk of the automatic column resizing on option fields causing problems for people using the DefineInputOptionsEvent event? I'm not using it myself anywhere, but I've been keeping it in my pocket since it seems like it'd be really useful.

To be more specific, my thought is that since the resizing occurs when the field is saved, additional options defined via that event might be longer than what's supported by the column size.

@brandonkelly
Copy link
Member Author

@Mosnar Good point! Yes, that could be an issue. And I don’t think we can safely call the event when determining the column length, because event handlers could return different options depending on the current entry, etc.

I’ve just added a new “Column Type” setting for Dropdown and Radio Buttons fields, which has two options – “Automatic” and “varchar”. The setting will be set to “varchar” for existing fields, and “Automatic” for new fields. So you will need to opt into the smart column sizing for existing fields (if you know it’s safe to do so), and you can opt out of it for new ones.

@lineke
Copy link

lineke commented May 1, 2023

@brandonkelly Any chance this could be ported to Craft CMS 3?

@brandonkelly
Copy link
Member Author

@lineke Craft 3 is now feature-locked. Bug/security fixes only now.

@lineke
Copy link

lineke commented May 15, 2023

@brandonkelly We are now upgrading to craft 4 in order to solve the issue. Any idea when 4.5 will be released?

@brandonkelly
Copy link
Member Author

@lineke Probably not for another month or so. You can start using it while it’s still in development by changing your craftcms/cms constraint in composer.json to 4.5.x-dev as 4.5.0 and running composer update.

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.

3 participants