diff --git a/.simplecov b/.simplecov index abad181..21eddfc 100644 --- a/.simplecov +++ b/.simplecov @@ -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}%") diff --git a/README.markdown b/README.markdown index 55d4390..a3dfb5b 100644 --- a/README.markdown +++ b/README.markdown @@ -181,6 +181,18 @@ remove_foreign_key(:comments, column: :post_id, remove_index: true) PgSaurus v4.X requires Rails 5. Rails 5.2 is recommended. You can use the new Rails 5 semantics to create comments and indexes inline. +You also need to use the index order options using the Rails 5 semantics. + +```ruby +# THIS FAILS +add_index :books, ["author_id DESC NULLS FIRST", "publisher_id DESC NULLS LAST"], + name: "books_author_id_and_publisher_id" + +# DO THIS INSTEAD +add_index :books, ["author_id", "publisher_id"], + name: "books_author_id_and_publisher_id", + order: { author_id: "DESC NULLS FIRST", publisher_id: "DESC NULLS LAST" } +``` ### Migration notes - upgrading from Rails 4.1 diff --git a/lib/core_ext/active_record/connection_adapters/postgresql/schema_statements.rb b/lib/core_ext/active_record/connection_adapters/postgresql/schema_statements.rb index e8d6dfe..8ad6906 100644 --- a/lib/core_ext/active_record/connection_adapters/postgresql/schema_statements.rb +++ b/lib/core_ext/active_record/connection_adapters/postgresql/schema_statements.rb @@ -42,11 +42,11 @@ def indexes(table_name) result.map do |row| index_name = row[0] - unique = row[1] - indkey = row[2].split(" ").map(&:to_i) - inddef = row[3] - oid = row[4] - comment = row[5] + unique = row[1] + indkey = row[2].split(" ").map(&:to_i) + inddef = row[3] + oid = row[4] + comment = row[5] using, expressions, where = inddef.scan(/ USING (\w+?) \((.+?)\)(?: WHERE (.+))?\z/m).flatten diff --git a/pg_saurus.gemspec b/pg_saurus.gemspec index 13a3752..1c20379 100644 --- a/pg_saurus.gemspec +++ b/pg_saurus.gemspec @@ -11,17 +11,17 @@ Gem::Specification.new do |s| 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-24" 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 = ["blake131313@gmail.com".freeze, "arthur.shagall@gmail.com".freeze, "cryo28@gmail.com".freeze, "matt.dressel@gmail.com".freeze, "rubygems.org@bruceburdick.com".freeze] + s.executables = ["rails".freeze] s.extra_rdoc_files = [ "README.markdown" ] s.files = [ "README.markdown", "lib/colorized_text.rb", - "lib/core_ext/active_record/connection_adapters/abstract/schema_statements.rb", - "lib/core_ext/active_record/connection_adapters/postgresql_adapter.rb", + "lib/core_ext/active_record/connection_adapters/postgresql/schema_statements.rb", "lib/core_ext/active_record/errors.rb", "lib/core_ext/active_record/schema_dumper.rb", "lib/generators/pg_saurus/install/install_generator.rb", @@ -37,7 +37,6 @@ Gem::Specification.new do |s| "lib/pg_saurus/connection_adapters/abstract_adapter/trigger_methods.rb", "lib/pg_saurus/connection_adapters/foreign_key_definition.rb", "lib/pg_saurus/connection_adapters/function_definition.rb", - "lib/pg_saurus/connection_adapters/index_definition.rb", "lib/pg_saurus/connection_adapters/postgresql_adapter.rb", "lib/pg_saurus/connection_adapters/postgresql_adapter/comment_methods.rb", "lib/pg_saurus/connection_adapters/postgresql_adapter/extension_methods.rb", @@ -78,7 +77,7 @@ Gem::Specification.new do |s| ] s.homepage = "https://github.com/HornsAndHooves/pg_saurus".freeze s.licenses = ["MIT".freeze] - s.rubygems_version = "3.0.4".freeze + s.rubygems_version = "2.7.9".freeze s.summary = "ActiveRecord extensions for PostgreSQL.".freeze if s.respond_to? :specification_version then @@ -97,6 +96,7 @@ Gem::Specification.new do |s| s.add_development_dependency(%q.freeze, [">= 0"]) s.add_development_dependency(%q.freeze, [">= 0"]) s.add_development_dependency(%q.freeze, [">= 0"]) + s.add_development_dependency(%q.freeze, [">= 0"]) else s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, ["~> 5.2.3"]) @@ -110,6 +110,7 @@ Gem::Specification.new do |s| s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, [">= 0"]) + s.add_dependency(%q.freeze, [">= 0"]) end else s.add_dependency(%q.freeze, [">= 0"]) @@ -124,6 +125,7 @@ Gem::Specification.new do |s| s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, [">= 0"]) + s.add_dependency(%q.freeze, [">= 0"]) end end diff --git a/spec/active_record/schema_dumper_spec.rb b/spec/active_record/schema_dumper_spec.rb index cef2a2d..aad4087 100644 --- a/spec/active_record/schema_dumper_spec.rb +++ b/spec/active_record/schema_dumper_spec.rb @@ -100,6 +100,10 @@ @dump.should =~ /t.index \["title"\], name: "index_books_on_title_varchar_pattern_ops", opclass: :varchar_pattern_ops/ end + it "dumps indexes with simple columns with an alternate order" do + @dump.should =~ /t.index \["author_id", "publisher_id"\], name: "books_author_id_and_publisher_id", order: { author_id: :desc, publisher_id: "DESC NULLS LAST" }/ + end + it "dumps functional indexes with longer operator strings" do @dump.should =~ /t.index "lower\(name\) DESC NULLS LAST", name: "index_pets_on_lower_name_desc_nulls_last"/ end diff --git a/spec/dummy/db/migrate/20190213151821_create_books.rb b/spec/dummy/db/migrate/20190213151821_create_books.rb index e89f642..fd3526c 100644 --- a/spec/dummy/db/migrate/20190213151821_create_books.rb +++ b/spec/dummy/db/migrate/20190213151821_create_books.rb @@ -1,12 +1,18 @@ class CreateBooks < ActiveRecord::Migration[5.2] 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", "publisher_id"], + name: "books_author_id_and_publisher_id", + order: { author_id: "DESC NULLS FIRST", publisher_id: "DESC NULLS LAST" } + add_index :books, "title varchar_pattern_ops" add_index :books, "((tags->'attrs'->>'edition')::int)", name: "books_tags_json_index", skip_column_quoting: true diff --git a/spec/dummy/db/schema.rb b/spec/dummy/db/schema.rb index b70618f..408293e 100644 --- a/spec/dummy/db/schema.rb +++ b/spec/dummy/db/schema.rb @@ -42,11 +42,14 @@ FUNCTION_DEFINITION create_table "books", comment: "Information about books", force: :cascade do |t| + t.integer "author_id" + t.integer "publisher_id" t.string "title", comment: "Book title" t.json "tags" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index "((((tags -> 'attrs'::text) ->> 'edition'::text))::integer)", name: "books_tags_json_index", skip_column_quoting: true + t.index ["author_id", "publisher_id"], name: "books_author_id_and_publisher_id", order: { author_id: :desc, publisher_id: "DESC NULLS LAST" } t.index ["title"], name: "index_books_on_title_varchar_pattern_ops", opclass: :varchar_pattern_ops end