updateable_views_inheritance 1.4.7 → 1.5.0

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.
Files changed (29) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/build.yml +3 -4
  3. data/CHANGELOG.md +8 -0
  4. data/lib/updateable_views_inheritance/active_record_extensions.rb +22 -0
  5. data/lib/updateable_views_inheritance/postgresql_adapter.rb +14 -19
  6. data/lib/updateable_views_inheritance/version.rb +1 -1
  7. data/lib/updateable_views_inheritance.rb +1 -1
  8. data/test/deep_hierarchy_test.rb +4 -6
  9. data/test/dummy/app/models/locomotive.rb +0 -1
  10. data/test/dummy/app/models/vehicle.rb +1 -2
  11. data/test/dummy/config/environments/test.rb +2 -2
  12. data/test/fixtures/migrations/1_add_updateable_views_inheritance.rb +1 -1
  13. data/test/fixtures/migrations/2_create_with_default_table.rb +1 -1
  14. data/test/fixtures/migrations/3_create_with_explicit_table.rb +3 -3
  15. data/test/fixtures/migrations/4_create_deeper_hierarchy.rb +2 -2
  16. data/test/fixtures/migrations/5_default_column_values.rb +3 -3
  17. data/test/fixtures/migrations/6_single_table_inheritance_view.rb +2 -2
  18. data/test/fixtures/migrations/7_second_deep_hierarchy.rb +11 -11
  19. data/test/fixtures/migrations/8_second_single_table_inheritance_view.rb +2 -2
  20. data/test/instantiation_test.rb +2 -2
  21. data/test/migration_test.rb +2 -1
  22. data/test/pg_insert_returning_with_rules_test.rb +60 -0
  23. data/test/schema_test.rb +56 -19
  24. data/test/single_table_inheritance_test.rb +6 -6
  25. data/test/test_helper.rb +3 -0
  26. data/updateable_views_inheritance.gemspec +5 -4
  27. metadata +16 -16
  28. data/lib/updateable_views_inheritance/active_record.rb +0 -18
  29. 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: 661b8ded59885a6e482cac9d0dfec5f9e1ab10050e3944b5f176e543958f8a33
4
- data.tar.gz: fa8e6f63a74a28f9a09d1574ec6e3ae788df2ebdf3eead5789ef691c40337aed
3
+ metadata.gz: 2f91d01980fe8925967bf0ad4372fd1e8fe7dd5ce279221976dde06ab62ae405
4
+ data.tar.gz: ff35d58879a312f046b560a4121bf2c8efe0e3d65cff769ccd90559031a872fd
5
5
  SHA512:
6
- metadata.gz: 1aec9cfeeb1aab4761e2efffc73219d7d9db7b605a8575fa470b5491783882415e51afdb8d7d48536242f0df4d2a448e8e3777c00ef9a65ec089001fb3e5a9e3
7
- data.tar.gz: 46fb1b1a91344c01d55a48f47ef96e02f0d058a7f7781462c22cda18fa7d2a6eaa90f1e8cb94e7c1a03523c6d83d2aa500b43d4a83d011db0549bc65cfa4d9b6
6
+ metadata.gz: 5340d92a39cb5a46c12d6bae63acc02b7f1139f7f52e9b2640ec05efd04bcfb1519559567de6e1ca1f7137526276827d93a0f7d611905376202b989f3c01b703
7
+ data.tar.gz: 29283b534f2fe02e503cecf6569abb3425e99cfce2a2f7180d7fc4147320a6824d60c0392bc5b4b6e436c7e9d3fd90514fd113f9d2f2cb54d5e4f9c41bda9480
@@ -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
- # uses: ruby/setup-ruby@v1
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: 1.17.3 # temporary set until rails 5 upgrade
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 2>/dev/null # disable warnings until ruby 3 upgrade
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
@@ -1,3 +1,11 @@
1
+ ## 1.5.0 (31 January 2025)
2
+
3
+ Upgrade to Rails 5
4
+
5
+ ## 1.4.8 (17 December 2024)
6
+ Bugfixes:
7
+ - Fix pk_and_sequence_for to be interoperable with other AR methods
8
+
1
9
  ## 1.4.7 (06 November 2024)
2
10
 
3
11
  Bugfixes:
@@ -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
+
@@ -126,13 +126,23 @@ module ActiveRecord #:nodoc:
126
126
  AND cons.contype = 'p'
127
127
  AND attr.attnum = ANY(cons.conkey)
128
128
  SQL
129
- if result.blank? #result.empty?
129
+
130
+ if result.nil? or result.empty?
130
131
  parent = parent_table(relation)
131
132
  pk_and_sequence_for(parent) if parent
132
133
  else
133
- # log(result[0], "PK for #{relation}") {}
134
- [result[0], query("SELECT pg_get_serial_sequence('#{relation}', '#{result[0]}') ")[0][0]]
134
+ pk = result[0]
135
+ sequence = query("SELECT pg_get_serial_sequence('#{relation}', '#{result[0]}') ")[0][0]
136
+ if sequence
137
+ # ActiveRecord expects PostgreSQL::Name object as sequence, not a string
138
+ sequence_with_schema = Utils.extract_schema_qualified_name(sequence)
139
+ [pk, sequence_with_schema]
140
+ else
141
+ [pk, nil]
142
+ end
135
143
  end
144
+ rescue
145
+ nil
136
146
  end
137
147
 
138
148
  # Drops a view from the database.
@@ -140,16 +150,6 @@ module ActiveRecord #:nodoc:
140
150
  execute "DROP VIEW #{quote_table_name(name)}"
141
151
  end
142
152
 
143
- # Return the list of all views in the schema search path.
144
- def views(name=nil)
145
- schemas = schema_search_path.split(/,\s*/).map { |p| quote(p) }.join(',')
146
- query(<<~SQL, name).map { |row| row[0] }
147
- SELECT viewname
148
- FROM pg_views
149
- WHERE schemaname IN (#{schemas})
150
- SQL
151
- end
152
-
153
153
  # Checks whether relation +name+ is a view.
154
154
  def is_view?(name)
155
155
  result = query(<<~SQL, name).map { |row| row[0] }
@@ -252,11 +252,6 @@ module ActiveRecord #:nodoc:
252
252
  false
253
253
  end
254
254
 
255
- def table_exists_with_updateable_views_inheritance_support?(name)
256
- is_view?(name) ? true : table_exists_without_updateable_views_inheritance_support?(name)
257
- end
258
- alias_method_chain :table_exists?, :updateable_views_inheritance_support
259
-
260
255
  module Tutuf #:nodoc:
261
256
  class ClassTableReflection
262
257
  class << self
@@ -393,7 +388,7 @@ module ActiveRecord #:nodoc:
393
388
  end
394
389
 
395
390
  def parent_table(relation)
396
- if table_exists?('updateable_views_inheritance')
391
+ if data_source_exists?('updateable_views_inheritance')
397
392
  res = query(<<-SQL, 'Parent relation')[0]
398
393
  SELECT parent_relation
399
394
  FROM updateable_views_inheritance
@@ -1,3 +1,3 @@
1
1
  module UpdateableViewsInheritance
2
- VERSION = "1.4.7"
2
+ VERSION = "1.5.0"
3
3
  end
@@ -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/active_record"
4
+ require "updateable_views_inheritance/active_record_extensions"
5
5
  require 'updateable_views_inheritance/postgresql_adapter'
@@ -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,10 +41,10 @@ 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.to_s, mag.name, mag.number_of_rails.to_s, mag.max_speed.to_s, mag.magnetic_field.to_s, (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)
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
47
+ class OrderColumnsInAggregateView < ActiveRecord::Migration[4.2]
50
48
  def self.up
51
49
  rebuild_single_table_inheritance_view(:all_vehicles,:vehicles, %w(max_speed number_of_wheels id))
52
50
  end
@@ -1,3 +1,2 @@
1
1
  class Locomotive < ActiveRecord::Base
2
- abstract_class = true
3
2
  end
@@ -1,4 +1,3 @@
1
1
  class Vehicle < ActiveRecord::Base
2
- abstract_class = true
3
2
  self.inheritance_column = :vehicle_type
4
- end
3
+ end
@@ -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.serve_static_files = true
12
- config.static_cache_control = "public, max-age=3600"
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,4 +1,4 @@
1
- class AddUpdateableViewsInheritance < ActiveRecord::Migration
1
+ class AddUpdateableViewsInheritance < ActiveRecord::Migration[4.2]
2
2
  def self.up
3
3
  create_table(:updateable_views_inheritance, :id => false) do |t|
4
4
  t.column :parent_relation, :string
@@ -1,4 +1,4 @@
1
- class CreateWithDefaultTable < ActiveRecord::Migration
1
+ class CreateWithDefaultTable < ActiveRecord::Migration[4.2]
2
2
  def self.up
3
3
  create_table :locomotives do |t|
4
4
  t.column :name, :string
@@ -1,11 +1,11 @@
1
- class CreateWithExplicitTable < ActiveRecord::Migration
1
+ class CreateWithExplicitTable < ActiveRecord::Migration[4.2]
2
2
  def self.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
1
+ class CreateDeeperHierarchy < ActiveRecord::Migration[4.2]
2
2
  def self.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,4 +1,4 @@
1
- class DefaultColumnValues < ActiveRecord::Migration
1
+ class DefaultColumnValues < ActiveRecord::Migration[4.2]
2
2
  def self.up
3
3
  create_child(:rack_locomotives, :parent => :locomotives) do |t|
4
4
  t.column :bidirectional, :boolean, :default => false
@@ -6,8 +6,8 @@ class DefaultColumnValues < ActiveRecord::Migration
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,4 +1,4 @@
1
- class SingleTableInheritanceView < ActiveRecord::Migration
1
+ class SingleTableInheritanceView < ActiveRecord::Migration[4.2]
2
2
  def self.up
3
3
  rebuild_parent_and_children_views(:locomotives)
4
4
  create_single_table_inheritance_view(:all_locomotives,:locomotives)
@@ -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
@@ -18,32 +18,32 @@ class SecondDeepHierarchy < ActiveRecord::Migration
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,4 +1,4 @@
1
- class SecondSingleTableInheritanceView < ActiveRecord::Migration
1
+ class SecondSingleTableInheritanceView < ActiveRecord::Migration[4.2]
2
2
  def self.up
3
3
  rebuild_parent_and_children_views(:vehicles)
4
4
  create_single_table_inheritance_view(:all_vehicles,:vehicles)
@@ -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
@@ -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
@@ -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(locomotives
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
@@ -7,7 +7,48 @@ class SchemaTest < ActiveSupport::TestCase
7
7
  end
8
8
 
9
9
  def test_pk_and_sequence_for
10
- assert_equal ['id', 'public.locomotives_id_seq'], @connection.pk_and_sequence_for(:maglev_locomotives), "Could not get pk and sequence for child aggregate view"
10
+ pk, seq = @connection.pk_and_sequence_for(:maglev_locomotives)
11
+ assert_equal 'id', pk
12
+ assert_equal 'public.locomotives_id_seq', seq.to_s
13
+ end
14
+
15
+ class CreateChildInSchemaWithPublicParent < ActiveRecord::Migration[4.2]
16
+ def self.up
17
+ execute "CREATE SCHEMA interrail"
18
+ create_child('interrail.steam_locomotives', parent: 'locomotives') do |t|
19
+ t.decimal :interrail_water_consumption, precision: 6, scale: 2
20
+ t.decimal :interrail_coal_consumption, precision: 6, scale: 2
21
+ end
22
+ end
23
+ end
24
+
25
+ def test_pk_and_sequence_for_child_and_parent_in_different_schemas
26
+ CreateChildInSchemaWithPublicParent.up
27
+ pk, seq = @connection.pk_and_sequence_for('interrail.steam_locomotives')
28
+ assert_equal 'id', pk
29
+ assert_equal 'public.locomotives_id_seq', seq.to_s
30
+ end
31
+
32
+ class CreateChildInSchemaWithParentInSchema < ActiveRecord::Migration[4.2]
33
+ def self.up
34
+ execute "CREATE SCHEMA interrail"
35
+ create_table 'interrail.locomotives' do |t|
36
+ t.column :interrail_name, :string
37
+ t.column :interrail_max_speed, :integer
38
+ t.column :type, :string
39
+ end
40
+ create_child('interrail.steam_locomotives', parent: 'interrail.locomotives') do |t|
41
+ t.decimal :interrail_water_consumption, precision: 6, scale: 2
42
+ t.decimal :interrail_coal_consumption, precision: 6, scale: 2
43
+ end
44
+ end
45
+ end
46
+
47
+ def test_pk_and_sequence_for_child_and_parent_in_same_nonpublic_schema
48
+ CreateChildInSchemaWithParentInSchema.up
49
+ pk, seq = @connection.pk_and_sequence_for('interrail.steam_locomotives')
50
+ assert_equal 'id', pk
51
+ assert_equal 'interrail.locomotives_id_seq', seq.to_s
11
52
  end
12
53
 
13
54
  def test_primary_key
@@ -23,7 +64,7 @@ class SchemaTest < ActiveSupport::TestCase
23
64
  @connection.views.sort
24
65
  end
25
66
 
26
- class ParentTableWithOnlyOneColumn < ActiveRecord::Migration
67
+ class ParentTableWithOnlyOneColumn < ActiveRecord::Migration[4.2]
27
68
  def self.up
28
69
  create_table(:parent_pk_only){}
29
70
  create_table :child_data do |t|
@@ -46,7 +87,7 @@ class SchemaTest < ActiveSupport::TestCase
46
87
  ParentTableWithOnlyOneColumn.down
47
88
  end
48
89
 
49
- class ChildTableWithOnlyOneColumn < ActiveRecord::Migration
90
+ class ChildTableWithOnlyOneColumn < ActiveRecord::Migration[4.2]
50
91
  def self.up
51
92
  create_table :parent do |t|
52
93
  t.column :name, :string
@@ -91,7 +132,7 @@ class SchemaTest < ActiveSupport::TestCase
91
132
  assert SteamLocomotive.columns.find { |c| c.name == 'water_consumption' }.null
92
133
  end
93
134
 
94
- class ChangeDefaultValueOfColumn < ActiveRecord::Migration
135
+ class ChangeDefaultValueOfColumn < ActiveRecord::Migration[4.2]
95
136
  def self.up
96
137
  remove_parent_and_children_views(:rack_locomotives)
97
138
  change_column_default(:rack_locomotives_data, :rail_system, 'Marsh')
@@ -105,7 +146,7 @@ class SchemaTest < ActiveSupport::TestCase
105
146
  assert_equal 'Marsh', RackLocomotive.new.rail_system
106
147
  end
107
148
 
108
- class RemoveChildrenViews < ActiveRecord::Migration
149
+ class RemoveChildrenViews < ActiveRecord::Migration[4.2]
109
150
  def self.up
110
151
  remove_parent_and_children_views(:locomotives)
111
152
  end
@@ -116,7 +157,7 @@ class SchemaTest < ActiveSupport::TestCase
116
157
  assert @connection.views.empty?
117
158
  end
118
159
 
119
- class RemoveColumnInParentTable < ActiveRecord::Migration
160
+ class RemoveColumnInParentTable < ActiveRecord::Migration[4.2]
120
161
  def self.up
121
162
  remove_parent_and_children_views(:locomotives)
122
163
  remove_column(:locomotives, :max_speed)
@@ -132,7 +173,7 @@ class SchemaTest < ActiveSupport::TestCase
132
173
  @connection.columns(:maglev_locomotives).map{ |c| c.name }.sort
133
174
  end
134
175
 
135
- class RenameColumnInParentTable < ActiveRecord::Migration
176
+ class RenameColumnInParentTable < ActiveRecord::Migration[4.2]
136
177
  def self.up
137
178
  ActiveRecord::FixtureSet.create_fixtures(File.dirname(__FILE__) + '/fixtures/', :electric_locomotives)
138
179
  ActiveRecord::FixtureSet.create_fixtures(File.dirname(__FILE__) + '/fixtures/', :maglev_locomotives)
@@ -153,7 +194,7 @@ class SchemaTest < ActiveSupport::TestCase
153
194
 
154
195
  end
155
196
 
156
- class AddColumnToParentTable < ActiveRecord::Migration
197
+ class AddColumnToParentTable < ActiveRecord::Migration[4.2]
157
198
  def self.up
158
199
  add_column(:raw_electric_locomotives, :number_of_engines, :integer)
159
200
  rebuild_parent_and_children_views(:electric_locomotives)
@@ -169,7 +210,7 @@ class SchemaTest < ActiveSupport::TestCase
169
210
 
170
211
  end
171
212
 
172
- class ChangeChildRelationView < ActiveRecord::Migration
213
+ class ChangeChildRelationView < ActiveRecord::Migration[4.2]
173
214
  def self.up
174
215
  remove_parent_and_children_views(:electric_locomotives)
175
216
  rename_column(:raw_electric_locomotives, :electricity_consumption, :electric_consumption)
@@ -183,7 +224,7 @@ class SchemaTest < ActiveSupport::TestCase
183
224
  @connection.columns(:electric_locomotives).map{ |c| c.name }.sort
184
225
  end
185
226
 
186
- class ChangeColumnInChildTable < ActiveRecord::Migration
227
+ class ChangeColumnInChildTable < ActiveRecord::Migration[4.2]
187
228
  def self.up
188
229
  drop_view(:steam_locomotives)
189
230
  rename_column(:steam_locomotives_data, :coal_consumption, :fuel_consumption)
@@ -197,11 +238,7 @@ class SchemaTest < ActiveSupport::TestCase
197
238
  @connection.columns(:steam_locomotives).map(&:name).sort
198
239
  end
199
240
 
200
- def test_table_exists
201
- #TODO: test table_exists? monkey patch
202
- end
203
-
204
- class CreateChildInSchema < ActiveRecord::Migration
241
+ class CreateChildInSchema < ActiveRecord::Migration[4.2]
205
242
  def self.up
206
243
  execute "CREATE SCHEMA interrail"
207
244
  create_table 'interrail.locomotives' do |t|
@@ -227,7 +264,7 @@ class SchemaTest < ActiveSupport::TestCase
227
264
  @connection.columns('interrail.steam_locomotives').map(&:name).sort
228
265
  end
229
266
 
230
- class ChangeTablesInTwoInheritanceChains < ActiveRecord::Migration
267
+ class ChangeTablesInTwoInheritanceChains < ActiveRecord::Migration[4.2]
231
268
  def self.up
232
269
  add_column(:maglev_locomotives_data, :levitation_height, :integer)
233
270
  add_column(:bicycles_data, :wheel_size, :integer)
@@ -246,7 +283,7 @@ class SchemaTest < ActiveSupport::TestCase
246
283
  "Newly added column not present in view after rebuild for 2. hierarchy"
247
284
  end
248
285
 
249
- class UseExistingTable < ActiveRecord::Migration
286
+ class UseExistingTable < ActiveRecord::Migration[4.2]
250
287
  def self.up
251
288
  create_table :tbl_diesel_locomotives do |t|
252
289
  t.belongs_to :locomotives
@@ -264,7 +301,7 @@ class SchemaTest < ActiveSupport::TestCase
264
301
  assert @connection.columns(:diesel_locomotives).map(&:name).include?("num_cylinders")
265
302
  end
266
303
 
267
- class ReservedSQLWords < ActiveRecord::Migration
304
+ class ReservedSQLWords < ActiveRecord::Migration[4.2]
268
305
  def self.up
269
306
  create_child(:table, parent: :locomotives) do |t|
270
307
  t.integer :column
@@ -282,7 +319,7 @@ class SchemaTest < ActiveSupport::TestCase
282
319
  ReservedSQLWords.down
283
320
  end
284
321
 
285
- class ChildTableIsActuallyView < ActiveRecord::Migration
322
+ class ChildTableIsActuallyView < ActiveRecord::Migration[4.2]
286
323
  def self.up
287
324
  execute <<-SQL.squish
288
325
  CREATE VIEW punk_locomotives_data AS (
@@ -30,7 +30,7 @@ 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
33
+ class AddColumnToParentTable < ActiveRecord::Migration[4.2]
34
34
  def self.up
35
35
  add_column(:raw_electric_locomotives, :number_of_engines, :integer)
36
36
  drop_view(:all_locomotives)
@@ -45,7 +45,7 @@ class SingleTableInheritanceAggregateViewTest < ActiveSupport::TestCase
45
45
  @connection.columns(:all_locomotives).map{ |c| c.name }.sort
46
46
  end
47
47
 
48
- class RemoveColumnInParentTable < ActiveRecord::Migration
48
+ class RemoveColumnInParentTable < ActiveRecord::Migration[4.2]
49
49
  def self.up
50
50
  drop_view(:all_locomotives)
51
51
  remove_parent_and_children_views(:locomotives)
@@ -61,7 +61,7 @@ class SingleTableInheritanceAggregateViewTest < ActiveSupport::TestCase
61
61
  @connection.columns(:all_locomotives).map{ |c| c.name }.sort
62
62
  end
63
63
 
64
- class RenameColumnInParentTable < ActiveRecord::Migration
64
+ class RenameColumnInParentTable < ActiveRecord::Migration[4.2]
65
65
  def self.up
66
66
  drop_view(:all_locomotives)
67
67
  remove_parent_and_children_views(:locomotives)
@@ -77,7 +77,7 @@ class SingleTableInheritanceAggregateViewTest < ActiveSupport::TestCase
77
77
  @connection.columns(:all_locomotives).map{ |c| c.name }.sort
78
78
  end
79
79
 
80
- class ChangeChildRelationView < ActiveRecord::Migration
80
+ class ChangeChildRelationView < ActiveRecord::Migration[4.2]
81
81
  def self.up
82
82
  drop_view(:all_locomotives)
83
83
  remove_parent_and_children_views(:electric_locomotives)
@@ -93,7 +93,7 @@ class SingleTableInheritanceAggregateViewTest < ActiveSupport::TestCase
93
93
  @connection.columns(:all_locomotives).map{ |c| c.name }.sort
94
94
  end
95
95
 
96
- class ConflictColumns < ActiveRecord::Migration
96
+ class ConflictColumns < ActiveRecord::Migration[4.2]
97
97
  def self.up
98
98
  drop_view(:all_locomotives)
99
99
  add_column(:raw_electric_locomotives, :number_of_engines, :integer)
@@ -111,7 +111,7 @@ class SingleTableInheritanceAggregateViewTest < ActiveSupport::TestCase
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
114
+ class ConflictColumnsWithValues < ActiveRecord::Migration[4.2]
115
115
  def self.up
116
116
  add_column(:raw_electric_locomotives, :number_of_engines, :integer)
117
117
  add_column(:steam_locomotives_data, :number_of_engines, :string)
data/test/test_helper.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  # Configure Rails Environment
2
2
  ENV["RAILS_ENV"] = "test"
3
3
 
4
+ require 'warning'
5
+ Warning.ignore(/.*in Ruby 3.*/)
6
+
4
7
  # test coverage
5
8
  require 'simplecov'
6
9
  require 'simplecov_json_formatter'
@@ -18,16 +18,17 @@ 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", "~> 4.2.8"
22
- s.add_dependency "pg", "~> 0.21"
21
+ s.add_dependency "activerecord", "~> 5.0.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", '= 4.2.11.1'
28
+ s.add_development_dependency "rails", '~> 5.0.7'
29
29
  s.add_development_dependency "rake"
30
30
  s.add_development_dependency "simplecov"
31
31
  s.add_development_dependency "simplecov_json_formatter"
32
- s.add_development_dependency "solargraph"
32
+ s.add_development_dependency "warning"
33
+
33
34
  end
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.7
4
+ version: 1.5.0
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: 2024-11-06 00:00:00.000000000 Z
12
+ date: 2025-01-31 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: 4.2.8
20
+ version: 5.0.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: 4.2.8
27
+ version: 5.0.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.21'
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.21'
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: 4.2.11.1
104
+ version: 5.0.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: 4.2.11.1
111
+ version: 5.0.7
112
112
  - !ruby/object:Gem::Dependency
113
113
  name: rake
114
114
  requirement: !ruby/object:Gem::Requirement
@@ -152,7 +152,7 @@ dependencies:
152
152
  - !ruby/object:Gem::Version
153
153
  version: '0'
154
154
  - !ruby/object:Gem::Dependency
155
- name: solargraph
155
+ name: warning
156
156
  requirement: !ruby/object:Gem::Requirement
157
157
  requirements:
158
158
  - - ">="
@@ -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/active_record.rb
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/pg_insert_returning_with_rules_spec.rb
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/pg_insert_returning_with_rules_spec.rb
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