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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +10 -2
- data/lib/pg_inheritance/active_record/inheritable.rb +33 -2
- data/lib/pg_inheritance/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 78bb787b636e2a7b0342c0a36e3fbca1ae71044d5c05c379700488887d7f7499
|
4
|
+
data.tar.gz: e215f62437672a866713dee32bf2bca85122ab0967e7fe5bd9916fd4137cf839
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb4dbac1ff3b0f545e2b64756c0d2073b13c53709395d21df84f786f93bc745fac7aafbd83ed8fe7987053b28824adc88f33c040935d002b720694e8d4eaf507
|
7
|
+
data.tar.gz: 8fa062de839c825c5bca5ed1c13dad4cee543ee4d02315af21c8d121e21585a223d626ebd1a57d6dff7f0119095ed7c060816fffdfa8e9fe2bb1764f8294767d
|
data/Gemfile.lock
CHANGED
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.
|
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
|
-
|
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
|
-
|
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
|