pg_inheritance 0.1.1 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 78df89fa727c1c8148e62de7f68cd62551c194b8d8052e1ffbfbce7c55a2c0d8
4
- data.tar.gz: 6da79709df0ea92f719a3728ef71c0f164d54928e17f31cf80eea762caed1bb7
3
+ metadata.gz: 78bb787b636e2a7b0342c0a36e3fbca1ae71044d5c05c379700488887d7f7499
4
+ data.tar.gz: e215f62437672a866713dee32bf2bca85122ab0967e7fe5bd9916fd4137cf839
5
5
  SHA512:
6
- metadata.gz: f0d417385e9a064b20f4019674963a333a570e1dd5f9376ec8a8e3bad881d14687cb0ac87a6462f27b842d6eb4468660896390c7dc420b18295cb3e18d47def6
7
- data.tar.gz: c7b9757f36549c722977cbd8cd967f1d8ca2249374db1e1ac5e41e1f584671f8af9b1991fcc452b964f7e6fa274801f162d78415cbdb350e8c446831c9dd13cd
6
+ metadata.gz: eb4dbac1ff3b0f545e2b64756c0d2073b13c53709395d21df84f786f93bc745fac7aafbd83ed8fe7987053b28824adc88f33c040935d002b720694e8d4eaf507
7
+ data.tar.gz: 8fa062de839c825c5bca5ed1c13dad4cee543ee4d02315af21c8d121e21585a223d626ebd1a57d6dff7f0119095ed7c060816fffdfa8e9fe2bb1764f8294767d
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pg_inheritance (0.1.0)
4
+ pg_inheritance (0.1.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -3,6 +3,10 @@
3
3
 
4
4
  # UNDER DEVELOPMENT
5
5
 
6
+ ## Supports
7
+
8
+ Tested on ActiveRecord `5.x`, `6.x`.
9
+
6
10
  ## Installation
7
11
 
8
12
  Add this line to your application's Gemfile:
@@ -39,7 +43,7 @@ After migrations members has available both columns.
39
43
 
40
44
  ### Drop inheritance
41
45
 
42
- In your migration call drop_inheritance method. This has no options.
46
+ In your migration call drop_inheritance method.
43
47
 
44
48
  ```ruby
45
49
  class DropMemberUsersInheritance < ActiveRecord::Migration
@@ -49,4 +53,8 @@ class DropMemberUsersInheritance < ActiveRecord::Migration
49
53
  end
50
54
  ```
51
55
 
52
- If inheritance exists this will be undeclared, but columns at inherited table will still be available. Otherwise will be raised an exception.
56
+ #### Options
57
+
58
+ * with_columns - if `true` then inherited columns will be dropped.
59
+
60
+ If inheritance has not exists it raises an exception.
@@ -5,16 +5,28 @@ module PGInheritance
5
5
  module Inheritable
6
6
  include PGInheritance::ActiveRecord::Parentable
7
7
 
8
- def drop_inheritance(table_name, parent_name)
8
+ TIMESTAMP_COLUMNS = %w[created_at updated_at].freeze
9
+
10
+ def drop_inheritance(table_name, parent_name, **options)
9
11
  unless inherited?(table_name, parent_name)
10
12
  raise PGInheritance::TableNotInheritedError, "#{table_name} not inherited from #{parent_name}"
11
13
  end
12
14
 
13
- execute(drop_inheritance_query(table_name, parent_name))
15
+ execute(drop_inheritance_query(table_name, parent_name)).tap do
16
+ drop_inherited_columns(table_name, parent_name) if options[:with_columns]
17
+ end
14
18
  end
15
19
 
16
20
  private
17
21
 
22
+ def drop_inherited_columns(table_name, parent_name)
23
+ cols_for_drop = columns_for_drop(table_name, parent_name)
24
+
25
+ return if cols_for_drop.empty?
26
+
27
+ execute(drop_columns_query(table_name, cols_for_drop))
28
+ end
29
+
18
30
  def inherited?(table_name, parent_name)
19
31
  parent_table(table_name) == parent_name.to_s
20
32
  end
@@ -22,6 +34,25 @@ module PGInheritance
22
34
  def drop_inheritance_query(table_name, parent_name)
23
35
  "ALTER TABLE #{table_name} NO INHERIT #{parent_name};"
24
36
  end
37
+
38
+ def drop_columns_query(table_name, columns)
39
+ sql = "ALTER TABLE #{table_name}"
40
+
41
+ columns_query = columns.map { |column| "DROP COLUMN IF EXISTS #{column}" }.join(', ')
42
+
43
+ sql + " #{columns_query};"
44
+ end
45
+
46
+ def columns_for_drop(table_name, parent_name)
47
+ child_cols = columns(table_name).map(&:name)
48
+ parent_cols = columns(parent_name).map(&:name)
49
+
50
+ (child_cols & parent_cols).tap do |cols|
51
+ TIMESTAMP_COLUMNS.each { |col| cols.delete(col) }
52
+
53
+ cols.delete(primary_key(table_name))
54
+ end
55
+ end
25
56
  end
26
57
  end
27
58
  end
@@ -1,3 +1,3 @@
1
1
  module PGInheritance
2
- VERSION = '0.1.1'.freeze
2
+ VERSION = '0.1.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_inheritance
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roman Kakorin