db_leftovers 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -106,7 +106,7 @@ You can repeat `table` calls for the same table several times if you like. This
106
106
 
107
107
  Lets you specify one or more tables you'd like db\_leftovers to ignore completely. No objects will be added/removed to these tables. This is useful if you have tables that shouldn't be managed under db\_leftovers.
108
108
 
109
- If you don't call `ignore`, the list of ignored tables defaults to `schema_migrations` and `delayed_jobs`. If you do call `ignore`, you should probably include those in your list. If you do want db\_leftovers to manage those tables, just say `ignore []`.
109
+ If you don't call `ignore`, the list of ignored tables defaults to `schema_migrations` and `delayed_jobs`. If you do call `ignore`, you should probably include those in your list also. If you want db\_leftovers to manage those tables after all, just say `ignore []`.
110
110
 
111
111
 
112
112
 
@@ -194,6 +194,12 @@ Contributing to db\_leftovers
194
194
  * Make be sure to add tests for it. This is important so I don't break it in a future version unintentionally.
195
195
  * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, that is fine, but please isolate that change to its own commit so I can cherry-pick around it.
196
196
 
197
+ Commands for building/releasing/installing:
198
+
199
+ * `rake build`
200
+ * `rake install`
201
+ * `rake release`
202
+
197
203
  Copyright
198
204
  ---------
199
205
 
@@ -17,10 +17,12 @@ module DBLeftovers
17
17
  def execute_add_index(idx)
18
18
  unique = idx.unique? ? 'UNIQUE' : ''
19
19
  where = idx.where_clause.present? ? "WHERE #{idx.where_clause}" : ''
20
+ using = idx.using_clause.present? ? "USING #{idx.using_clause}" : ''
20
21
 
21
22
  sql = <<-EOQ
22
23
  CREATE #{unique} INDEX #{idx.index_name}
23
24
  ON #{idx.table_name}
25
+ #{using}
24
26
  (#{idx.column_names.join(', ')})
25
27
  #{where}
26
28
  EOQ
@@ -3,19 +3,21 @@ module DBLeftovers
3
3
  # Just a struct to hold all the info for one index:
4
4
  class Index
5
5
  attr_accessor :table_name, :column_names, :index_name,
6
- :where_clause, :unique
6
+ :where_clause, :using_clause, :unique
7
7
 
8
8
  def initialize(table_name, column_names, opts={})
9
9
  opts = {
10
10
  :where => nil,
11
11
  :unique => false,
12
+ :using => nil
12
13
  }.merge(opts)
13
14
  opts.keys.each do |k|
14
- raise "Unknown option: #{k}" unless [:where, :unique, :name].include?(k)
15
+ raise "Unknown option: #{k}" unless [:where, :unique, :using, :name].include?(k)
15
16
  end
16
17
  @table_name = table_name.to_s
17
18
  @column_names = [column_names].flatten.map{|x| x.to_s}
18
19
  @where_clause = opts[:where]
20
+ @using_clause = opts[:using]
19
21
  @unique = !!opts[:unique]
20
22
  @index_name = (opts[:name] || choose_name(@table_name, @column_names)).to_s
21
23
  end
@@ -29,11 +31,12 @@ module DBLeftovers
29
31
  other.column_names == column_names and
30
32
  other.index_name == index_name and
31
33
  other.where_clause == where_clause and
34
+ other.using_clause == using_clause and
32
35
  other.unique == unique
33
36
  end
34
37
 
35
38
  def to_s
36
- "<#{@index_name}: #{@table_name}.[#{column_names.join(",")}] unique=#{@unique}, where=#{@where_clause}>"
39
+ "<#{@index_name}: #{@table_name}.[#{column_names.join(",")}] unique=#{@unique}, where=#{@where_clause}, using=#{@using_clause}>"
37
40
  end
38
41
 
39
42
  private
@@ -15,6 +15,7 @@ module DBLeftovers
15
15
  i.relname AS index_name,
16
16
  ix.indisunique AS is_unique,
17
17
  array_to_string(ix.indkey, ',') AS column_numbers,
18
+ array_to_string(ix.indclass, ',') AS op_numbers,
18
19
  pg_get_expr(ix.indpred, ix.indrelid) AS where_clause
19
20
  FROM pg_class t,
20
21
  pg_class i,
@@ -33,16 +34,20 @@ module DBLeftovers
33
34
  ix.indexrelid,
34
35
  ix.indrelid,
35
36
  ix.indkey,
37
+ ix.indclass,
36
38
  ix.indpred
37
39
  ORDER BY t.relname, i.relname
38
40
  EOQ
39
- @conn.select_rows(sql).each do |indexrelid, indrelid, table_name, index_name, is_unique, column_numbers, where_clause|
41
+ @conn.select_rows(sql).each do |indexrelid, indrelid, table_name, index_name, is_unique, column_numbers, op_numbers, where_clause|
40
42
  where_clause = remove_outer_parens(where_clause) if where_clause
43
+ index_method = index_method_for_op(op_numbers.split(",").first)
44
+ index_method = nil if index_method == 'btree'
41
45
  ret[index_name] = Index.new(
42
46
  table_name,
43
47
  column_names_for_index(indrelid, column_numbers.split(",")),
44
48
  unique: is_unique == 't',
45
49
  where: where_clause,
50
+ using: index_method,
46
51
  name: index_name
47
52
  )
48
53
  end
@@ -133,6 +138,15 @@ module DBLeftovers
133
138
  end
134
139
  end
135
140
 
141
+ def index_method_for_op(op_id)
142
+ @conn.select_value(<<-EOQ)
143
+ SELECT am.amname
144
+ FROM pg_opclass op, pg_am am
145
+ WHERE op.oid = #{op_id}
146
+ AND op.opcmethod = am.oid
147
+ EOQ
148
+ end
149
+
136
150
  def remove_outer_parens(str)
137
151
  str ? str.gsub(/^\((.*)\)$/, '\1') : nil
138
152
  end
@@ -1,3 +1,3 @@
1
1
  module DbLeftovers
2
- VERSION = '1.2.0'
2
+ VERSION = '1.3.0'
3
3
  end
@@ -500,4 +500,15 @@ describe DBLeftovers do
500
500
  @db.sqls.should have(0).items
501
501
  end
502
502
 
503
+ it "should accept `USING` for indexes" do
504
+ @db.starts_with([], [], [])
505
+ DBLeftovers::Definition.define :db_interface => @db do
506
+ index :libraries, :lonlat, using: 'gist'
507
+ end
508
+ @db.sqls.should have(1).item
509
+ @db.should have_seen_sql <<-EOQ
510
+ CREATE INDEX index_libraries_on_lonlat ON libraries USING gist (lonlat)
511
+ EOQ
512
+ end
513
+
503
514
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: db_leftovers
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -111,7 +111,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
111
111
  version: '0'
112
112
  segments:
113
113
  - 0
114
- hash: 1094789418074050073
114
+ hash: 200664402728863992
115
115
  required_rubygems_version: !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements: