updateable_views_inheritance 1.4.8 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/build.yml +3 -4
- data/CHANGELOG.md +8 -0
- data/lib/updateable_views_inheritance/active_record_extensions.rb +22 -0
- data/lib/updateable_views_inheritance/postgresql_adapter.rb +26 -37
- data/lib/updateable_views_inheritance/version.rb +1 -1
- data/lib/updateable_views_inheritance.rb +1 -1
- data/test/deep_hierarchy_test.rb +6 -8
- data/test/dummy/app/models/locomotive.rb +0 -1
- data/test/dummy/app/models/vehicle.rb +1 -2
- data/test/dummy/config/environments/test.rb +2 -2
- data/test/fixtures/migrations/1_add_updateable_views_inheritance.rb +2 -2
- data/test/fixtures/migrations/2_create_with_default_table.rb +2 -2
- data/test/fixtures/migrations/3_create_with_explicit_table.rb +4 -4
- data/test/fixtures/migrations/4_create_deeper_hierarchy.rb +3 -3
- data/test/fixtures/migrations/5_default_column_values.rb +4 -4
- data/test/fixtures/migrations/6_single_table_inheritance_view.rb +3 -3
- data/test/fixtures/migrations/7_second_deep_hierarchy.rb +12 -12
- data/test/fixtures/migrations/8_second_single_table_inheritance_view.rb +3 -3
- data/test/instantiation_test.rb +2 -2
- data/test/migration_test.rb +2 -1
- data/test/pg_insert_returning_with_rules_test.rb +60 -0
- data/test/schema_test.rb +48 -52
- data/test/single_table_inheritance_test.rb +18 -18
- data/updateable_views_inheritance.gemspec +3 -3
- metadata +15 -15
- data/lib/updateable_views_inheritance/active_record.rb +0 -18
- data/test/pg_insert_returning_with_rules_spec.rb +0 -58
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a4134e2e8c505f5ca5c6d53762497a18b168b5cb43027a121f337908d1595d5b
|
4
|
+
data.tar.gz: 7dc39d94d346f6efef8862c2482afe72d97f745b822b59b313041dcb21fb9ebc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e50eeaa1e8e686fe94bd47f02f8109335b7f38efa94e65c108dc5e0c5dcc7509e476ee14ce3de31160673d6bcce0f540dcbf0142f774b1db7f017ed106251b15
|
7
|
+
data.tar.gz: e2e3e32f9b7e6f77c113c1d80701f6a1345a4352df0779d5cfc3f450bfe11fe04c6b65955ba07de440c4733df71ba84073294f274e6c14e4b80fda1024391208
|
data/.github/workflows/build.yml
CHANGED
@@ -41,15 +41,14 @@ jobs:
|
|
41
41
|
- name: Set up Ruby
|
42
42
|
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
|
43
43
|
# change this to (see https://github.com/ruby/setup-ruby#versioning):
|
44
|
-
|
45
|
-
uses: ruby/setup-ruby@55283cc23133118229fd3f97f9336ee23a179fcf # v1.146.0
|
44
|
+
uses: ruby/setup-ruby@v1
|
46
45
|
with:
|
47
46
|
ruby-version: ${{ matrix.ruby-version }}
|
48
|
-
bundler:
|
47
|
+
bundler: 2.4.22 # temporary set until rails 5 upgrade
|
49
48
|
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
50
49
|
|
51
50
|
- name: Run tests
|
52
|
-
run: bundle exec rake
|
51
|
+
run: bundle exec rake
|
53
52
|
|
54
53
|
# From https://stackoverflow.com/questions/74199483/sonarcloud-ci-cant-find-source-files-for-ruby-simplecov-coverage
|
55
54
|
- name: Fix code coverage paths for DeepSource
|
data/CHANGELOG.md
CHANGED
@@ -0,0 +1,22 @@
|
|
1
|
+
|
2
|
+
module UpdateableViewsInheritance# :nodoc:
|
3
|
+
module ActiveRecordExtensions #:nodoc:
|
4
|
+
module ClassMethods #:nodoc:
|
5
|
+
attr_accessor :disable_inheritance_instantiation
|
6
|
+
|
7
|
+
def instantiate(attributes, column_types = {})
|
8
|
+
object = super(attributes, column_types = {})
|
9
|
+
if object.class.name == self.name || self.disable_inheritance_instantiation
|
10
|
+
object
|
11
|
+
else
|
12
|
+
object.class.find(attributes.with_indifferent_access[:id])
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Here we override AR class methods. If you need to override
|
20
|
+
# instance methods, prepend() them to the class, not to the singleton class
|
21
|
+
ActiveRecord::Base.singleton_class.send :prepend, UpdateableViewsInheritance::ActiveRecordExtensions::ClassMethods
|
22
|
+
|
@@ -150,16 +150,6 @@ module ActiveRecord #:nodoc:
|
|
150
150
|
execute "DROP VIEW #{quote_table_name(name)}"
|
151
151
|
end
|
152
152
|
|
153
|
-
# Return the list of all views in the schema search path.
|
154
|
-
def views(name=nil)
|
155
|
-
schemas = schema_search_path.split(/,\s*/).map { |p| quote(p) }.join(',')
|
156
|
-
query(<<~SQL, name).map { |row| row[0] }
|
157
|
-
SELECT viewname
|
158
|
-
FROM pg_views
|
159
|
-
WHERE schemaname IN (#{schemas})
|
160
|
-
SQL
|
161
|
-
end
|
162
|
-
|
163
153
|
# Checks whether relation +name+ is a view.
|
164
154
|
def is_view?(name)
|
165
155
|
result = query(<<~SQL, name).map { |row| row[0] }
|
@@ -262,11 +252,6 @@ module ActiveRecord #:nodoc:
|
|
262
252
|
false
|
263
253
|
end
|
264
254
|
|
265
|
-
def table_exists_with_updateable_views_inheritance_support?(name)
|
266
|
-
is_view?(name) ? true : table_exists_without_updateable_views_inheritance_support?(name)
|
267
|
-
end
|
268
|
-
alias_method_chain :table_exists?, :updateable_views_inheritance_support
|
269
|
-
|
270
255
|
module Tutuf #:nodoc:
|
271
256
|
class ClassTableReflection
|
272
257
|
class << self
|
@@ -357,33 +342,37 @@ module ActiveRecord #:nodoc:
|
|
357
342
|
SQL
|
358
343
|
|
359
344
|
# update
|
360
|
-
|
345
|
+
update_rule = <<~SQL
|
361
346
|
CREATE OR REPLACE RULE #{quote_column_name("#{child_view}_update")} AS
|
362
347
|
ON UPDATE TO #{quote_table_name(child_view)} DO INSTEAD (
|
363
|
-
#{ if parent_columns.empty?
|
364
|
-
''
|
365
|
-
else
|
366
|
-
"UPDATE #{parent_table}
|
367
|
-
SET #{ parent_columns.map { |col| "#{quote_column_name(col)} = NEW.#{quote_column_name(col)}" }.join(', ')}
|
368
|
-
WHERE #{parent_pk} = OLD.#{parent_pk};"
|
369
|
-
end }
|
370
|
-
#{ if child_columns.empty?
|
371
|
-
''
|
372
|
-
else
|
373
|
-
"UPDATE #{child_table}
|
374
|
-
SET #{ child_columns.map { |col| "#{quote_column_name(col)} = NEW.#{quote_column_name(col)}" }.join(', ')}
|
375
|
-
WHERE #{child_pk} = OLD.#{parent_pk}"
|
376
|
-
end }
|
377
|
-
)
|
378
348
|
SQL
|
349
|
+
unless parent_columns.empty?
|
350
|
+
update_rule += <<~SQL
|
351
|
+
UPDATE #{parent_table}
|
352
|
+
SET #{parent_columns.map { |col| "#{quote_column_name(col)} = NEW.#{quote_column_name(col)}" }.join(', ')}
|
353
|
+
WHERE #{parent_pk} = OLD.#{parent_pk};
|
354
|
+
SQL
|
355
|
+
end
|
356
|
+
unless child_columns.empty?
|
357
|
+
update_rule += <<~SQL
|
358
|
+
UPDATE #{child_table}
|
359
|
+
SET #{ child_columns.map { |col| "#{quote_column_name(col)} = NEW.#{quote_column_name(col)}" }.join(', ')}
|
360
|
+
WHERE #{child_pk} = OLD.#{parent_pk}
|
361
|
+
SQL
|
362
|
+
end
|
363
|
+
update_rule += ")"
|
364
|
+
execute(update_rule)
|
379
365
|
end
|
380
366
|
|
381
367
|
def insert_returning_clause(parent_pk, child_pk, child_view)
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
368
|
+
columns_cast = columns(child_view).map do |c|
|
369
|
+
if c.name == parent_pk
|
370
|
+
"#{child_pk}::#{c.sql_type}"
|
371
|
+
else
|
372
|
+
"NULL::#{c.sql_type}"
|
373
|
+
end
|
374
|
+
end.join(", ")
|
375
|
+
"RETURNING #{columns_cast}"
|
387
376
|
end
|
388
377
|
|
389
378
|
def create_system_table_records(parent_relation, child_aggregate_view, child_relation)
|
@@ -403,7 +392,7 @@ module ActiveRecord #:nodoc:
|
|
403
392
|
end
|
404
393
|
|
405
394
|
def parent_table(relation)
|
406
|
-
if
|
395
|
+
if data_source_exists?('updateable_views_inheritance')
|
407
396
|
res = query(<<-SQL, 'Parent relation')[0]
|
408
397
|
SELECT parent_relation
|
409
398
|
FROM updateable_views_inheritance
|
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'active_record'
|
2
2
|
require 'active_record/connection_adapters/postgresql_adapter'
|
3
3
|
require "updateable_views_inheritance/version"
|
4
|
-
require "updateable_views_inheritance/
|
4
|
+
require "updateable_views_inheritance/active_record_extensions"
|
5
5
|
require 'updateable_views_inheritance/postgresql_adapter'
|
data/test/deep_hierarchy_test.rb
CHANGED
@@ -3,6 +3,8 @@ require File.join(File.dirname(__FILE__), 'test_helper')
|
|
3
3
|
class DeepHierarchyTest < ActiveSupport::TestCase
|
4
4
|
def setup
|
5
5
|
ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 8)
|
6
|
+
|
7
|
+
ActiveRecord::FixtureSet.reset_cache
|
6
8
|
# order of fixtures is important for the test - last loaded should not be with max(id)
|
7
9
|
%w(boats electric_trains rack_trains steam_trains cars maglev_trains bicycles).each do |f|
|
8
10
|
ActiveRecord::FixtureSet.create_fixtures(File.dirname(__FILE__) + '/fixtures/', f)
|
@@ -10,10 +12,6 @@ class DeepHierarchyTest < ActiveSupport::TestCase
|
|
10
12
|
@connection = ActiveRecord::Base.connection
|
11
13
|
end
|
12
14
|
|
13
|
-
def teardown
|
14
|
-
ActiveRecord::FixtureSet.reset_cache
|
15
|
-
end
|
16
|
-
|
17
15
|
def test_deeper_hierarchy
|
18
16
|
assert_equal [["boats"], ["railed_vehicles", ["trains", ["steam_trains"], ["rack_trains"], ["electric_trains", ["maglev_trains"]]]], ["wheeled_vehicles", ["bicycles"], ["cars"]]].sort,
|
19
17
|
@connection.send(:get_view_hierarchy_for, :vehicles).sort
|
@@ -43,17 +41,17 @@ class DeepHierarchyTest < ActiveSupport::TestCase
|
|
43
41
|
|
44
42
|
def test_single_table_inheritance_deeper_hierarchy_contents
|
45
43
|
mag = MaglevTrain.first
|
46
|
-
assert_equal [mag.id
|
44
|
+
assert_equal [mag.id, mag.name, mag.number_of_rails, mag.max_speed, mag.magnetic_field, (sprintf("%.2f",mag.electricity_consumption))], (@connection.query("SELECT id, name, number_of_rails, max_speed, magnetic_field, electricity_consumption FROM all_vehicles WHERE id=#{mag.id}").first)
|
47
45
|
end
|
48
46
|
|
49
|
-
class OrderColumnsInAggregateView < ActiveRecord::Migration
|
50
|
-
def
|
47
|
+
class OrderColumnsInAggregateView < ActiveRecord::Migration[4.2]
|
48
|
+
def up
|
51
49
|
rebuild_single_table_inheritance_view(:all_vehicles,:vehicles, %w(max_speed number_of_wheels id))
|
52
50
|
end
|
53
51
|
end
|
54
52
|
|
55
53
|
def test_single_table_inheritance_view_order_view_columns
|
56
|
-
OrderColumnsInAggregateView.up
|
54
|
+
OrderColumnsInAggregateView.new.up
|
57
55
|
assert_equal %w(max_speed number_of_wheels id),
|
58
56
|
(@connection.query("SELECT attname
|
59
57
|
FROM pg_class, pg_attribute
|
@@ -8,8 +8,8 @@ Dummy::Application.configure do
|
|
8
8
|
config.cache_classes = true
|
9
9
|
|
10
10
|
# Configure static asset server for tests with Cache-Control for performance
|
11
|
-
config.
|
12
|
-
config.
|
11
|
+
config.public_file_server.enabled = true
|
12
|
+
config.public_file_server.headers = { 'Cache-Control' => 'public, max-age=3600' }
|
13
13
|
|
14
14
|
# Show full error reports and disable caching
|
15
15
|
config.consider_all_requests_local = true
|
@@ -1,5 +1,5 @@
|
|
1
|
-
class AddUpdateableViewsInheritance < ActiveRecord::Migration
|
2
|
-
def
|
1
|
+
class AddUpdateableViewsInheritance < ActiveRecord::Migration[4.2]
|
2
|
+
def up
|
3
3
|
create_table(:updateable_views_inheritance, :id => false) do |t|
|
4
4
|
t.column :parent_relation, :string
|
5
5
|
t.column :child_aggregate_view, :string
|
@@ -1,11 +1,11 @@
|
|
1
|
-
class CreateWithExplicitTable < ActiveRecord::Migration
|
2
|
-
def
|
1
|
+
class CreateWithExplicitTable < ActiveRecord::Migration[4.2]
|
2
|
+
def up
|
3
3
|
create_child(:electric_locomotives, :table => :raw_electric_locomotives, :parent => :locomotives) do |t|
|
4
4
|
t.decimal :electricity_consumption, :precision => 6, :scale => 2
|
5
5
|
end
|
6
6
|
end
|
7
|
-
|
7
|
+
|
8
8
|
def self.down
|
9
9
|
drop_child :electric_locomotives
|
10
10
|
end
|
11
|
-
end
|
11
|
+
end
|
@@ -1,10 +1,10 @@
|
|
1
|
-
class CreateDeeperHierarchy < ActiveRecord::Migration
|
2
|
-
def
|
1
|
+
class CreateDeeperHierarchy < ActiveRecord::Migration[4.2]
|
2
|
+
def up
|
3
3
|
create_child(:maglev_locomotives, :parent => :electric_locomotives) do |t|
|
4
4
|
t.column :magnetic_field, :integer
|
5
5
|
end
|
6
6
|
end
|
7
|
-
|
7
|
+
|
8
8
|
def self.down
|
9
9
|
drop_child :maglev_locomotives
|
10
10
|
end
|
@@ -1,13 +1,13 @@
|
|
1
|
-
class DefaultColumnValues < ActiveRecord::Migration
|
2
|
-
def
|
1
|
+
class DefaultColumnValues < ActiveRecord::Migration[4.2]
|
2
|
+
def up
|
3
3
|
create_child(:rack_locomotives, :parent => :locomotives) do |t|
|
4
4
|
t.column :bidirectional, :boolean, :default => false
|
5
5
|
t.column :narrow_gauge, :boolean, :default => true
|
6
6
|
t.column :rail_system, :string, :default => 'Abt'
|
7
7
|
end
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
def self.down
|
11
11
|
drop_child :rack_locomotives
|
12
12
|
end
|
13
|
-
end
|
13
|
+
end
|
@@ -1,5 +1,5 @@
|
|
1
|
-
class SingleTableInheritanceView < ActiveRecord::Migration
|
2
|
-
def
|
1
|
+
class SingleTableInheritanceView < ActiveRecord::Migration[4.2]
|
2
|
+
def up
|
3
3
|
rebuild_parent_and_children_views(:locomotives)
|
4
4
|
create_single_table_inheritance_view(:all_locomotives,:locomotives)
|
5
5
|
end
|
@@ -7,4 +7,4 @@ class SingleTableInheritanceView < ActiveRecord::Migration
|
|
7
7
|
def self.down
|
8
8
|
drop_view(:all_locomotives)
|
9
9
|
end
|
10
|
-
end
|
10
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
class SecondDeepHierarchy < ActiveRecord::Migration
|
1
|
+
class SecondDeepHierarchy < ActiveRecord::Migration[4.2]
|
2
2
|
# Create tables and views for the following inheritance hierarchy:
|
3
3
|
#
|
4
4
|
# vehicles
|
@@ -13,37 +13,37 @@ class SecondDeepHierarchy < ActiveRecord::Migration
|
|
13
13
|
# steam_trains rack_trains electric_trains
|
14
14
|
# |
|
15
15
|
# maglev_trains
|
16
|
-
def
|
16
|
+
def up
|
17
17
|
create_table :vehicles do |t|
|
18
18
|
t.column :name, :string
|
19
19
|
t.column :vehicle_type, :string
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
create_child(:wheeled_vehicles, :parent => :vehicles) do |t|
|
23
23
|
t.column :number_of_wheels, :integer
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
create_child(:bicycles, :parent => :wheeled_vehicles) do |t|
|
27
27
|
t.column :number_of_gears, :integer
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
create_child(:cars, :parent => :wheeled_vehicles) do |t|
|
31
31
|
t.column :number_of_doors, :integer
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
create_child(:railed_vehicles, :parent => :vehicles) do |t|
|
35
35
|
t.column :number_of_rails, :integer
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
create_child(:trains, :parent => :railed_vehicles) do |t|
|
39
39
|
t.column :max_speed, :integer
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
create_child(:steam_trains, :parent => :trains) do |t|
|
43
43
|
t.column :water_consumption, :decimal, :precision => 6, :scale => 2
|
44
44
|
t.column :coal_consumption, :decimal, :precision => 6, :scale => 2
|
45
45
|
end
|
46
|
-
|
46
|
+
|
47
47
|
create_child(:rack_trains, :parent => :trains) do |t|
|
48
48
|
t.column :bidirectional, :boolean, :default => false
|
49
49
|
t.column :narrow_gauge, :boolean, :default => true
|
@@ -53,7 +53,7 @@ class SecondDeepHierarchy < ActiveRecord::Migration
|
|
53
53
|
create_child(:electric_trains, :parent => :trains) do |t|
|
54
54
|
t.column :electricity_consumption, :decimal, :precision => 6, :scale => 2
|
55
55
|
end
|
56
|
-
|
56
|
+
|
57
57
|
create_child(:maglev_trains, :parent => :electric_trains) do |t|
|
58
58
|
t.column :magnetic_field, :integer
|
59
59
|
end
|
@@ -62,9 +62,9 @@ class SecondDeepHierarchy < ActiveRecord::Migration
|
|
62
62
|
t.column :mast_number, :integer
|
63
63
|
end
|
64
64
|
end
|
65
|
-
|
65
|
+
|
66
66
|
def self.down
|
67
67
|
%w(boats maglev_trains electric_trains steam_trains rack_trains trains railed_vehicles cars bicycles wheeled_vehicles).each{|child| drop_child(child)}
|
68
68
|
drop_table :vehicles
|
69
69
|
end
|
70
|
-
end
|
70
|
+
end
|
@@ -1,5 +1,5 @@
|
|
1
|
-
class SecondSingleTableInheritanceView < ActiveRecord::Migration
|
2
|
-
def
|
1
|
+
class SecondSingleTableInheritanceView < ActiveRecord::Migration[4.2]
|
2
|
+
def up
|
3
3
|
rebuild_parent_and_children_views(:vehicles)
|
4
4
|
create_single_table_inheritance_view(:all_vehicles,:vehicles)
|
5
5
|
end
|
@@ -7,4 +7,4 @@ class SecondSingleTableInheritanceView < ActiveRecord::Migration
|
|
7
7
|
def self.down
|
8
8
|
drop_view(:all_vehicles)
|
9
9
|
end
|
10
|
-
end
|
10
|
+
end
|
data/test/instantiation_test.rb
CHANGED
@@ -3,11 +3,12 @@ require_relative 'test_helper'
|
|
3
3
|
class InstantiationTest < ActiveSupport::TestCase
|
4
4
|
def setup
|
5
5
|
ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 7)
|
6
|
+
|
7
|
+
ActiveRecord::FixtureSet.reset_cache
|
6
8
|
# order of fixtures is important for the test - last loaded should not be with max(id)
|
7
9
|
%w[steam_locomotives electric_locomotives maglev_locomotives bicycles].each do |f|
|
8
10
|
ActiveRecord::FixtureSet.create_fixtures(File.dirname(__FILE__) + '/fixtures/', f)
|
9
11
|
end
|
10
|
-
@connection = ActiveRecord::Base.connection
|
11
12
|
|
12
13
|
Locomotive.disable_inheritance_instantiation = true
|
13
14
|
ElectricLocomotive.disable_inheritance_instantiation = false
|
@@ -15,7 +16,6 @@ class InstantiationTest < ActiveSupport::TestCase
|
|
15
16
|
|
16
17
|
def teardown
|
17
18
|
Locomotive.disable_inheritance_instantiation = false
|
18
|
-
ActiveRecord::FixtureSet.reset_cache
|
19
19
|
end
|
20
20
|
|
21
21
|
# FIXME: flaky test_setting_disable_inheritance_instantiation_does_not_load_child_columns
|
data/test/migration_test.rb
CHANGED
@@ -22,7 +22,8 @@ class UpdateableViewsInheritanceMigrationTest < ActiveSupport::TestCase
|
|
22
22
|
ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 3)
|
23
23
|
ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/', 2)
|
24
24
|
assert_equal %w(steam_locomotives), @connection.views.sort
|
25
|
-
assert_equal %w(
|
25
|
+
assert_equal %w(ar_internal_metadata
|
26
|
+
locomotives
|
26
27
|
schema_migrations
|
27
28
|
steam_locomotives_data
|
28
29
|
updateable_views_inheritance), @connection.tables.sort
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require_relative 'test_helper'
|
2
|
+
|
3
|
+
class InsertReturningOnViewWithRulesAndDefaultValue < ActiveSupport::TestCase
|
4
|
+
def setup
|
5
|
+
@conn = ActiveRecord::Base.connection.raw_connection
|
6
|
+
@conn.exec("SET client_min_messages TO 'ERROR'")
|
7
|
+
|
8
|
+
@conn.exec(<<-SQL.squish)
|
9
|
+
CREATE TABLE parent (
|
10
|
+
id SERIAL PRIMARY KEY,
|
11
|
+
name TEXT
|
12
|
+
)
|
13
|
+
SQL
|
14
|
+
|
15
|
+
@conn.exec(<<-SQL.squish)
|
16
|
+
CREATE TABLE child (
|
17
|
+
parent_id INTEGER PRIMARY KEY REFERENCES parent(id),
|
18
|
+
surname TEXT
|
19
|
+
)
|
20
|
+
SQL
|
21
|
+
|
22
|
+
@conn.exec(<<-SQL.squish)
|
23
|
+
CREATE VIEW v AS (
|
24
|
+
SELECT id, name, surname
|
25
|
+
FROM parent JOIN child ON parent.id=child.parent_id
|
26
|
+
)
|
27
|
+
SQL
|
28
|
+
|
29
|
+
@conn.exec(<<-SQL.squish)
|
30
|
+
ALTER VIEW v ALTER id SET DEFAULT nextval('parent_id_seq'::regclass)
|
31
|
+
SQL
|
32
|
+
|
33
|
+
@conn.exec(<<-SQL.squish)
|
34
|
+
CREATE RULE v_on_insert AS ON INSERT TO v DO INSTEAD (
|
35
|
+
INSERT INTO parent (id, name)
|
36
|
+
VALUES( DEFAULT, NEW.name );
|
37
|
+
INSERT INTO child (parent_id, surname)
|
38
|
+
VALUES( currval('parent_id_seq'), NEW.surname ) RETURNING parent_id, NULL::text, NULL::text;
|
39
|
+
)
|
40
|
+
SQL
|
41
|
+
|
42
|
+
@sql = "INSERT INTO v (name, surname) VALUES ('parent', 'child') RETURNING id"
|
43
|
+
end
|
44
|
+
|
45
|
+
def teardown
|
46
|
+
@conn.exec("DROP VIEW IF EXISTS v")
|
47
|
+
@conn.exec("DROP TABLE IF EXISTS parent CASCADE")
|
48
|
+
@conn.exec("DROP TABLE IF EXISTS child CASCADE")
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_async_exec_with_empty_binds
|
52
|
+
res = @conn.async_exec(@sql, [])
|
53
|
+
assert_equal [[1]], res.values
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_async_exec_with_no_binds
|
57
|
+
res = @conn.async_exec(@sql)
|
58
|
+
assert_equal [[1]], res.values
|
59
|
+
end
|
60
|
+
end
|
data/test/schema_test.rb
CHANGED
@@ -12,8 +12,8 @@ class SchemaTest < ActiveSupport::TestCase
|
|
12
12
|
assert_equal 'public.locomotives_id_seq', seq.to_s
|
13
13
|
end
|
14
14
|
|
15
|
-
class CreateChildInSchemaWithPublicParent < ActiveRecord::Migration
|
16
|
-
def
|
15
|
+
class CreateChildInSchemaWithPublicParent < ActiveRecord::Migration[4.2]
|
16
|
+
def up
|
17
17
|
execute "CREATE SCHEMA interrail"
|
18
18
|
create_child('interrail.steam_locomotives', parent: 'locomotives') do |t|
|
19
19
|
t.decimal :interrail_water_consumption, precision: 6, scale: 2
|
@@ -23,14 +23,14 @@ class SchemaTest < ActiveSupport::TestCase
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def test_pk_and_sequence_for_child_and_parent_in_different_schemas
|
26
|
-
CreateChildInSchemaWithPublicParent.up
|
26
|
+
CreateChildInSchemaWithPublicParent.new.up
|
27
27
|
pk, seq = @connection.pk_and_sequence_for('interrail.steam_locomotives')
|
28
28
|
assert_equal 'id', pk
|
29
29
|
assert_equal 'public.locomotives_id_seq', seq.to_s
|
30
30
|
end
|
31
31
|
|
32
|
-
class CreateChildInSchemaWithParentInSchema < ActiveRecord::Migration
|
33
|
-
def
|
32
|
+
class CreateChildInSchemaWithParentInSchema < ActiveRecord::Migration[4.2]
|
33
|
+
def up
|
34
34
|
execute "CREATE SCHEMA interrail"
|
35
35
|
create_table 'interrail.locomotives' do |t|
|
36
36
|
t.column :interrail_name, :string
|
@@ -45,7 +45,7 @@ class SchemaTest < ActiveSupport::TestCase
|
|
45
45
|
end
|
46
46
|
|
47
47
|
def test_pk_and_sequence_for_child_and_parent_in_same_nonpublic_schema
|
48
|
-
CreateChildInSchemaWithParentInSchema.up
|
48
|
+
CreateChildInSchemaWithParentInSchema.new.up
|
49
49
|
pk, seq = @connection.pk_and_sequence_for('interrail.steam_locomotives')
|
50
50
|
assert_equal 'id', pk
|
51
51
|
assert_equal 'interrail.locomotives_id_seq', seq.to_s
|
@@ -64,8 +64,8 @@ class SchemaTest < ActiveSupport::TestCase
|
|
64
64
|
@connection.views.sort
|
65
65
|
end
|
66
66
|
|
67
|
-
class ParentTableWithOnlyOneColumn < ActiveRecord::Migration
|
68
|
-
def
|
67
|
+
class ParentTableWithOnlyOneColumn < ActiveRecord::Migration[4.2]
|
68
|
+
def up
|
69
69
|
create_table(:parent_pk_only){}
|
70
70
|
create_table :child_data do |t|
|
71
71
|
t.column :name, :string
|
@@ -80,15 +80,15 @@ class SchemaTest < ActiveSupport::TestCase
|
|
80
80
|
end
|
81
81
|
|
82
82
|
def test_parent_table_with_only_one_column
|
83
|
-
ParentTableWithOnlyOneColumn.up
|
83
|
+
ParentTableWithOnlyOneColumn.new.up
|
84
84
|
assert @connection.views.include?('child')
|
85
85
|
assert_equal %w(id name), @connection.columns(:child).map{|c| c.name}.sort
|
86
86
|
ensure
|
87
87
|
ParentTableWithOnlyOneColumn.down
|
88
88
|
end
|
89
89
|
|
90
|
-
class ChildTableWithOnlyOneColumn < ActiveRecord::Migration
|
91
|
-
def
|
90
|
+
class ChildTableWithOnlyOneColumn < ActiveRecord::Migration[4.2]
|
91
|
+
def up
|
92
92
|
create_table :parent do |t|
|
93
93
|
t.column :name, :string
|
94
94
|
end
|
@@ -98,7 +98,7 @@ class SchemaTest < ActiveSupport::TestCase
|
|
98
98
|
end
|
99
99
|
|
100
100
|
def test_child_table_with_only_one_column
|
101
|
-
ChildTableWithOnlyOneColumn.up
|
101
|
+
ChildTableWithOnlyOneColumn.new.up
|
102
102
|
assert @connection.views.include?('child_pk_only'), "Could not create child view when child table has only one column"
|
103
103
|
assert_equal %w(id name), @connection.columns(:child_pk_only).map{|c| c.name}.sort
|
104
104
|
end
|
@@ -132,8 +132,8 @@ class SchemaTest < ActiveSupport::TestCase
|
|
132
132
|
assert SteamLocomotive.columns.find { |c| c.name == 'water_consumption' }.null
|
133
133
|
end
|
134
134
|
|
135
|
-
class ChangeDefaultValueOfColumn < ActiveRecord::Migration
|
136
|
-
def
|
135
|
+
class ChangeDefaultValueOfColumn < ActiveRecord::Migration[4.2]
|
136
|
+
def up
|
137
137
|
remove_parent_and_children_views(:rack_locomotives)
|
138
138
|
change_column_default(:rack_locomotives_data, :rail_system, 'Marsh')
|
139
139
|
rebuild_parent_and_children_views(:rack_locomotives)
|
@@ -141,24 +141,24 @@ class SchemaTest < ActiveSupport::TestCase
|
|
141
141
|
end
|
142
142
|
|
143
143
|
def test_change_default_value_of_column
|
144
|
-
ChangeDefaultValueOfColumn.up
|
144
|
+
ChangeDefaultValueOfColumn.new.up
|
145
145
|
RackLocomotive.reset_column_information
|
146
146
|
assert_equal 'Marsh', RackLocomotive.new.rail_system
|
147
147
|
end
|
148
148
|
|
149
|
-
class RemoveChildrenViews < ActiveRecord::Migration
|
150
|
-
def
|
149
|
+
class RemoveChildrenViews < ActiveRecord::Migration[4.2]
|
150
|
+
def up
|
151
151
|
remove_parent_and_children_views(:locomotives)
|
152
152
|
end
|
153
153
|
end
|
154
154
|
|
155
155
|
def test_remove_parent_and_children_views
|
156
|
-
RemoveChildrenViews.up
|
156
|
+
RemoveChildrenViews.new.up
|
157
157
|
assert @connection.views.empty?
|
158
158
|
end
|
159
159
|
|
160
|
-
class RemoveColumnInParentTable < ActiveRecord::Migration
|
161
|
-
def
|
160
|
+
class RemoveColumnInParentTable < ActiveRecord::Migration[4.2]
|
161
|
+
def up
|
162
162
|
remove_parent_and_children_views(:locomotives)
|
163
163
|
remove_column(:locomotives, :max_speed)
|
164
164
|
rebuild_parent_and_children_views(:locomotives)
|
@@ -166,15 +166,15 @@ class SchemaTest < ActiveSupport::TestCase
|
|
166
166
|
end
|
167
167
|
|
168
168
|
def test_remove_column_parent_table
|
169
|
-
RemoveColumnInParentTable.up
|
169
|
+
RemoveColumnInParentTable.new.up
|
170
170
|
assert_equal %w(coal_consumption id name type water_consumption),
|
171
171
|
@connection.columns(:steam_locomotives).map{ |c| c.name }.sort
|
172
172
|
assert_equal %w(electricity_consumption id magnetic_field name type),
|
173
173
|
@connection.columns(:maglev_locomotives).map{ |c| c.name }.sort
|
174
174
|
end
|
175
175
|
|
176
|
-
class RenameColumnInParentTable < ActiveRecord::Migration
|
177
|
-
def
|
176
|
+
class RenameColumnInParentTable < ActiveRecord::Migration[4.2]
|
177
|
+
def up
|
178
178
|
ActiveRecord::FixtureSet.create_fixtures(File.dirname(__FILE__) + '/fixtures/', :electric_locomotives)
|
179
179
|
ActiveRecord::FixtureSet.create_fixtures(File.dirname(__FILE__) + '/fixtures/', :maglev_locomotives)
|
180
180
|
ActiveRecord::FixtureSet.create_fixtures(File.dirname(__FILE__) + '/fixtures/', :steam_locomotives)
|
@@ -186,7 +186,7 @@ class SchemaTest < ActiveSupport::TestCase
|
|
186
186
|
end
|
187
187
|
|
188
188
|
def test_rename_column_parent_table
|
189
|
-
RenameColumnInParentTable.up
|
189
|
+
RenameColumnInParentTable.new.up
|
190
190
|
assert_equal %w(coal_consumption id maximal_speed name type water_consumption),
|
191
191
|
@connection.columns(:steam_locomotives).map{ |c| c.name }.sort
|
192
192
|
assert_equal %w(electricity_consumption id magnetic_field maximal_speed name type),
|
@@ -194,15 +194,15 @@ class SchemaTest < ActiveSupport::TestCase
|
|
194
194
|
|
195
195
|
end
|
196
196
|
|
197
|
-
class AddColumnToParentTable < ActiveRecord::Migration
|
198
|
-
def
|
197
|
+
class AddColumnToParentTable < ActiveRecord::Migration[4.2]
|
198
|
+
def up
|
199
199
|
add_column(:raw_electric_locomotives, :number_of_engines, :integer)
|
200
200
|
rebuild_parent_and_children_views(:electric_locomotives)
|
201
201
|
end
|
202
202
|
end
|
203
203
|
|
204
204
|
def test_add_column_to_parent_table
|
205
|
-
AddColumnToParentTable.up
|
205
|
+
AddColumnToParentTable.new.up
|
206
206
|
assert_equal %w(electricity_consumption id max_speed name number_of_engines type),
|
207
207
|
@connection.columns(:electric_locomotives).map{ |c| c.name }.sort
|
208
208
|
assert_equal %w(electricity_consumption id magnetic_field max_speed name number_of_engines type),
|
@@ -210,8 +210,8 @@ class SchemaTest < ActiveSupport::TestCase
|
|
210
210
|
|
211
211
|
end
|
212
212
|
|
213
|
-
class ChangeChildRelationView < ActiveRecord::Migration
|
214
|
-
def
|
213
|
+
class ChangeChildRelationView < ActiveRecord::Migration[4.2]
|
214
|
+
def up
|
215
215
|
remove_parent_and_children_views(:electric_locomotives)
|
216
216
|
rename_column(:raw_electric_locomotives, :electricity_consumption, :electric_consumption)
|
217
217
|
rebuild_parent_and_children_views(:electric_locomotives)
|
@@ -219,13 +219,13 @@ class SchemaTest < ActiveSupport::TestCase
|
|
219
219
|
end
|
220
220
|
|
221
221
|
def test_change_child_relation_view
|
222
|
-
ChangeChildRelationView.up
|
222
|
+
ChangeChildRelationView.new.up
|
223
223
|
assert_equal %w(electric_consumption id max_speed name type),
|
224
224
|
@connection.columns(:electric_locomotives).map{ |c| c.name }.sort
|
225
225
|
end
|
226
226
|
|
227
|
-
class ChangeColumnInChildTable < ActiveRecord::Migration
|
228
|
-
def
|
227
|
+
class ChangeColumnInChildTable < ActiveRecord::Migration[4.2]
|
228
|
+
def up
|
229
229
|
drop_view(:steam_locomotives)
|
230
230
|
rename_column(:steam_locomotives_data, :coal_consumption, :fuel_consumption)
|
231
231
|
create_child_view(:locomotives, :steam_locomotives)
|
@@ -233,17 +233,13 @@ class SchemaTest < ActiveSupport::TestCase
|
|
233
233
|
end
|
234
234
|
|
235
235
|
def test_change_column_in_child_table
|
236
|
-
ChangeColumnInChildTable.up
|
236
|
+
ChangeColumnInChildTable.new.up
|
237
237
|
assert_equal %w(fuel_consumption id max_speed name type water_consumption),
|
238
238
|
@connection.columns(:steam_locomotives).map(&:name).sort
|
239
239
|
end
|
240
240
|
|
241
|
-
|
242
|
-
|
243
|
-
end
|
244
|
-
|
245
|
-
class CreateChildInSchema < ActiveRecord::Migration
|
246
|
-
def self.up
|
241
|
+
class CreateChildInSchema < ActiveRecord::Migration[4.2]
|
242
|
+
def up
|
247
243
|
execute "CREATE SCHEMA interrail"
|
248
244
|
create_table 'interrail.locomotives' do |t|
|
249
245
|
t.column :interrail_name, :string
|
@@ -258,7 +254,7 @@ class SchemaTest < ActiveSupport::TestCase
|
|
258
254
|
end
|
259
255
|
|
260
256
|
def test_create_child_in_schema
|
261
|
-
CreateChildInSchema.up
|
257
|
+
CreateChildInSchema.new.up
|
262
258
|
assert_equal %w[id
|
263
259
|
interrail_coal_consumption
|
264
260
|
interrail_max_speed
|
@@ -268,8 +264,8 @@ class SchemaTest < ActiveSupport::TestCase
|
|
268
264
|
@connection.columns('interrail.steam_locomotives').map(&:name).sort
|
269
265
|
end
|
270
266
|
|
271
|
-
class ChangeTablesInTwoInheritanceChains < ActiveRecord::Migration
|
272
|
-
def
|
267
|
+
class ChangeTablesInTwoInheritanceChains < ActiveRecord::Migration[4.2]
|
268
|
+
def up
|
273
269
|
add_column(:maglev_locomotives_data, :levitation_height, :integer)
|
274
270
|
add_column(:bicycles_data, :wheel_size, :integer)
|
275
271
|
rebuild_all_parent_and_children_views
|
@@ -279,7 +275,7 @@ class SchemaTest < ActiveSupport::TestCase
|
|
279
275
|
def test_rebuild_all_parent_and_children_views
|
280
276
|
ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 7)
|
281
277
|
@connection.execute "DROP VIEW all_locomotives" #FIXME: single table inheritance view should be rebuilt as well
|
282
|
-
ChangeTablesInTwoInheritanceChains.up
|
278
|
+
ChangeTablesInTwoInheritanceChains.new.up
|
283
279
|
|
284
280
|
assert @connection.columns(:maglev_locomotives).map{ |c| c.name }.include?('levitation_height'),
|
285
281
|
"Newly added column not present in view after rebuild for 1. hierarchy"
|
@@ -287,8 +283,8 @@ class SchemaTest < ActiveSupport::TestCase
|
|
287
283
|
"Newly added column not present in view after rebuild for 2. hierarchy"
|
288
284
|
end
|
289
285
|
|
290
|
-
class UseExistingTable < ActiveRecord::Migration
|
291
|
-
def
|
286
|
+
class UseExistingTable < ActiveRecord::Migration[4.2]
|
287
|
+
def up
|
292
288
|
create_table :tbl_diesel_locomotives do |t|
|
293
289
|
t.belongs_to :locomotives
|
294
290
|
t.integer :num_cylinders
|
@@ -301,12 +297,12 @@ class SchemaTest < ActiveSupport::TestCase
|
|
301
297
|
end
|
302
298
|
|
303
299
|
def test_skip_creating_child_table
|
304
|
-
UseExistingTable.up
|
300
|
+
UseExistingTable.new.up
|
305
301
|
assert @connection.columns(:diesel_locomotives).map(&:name).include?("num_cylinders")
|
306
302
|
end
|
307
303
|
|
308
|
-
class ReservedSQLWords < ActiveRecord::Migration
|
309
|
-
def
|
304
|
+
class ReservedSQLWords < ActiveRecord::Migration[4.2]
|
305
|
+
def up
|
310
306
|
create_child(:table, parent: :locomotives) do |t|
|
311
307
|
t.integer :column
|
312
308
|
end
|
@@ -317,14 +313,14 @@ class SchemaTest < ActiveSupport::TestCase
|
|
317
313
|
end
|
318
314
|
|
319
315
|
def test_reserved_words_in_tables_and_columns
|
320
|
-
ReservedSQLWords.up
|
316
|
+
ReservedSQLWords.new.up
|
321
317
|
assert @connection.columns(:table).map(&:name).include?("column")
|
322
318
|
ensure
|
323
319
|
ReservedSQLWords.down
|
324
320
|
end
|
325
321
|
|
326
|
-
class ChildTableIsActuallyView < ActiveRecord::Migration
|
327
|
-
def
|
322
|
+
class ChildTableIsActuallyView < ActiveRecord::Migration[4.2]
|
323
|
+
def up
|
328
324
|
execute <<-SQL.squish
|
329
325
|
CREATE VIEW punk_locomotives_data AS (
|
330
326
|
SELECT steam_locomotives.id,
|
@@ -346,7 +342,7 @@ class SchemaTest < ActiveSupport::TestCase
|
|
346
342
|
end
|
347
343
|
|
348
344
|
def test_child_table_is_view
|
349
|
-
ChildTableIsActuallyView.up
|
345
|
+
ChildTableIsActuallyView.new.up
|
350
346
|
assert_equal @connection.columns(:punk_locomotives).map(&:name).sort,
|
351
347
|
%w(coal electro id max_speed name type)
|
352
348
|
end
|
@@ -30,8 +30,8 @@ class SingleTableInheritanceAggregateViewTest < ActiveSupport::TestCase
|
|
30
30
|
assert_equal electric_locomotive.name, @connection.query("SELECT name FROm all_locomotives WHERE id=#{electric_locomotive.id}").first.first
|
31
31
|
end
|
32
32
|
|
33
|
-
class AddColumnToParentTable < ActiveRecord::Migration
|
34
|
-
def
|
33
|
+
class AddColumnToParentTable < ActiveRecord::Migration[4.2]
|
34
|
+
def up
|
35
35
|
add_column(:raw_electric_locomotives, :number_of_engines, :integer)
|
36
36
|
drop_view(:all_locomotives)
|
37
37
|
rebuild_parent_and_children_views(:electric_locomotives)
|
@@ -40,13 +40,13 @@ class SingleTableInheritanceAggregateViewTest < ActiveSupport::TestCase
|
|
40
40
|
end
|
41
41
|
|
42
42
|
def test_single_table_inheritance_view_add_column_to_parent_table
|
43
|
-
AddColumnToParentTable.up
|
43
|
+
AddColumnToParentTable.new.up
|
44
44
|
assert_equal %w(coal_consumption id max_speed name type water_consumption electricity_consumption bidirectional narrow_gauge magnetic_field rail_system number_of_engines).sort,
|
45
45
|
@connection.columns(:all_locomotives).map{ |c| c.name }.sort
|
46
46
|
end
|
47
47
|
|
48
|
-
class RemoveColumnInParentTable < ActiveRecord::Migration
|
49
|
-
def
|
48
|
+
class RemoveColumnInParentTable < ActiveRecord::Migration[4.2]
|
49
|
+
def up
|
50
50
|
drop_view(:all_locomotives)
|
51
51
|
remove_parent_and_children_views(:locomotives)
|
52
52
|
remove_column(:locomotives, :max_speed)
|
@@ -56,13 +56,13 @@ class SingleTableInheritanceAggregateViewTest < ActiveSupport::TestCase
|
|
56
56
|
end
|
57
57
|
|
58
58
|
def test_single_table_inheritance_view_remove_column_parent_table
|
59
|
-
RemoveColumnInParentTable.up
|
59
|
+
RemoveColumnInParentTable.new.up
|
60
60
|
assert_equal %w(coal_consumption id name type water_consumption electricity_consumption bidirectional narrow_gauge magnetic_field rail_system).sort,
|
61
61
|
@connection.columns(:all_locomotives).map{ |c| c.name }.sort
|
62
62
|
end
|
63
63
|
|
64
|
-
class RenameColumnInParentTable < ActiveRecord::Migration
|
65
|
-
def
|
64
|
+
class RenameColumnInParentTable < ActiveRecord::Migration[4.2]
|
65
|
+
def up
|
66
66
|
drop_view(:all_locomotives)
|
67
67
|
remove_parent_and_children_views(:locomotives)
|
68
68
|
rename_column(:locomotives, :max_speed, :maximal_speed)
|
@@ -72,13 +72,13 @@ class SingleTableInheritanceAggregateViewTest < ActiveSupport::TestCase
|
|
72
72
|
end
|
73
73
|
|
74
74
|
def test_single_table_inheritance_view_rename_column_parent_table
|
75
|
-
RenameColumnInParentTable.up
|
75
|
+
RenameColumnInParentTable.new.up
|
76
76
|
assert_equal %w(coal_consumption id maximal_speed name type water_consumption electricity_consumption bidirectional narrow_gauge magnetic_field rail_system).sort,
|
77
77
|
@connection.columns(:all_locomotives).map{ |c| c.name }.sort
|
78
78
|
end
|
79
79
|
|
80
|
-
class ChangeChildRelationView < ActiveRecord::Migration
|
81
|
-
def
|
80
|
+
class ChangeChildRelationView < ActiveRecord::Migration[4.2]
|
81
|
+
def up
|
82
82
|
drop_view(:all_locomotives)
|
83
83
|
remove_parent_and_children_views(:electric_locomotives)
|
84
84
|
rename_column(:raw_electric_locomotives, :electricity_consumption, :electric_consumption)
|
@@ -88,13 +88,13 @@ class SingleTableInheritanceAggregateViewTest < ActiveSupport::TestCase
|
|
88
88
|
end
|
89
89
|
|
90
90
|
def test_single_table_inheritance_view_change_child_relation_view
|
91
|
-
ChangeChildRelationView.up
|
91
|
+
ChangeChildRelationView.new.up
|
92
92
|
assert_equal %w(coal_consumption id max_speed name type water_consumption electric_consumption bidirectional narrow_gauge magnetic_field rail_system).sort,
|
93
93
|
@connection.columns(:all_locomotives).map{ |c| c.name }.sort
|
94
94
|
end
|
95
95
|
|
96
|
-
class ConflictColumns < ActiveRecord::Migration
|
97
|
-
def
|
96
|
+
class ConflictColumns < ActiveRecord::Migration[4.2]
|
97
|
+
def up
|
98
98
|
drop_view(:all_locomotives)
|
99
99
|
add_column(:raw_electric_locomotives, :number_of_engines, :integer)
|
100
100
|
add_column(:steam_locomotives_data, :number_of_engines, :string)
|
@@ -105,14 +105,14 @@ class SingleTableInheritanceAggregateViewTest < ActiveSupport::TestCase
|
|
105
105
|
end
|
106
106
|
|
107
107
|
def test_single_table_inheritance_view_conflict_columns
|
108
|
-
ConflictColumns.up
|
108
|
+
ConflictColumns.new.up
|
109
109
|
assert_equal %w(coal_consumption id max_speed name type water_consumption electricity_consumption bidirectional narrow_gauge magnetic_field rail_system number_of_engines).sort,
|
110
110
|
@connection.columns(:all_locomotives).map{ |c| c.name }.sort
|
111
111
|
assert_equal 'text', @connection.columns(:all_locomotives).detect{|c| c.name == "number_of_engines"}.sql_type
|
112
112
|
end
|
113
113
|
|
114
|
-
class ConflictColumnsWithValues < ActiveRecord::Migration
|
115
|
-
def
|
114
|
+
class ConflictColumnsWithValues < ActiveRecord::Migration[4.2]
|
115
|
+
def up
|
116
116
|
add_column(:raw_electric_locomotives, :number_of_engines, :integer)
|
117
117
|
add_column(:steam_locomotives_data, :number_of_engines, :string)
|
118
118
|
execute("UPDATE raw_electric_locomotives SET number_of_engines = 2")
|
@@ -126,7 +126,7 @@ class SingleTableInheritanceAggregateViewTest < ActiveSupport::TestCase
|
|
126
126
|
|
127
127
|
# FIXME: flaky test_single_table_inheritance_view_conflict_columns_with_values
|
128
128
|
# def test_single_table_inheritance_view_conflict_columns_with_values
|
129
|
-
# ConflictColumnsWithValues.up
|
129
|
+
# ConflictColumnsWithValues.new.up
|
130
130
|
# ::SteamLocomotive.reset_column_information
|
131
131
|
# ::ElectricLocomotive.reset_column_information
|
132
132
|
# assert_equal %w(coal_consumption id max_speed name type water_consumption electricity_consumption bidirectional narrow_gauge magnetic_field rail_system number_of_engines).sort,
|
@@ -18,14 +18,14 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
19
19
|
s.require_paths = ["lib"]
|
20
20
|
|
21
|
-
s.add_dependency "activerecord", "~>
|
22
|
-
s.add_dependency "pg"
|
21
|
+
s.add_dependency "activerecord", "~> 5.1.7"
|
22
|
+
s.add_dependency "pg"
|
23
23
|
|
24
24
|
s.add_development_dependency 'bigdecimal', '1.3.5'
|
25
25
|
s.add_development_dependency "bundler"
|
26
26
|
s.add_development_dependency "minitest"
|
27
27
|
s.add_development_dependency "minitest-reporters"
|
28
|
-
s.add_development_dependency "rails", '
|
28
|
+
s.add_development_dependency "rails", '~> 5.1.7'
|
29
29
|
s.add_development_dependency "rake"
|
30
30
|
s.add_development_dependency "simplecov"
|
31
31
|
s.add_development_dependency "simplecov_json_formatter"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: updateable_views_inheritance
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sava Chankov
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2025-02-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -17,28 +17,28 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - "~>"
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version:
|
20
|
+
version: 5.1.7
|
21
21
|
type: :runtime
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
25
|
- - "~>"
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version:
|
27
|
+
version: 5.1.7
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: pg
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
31
31
|
requirements:
|
32
|
-
- - "
|
32
|
+
- - ">="
|
33
33
|
- !ruby/object:Gem::Version
|
34
|
-
version: '0
|
34
|
+
version: '0'
|
35
35
|
type: :runtime
|
36
36
|
prerelease: false
|
37
37
|
version_requirements: !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
39
|
-
- - "
|
39
|
+
- - ">="
|
40
40
|
- !ruby/object:Gem::Version
|
41
|
-
version: '0
|
41
|
+
version: '0'
|
42
42
|
- !ruby/object:Gem::Dependency
|
43
43
|
name: bigdecimal
|
44
44
|
requirement: !ruby/object:Gem::Requirement
|
@@ -99,16 +99,16 @@ dependencies:
|
|
99
99
|
name: rails
|
100
100
|
requirement: !ruby/object:Gem::Requirement
|
101
101
|
requirements:
|
102
|
-
- -
|
102
|
+
- - "~>"
|
103
103
|
- !ruby/object:Gem::Version
|
104
|
-
version:
|
104
|
+
version: 5.1.7
|
105
105
|
type: :development
|
106
106
|
prerelease: false
|
107
107
|
version_requirements: !ruby/object:Gem::Requirement
|
108
108
|
requirements:
|
109
|
-
- -
|
109
|
+
- - "~>"
|
110
110
|
- !ruby/object:Gem::Version
|
111
|
-
version:
|
111
|
+
version: 5.1.7
|
112
112
|
- !ruby/object:Gem::Dependency
|
113
113
|
name: rake
|
114
114
|
requirement: !ruby/object:Gem::Requirement
|
@@ -187,7 +187,7 @@ files:
|
|
187
187
|
- lib/generators/updateable_views_inheritance/install_generator.rb
|
188
188
|
- lib/generators/updateable_views_inheritance/templates/create_updateable_views_inheritance.rb
|
189
189
|
- lib/updateable_views_inheritance.rb
|
190
|
-
- lib/updateable_views_inheritance/
|
190
|
+
- lib/updateable_views_inheritance/active_record_extensions.rb
|
191
191
|
- lib/updateable_views_inheritance/postgresql_adapter.rb
|
192
192
|
- lib/updateable_views_inheritance/version.rb
|
193
193
|
- tasks/updateable_views_inheritance_tasks.rake
|
@@ -261,7 +261,7 @@ files:
|
|
261
261
|
- test/install_generator_test.rb
|
262
262
|
- test/instantiation_test.rb
|
263
263
|
- test/migration_test.rb
|
264
|
-
- test/
|
264
|
+
- test/pg_insert_returning_with_rules_test.rb
|
265
265
|
- test/schema_test.rb
|
266
266
|
- test/single_table_inheritance_test.rb
|
267
267
|
- test/test_helper.rb
|
@@ -360,7 +360,7 @@ test_files:
|
|
360
360
|
- test/install_generator_test.rb
|
361
361
|
- test/instantiation_test.rb
|
362
362
|
- test/migration_test.rb
|
363
|
-
- test/
|
363
|
+
- test/pg_insert_returning_with_rules_test.rb
|
364
364
|
- test/schema_test.rb
|
365
365
|
- test/single_table_inheritance_test.rb
|
366
366
|
- test/test_helper.rb
|
@@ -1,18 +0,0 @@
|
|
1
|
-
module ActiveRecord #:nodoc:
|
2
|
-
class Base #:nodoc:
|
3
|
-
class << self
|
4
|
-
attr_accessor :disable_inheritance_instantiation
|
5
|
-
|
6
|
-
private
|
7
|
-
def instantiate_with_updateable_views_inheritance_support(attributes, column_types = {})
|
8
|
-
object = instantiate_without_updateable_views_inheritance_support(attributes, column_types = {})
|
9
|
-
if object.class.name == self.name || self.disable_inheritance_instantiation
|
10
|
-
object
|
11
|
-
else
|
12
|
-
object.class.find(attributes.with_indifferent_access[:id])
|
13
|
-
end
|
14
|
-
end
|
15
|
-
alias_method_chain :instantiate, :updateable_views_inheritance_support
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
@@ -1,58 +0,0 @@
|
|
1
|
-
gem "pg", "0.18.2"
|
2
|
-
|
3
|
-
require "pg"
|
4
|
-
require "rspec"
|
5
|
-
|
6
|
-
describe "Insert returning on view with rules and default value" do
|
7
|
-
before(:each) do
|
8
|
-
@conn = PG.connect(dbname: 'updateable_views_inheritance_test')
|
9
|
-
@conn.exec(%q{ SET client_min_messages TO 'ERROR' })
|
10
|
-
|
11
|
-
@conn.exec(%q{ CREATE TABLE parent ( id SERIAL PRIMARY KEY,
|
12
|
-
name TEXT) })
|
13
|
-
@conn.exec(%q{ CREATE TABLE child ( parent_id INTEGER PRIMARY KEY REFERENCES parent(id),
|
14
|
-
surname TEXT) })
|
15
|
-
|
16
|
-
@conn.exec(%q{ CREATE VIEW v AS (SELECT id, name, surname FROM parent JOIN child ON parent.id=child.parent_id) })
|
17
|
-
@conn.exec(%q{ ALTER VIEW v ALTER id SET DEFAULT nextval('parent_id_seq'::regclass) })
|
18
|
-
#
|
19
|
-
# The old way that didn't return anything when binds are empty
|
20
|
-
#
|
21
|
-
# @conn.exec(%q{ CREATE RULE v_on_insert AS ON INSERT TO v DO INSTEAD
|
22
|
-
# (
|
23
|
-
# SELECT setval('parent_id_seq', NEW.id);
|
24
|
-
# INSERT INTO parent (id, name)
|
25
|
-
# VALUES( currval('parent_id_seq'), NEW.name ) RETURNING id, name, NULL::text;
|
26
|
-
# INSERT INTO child (parent_id, surname)
|
27
|
-
# VALUES( currval('parent_id_seq'), NEW.surname );
|
28
|
-
#
|
29
|
-
# )
|
30
|
-
# })
|
31
|
-
@conn.exec(%q{ CREATE RULE v_on_insert AS ON INSERT TO v DO INSTEAD
|
32
|
-
(
|
33
|
-
INSERT INTO parent (id, name)
|
34
|
-
VALUES( DEFAULT, NEW.name );
|
35
|
-
INSERT INTO child (parent_id, surname)
|
36
|
-
VALUES( currval('parent_id_seq'), NEW.surname ) RETURNING parent_id, NULL::text, NULL::te;
|
37
|
-
)
|
38
|
-
})
|
39
|
-
|
40
|
-
@sql = %q{ INSERT INTO v (name, surname) VALUES ('parent', 'child') RETURNING id}
|
41
|
-
end
|
42
|
-
|
43
|
-
after(:each) do
|
44
|
-
@conn.exec(%q{ DROP VIEW IF EXISTS v })
|
45
|
-
@conn.exec(%q{ DROP TABLE IF EXISTS parent CASCADE})
|
46
|
-
@conn.exec(%q{ DROP TABLE IF EXISTS child CASCADE})
|
47
|
-
end
|
48
|
-
|
49
|
-
it 'async exec with empty binds' do
|
50
|
-
res = @conn.async_exec(@sql, [])
|
51
|
-
expect(res.values).to eq([["1"]])
|
52
|
-
end
|
53
|
-
|
54
|
-
it 'async exec with no binds' do
|
55
|
-
res = @conn.async_exec(@sql)
|
56
|
-
expect(res.values).to eq([["1"]])
|
57
|
-
end
|
58
|
-
end
|