db_leftovers 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +18 -0
- data/Gemfile.lock +98 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +57 -0
- data/VERSION +1 -0
- data/db_leftovers.gemspec +64 -0
- data/lib/db_leftovers/dsl.rb +281 -0
- data/lib/db_leftovers.rb +12 -0
- data/lib/tasks/leftovers.rake +20 -0
- data/test/helper.rb +18 -0
- data/test/test_db_leftovers.rb +7 -0
- metadata +120 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
gem 'rails', '>= 3.0.0'
|
7
|
+
# gem 'rake'
|
8
|
+
# gem 'activemodel', '= 3.0.11'
|
9
|
+
# gem 'activesupport', '= 3.0.11'
|
10
|
+
|
11
|
+
# Add dependencies to develop your gem here.
|
12
|
+
# Include everything needed to run rake, tests, features, etc.
|
13
|
+
group :development do
|
14
|
+
gem "shoulda", ">= 0"
|
15
|
+
gem "bundler", "~> 1.0.0"
|
16
|
+
gem "jeweler", "~> 1.6.4"
|
17
|
+
gem "rcov", ">= 0"
|
18
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
actionmailer (3.1.3)
|
5
|
+
actionpack (= 3.1.3)
|
6
|
+
mail (~> 2.3.0)
|
7
|
+
actionpack (3.1.3)
|
8
|
+
activemodel (= 3.1.3)
|
9
|
+
activesupport (= 3.1.3)
|
10
|
+
builder (~> 3.0.0)
|
11
|
+
erubis (~> 2.7.0)
|
12
|
+
i18n (~> 0.6)
|
13
|
+
rack (~> 1.3.5)
|
14
|
+
rack-cache (~> 1.1)
|
15
|
+
rack-mount (~> 0.8.2)
|
16
|
+
rack-test (~> 0.6.1)
|
17
|
+
sprockets (~> 2.0.3)
|
18
|
+
activemodel (3.1.3)
|
19
|
+
activesupport (= 3.1.3)
|
20
|
+
builder (~> 3.0.0)
|
21
|
+
i18n (~> 0.6)
|
22
|
+
activerecord (3.1.3)
|
23
|
+
activemodel (= 3.1.3)
|
24
|
+
activesupport (= 3.1.3)
|
25
|
+
arel (~> 2.2.1)
|
26
|
+
tzinfo (~> 0.3.29)
|
27
|
+
activeresource (3.1.3)
|
28
|
+
activemodel (= 3.1.3)
|
29
|
+
activesupport (= 3.1.3)
|
30
|
+
activesupport (3.1.3)
|
31
|
+
multi_json (~> 1.0)
|
32
|
+
arel (2.2.1)
|
33
|
+
builder (3.0.0)
|
34
|
+
erubis (2.7.0)
|
35
|
+
git (1.2.5)
|
36
|
+
hike (1.2.1)
|
37
|
+
i18n (0.6.0)
|
38
|
+
jeweler (1.6.4)
|
39
|
+
bundler (~> 1.0)
|
40
|
+
git (>= 1.2.5)
|
41
|
+
rake
|
42
|
+
json (1.6.5)
|
43
|
+
mail (2.3.0)
|
44
|
+
i18n (>= 0.4.0)
|
45
|
+
mime-types (~> 1.16)
|
46
|
+
treetop (~> 1.4.8)
|
47
|
+
mime-types (1.17.2)
|
48
|
+
multi_json (1.0.4)
|
49
|
+
polyglot (0.3.3)
|
50
|
+
rack (1.3.6)
|
51
|
+
rack-cache (1.1)
|
52
|
+
rack (>= 0.4)
|
53
|
+
rack-mount (0.8.3)
|
54
|
+
rack (>= 1.0.0)
|
55
|
+
rack-ssl (1.3.2)
|
56
|
+
rack
|
57
|
+
rack-test (0.6.1)
|
58
|
+
rack (>= 1.0)
|
59
|
+
rails (3.1.3)
|
60
|
+
actionmailer (= 3.1.3)
|
61
|
+
actionpack (= 3.1.3)
|
62
|
+
activerecord (= 3.1.3)
|
63
|
+
activeresource (= 3.1.3)
|
64
|
+
activesupport (= 3.1.3)
|
65
|
+
bundler (~> 1.0)
|
66
|
+
railties (= 3.1.3)
|
67
|
+
railties (3.1.3)
|
68
|
+
actionpack (= 3.1.3)
|
69
|
+
activesupport (= 3.1.3)
|
70
|
+
rack-ssl (~> 1.3.2)
|
71
|
+
rake (>= 0.8.7)
|
72
|
+
rdoc (~> 3.4)
|
73
|
+
thor (~> 0.14.6)
|
74
|
+
rake (0.9.2.2)
|
75
|
+
rcov (0.9.11)
|
76
|
+
rdoc (3.12)
|
77
|
+
json (~> 1.4)
|
78
|
+
shoulda (2.11.3)
|
79
|
+
sprockets (2.0.3)
|
80
|
+
hike (~> 1.2)
|
81
|
+
rack (~> 1.0)
|
82
|
+
tilt (!= 1.3.0, ~> 1.1)
|
83
|
+
thor (0.14.6)
|
84
|
+
tilt (1.3.3)
|
85
|
+
treetop (1.4.10)
|
86
|
+
polyglot
|
87
|
+
polyglot (>= 0.3.1)
|
88
|
+
tzinfo (0.3.31)
|
89
|
+
|
90
|
+
PLATFORMS
|
91
|
+
ruby
|
92
|
+
|
93
|
+
DEPENDENCIES
|
94
|
+
bundler (~> 1.0.0)
|
95
|
+
jeweler (~> 1.6.4)
|
96
|
+
rails (>= 3.0.0)
|
97
|
+
rcov
|
98
|
+
shoulda
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Paul A. Jungwirth
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= db_leftovers
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Contributing to db_leftovers
|
6
|
+
|
7
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
8
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
9
|
+
* Fork the project
|
10
|
+
* Start a feature/bugfix branch
|
11
|
+
* Commit and push until you are happy with your contribution
|
12
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2012 Paul A. Jungwirth. See LICENSE.txt for
|
18
|
+
further details.
|
19
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
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
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "db_leftovers"
|
18
|
+
gem.homepage = "http://github.com/pjungwir/db_leftovers"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = 'Used to define indexes and foreign keys for your Rails app'
|
21
|
+
gem.description = <<-EOT
|
22
|
+
Define indexes and foreign keys for your Rails app
|
23
|
+
in one place using an easy-to-read DSL,
|
24
|
+
then run a rake task to bring your database up-to-date.
|
25
|
+
EOT
|
26
|
+
gem.email = "pj@illuminatedcomputing.com"
|
27
|
+
gem.authors = ["Paul A. Jungwirth"]
|
28
|
+
# dependencies defined in Gemfile
|
29
|
+
end
|
30
|
+
Jeweler::RubygemsDotOrgTasks.new
|
31
|
+
|
32
|
+
require 'rake/testtask'
|
33
|
+
Rake::TestTask.new(:test) do |test|
|
34
|
+
test.libs << 'lib' << 'test'
|
35
|
+
test.pattern = 'test/**/test_*.rb'
|
36
|
+
test.verbose = true
|
37
|
+
end
|
38
|
+
|
39
|
+
require 'rcov/rcovtask'
|
40
|
+
Rcov::RcovTask.new do |test|
|
41
|
+
test.libs << 'test'
|
42
|
+
test.pattern = 'test/**/test_*.rb'
|
43
|
+
test.verbose = true
|
44
|
+
test.rcov_opts << '--exclude "gems/*"'
|
45
|
+
end
|
46
|
+
|
47
|
+
task :default => :test
|
48
|
+
|
49
|
+
require 'rake/rdoctask'
|
50
|
+
Rake::RDocTask.new do |rdoc|
|
51
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
52
|
+
|
53
|
+
rdoc.rdoc_dir = 'rdoc'
|
54
|
+
rdoc.title = "db_leftovers #{version}"
|
55
|
+
rdoc.rdoc_files.include('README*')
|
56
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
57
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "db_leftovers"
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Paul A. Jungwirth"]
|
12
|
+
s.date = "2012-01-17"
|
13
|
+
s.description = " Define indexes and foreign keys for your Rails app\n in one place using an easy-to-read DSL,\n then run a rake task to bring your database up-to-date.\n"
|
14
|
+
s.email = "pj@illuminatedcomputing.com"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
"Gemfile",
|
22
|
+
"Gemfile.lock",
|
23
|
+
"LICENSE.txt",
|
24
|
+
"README.rdoc",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"db_leftovers.gemspec",
|
28
|
+
"lib/db_leftovers.rb",
|
29
|
+
"lib/db_leftovers/dsl.rb",
|
30
|
+
"lib/tasks/leftovers.rake",
|
31
|
+
"test/helper.rb",
|
32
|
+
"test/test_db_leftovers.rb"
|
33
|
+
]
|
34
|
+
s.homepage = "http://github.com/pjungwir/db_leftovers"
|
35
|
+
s.licenses = ["MIT"]
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
s.rubygems_version = "1.8.10"
|
38
|
+
s.summary = "Used to define indexes and foreign keys for your Rails app"
|
39
|
+
|
40
|
+
if s.respond_to? :specification_version then
|
41
|
+
s.specification_version = 3
|
42
|
+
|
43
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
44
|
+
s.add_runtime_dependency(%q<rails>, [">= 3.0.0"])
|
45
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
46
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
47
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
|
48
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
49
|
+
else
|
50
|
+
s.add_dependency(%q<rails>, [">= 3.0.0"])
|
51
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
52
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
53
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
54
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
55
|
+
end
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<rails>, [">= 3.0.0"])
|
58
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
59
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
60
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
61
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
@@ -0,0 +1,281 @@
|
|
1
|
+
module DBLeftovers
|
2
|
+
|
3
|
+
class Definition
|
4
|
+
def self.define(opts={}, &block)
|
5
|
+
opts = {
|
6
|
+
:do_indexes => true,
|
7
|
+
:do_foreign_keys => true
|
8
|
+
}.merge(opts)
|
9
|
+
dsl = DSL.new
|
10
|
+
dsl.define(&block)
|
11
|
+
dsl.record_indexes if opts[:do_indexes]
|
12
|
+
dsl.record_foreign_keys if opts[:do_foreign_keys]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
# Just a struct to hold all the info for one index:
|
20
|
+
class Index
|
21
|
+
attr_accessor :table_name, :column_names, :index_name,
|
22
|
+
:where_clause, :unique
|
23
|
+
|
24
|
+
def initialize(table_name, column_names, opts={})
|
25
|
+
opts = {
|
26
|
+
:where => nil,
|
27
|
+
:unique => false,
|
28
|
+
}.merge(opts)
|
29
|
+
opts.keys.each do |k|
|
30
|
+
raise "Unknown option: #{k}" unless [:where, :unique, :name].include?(k)
|
31
|
+
end
|
32
|
+
@table_name = table_name
|
33
|
+
@column_names = column_names
|
34
|
+
@where_clause = opts[:where]
|
35
|
+
@unique = opts[:unique]
|
36
|
+
@index_name = opts[:name] || choose_name(table_name, column_names)
|
37
|
+
end
|
38
|
+
|
39
|
+
def unique?
|
40
|
+
@unique
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def choose_name(table_name, column_names)
|
46
|
+
"index_#{table_name}_on_#{column_names.join('_and_')}"
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
class ForeignKey
|
53
|
+
attr_accessor :constraint_name, :from_table, :from_column, :to_table, :to_column, :set_null, :cascade
|
54
|
+
|
55
|
+
def initialize(constraint_name, from_table, from_column, to_table, to_column, opts={})
|
56
|
+
opts = {
|
57
|
+
:set_null => false,
|
58
|
+
:cascade => false
|
59
|
+
}.merge(opts)
|
60
|
+
opts.keys.each do |k|
|
61
|
+
raise "Unknown option: #{k}" unless [:set_null, :cascade].include?(k)
|
62
|
+
end
|
63
|
+
@constraint_name = constraint_name
|
64
|
+
@from_table = from_table
|
65
|
+
@from_column = from_column
|
66
|
+
@to_table = to_table
|
67
|
+
@to_column = to_column
|
68
|
+
|
69
|
+
@set_null = opts[:set_null]
|
70
|
+
@cascade = opts[:cascade]
|
71
|
+
|
72
|
+
raise "ON DELETE can't be both set_null and cascade" if @set_null and @cascade
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
class TableDSL
|
77
|
+
def initialize(dsl, table_name)
|
78
|
+
@dsl = dsl
|
79
|
+
@table_name = table_name
|
80
|
+
end
|
81
|
+
|
82
|
+
def define(&block)
|
83
|
+
instance_eval(&block)
|
84
|
+
end
|
85
|
+
|
86
|
+
def index(column_names, opts={})
|
87
|
+
@dsl.index(@table_name, column_names, opts)
|
88
|
+
end
|
89
|
+
|
90
|
+
def foreign_key(from_column, to_table, to_column='id', opts={})
|
91
|
+
@dsl.foreign_key(@table_name, from_column, to_table, to_column, opts)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
class DSL
|
96
|
+
def initialize
|
97
|
+
@indexes_by_table = {} # Set from the DSL
|
98
|
+
@old_indexes = lookup_all_indexes
|
99
|
+
@new_indexes = {}
|
100
|
+
|
101
|
+
@foreign_keys_by_table = {} # Set from the DSL
|
102
|
+
@old_foreign_keys = lookup_all_foreign_keys
|
103
|
+
@new_foreign_keys = {}
|
104
|
+
end
|
105
|
+
|
106
|
+
def define(&block)
|
107
|
+
instance_eval(&block)
|
108
|
+
end
|
109
|
+
|
110
|
+
def table(table_name, &block)
|
111
|
+
table_dsl = TableDSL.new(self, table_name)
|
112
|
+
table_dsl.define(&block)
|
113
|
+
end
|
114
|
+
|
115
|
+
def index(table_name, column_names, opts={})
|
116
|
+
column_names = [column_names].flatten
|
117
|
+
# puts "#{table_name}.[#{column_names.join(',')}]"
|
118
|
+
add_index(Index.new(table_name, column_names, opts))
|
119
|
+
end
|
120
|
+
|
121
|
+
def foreign_key(from_table, from_column, to_table, to_column='id', opts={})
|
122
|
+
add_foreign_key(ForeignKey.new(name_constraint(from_table, from_column), from_table, from_column, to_table, to_column, opts))
|
123
|
+
end
|
124
|
+
|
125
|
+
def record_indexes
|
126
|
+
# First create any new indexes:
|
127
|
+
@indexes_by_table.each do |table_name, indexes|
|
128
|
+
indexes.each do |idx|
|
129
|
+
# puts "#{idx.table_name}.[#{idx.column_names.join(',')}]"
|
130
|
+
if index_exists?(idx)
|
131
|
+
puts "Index already exists: #{idx.index_name} on #{idx.table_name}"
|
132
|
+
else
|
133
|
+
execute_add_index(idx)
|
134
|
+
puts "Created index: #{idx.index_name} on #{idx.table_name}"
|
135
|
+
end
|
136
|
+
@new_indexes[truncate_index_name(idx.index_name)] = table_name
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
# Now drop any old indexes that are no longer in the definition file:
|
141
|
+
@old_indexes.each do |index_name, table_name|
|
142
|
+
if not @new_indexes[index_name]
|
143
|
+
# puts "#{index_name} #{table_name}"
|
144
|
+
execute_drop_index(table_name, index_name)
|
145
|
+
puts "Dropped index: #{index_name} on #{table_name}"
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
def record_foreign_keys
|
151
|
+
# First create any new foreign keys:
|
152
|
+
@foreign_keys_by_table.each do |table_name, fks|
|
153
|
+
fks.each do |fk|
|
154
|
+
if foreign_key_exists?(fk)
|
155
|
+
puts "Foreign Key already exists: #{fk.constraint_name} on #{fk.from_table}"
|
156
|
+
else
|
157
|
+
execute_add_foreign_key(fk)
|
158
|
+
puts "Created foreign key: #{fk.constraint_name} on #{fk.from_table}"
|
159
|
+
end
|
160
|
+
@new_foreign_keys[fk.constraint_name] = fk
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
# Now drop any old foreign keys that are no longer in the definition file:
|
165
|
+
@old_foreign_keys.each do |constraint_name, fk|
|
166
|
+
if not @new_foreign_keys[constraint_name]
|
167
|
+
execute_drop_foreign_key(fk.from_table, fk.from_column)
|
168
|
+
puts "Dropped foreign key: #{constraint_name} on #{fk.from_table}"
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
private
|
174
|
+
|
175
|
+
def add_index(idx)
|
176
|
+
t = (@indexes_by_table[idx.table_name] ||= [])
|
177
|
+
t << idx
|
178
|
+
end
|
179
|
+
|
180
|
+
def add_foreign_key(fk)
|
181
|
+
t = (@foreign_keys_by_table[fk.from_table] ||= [])
|
182
|
+
t << fk
|
183
|
+
end
|
184
|
+
|
185
|
+
def lookup_all_indexes
|
186
|
+
ret = {}
|
187
|
+
sql = <<-EOQ
|
188
|
+
SELECT n.nspname as "Schema", c.relname as "Name",
|
189
|
+
CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' END as "Type",
|
190
|
+
u.usename as "Owner",
|
191
|
+
c2.relname as "Table"
|
192
|
+
FROM pg_catalog.pg_class c
|
193
|
+
JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid
|
194
|
+
JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid
|
195
|
+
LEFT JOIN pg_catalog.pg_user u ON u.usesysid = c.relowner
|
196
|
+
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
|
197
|
+
WHERE c.relkind IN ('i','')
|
198
|
+
AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
|
199
|
+
AND pg_catalog.pg_table_is_visible(c.oid)
|
200
|
+
AND c.relname NOT LIKE '%_pkey'
|
201
|
+
AND c2.relname NOT IN ('delayed_jobs', 'schema_migrations')
|
202
|
+
ORDER BY 1,2;
|
203
|
+
EOQ
|
204
|
+
ActiveRecord::Base.connection.select_rows(sql).each do |schema, index_name, object_type, owner, table_name|
|
205
|
+
ret[index_name] = table_name
|
206
|
+
end
|
207
|
+
return ret
|
208
|
+
end
|
209
|
+
|
210
|
+
def lookup_all_foreign_keys
|
211
|
+
ret = {}
|
212
|
+
sql = <<-EOQ
|
213
|
+
SELECT t.constraint_name, t.table_name, k.column_name, t.constraint_type, c.table_name, c.column_name
|
214
|
+
FROM information_schema.table_constraints t,
|
215
|
+
information_schema.constraint_column_usage c,
|
216
|
+
information_schema.key_column_usage k
|
217
|
+
WHERE t.constraint_name = c.constraint_name
|
218
|
+
AND k.constraint_name = c.constraint_name
|
219
|
+
AND t.constraint_type = 'FOREIGN KEY'
|
220
|
+
EOQ
|
221
|
+
ActiveRecord::Base.connection.select_rows(sql).each do |constr_name, from_table, from_column, constr_type, to_table, to_column|
|
222
|
+
ret[constr_name] = ForeignKey.new(constr_name, from_table, from_column, to_table, to_column)
|
223
|
+
end
|
224
|
+
return ret
|
225
|
+
end
|
226
|
+
|
227
|
+
def truncate_index_name(index_name)
|
228
|
+
index_name[0,63]
|
229
|
+
end
|
230
|
+
|
231
|
+
def index_exists?(idx)
|
232
|
+
@old_indexes[truncate_index_name(idx.index_name)]
|
233
|
+
end
|
234
|
+
|
235
|
+
def foreign_key_exists?(fk)
|
236
|
+
@old_foreign_keys[fk.constraint_name]
|
237
|
+
end
|
238
|
+
|
239
|
+
def execute_add_index(idx)
|
240
|
+
unique = idx.unique? ? 'UNIQUE' : ''
|
241
|
+
where = idx.where_clause.present? ? "WHERE #{idx.where_clause}" : ''
|
242
|
+
|
243
|
+
sql = <<-EOQ
|
244
|
+
CREATE #{unique} INDEX #{idx.index_name}
|
245
|
+
ON #{idx.table_name}
|
246
|
+
(#{idx.column_names.join(', ')})
|
247
|
+
#{where}
|
248
|
+
EOQ
|
249
|
+
ActiveRecord::Base.connection.execute(sql)
|
250
|
+
end
|
251
|
+
|
252
|
+
def execute_drop_index(table_name, index_name)
|
253
|
+
sql = <<-EOQ
|
254
|
+
DROP INDEX #{index_name}
|
255
|
+
EOQ
|
256
|
+
ActiveRecord::Base.connection.execute(sql)
|
257
|
+
end
|
258
|
+
|
259
|
+
def execute_add_foreign_key(fk)
|
260
|
+
on_delete = "ON DELETE CASCADE" if fk.cascade
|
261
|
+
on_delete = "ON DELETE SET NULL" if fk.set_null
|
262
|
+
ActiveRecord::Base.connection.execute %{ALTER TABLE #{fk.from_table}
|
263
|
+
ADD CONSTRAINT #{fk.constraint_name}
|
264
|
+
FOREIGN KEY (#{fk.from_column})
|
265
|
+
REFERENCES #{fk.to_table} (#{fk.to_column})
|
266
|
+
#{on_delete}}
|
267
|
+
end
|
268
|
+
|
269
|
+
def execute_drop_foreign_key(from_table, from_column)
|
270
|
+
constraint_name = name_constraint(from_table, from_column)
|
271
|
+
ActiveRecord::Base.connection.execute %{ALTER TABLE #{from_table}
|
272
|
+
DROP CONSTRAINT #{constraint_name}}
|
273
|
+
end
|
274
|
+
|
275
|
+
def name_constraint(from_table, from_column)
|
276
|
+
"fk_#{from_table}_#{from_column}"
|
277
|
+
end
|
278
|
+
|
279
|
+
end
|
280
|
+
|
281
|
+
end
|
data/lib/db_leftovers.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
namespace :db do
|
2
|
+
|
3
|
+
desc "Set up indexes and foreign keys"
|
4
|
+
task :leftovers, [] => [:environment] do
|
5
|
+
load File.join(::Rails.root.to_s, 'config', 'db_leftovers.rb')
|
6
|
+
end
|
7
|
+
|
8
|
+
desc "Drop all the indexes"
|
9
|
+
task :drop_indexes, [] => [:environment] do
|
10
|
+
DBLeftovers::Definition.define(:do_indexes => true, :do_foreign_keys => false) do
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
desc "Drop all the foreign keys"
|
15
|
+
task :drop_foreign_keys, [] => [:environment] do
|
16
|
+
DBLeftovers::Definition.define(:do_indexes => false, :do_foreign_keys => true) do
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'shoulda'
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
+
require 'db_leftovers'
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: db_leftovers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Paul A. Jungwirth
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-17 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: &76729710 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *76729710
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: shoulda
|
27
|
+
requirement: &76729080 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *76729080
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: bundler
|
38
|
+
requirement: &76615000 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.0.0
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *76615000
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: jeweler
|
49
|
+
requirement: &76614690 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.6.4
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *76614690
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: rcov
|
60
|
+
requirement: &76614430 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *76614430
|
69
|
+
description: ! " Define indexes and foreign keys for your Rails app\n in
|
70
|
+
one place using an easy-to-read DSL,\n then run a rake task to bring your
|
71
|
+
database up-to-date.\n"
|
72
|
+
email: pj@illuminatedcomputing.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files:
|
76
|
+
- LICENSE.txt
|
77
|
+
- README.rdoc
|
78
|
+
files:
|
79
|
+
- .document
|
80
|
+
- Gemfile
|
81
|
+
- Gemfile.lock
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.rdoc
|
84
|
+
- Rakefile
|
85
|
+
- VERSION
|
86
|
+
- db_leftovers.gemspec
|
87
|
+
- lib/db_leftovers.rb
|
88
|
+
- lib/db_leftovers/dsl.rb
|
89
|
+
- lib/tasks/leftovers.rake
|
90
|
+
- test/helper.rb
|
91
|
+
- test/test_db_leftovers.rb
|
92
|
+
homepage: http://github.com/pjungwir/db_leftovers
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
none: false
|
101
|
+
requirements:
|
102
|
+
- - ! '>='
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
segments:
|
106
|
+
- 0
|
107
|
+
hash: -340944739
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
none: false
|
110
|
+
requirements:
|
111
|
+
- - ! '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 1.8.10
|
117
|
+
signing_key:
|
118
|
+
specification_version: 3
|
119
|
+
summary: Used to define indexes and foreign keys for your Rails app
|
120
|
+
test_files: []
|