Skip to content

Conversation

fatkodima
Copy link
Member

@fatkodima fatkodima commented Dec 12, 2020

Fixes #38218
Fixes #41240

All adapters support text columns with defaults, except MySQL (but MariaDB >= 10.2.1 supports).

@fatkodima fatkodima force-pushed the fix-mysql-column-default branch from 72b6e50 to d08c5bf Compare December 12, 2020 21:24
@fatkodima fatkodima changed the title Fix retrieving default value for text column for MySQL Fix retrieving default value for text column for MariaDB Dec 12, 2020
@fatkodima fatkodima force-pushed the fix-mysql-column-default branch 2 times, most recently from 283ffb5 to 9e9e11f Compare December 12, 2020 22:11
@fatkodima fatkodima force-pushed the fix-mysql-column-default branch from 9e9e11f to 5595d7b Compare December 13, 2020 01:26
Base automatically changed from master to main January 14, 2021 17:02
@intrip
Copy link
Contributor

intrip commented Mar 22, 2021

@fatkodima @kamipo I didn't see your PR and accidentally created a similar one #41726 🤦‍♂️

I'll close my PR buy if you want you can port the extra test that I've added for the SchemaDumper:

def test_schema_dump_with_text_column
output = dump_table_schema("dump_defaults")
assert_match %r{t\.text\s+"text_with_default",.*?default: "John' Doe"}, output
end if supports_default_text?

@janko
Copy link
Contributor

janko commented Mar 30, 2021

Would love to see this one merged 🤘. Currently this issue is preventing us from setting default values for serialized JSON columns on MariaDB.

@lfalcao
Copy link
Contributor

lfalcao commented May 30, 2021

👀

@intrip
Copy link
Contributor

intrip commented May 31, 2021

@fatkodima since you're the author could you please fix the conflicts? Also please check #40822 (comment) incase you'd like to pull in the extra tests.

If you can't work on this I don't mind opening a new PR.

@fatkodima fatkodima force-pushed the fix-mysql-column-default branch from 5595d7b to d836faf Compare May 31, 2021 11:03
@fatkodima
Copy link
Member Author

Rebased and pulled the test case.
Wow, got a nice build number on Buildkite - https://buildkite.com/rails/rails/builds/77777 😄

@@ -167,6 +167,9 @@ def new_column_from_field(table_name, field)
elsif type_metadata.extra == "DEFAULT_GENERATED"
default = +"(#{default})" unless default.start_with?("(")
default, default_function = nil, default
elsif type_metadata.type == :text && default
# strip and unescape quotes
default = default[1...-1].gsub("\\'", "'")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is gsub the right operation here? I wonder if this might be costly 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean replace it with gsub!? text columns with default are not popular enough, I think, and default values in the db most of the time have small values in them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fatkodima Sorry I was misconnecting the dots here. This code probably isn't being called during a request so maybe we don't have to worry about the performance of gsub. Please correct me if I'm wrong! 🙇

Copy link
Member Author

@fatkodima fatkodima Jun 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is used in

def columns(table_name)
table_name = table_name.to_s
column_definitions(table_name).map do |field|
new_column_from_field(table_name, field)
end
end

which runs once for each table, when columns list for the first time is needed, and then cached in SchemaCache -
def columns(table_name)
@columns.fetch(table_name) do
@columns[deep_deduplicate(table_name)] = deep_deduplicate(connection.columns(table_name))
end
end

So, no need to worry about the performance.

@zzak zzak added the ready PRs ready to merge label Jun 20, 2021
@zzak zzak requested a review from kamipo June 20, 2021 21:27
@byroot byroot merged commit 796aba1 into rails:main Jul 27, 2021
@fatkodima fatkodima deleted the fix-mysql-column-default branch July 27, 2021 21:35
rafaelfranca pushed a commit that referenced this pull request Jul 28, 2021
Fix retrieving default value for text column for MariaDB
rafiss added a commit to rafiss/activerecord-cockroachdb-adapter that referenced this pull request Mar 14, 2022
This was backported to Rails 6.1 in rails/rails#40822
and the tests require it now.
@kmooventhan98
Copy link

Why Mysql2 not supporting this?

Mysql2::Error: BLOB, TEXT, GEOMETRY or JSON column 'my_field_name' can't have a default value

@byroot
Copy link
Member

byroot commented Feb 21, 2025

This error is returned by the MySQL server. You won't get an answer on this thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
activerecord ready PRs ready to merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to set default JSON value on a text column with MariaDB Migration on Mariadb: text column default value reads back with extra quotes
8 participants