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
Add support for generated columns in SQLite3 adapter #49346
Conversation
ef9ab69
to
6b61b49
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small comment, but generally looks good
activerecord/lib/active_record/connection_adapters/sqlite3/column.rb
Outdated
Show resolved
Hide resolved
6b61b49
to
941f83b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rb
Outdated
Show resolved
Hide resolved
941f83b
to
5f68fa4
Compare
@skipkayhil Addressed your one comment. Everything should be good to go now |
5f68fa4
to
cf882dc
Compare
cf882dc
to
14fe38b
Compare
d2803e3
to
09b1be4
Compare
3610017
to
a9c77e4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two minor nitpick. I also rebased your branch to replace the merge commit.
Given how small the changes are I'll apply them myself.
@@ -20,6 +21,18 @@ def auto_incremented_by_db? | |||
auto_increment? || rowid | |||
end | |||
|
|||
def virtual? | |||
@generated_type.present? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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?
@@ -503,7 +511,8 @@ def has_default_function?(default_value, default) | |||
# SQLite has an additional restriction on the ALTER TABLE statement | |||
def invalid_alter_table_type?(type, options) | |||
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]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
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
STORED
andVIRTUAL
generated columns, while PostgreSQL only supportsVIRTUAL
. Secondly, SQLite doesn't supportALTER TABLE
commands 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_options
ActiveRecord::ConnectionAdapters::SQLite3::SchemaDefinitions#new_column_definition
ActiveRecord::ConnectionAdapters::SQLite3::SchemaDumper#prepare_column_options
ActiveRecord::ConnectionAdapters::SQLite3::SchemaStatements#new_column_from_field
ActiveRecord::ConnectionAdapters::SQLite3Adapter#table_structure
ActiveRecord::ConnectionAdapters::SQLite3Adapter#invalid_alter_table_type?
ActiveRecord::ConnectionAdapters::SQLite3Adapter#table_structure_with_collation
Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]