Skip to content

Comments

Ensure the SQLite3 adapter handles default functions with the || concatenation operator#49287

Merged
guilleiguaran merged 1 commit intorails:mainfrom
fractaledmind:ar-sqlite3-concatentation
Sep 20, 2023
Merged

Ensure the SQLite3 adapter handles default functions with the || concatenation operator#49287
guilleiguaran merged 1 commit intorails:mainfrom
fractaledmind:ar-sqlite3-concatentation

Conversation

@fractaledmind
Copy link
Contributor

Previously, this default function would produce the static string "'Ruby ' || 'on ' || 'Rails'". Now, the adapter will appropriately receive and use "Ruby on Rails".

change_column_default "test_models", "ruby_on_rails", -> { "('Ruby ' || 'on ' || 'Rails')" }

Motivation / Background

The wave of using SQLite in production is growing, but the ActiveRecord SQLite3Adapter doesn't take full advantage of the power and functionality of the SQLite engine. In order to begin making the SQLite3Adapter feature-comparable with the MySQLAdapter and PostgreSQLAdapter, I am undertaking a multi-step task to level up the SQLite3Adapter.

Detail

I am beginning with the simplest improvement I initially found, in order to "get my feet wet" with the process of upstreaming feature to Rails and ActiveRecord. Instead of using the CONCAT() function, SQLite provides the || concatenation operator. The adapter does not recognize usage of this operator as a dynamic default_function, however.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated 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.

@fractaledmind fractaledmind force-pushed the ar-sqlite3-concatentation branch from 4aa6140 to 40ab8c4 Compare September 15, 2023 20:02
@yahonda
Copy link
Member

yahonda commented Sep 19, 2023

Thank you for opening a pull request.
Would you explain why concatenating strings for the default function is helpful?
I'm having difficulty thinking of any useful use cases at the moment.

@fractaledmind
Copy link
Contributor Author

Thank you for opening a pull request.
Would you explain why concatenating strings for the default function is helpful?
I'm having difficulty thinking of any useful use cases at the moment.

Hey @yahonda, thanks for taking the time to review.

To be perfectly honest, the primary reason that I opened this PR was because a Rails test schema for PostgreSQL used CONCAT in a default function, and I was working on a separate PR (#49290) that was aiming for feature parity with PostgreSQL on the feature that schema was being used to test. I am not certain why the test schema uses CONCAT, but my presumption would be that production usage wouldn't simply CONCAT strings, but rather join a static string prefix or suffix with some dynamic content.

Here is a quick GitHub search I ran to find some instances of CONCAT used in a Rails migration: https://github.com/search?q=%22CONCAT%22+path%3A*.rb+path%3A%2Fmigrations+language%3ARuby&type=Code&ref=advsearch&l=Ruby&l=

There is some noise in the results, but you will see a number of usages, which all indeed are joining some dynamic values. I presume that the joining of all static strings was used in the tests for simplicity.

@guilleiguaran
Copy link
Member

can you rebase this?

@fractaledmind fractaledmind force-pushed the ar-sqlite3-concatentation branch from 40ab8c4 to f1104e6 Compare September 20, 2023 06:50
@fractaledmind
Copy link
Contributor Author

can you rebase this?

@guilleiguaran: Done

…ncatenation operator

Previously, this default function would produce the static string `"'Ruby ' || 'on ' || 'Rails'"`.
Now, the adapter will appropriately receive and use `"Ruby on Rails"`.

```ruby
change_column_default "test_models", "ruby_on_rails", -> { "('Ruby ' || 'on ' || 'Rails')" }
```
@fractaledmind fractaledmind force-pushed the ar-sqlite3-concatentation branch from f1104e6 to 828eec6 Compare September 20, 2023 07:14
@guilleiguaran guilleiguaran merged commit c8830a2 into rails:main Sep 20, 2023
@fractaledmind fractaledmind deleted the ar-sqlite3-concatentation branch September 20, 2023 16:53
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.

4 participants