Skip to content

Commit 56f4cc4

Browse files
alnokares
authored andcommitted
Dumping partial indexes in schema
1 parent a8d8213 commit 56f4cc4

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

lib/arjdbc/postgresql/adapter.rb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,15 +1340,24 @@ def indexes(table_name, name = nil)
13401340
WHERE a.attrelid = #{oid}
13411341
AND a.attnum IN (#{indkey.join(",")})
13421342
SQL
1343-
1343+
13441344
columns = Hash[ columns.each { |column| column[0] = column[0].to_s } ]
13451345
column_names = columns.values_at(*indkey).compact
13461346

1347-
# add info on sort order for columns (only desc order is explicitly specified, asc is the default)
1348-
desc_order_columns = inddef.scan(/(\w+) DESC/).flatten
1349-
orders = desc_order_columns.any? ? Hash[ desc_order_columns.map { |column| [column, :desc] } ] : {}
1350-
1351-
column_names.empty? ? nil : IndexDefinition.new(table_name, index_name, unique, column_names, [], orders)
1347+
unless column_names.empty?
1348+
# add info on sort order for columns (only desc order is explicitly specified, asc is the default)
1349+
desc_order_columns = inddef.scan(/(\w+) DESC/).flatten
1350+
orders = desc_order_columns.any? ? Hash[ desc_order_columns.map { |column| [column, :desc] } ] : {}
1351+
1352+
if ActiveRecord::VERSION::MAJOR > 3 # AR4 supports `where` and `using` index options
1353+
where = inddef.scan(/WHERE (.+)$/).flatten[0]
1354+
using = inddef.scan(/USING (.+?) /).flatten[0].to_sym
1355+
1356+
IndexDefinition.new(table_name, index_name, unique, column_names, [], orders, where, nil, using)
1357+
else
1358+
IndexDefinition.new(table_name, index_name, unique, column_names, [], orders)
1359+
end
1360+
end
13521361
end
13531362
result.compact!
13541363
result

test/db/postgres/schema_dump_test.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# encoding: utf-8
12
require 'db/postgres'
23
require 'schema_dump'
34

@@ -76,9 +77,15 @@ def test_schema_dump_integer_with_limit_8_should_have_limit_8
7677
assert ! lines.empty?
7778
lines.each {|line| assert line =~ /(limit => 8)|(limit: 8)/ }
7879
end
79-
80+
81+
def test_schema_dumps_partial_indices
82+
index_definition = standard_dump.split(/\n/).grep(/add_index.*thing_partial_index/).first.strip
83+
84+
assert_equal 'add_index "things", ["created_at"], name: "thing_partial_index", where: "(name IS NOT NULL)", using: :btree', index_definition
85+
end if ar_version('4.0')
86+
8087
private
81-
88+
8289
def dump_with_data_types(io = StringIO.new)
8390
ActiveRecord::SchemaDumper.ignore_tables = [/^[^d]/] # keep data_types
8491
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, io)

test/models/thing.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ def self.up
55
t.timestamps
66
end
77
add_index :things, :name, :unique => true
8+
add_index :things, :created_at, :where => 'name IS NOT NULL', :name => "thing_partial_index"
89
end
910

1011
def self.down

0 commit comments

Comments
 (0)