Skip to content

Commit 64fd666

Browse files
committed
Incorporate feedback
1 parent 2cf41d7 commit 64fd666

File tree

6 files changed

+16
-4
lines changed

6 files changed

+16
-4
lines changed

activerecord/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
```ruby
77
create_table :users do |t|
88
t.string :name
9-
t.virtual :name_upcased, type: :string, as: 'upper(name)'
9+
t.virtual :name_upcased, type: :string, as: 'upper(name)', stored: true
1010
end
1111
```
1212

activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ def default_insert_value(column)
456456
end
457457

458458
def build_fixture_sql(fixtures, table_name)
459-
columns = schema_cache.columns_hash(table_name)
459+
columns = schema_cache.columns_hash(table_name).reject { |_, column| supports_virtual_columns? && column.virtual? }
460460

461461
values_list = fixtures.map do |fixture|
462462
fixture = fixture.stringify_keys

activerecord/lib/active_record/connection_adapters/postgresql/column.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require "active_support/core_ext/object/blank"
4+
35
module ActiveRecord
46
module ConnectionAdapters
57
module PostgreSQL
@@ -17,7 +19,8 @@ def serial?
1719
end
1820

1921
def virtual?
20-
@generated == "s"
22+
# We assume every generated column is virtual, no matter the concrete type
23+
@generated.present?
2124
end
2225

2326
def has_default?

activerecord/test/cases/adapters/postgresql/virtual_column_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,9 @@ def test_schema_dumping
6969
assert_match(/t\.virtual\s+"name_length",\s+type: :integer,\s+as: "length\(\(name\)::text\)", stored: true$/i, output)
7070
assert_match(/t\.virtual\s+"name_octet_length",\s+type: :integer,\s+as: "octet_length\(\(name\)::text\)", stored: true$/i, output)
7171
end
72+
73+
def test_build_fixture_sql
74+
ActiveRecord::FixtureSet.create_fixtures(FIXTURES_ROOT, :virtual_columns)
75+
end
7276
end
7377
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
one:
2+
name: hello
3+
4+
two:
5+
name: world

guides/source/active_record_postgresql.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ NOTE: Generated columns are supported since version 12.0 of PostgreSQL.
512512
# db/migrate/20131220144913_create_users.rb
513513
create_table :users do |t|
514514
t.string :name
515-
t.virtual :name_upcased, type: :string, as: 'upper(name)'
515+
t.virtual :name_upcased, type: :string, as: 'upper(name)', stored: true
516516
end
517517

518518
# app/models/user.rb

0 commit comments

Comments
 (0)