Add support for generated columns in SQLite3 adapter#49346
Merged
byroot merged 1 commit intorails:mainfrom Dec 14, 2023
Merged
Conversation
ef9ab69 to
6b61b49
Compare
skipkayhil
reviewed
Sep 25, 2023
Member
skipkayhil
left a comment
There was a problem hiding this comment.
Small comment, but generally looks good
activerecord/lib/active_record/connection_adapters/sqlite3/column.rb
Outdated
Show resolved
Hide resolved
6b61b49 to
941f83b
Compare
nvasilevski
approved these changes
Sep 25, 2023
activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rb
Outdated
Show resolved
Hide resolved
941f83b to
5f68fa4
Compare
Contributor
Author
|
@skipkayhil Addressed your one comment. Everything should be good to go now |
5f68fa4 to
cf882dc
Compare
yahonda
reviewed
Oct 3, 2023
activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb
Outdated
Show resolved
Hide resolved
cf882dc to
14fe38b
Compare
d2803e3 to
09b1be4
Compare
3610017 to
a9c77e4
Compare
byroot
approved these changes
Dec 14, 2023
Member
byroot
left a comment
There was a problem hiding this comment.
Two minor nitpick. I also rebased your branch to replace the merge commit.
Given how small the changes are I'll apply them myself.
| end | ||
|
|
||
| def virtual? | ||
| @generated_type.present? |
Member
There was a problem hiding this comment.
Suggested change
| @generated_type.present? | |
| @generated_type |
.present? isn't a "to_boolean" cast, it has fairly peculiar rules, and is rarely what you want.
Here we can just return @generated_type, or if you are adamant about returning a boolean, you can use !!@generated_type or !@generated_type.nil?
| type.to_sym == :primary_key || options[:primary_key] || | ||
| options[:null] == false && options[:default].nil? | ||
| options[:null] == false && options[:default].nil? || | ||
| (type.to_sym == :virtual && options[:stored]) |
Member
There was a problem hiding this comment.
We should do the to_sym casting in add_column
a9c77e4 to
e3ad3e8
Compare
Generated columns (both stored and dynamic) are supported since version 3.31.0 of SQLite. This adds support for those to the SQLite3 adapter. ```ruby create_table :users do |t| t.string :name t.virtual :name_upper, type: :string, as: 'UPPER(name)' t.virtual :name_lower, type: :string, as: 'LOWER(name)', stored: true end ```
e3ad3e8 to
4e7bdcf
Compare
Merged
4 tasks
rossta
added a commit
to joyofrails/joyofrails.com
that referenced
this pull request
Oct 3, 2024
…hancedsqlite3-adapter Most of the enhancements provided by activerecord-enhancedsqlite3-adapter have been upstreamed into Rails, including: * Add support for generated columns in SQLite3 adapter rails/rails#49346 * Add SQLite3 support for supports_insert_returning? rails/rails#49290 * Allow overriding SQLite defaults from database.yml rails/rails#50460 * Performance tune the SQLite3 adapter connection configuration rails/rails#49349 * Allow SQLite3 busy_handler to be configured with simple max number of retries rails/rails#49352 One enhancement Joy still needs is a way to load SQLite3 extensions. There is a sqlpkg gem that would provide this, which includes generating an initializer that overrides the Rails SQLite3 adapter. Instead, I‘ve just adapted the enhancedsqlite3-adapter code and droppped into the lib/ directory. The code here also knows how to load an extension by gem name which is makes it useful for the sqlite_ulid gem currently in use.
rossta
added a commit
to joyofrails/joyofrails.com
that referenced
this pull request
Oct 3, 2024
…hancedsqlite3-adapter Most of the enhancements provided by activerecord-enhancedsqlite3-adapter have been upstreamed into Rails, including: * Add support for generated columns in SQLite3 adapter rails/rails#49346 * Add SQLite3 support for supports_insert_returning? rails/rails#49290 * Allow overriding SQLite defaults from database.yml rails/rails#50460 * Performance tune the SQLite3 adapter connection configuration rails/rails#49349 * Allow SQLite3 busy_handler to be configured with simple max number of retries rails/rails#49352 One enhancement Joy still needs is a way to load SQLite3 extensions. There is a sqlpkg gem that would provide this, which includes generating an initializer that overrides the Rails SQLite3 adapter. Instead, I‘ve just adapted the enhancedsqlite3-adapter code and droppped into the lib/ directory. The code here also knows how to load an extension by gem name which is makes it useful for the sqlite_ulid gem currently in use.
rossta
added a commit
to joyofrails/joyofrails.com
that referenced
this pull request
Oct 3, 2024
…hancedsqlite3-adapter Most of the enhancements provided by activerecord-enhancedsqlite3-adapter have been upstreamed into Rails, including: * Add support for generated columns in SQLite3 adapter rails/rails#49346 * Add SQLite3 support for supports_insert_returning? rails/rails#49290 * Allow overriding SQLite defaults from database.yml rails/rails#50460 * Performance tune the SQLite3 adapter connection configuration rails/rails#49349 * Allow SQLite3 busy_handler to be configured with simple max number of retries rails/rails#49352 One enhancement Joy still needs is a way to load SQLite3 extensions. There is a sqlpkg gem that would provide this, which includes generating an initializer that overrides the Rails SQLite3 adapter. Instead, I‘ve just adapted the enhancedsqlite3-adapter code and droppped into the lib/ directory. The code here also knows how to load an extension by gem name which is makes it useful for the sqlite_ulid gem currently in use.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation / Background
SQLite is a feature-rich database engine, and production usage is only growing. We need Rails' support to offer developers the range and scope of its features.
Both the MySQL and PostgreSQL adapters support virtual columns, and SQLite itself has support generated columns since 2020.
Detail
PR #41856 introduced virtual columns to the PostgreSQLAdapter. This is effectively a clone of that feature, with some necessary tweaks.
Firstly, SQLite supported both
STOREDandVIRTUALgenerated columns, while PostgreSQL only supportsVIRTUAL. Secondly, SQLite doesn't supportALTER TABLEcommands with generated columns.In this PR, I am adding full support for the SQLite3Adapter by implementing:
ActiveRecord::ConnectionAdapters::SQLite3::Column#virtual?ActiveRecord::ConnectionAdapters::SQLite3Adapter#supports_virtual_columns?and altering:
ActiveRecord::ConnectionAdapters::SQLite3::SchemaCreation#add_column_optionsActiveRecord::ConnectionAdapters::SQLite3::SchemaDefinitions#new_column_definitionActiveRecord::ConnectionAdapters::SQLite3::SchemaDumper#prepare_column_optionsActiveRecord::ConnectionAdapters::SQLite3::SchemaStatements#new_column_from_fieldActiveRecord::ConnectionAdapters::SQLite3Adapter#table_structureActiveRecord::ConnectionAdapters::SQLite3Adapter#invalid_alter_table_type?ActiveRecord::ConnectionAdapters::SQLite3Adapter#table_structure_with_collationChecklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]