forceful_foreign_key 0.1.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.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +24 -0
- data/Rakefile +6 -0
- data/bin/console +19 -0
- data/bin/setup +8 -0
- data/forceful_foreign_key.gemspec +31 -0
- data/lib/forceful_foreign_key/graph.rb +93 -0
- data/lib/forceful_foreign_key/version.rb +3 -0
- data/lib/forceful_foreign_key.rb +19 -0
- metadata +155 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6ede7c1b031e46881a77748a7718bdec296f863c
|
4
|
+
data.tar.gz: 5b1d06921229951f3487eff5ccc6a60f45d45f2b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1a634192531f703424f176eabceab9a627ea6145efbfb0ccd724e0321c20cf2c50dfc8167322979bd44daf69e69b6cddadc9f00b8265dec69ae8569d00fc8d06
|
7
|
+
data.tar.gz: e4962775284f205da2b22de3973a8e7709807007d7e209dbdb9e0c663c9e2de7821ba8422958f24db02af29241db391d96c839f9509c89c94bee1eb42e717ed8
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 shaw3257
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# ForcefulForeignKey
|
2
|
+
|
3
|
+
When `force: true` is set, It will delete orphaned data before applying the foreign key constraint. Use cautiously!
|
4
|
+
|
5
|
+
```Ruby
|
6
|
+
class AddFKContraint < ActiveRecord::Migration
|
7
|
+
def change
|
8
|
+
add_foreign_key 'foo', 'bar', force: true
|
9
|
+
end
|
10
|
+
end
|
11
|
+
```
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'forceful_foreign_key'
|
19
|
+
```
|
20
|
+
tall forceful_foreign_key
|
21
|
+
|
22
|
+
## License
|
23
|
+
|
24
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require 'active_record'
|
5
|
+
require "forceful_foreign_key"
|
6
|
+
|
7
|
+
`createdb forceful_foreign_key_dev > /dev/null 2>&1`
|
8
|
+
`createuser -s forceful_foreign_key_dev > /dev/null 2>&1`
|
9
|
+
|
10
|
+
ActiveRecord::Base.establish_connection(
|
11
|
+
:adapter => "postgresql",
|
12
|
+
:host => "localhost",
|
13
|
+
:username => "forceful_foreign_key_dev",
|
14
|
+
:password => "",
|
15
|
+
:database => "forceful_foreign_key_dev"
|
16
|
+
)
|
17
|
+
|
18
|
+
require "irb"
|
19
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'forceful_foreign_key/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "forceful_foreign_key"
|
8
|
+
spec.version = ForcefulForeignKey::VERSION
|
9
|
+
spec.authors = ["shaw3257"]
|
10
|
+
spec.email = ["shaw3257@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "Forcefully creates a foreign key"
|
13
|
+
spec.description = "Forcefully creates a foreign key and will recursively delete"
|
14
|
+
spec.homepage = "https://github.com/viewthespace/forceful-foreign-key"
|
15
|
+
spec.license = "MIT"
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_dependency('activerecord', '>= 3.0')
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
28
|
+
spec.add_development_dependency "pg"
|
29
|
+
spec.add_development_dependency "database_cleaner"
|
30
|
+
spec.add_development_dependency "pry"
|
31
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
module ForcefulForeignKey
|
2
|
+
class Graph
|
3
|
+
|
4
|
+
attr_reader :graph
|
5
|
+
attr_reader :dry_run
|
6
|
+
|
7
|
+
def initialize dry_run: false
|
8
|
+
@dry_run = dry_run
|
9
|
+
init_dep_graph
|
10
|
+
build_graph
|
11
|
+
end
|
12
|
+
|
13
|
+
def delete_orphans(from_table:, from_column:, target_table:, target_column:, parent_ids: [])
|
14
|
+
limit = 1_000
|
15
|
+
offset = 0
|
16
|
+
if parent_ids.empty?
|
17
|
+
sql = "select distinct #{from_table}.id from #{from_table} left outer join #{target_table} on #{target_table}.#{target_column} = #{from_table}.#{from_column} where #{from_table}.#{from_column} is not null and #{target_table}.#{target_column} is null"
|
18
|
+
else
|
19
|
+
sql = "select distinct #{from_table}.id from #{from_table} left outer join #{target_table} on #{target_table}.#{target_column} = #{from_table}.#{from_column} where #{target_table}.#{target_column} in(#{parent_ids.join(',')})"
|
20
|
+
end
|
21
|
+
sql += " limit #{limit} offset 0"
|
22
|
+
loop do
|
23
|
+
ids = ActiveRecord::Base.connection.execute(sql).map{ |r| r['id'] }
|
24
|
+
break if ids.empty?
|
25
|
+
if dry_run
|
26
|
+
sql.gsub!(/\d+/, (offset += limit).to_s)
|
27
|
+
end
|
28
|
+
graph[from_table][:edges].each do |edge|
|
29
|
+
delete_orphans(from_table: edge[:table], from_column: edge[:column], target_table: from_table, target_column: 'id', parent_ids: ids)
|
30
|
+
end
|
31
|
+
delete_sql = "delete from #{from_table} where id in (#{ids.join(',')})"
|
32
|
+
puts delete_sql
|
33
|
+
select_sql = "select * from #{from_table} where id in (#{ids.join(',')})"
|
34
|
+
r = ActiveRecord::Base.connection.execute(select_sql).to_a
|
35
|
+
unless dry_run
|
36
|
+
ActiveRecord::Base.connection.execute(delete_sql)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def init_dep_graph
|
42
|
+
@graph = tables.each_with_object({}){ |tables, graph| graph[tables] = { edges: [] } }
|
43
|
+
end
|
44
|
+
|
45
|
+
def build_graph
|
46
|
+
init_dep_graph
|
47
|
+
graph.each do |table, obj|
|
48
|
+
fks.select{ |fk| fk['references_table'] == table }.each do |fk|
|
49
|
+
obj[:edges].push({table: fk['table_name'], column: fk['column_name'] })
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def fks
|
55
|
+
ActiveRecord::Base.connection.execute(
|
56
|
+
<<-SQL
|
57
|
+
SELECT tc.table_name,
|
58
|
+
kcu.constraint_name,
|
59
|
+
kcu.column_name,
|
60
|
+
ccu.table_name AS references_table,
|
61
|
+
ccu.column_name AS references_field
|
62
|
+
FROM information_schema.table_constraints tc
|
63
|
+
|
64
|
+
LEFT JOIN information_schema.key_column_usage kcu
|
65
|
+
ON tc.constraint_catalog = kcu.constraint_catalog
|
66
|
+
AND tc.constraint_schema = kcu.constraint_schema
|
67
|
+
AND tc.constraint_name = kcu.constraint_name
|
68
|
+
|
69
|
+
LEFT JOIN information_schema.referential_constraints rc
|
70
|
+
ON tc.constraint_catalog = rc.constraint_catalog
|
71
|
+
AND tc.constraint_schema = rc.constraint_schema
|
72
|
+
AND tc.constraint_name = rc.constraint_name
|
73
|
+
|
74
|
+
LEFT JOIN information_schema.constraint_column_usage ccu
|
75
|
+
ON rc.unique_constraint_catalog = ccu.constraint_catalog
|
76
|
+
AND rc.unique_constraint_schema = ccu.constraint_schema
|
77
|
+
AND rc.unique_constraint_name = ccu.constraint_name
|
78
|
+
WHERE lower(tc.constraint_type) in ('foreign key')
|
79
|
+
SQL
|
80
|
+
).to_a
|
81
|
+
end
|
82
|
+
|
83
|
+
def tables
|
84
|
+
ActiveRecord::Base.connection.execute(
|
85
|
+
<<-SQL
|
86
|
+
SELECT * FROM information_schema.tables
|
87
|
+
WHERE table_schema = 'public'
|
88
|
+
SQL
|
89
|
+
).to_a.map{ |r| r['table_name'] }
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require "forceful_foreign_key/version"
|
2
|
+
require "forceful_foreign_key/graph"
|
3
|
+
|
4
|
+
module ActiveRecord::ConnectionAdapters::SchemaStatements
|
5
|
+
alias_method :old_add_foreign_key, :add_foreign_key
|
6
|
+
|
7
|
+
def add_foreign_key(from_table, to_table, options = {})
|
8
|
+
return unless supports_foreign_keys?
|
9
|
+
if options[:force]
|
10
|
+
graph = ForcefulForeignKey::Graph.new
|
11
|
+
graph.build_graph
|
12
|
+
graph.delete_orphans(from_table: from_table,
|
13
|
+
from_column: options[:column] || "#{to_table}_id",
|
14
|
+
target_table: to_table,
|
15
|
+
target_column: options[:primary_key] || 'id')
|
16
|
+
end
|
17
|
+
old_add_foreign_key from_table, to_table, options
|
18
|
+
end
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: forceful_foreign_key
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- shaw3257
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-02-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.13'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.13'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pg
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: database_cleaner
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pry
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: Forcefully creates a foreign key and will recursively delete
|
112
|
+
email:
|
113
|
+
- shaw3257@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- ".rspec"
|
120
|
+
- ".travis.yml"
|
121
|
+
- Gemfile
|
122
|
+
- LICENSE.txt
|
123
|
+
- README.md
|
124
|
+
- Rakefile
|
125
|
+
- bin/console
|
126
|
+
- bin/setup
|
127
|
+
- forceful_foreign_key.gemspec
|
128
|
+
- lib/forceful_foreign_key.rb
|
129
|
+
- lib/forceful_foreign_key/graph.rb
|
130
|
+
- lib/forceful_foreign_key/version.rb
|
131
|
+
homepage: https://github.com/viewthespace/forceful-foreign-key
|
132
|
+
licenses:
|
133
|
+
- MIT
|
134
|
+
metadata: {}
|
135
|
+
post_install_message:
|
136
|
+
rdoc_options: []
|
137
|
+
require_paths:
|
138
|
+
- lib
|
139
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
140
|
+
requirements:
|
141
|
+
- - ">="
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: '0'
|
144
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
145
|
+
requirements:
|
146
|
+
- - ">="
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
version: '0'
|
149
|
+
requirements: []
|
150
|
+
rubyforge_project:
|
151
|
+
rubygems_version: 2.5.2
|
152
|
+
signing_key:
|
153
|
+
specification_version: 4
|
154
|
+
summary: Forcefully creates a foreign key
|
155
|
+
test_files: []
|