-
Notifications
You must be signed in to change notification settings - Fork 565
Added support for computed columns #1367
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
dcfc7e1
Added support for computed columns
aidanharan c2b9952
Standardrb
aidanharan c504200
Update table_definition.rb
aidanharan ec766d0
Update coerced_tests.rb
aidanharan 7a9b2b7
Type not required for virtual column
aidanharan 7e4bf73
Merge branch 'main' into virtual-columns
aidanharan e53a0b7
Update schema_statements.rb
aidanharan 409c1a3
Test that by default virtual columns are not persisted
aidanharan b8ae9c9
Merge branch 'main' into virtual-columns
aidanharan c2d05ae
Add documentation
aidanharan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "cases/helper_sqlserver" | ||
| require "support/schema_dumping_helper" | ||
|
|
||
| class VirtualColumnTestSQLServer < ActiveRecord::TestCase | ||
| include SchemaDumpingHelper | ||
|
|
||
| class VirtualColumn < ActiveRecord::Base | ||
| end | ||
|
|
||
| def setup | ||
| @connection = ActiveRecord::Base.lease_connection | ||
| @connection.create_table :virtual_columns, force: true do |t| | ||
| t.string :name | ||
| t.virtual :upper_name, as: "UPPER(name)", stored: true | ||
| t.virtual :lower_name, as: "LOWER(name)", stored: false | ||
| t.virtual :octet_name, as: "LEN(name)" | ||
| t.virtual :mutated_name, as: "REPLACE(name, 'l', 'L')" | ||
| t.integer :column1 | ||
| end | ||
| VirtualColumn.create(name: "Rails", column1: 10) | ||
| end | ||
|
|
||
| def teardown | ||
| @connection.drop_table :virtual_columns, if_exists: true | ||
| VirtualColumn.reset_column_information | ||
| end | ||
|
|
||
| def test_virtual_column_with_full_inserts | ||
| partial_inserts_was = VirtualColumn.partial_inserts | ||
| VirtualColumn.partial_inserts = false | ||
| assert_nothing_raised do | ||
| VirtualColumn.create!(name: "Rails") | ||
| end | ||
| ensure | ||
| VirtualColumn.partial_inserts = partial_inserts_was | ||
| end | ||
|
|
||
| def test_stored_column | ||
| column = VirtualColumn.columns_hash["upper_name"] | ||
| assert_predicate column, :virtual? | ||
| assert_predicate column, :virtual_stored? | ||
| assert_equal "RAILS", VirtualColumn.take.upper_name | ||
| end | ||
|
|
||
| def test_explicit_virtual_column | ||
| column = VirtualColumn.columns_hash["lower_name"] | ||
| assert_predicate column, :virtual? | ||
| assert_not_predicate column, :virtual_stored? | ||
| assert_equal "rails", VirtualColumn.take.lower_name | ||
| end | ||
|
|
||
| def test_implicit_virtual_column | ||
| column = VirtualColumn.columns_hash["octet_name"] | ||
| assert_predicate column, :virtual? | ||
| assert_not_predicate column, :virtual_stored? | ||
| assert_equal 5, VirtualColumn.take.octet_name | ||
| end | ||
|
|
||
| def test_virtual_column_with_comma_in_definition | ||
| column = VirtualColumn.columns_hash["mutated_name"] | ||
| assert_predicate column, :virtual? | ||
| assert_not_predicate column, :virtual_stored? | ||
| assert_not_nil column.default_function | ||
| assert_equal "RaiLs", VirtualColumn.take.mutated_name | ||
| end | ||
|
|
||
| def test_change_table_with_stored_generated_column | ||
| @connection.change_table :virtual_columns do |t| | ||
| t.virtual :decr_column1, as: "column1 - 1", stored: true | ||
| end | ||
| VirtualColumn.reset_column_information | ||
| column = VirtualColumn.columns_hash["decr_column1"] | ||
| assert_predicate column, :virtual? | ||
| assert_predicate column, :virtual_stored? | ||
| assert_equal 9, VirtualColumn.take.decr_column1 | ||
| end | ||
|
|
||
| def test_change_table_with_explicit_virtual_generated_column | ||
| @connection.change_table :virtual_columns do |t| | ||
| t.virtual :incr_column1, as: "column1 + 1", stored: false | ||
| end | ||
| VirtualColumn.reset_column_information | ||
| column = VirtualColumn.columns_hash["incr_column1"] | ||
| assert_predicate column, :virtual? | ||
| assert_not_predicate column, :virtual_stored? | ||
| assert_equal 11, VirtualColumn.take.incr_column1 | ||
| end | ||
|
|
||
| def test_change_table_with_implicit_virtual_generated_column | ||
| @connection.change_table :virtual_columns do |t| | ||
| t.virtual :sqr_column1, as: "power(column1, 2)" | ||
| end | ||
| VirtualColumn.reset_column_information | ||
| column = VirtualColumn.columns_hash["sqr_column1"] | ||
| assert_predicate column, :virtual? | ||
| assert_not_predicate column, :virtual_stored? | ||
| assert_equal 100, VirtualColumn.take.sqr_column1 | ||
| end | ||
|
|
||
| def test_schema_dumping | ||
| output = dump_table_schema("virtual_columns") | ||
| assert_match(/t\.virtual\s+"lower_name",\s+as: "\(lower\(\[name\]\)\)", stored: false$/i, output) | ||
| assert_match(/t\.virtual\s+"upper_name",\s+as: "\(upper\(\[name\]\)\)", stored: true$/i, output) | ||
| assert_match(/t\.virtual\s+"octet_name",\s+as: "\(len\(\[name\]\)\)", stored: false$/i, output) | ||
| end | ||
|
|
||
| def test_build_fixture_sql | ||
| fixtures = ActiveRecord::FixtureSet.create_fixtures(FIXTURES_ROOT, :virtual_columns).first | ||
| assert_equal 2, fixtures.size | ||
| end | ||
| end |
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.
Uh oh!
There was an error while loading. Please reload this page.