Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/Support_dumping_single_column_in…
Browse files Browse the repository at this point in the history
…dexes_with_an_alternate_order'

* origin/Support_dumping_single_column_indexes_with_an_alternate_order:
  Updated logic to correctly dumping single column indexes with an alternate order. diff --git a/lib/core_ext/active_record/connection_adapters/postgresql_adapter.rb b/lib/core_ext/active_record/connection_adapters/postgresql_adapter.rb index fd91fdb..7055acc 100644 --- a/lib/core_ext/active_record/connection_adapters/postgresql_adapter.rb +++ b/lib/core_ext/active_record/connection_adapters/postgresql_adapter.rb @@ -9,6 +9,12 @@ module ActiveRecord # :nodoc:        # Regex to find where clause in index statements        INDEX_WHERE_EXPRESSION = /WHERE (.+)$/

Version 3.7.1
  • Loading branch information
albertosaurus committed Oct 18, 2019
2 parents 51eb12d + 28847b6 commit 3e7ca40
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .simplecov
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ SimpleCov.start do
# Fail the build when coverage is weak:
at_exit do
SimpleCov.result.format!
threshold, actual = 97.83, SimpleCov.result.covered_percent
threshold, actual = 97.839, SimpleCov.result.covered_percent
if actual < threshold
msg = "\nLow coverage: "
msg << red("#{actual}%")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ class PostgreSQLAdapter
# Regex to find where clause in index statements
INDEX_WHERE_EXPRESSION = /WHERE (.+)$/

# Taken from https://github.com/postgres/postgres/blob/master/src/include/catalog/pg_index.h#L75
# Values are in reverse order
INDOPTION_DESC = 1
# NULLs are first instead of last
INDOPTION_NULLS_FIRST = 2

# Returns the list of all tables in the schema search path or a specified schema.
#
# == Patch:
Expand Down Expand Up @@ -71,7 +77,14 @@ def indexes(table_name, name = nil)
schemas = schema ? "ARRAY['#{schema}']" : 'current_schemas(false)'

result = query(<<-SQL, name)
SELECT distinct i.relname, d.indisunique, d.indkey, pg_get_indexdef(d.indexrelid), t.oid, am.amname, d.indclass
SELECT distinct i.relname,
d.indisunique,
d.indkey,
pg_get_indexdef(d.indexrelid),
t.oid,
am.amname,
d.indclass,
d.indoption
FROM pg_class t
INNER JOIN pg_index d ON t.oid = d.indrelid
INNER JOIN pg_class i ON d.indexrelid = i.oid
Expand All @@ -91,7 +104,8 @@ def indexes(table_name, name = nil)
:definition => row[3],
:id => row[4],
:access_method => row[5],
:operators => row[6].split(" ")
:operators => row[6].split(" "),
:options => row[7].split(" ").map(&:to_i)
}

column_names = find_column_names(table_name, index)
Expand Down Expand Up @@ -140,6 +154,25 @@ def find_column_names(table_name, index)
remove_type(functional_name)
end
end
else
# In case if column_names if not empty it contains list of column name taken from pg_attribute table.
# So we need to check indoption column and add DESC and NULLS LAST based on its value.
# https://stackoverflow.com/questions/18121103/how-to-get-the-index-column-orderasc-desc-nulls-first-from-postgresql/18128457#18128457
column_names = column_names.map.with_index do |column_name, column_index|
option = index[:options][column_index]

if option != 0
column_name << " DESC" if option & INDOPTION_DESC > 0

if option & INDOPTION_NULLS_FIRST > 0
column_name << " NULLS FIRST"
else
column_name << " NULLS LAST"
end
end

column_name
end
end

column_names
Expand Down
2 changes: 1 addition & 1 deletion lib/pg_saurus/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module PgSaurus
# Version of pg_saurus gem.
VERSION = "3.7.0"
VERSION = "3.7.1"
end
6 changes: 3 additions & 3 deletions pg_saurus.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
# -*- encoding: utf-8 -*-
# stub: pg_saurus 3.7.0 ruby lib
# stub: pg_saurus 3.7.1 ruby lib

Gem::Specification.new do |s|
s.name = "pg_saurus".freeze
s.version = "3.7.0"
s.version = "3.7.1"

s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
s.require_paths = ["lib".freeze]
s.authors = ["Potapov Sergey".freeze, "Arthur Shagall".freeze, "Artem Ignatyev".freeze, "Matt Dressel".freeze, "Bruce Burdick".freeze, "HornsAndHooves".freeze]
s.date = "2019-09-17"
s.date = "2019-10-18"
s.description = "ActiveRecord extensions for PostgreSQL. Provides useful tools for schema, foreign_key, index, function, trigger, comment and extension manipulations in migrations.".freeze
s.email = ["[email protected]".freeze, "[email protected]".freeze, "[email protected]".freeze, "[email protected]".freeze, "[email protected]".freeze]
s.extra_rdoc_files = [
Expand Down
4 changes: 4 additions & 0 deletions spec/active_record/schema_dumper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@
@dump.should =~ /add_index "books", \["title varchar_pattern_ops"\]/
end

it "dumps indexes with simple columns with an alternate order" do
@dump.should =~ /add_index "books", \["author_id DESC NULLS FIRST", "publisher_id DESC NULLS LAST"\]/
end

it "dumps functional indexes with longer operator strings" do
@dump.should =~ /add_index "pets", \["lower\(name\) DESC NULLS LAST"\]/
end
Expand Down
5 changes: 5 additions & 0 deletions spec/dummy/db/migrate/20190213151821_create_books.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
class CreateBooks < ActiveRecord::Migration
def change
create_table :books do |t|
t.integer :author_id
t.integer :publisher_id
t.string :title
t.json :tags

t.timestamps
end

add_index :books, ["author_id DESC NULLS FIRST", "publisher_id DESC NULLS LAST"],
name: "books_author_id_and_publisher_id"

add_index :books, "title varchar_pattern_ops"

add_index :books, "((tags->'attrs'->>'edition')::int)", name: "books_tags_json_index", skip_column_quoting: true
Expand Down
3 changes: 3 additions & 0 deletions spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@
FUNCTION_DEFINITION

create_table "books", force: :cascade do |t|
t.integer "author_id"
t.integer "publisher_id"
t.string "title"
t.json "tags"
t.datetime "created_at"
t.datetime "updated_at"
end

add_index "books", "((((tags -> 'attrs'::text) ->> 'edition'::text))::integer)", :name => "books_tags_json_index", :skip_column_quoting => true
add_index "books", ["author_id DESC NULLS FIRST", "publisher_id DESC NULLS LAST"], :name => "books_author_id_and_publisher_id"
add_index "books", ["title varchar_pattern_ops"], :name => "index_books_on_title_varchar_pattern_ops"

create_table "breeds", force: :cascade do |t|
Expand Down

0 comments on commit 3e7ca40

Please sign in to comment.