orphan_records 0.0.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.
- data/Gemfile +4 -0
- data/Gemfile.lock +14 -0
- data/LICENSE.txt +60 -0
- data/README.md +12 -0
- data/Rakefile +7 -0
- data/lib/orphan_records.rb +95 -0
- data/lib/orphan_records/railtie.rb +9 -0
- data/lib/tasks/orphan_records_tasks.rake +14 -0
- data/orphan_records.gemspec +34 -0
- data/test/test_helper.rb +66 -0
- data/test/test_orphan_records.rb +28 -0
- metadata +60 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
Ruby License
|
2
|
+
http://www.ruby-lang.org/en/LICENSE.txt
|
3
|
+
|
4
|
+
Ruby is copyrighted free software by Yukihiro Matsumoto <matz@netlab.co.jp>.
|
5
|
+
You can redistribute it and/or modify it under either the terms of the GPL
|
6
|
+
(see COPYING.txt file), or the conditions below:
|
7
|
+
|
8
|
+
1. You may make and give away verbatim copies of the source form of the
|
9
|
+
software without restriction, provided that you duplicate all of the
|
10
|
+
original copyright notices and associated disclaimers.
|
11
|
+
|
12
|
+
2. You may modify your copy of the software in any way, provided that
|
13
|
+
you do at least ONE of the following:
|
14
|
+
|
15
|
+
a) place your modifications in the Public Domain or otherwise
|
16
|
+
make them Freely Available, such as by posting said
|
17
|
+
modifications to Usenet or an equivalent medium, or by allowing
|
18
|
+
the author to include your modifications in the software.
|
19
|
+
|
20
|
+
b) use the modified software only within your corporation or
|
21
|
+
organization.
|
22
|
+
|
23
|
+
c) rename any non-standard executables so the names do not conflict
|
24
|
+
with standard executables, which must also be provided.
|
25
|
+
|
26
|
+
d) make other distribution arrangements with the author.
|
27
|
+
|
28
|
+
3. You may distribute the software in object code or executable
|
29
|
+
form, provided that you do at least ONE of the following:
|
30
|
+
|
31
|
+
a) distribute the executables and library files of the software,
|
32
|
+
together with instructions (in the manual page or equivalent)
|
33
|
+
on where to get the original distribution.
|
34
|
+
|
35
|
+
b) accompany the distribution with the machine-readable source of
|
36
|
+
the software.
|
37
|
+
|
38
|
+
c) give non-standard executables non-standard names, with
|
39
|
+
instructions on where to get the original software distribution.
|
40
|
+
|
41
|
+
d) make other distribution arrangements with the author.
|
42
|
+
|
43
|
+
4. You may modify and include the part of the software into any other
|
44
|
+
software (possibly commercial). But some files in the distribution
|
45
|
+
are not written by the author, so that they are not under this terms.
|
46
|
+
|
47
|
+
They are gc.c(partly), utils.c(partly), regex.[ch], st.[ch] and some
|
48
|
+
files under the ./missing directory. See each file for the copying
|
49
|
+
condition.
|
50
|
+
|
51
|
+
5. The scripts and library files supplied as input to or produced as
|
52
|
+
output from the software do not automatically fall under the
|
53
|
+
copyright of the software, but belong to whomever generated them,
|
54
|
+
and may be sold commercially, and may be aggregated with this
|
55
|
+
software.
|
56
|
+
|
57
|
+
6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
|
58
|
+
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
59
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
60
|
+
PURPOSE.
|
data/README.md
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
OrphanRecords
|
2
|
+
===============
|
3
|
+
This program is based on the Annotate Models plugin written by Dave Thomas and VisualizeModels plugin by Nils Franzen.
|
4
|
+
<br/>For more information about Annotate Models, see http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Tech/Ruby/AnnotateModels.rdoc
|
5
|
+
<br/>For more information about Visualize Models, see http://visualizemodels.rubyforge.org/
|
6
|
+
|
7
|
+
Authors:
|
8
|
+
> Abhilash M A
|
9
|
+
<br/> Nils Franzen (Visualize Models)
|
10
|
+
<br/> Dave Thomas (Annotate Models)
|
11
|
+
|
12
|
+
Released under the same license as Ruby. No Support. No Warranty.
|
data/Rakefile
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'active_support/all'
|
3
|
+
|
4
|
+
TableInfo = Struct.new(:name, :attributes)
|
5
|
+
Attribute = Struct.new(:name, :type, :default, :isnull)
|
6
|
+
Association = Struct.new(:attr, :node1, :node2)
|
7
|
+
|
8
|
+
module OrphanRecords
|
9
|
+
|
10
|
+
# Use the column information in an ActiveRecord class
|
11
|
+
# to create an 'Attribute' for each column.
|
12
|
+
# The line contains the column name, the type
|
13
|
+
# (and length), and any optional attributes
|
14
|
+
def self.get_schema_info(table_name)
|
15
|
+
attrs = []
|
16
|
+
ActiveRecord::Base.connection.columns(table_name).each do |col|
|
17
|
+
col_type = col.type.to_s
|
18
|
+
col_type << "(#{col.limit})" if col.limit
|
19
|
+
|
20
|
+
attrs << Attribute.new(col.name, col_type, col.default, !col.null)
|
21
|
+
end
|
22
|
+
|
23
|
+
return TableInfo.new(ActiveSupport::Inflector.classify(table_name), attrs)
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
def self.create_association_info(tableInfos)
|
28
|
+
assocs = []
|
29
|
+
# Draw the tables as boxes
|
30
|
+
tableInfos.each do |table |
|
31
|
+
attrs = ""
|
32
|
+
table.attributes.each do | attr |
|
33
|
+
if attr.name =~ /\_id$/
|
34
|
+
# Create an association to other table
|
35
|
+
table_name = ActiveSupport::Inflector.camelize(attr.name.sub(/\_id$/, ''))
|
36
|
+
other_table = tableInfos.find { | other | other.name == table_name }
|
37
|
+
assocs << Association.new(attr, table, other_table) if other_table != nil
|
38
|
+
end
|
39
|
+
attrs << "#{attr.name} : #{attr.type}"
|
40
|
+
attrs << ", default: \\\"#{attr.default}\\\"" if attr.default
|
41
|
+
attrs << "\\n"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
assocs
|
46
|
+
end
|
47
|
+
|
48
|
+
# We're passed a name of things that might be
|
49
|
+
# ActiveRecord models. If we can find the class, and
|
50
|
+
# if its a subclass of ActiveRecord::Base,
|
51
|
+
# then pass it to the associated block
|
52
|
+
def self.get_associations
|
53
|
+
tableInfos = []
|
54
|
+
configurations = YAML::load(File.open(File.join(Rails.root, "config/database.yml")))
|
55
|
+
ActiveRecord::Base.establish_connection configurations[Rails.env]
|
56
|
+
ActiveRecord::Base.connection.tables.each do |table_name|
|
57
|
+
# puts "Looking at table: #{table_name}"
|
58
|
+
tableInfos << get_schema_info(table_name)
|
59
|
+
end
|
60
|
+
|
61
|
+
create_association_info(tableInfos)
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.find_orphan_records(options = {:delete => false})
|
65
|
+
require File.join(Rails.root, "config/environment")
|
66
|
+
associations = get_associations
|
67
|
+
associations.each do | assoc |
|
68
|
+
# puts "\t\"#{assoc.node1.name}\" -> \"#{assoc.node2.name}\" [label=\"#{assoc.attr.name}\"]\n"
|
69
|
+
begin
|
70
|
+
model = eval(assoc.node1.name)
|
71
|
+
model.all.each do |obj|
|
72
|
+
id = obj.send(assoc.attr.name)
|
73
|
+
record = eval(assoc.node2.name).find_by_id(id)
|
74
|
+
if(record.nil?)
|
75
|
+
puts "\t\"#{assoc.node1.name} ID: #{obj.id} associated with #{assoc.node2.name} ID: #{id} which doesn't exist"
|
76
|
+
if(options[:delete])
|
77
|
+
obj.delete
|
78
|
+
puts "\t\"#{assoc.node1.name} ID: #{obj.id} deleted"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
rescue Exception => e
|
83
|
+
# puts "\t\t\"#{e.message}"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def self.delete_orphan_records
|
89
|
+
find_orphan_records({:delete => true})
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
require File.join(File.dirname(__FILE__), "orphan_records/railtie") if defined?(Rails)
|
95
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
desc "Orphan Records"
|
2
|
+
namespace :orphan_records do
|
3
|
+
desc "Show the orphan records"
|
4
|
+
task :show do
|
5
|
+
require File.join(File.dirname(__FILE__), "../orphan_records.rb")
|
6
|
+
OrphanRecords.find_orphan_records
|
7
|
+
end
|
8
|
+
|
9
|
+
desc "Delete the orphan records"
|
10
|
+
task :delete do
|
11
|
+
require File.join(File.dirname(__FILE__), "../orphan_records.rb")
|
12
|
+
OrphanRecords.delete_orphan_records
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'orphan_records'
|
3
|
+
s.version = "0.0.1"
|
4
|
+
|
5
|
+
s.authors = [%q{abhilash}]
|
6
|
+
s.date = %q{2013-04-07}
|
7
|
+
s.description = %q{Show/Delete Orphan Records in your Rails application}
|
8
|
+
s.email = %q{abhidsm@gmail.com}
|
9
|
+
s.extra_rdoc_files = [
|
10
|
+
"LICENSE.txt",
|
11
|
+
"README.md"
|
12
|
+
]
|
13
|
+
s.files = [
|
14
|
+
"Gemfile",
|
15
|
+
"Gemfile.lock",
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.md",
|
18
|
+
"Rakefile",
|
19
|
+
"lib/orphan_records.rb",
|
20
|
+
"lib/orphan_records/railtie.rb",
|
21
|
+
"lib/tasks/orphan_records_tasks.rake",
|
22
|
+
"test/test_helper.rb",
|
23
|
+
"test/test_orphan_records.rb",
|
24
|
+
"orphan_records.gemspec"
|
25
|
+
]
|
26
|
+
s.homepage = %q{https://github.com/abhidsm/OrphanRecords}
|
27
|
+
s.licenses = [%q{Ruby}]
|
28
|
+
s.require_paths = [%q{lib}]
|
29
|
+
s.summary = %q{Show/Delete Orphan Records in your Rails application}
|
30
|
+
s.test_files = [
|
31
|
+
"test/test_helper.rb",
|
32
|
+
"test/test_orphan_records.rb"
|
33
|
+
]
|
34
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
require 'active_support'
|
4
|
+
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
|
13
|
+
module Rails
|
14
|
+
def self.env
|
15
|
+
ActiveSupport::StringInquirer.new("test")
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.root
|
19
|
+
"test"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class File
|
24
|
+
def self.join(first, second)
|
25
|
+
"minitest/spec"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
module ActiveRecord
|
30
|
+
class Column
|
31
|
+
def type
|
32
|
+
"type"
|
33
|
+
end
|
34
|
+
def limit
|
35
|
+
12
|
36
|
+
end
|
37
|
+
def name
|
38
|
+
"asd"
|
39
|
+
end
|
40
|
+
def default
|
41
|
+
"default"
|
42
|
+
end
|
43
|
+
def null
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
class Table
|
48
|
+
def tables
|
49
|
+
[Table.new, Table.new]
|
50
|
+
end
|
51
|
+
def columns(name)
|
52
|
+
[Column.new, Column.new]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class Base
|
57
|
+
def self.connection
|
58
|
+
Table.new
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
require 'minitest/spec'
|
64
|
+
require 'minitest/autorun'
|
65
|
+
require_relative "../lib/orphan_records"
|
66
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative 'test_helper.rb'
|
2
|
+
|
3
|
+
describe "when asked for get_schema_info" do
|
4
|
+
it "must create and return the TableInfo values" do
|
5
|
+
OrphanRecords.get_schema_info("table_name").must_be_instance_of(TableInfo)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "when asked for create_association_info" do
|
10
|
+
it "must create and return the associations details" do
|
11
|
+
attrs = []
|
12
|
+
table_infos = []
|
13
|
+
attrs << Attribute.new("title", "asd", "qwe", true)
|
14
|
+
table_infos << TableInfo.new("Table1", attrs)
|
15
|
+
attrs = []
|
16
|
+
attrs << Attribute.new("table1_id", "asd", "qwe", true)
|
17
|
+
table_infos << TableInfo.new("Table2", attrs)
|
18
|
+
associations = OrphanRecords.create_association_info(table_infos)
|
19
|
+
associations.count.must_equal(1)
|
20
|
+
attributes = associations.first.attr
|
21
|
+
assert_equal attributes.name, 'table1_id'
|
22
|
+
assert_equal attributes.type, 'asd'
|
23
|
+
assert_equal attributes.default, 'qwe'
|
24
|
+
assert_equal associations.first.node1, table_infos.last
|
25
|
+
assert_equal associations.first.node2, table_infos.first
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: orphan_records
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- abhilash
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-07 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Show/Delete Orphan Records in your Rails application
|
15
|
+
email: abhidsm@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files:
|
19
|
+
- LICENSE.txt
|
20
|
+
- README.md
|
21
|
+
files:
|
22
|
+
- Gemfile
|
23
|
+
- Gemfile.lock
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- lib/orphan_records.rb
|
28
|
+
- lib/orphan_records/railtie.rb
|
29
|
+
- lib/tasks/orphan_records_tasks.rake
|
30
|
+
- test/test_helper.rb
|
31
|
+
- test/test_orphan_records.rb
|
32
|
+
- orphan_records.gemspec
|
33
|
+
homepage: https://github.com/abhidsm/OrphanRecords
|
34
|
+
licenses:
|
35
|
+
- Ruby
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.8.24
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: Show/Delete Orphan Records in your Rails application
|
58
|
+
test_files:
|
59
|
+
- test/test_helper.rb
|
60
|
+
- test/test_orphan_records.rb
|