|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +RSpec.describe 'Rails always add db index' do |
| 6 | + before { load_helpers(%w[helpers/parse_rails]) } |
| 7 | + |
| 8 | + context 'index exists' do |
| 9 | + let(:rewriter_name) { 'rails_best_practices/always_add_db_index' } |
| 10 | + let(:file_paths) { %w[db/schema.rb app/models/comment.rb] } |
| 11 | + let(:test_contents) { [<<~EOF.strip, <<~EOF.strip] } |
| 12 | + ActiveRecord::Schema[7.1].define(version: 2024_04_22_081242) do |
| 13 | + enable_extension "plpgsql" |
| 14 | +
|
| 15 | + create_table "comments", force: :cascade do |t| |
| 16 | + t.string "content", null: false |
| 17 | + t.bigint "post_id", null: false |
| 18 | + t.integer "user_id", null: false |
| 19 | + t.index ["post_id"], name: "index_comments_on_post_id" |
| 20 | + t.index ["user_id"], name: "index_comments_on_user_id" |
| 21 | + end |
| 22 | + end |
| 23 | + EOF |
| 24 | + class Comment < ApplicationRecord |
| 25 | + belongs_to :post |
| 26 | + belongs_to :user |
| 27 | + end |
| 28 | + EOF |
| 29 | + let(:warnings) { [] } |
| 30 | + |
| 31 | + include_examples 'warnable' |
| 32 | + end |
| 33 | + |
| 34 | + context 'index is missing' do |
| 35 | + let(:rewriter_name) { 'rails_best_practices/always_add_db_index' } |
| 36 | + let(:file_paths) { %w[db/schema.rb app/models/comment.rb] } |
| 37 | + let(:test_contents) { [<<~EOF.strip, <<~EOF.strip] } |
| 38 | + ActiveRecord::Schema[7.1].define(version: 2024_04_22_081242) do |
| 39 | + enable_extension "plpgsql" |
| 40 | +
|
| 41 | + create_table "comments", force: :cascade do |t| |
| 42 | + t.string "content", null: false |
| 43 | + t.bigint "post_id", null: false |
| 44 | + t.integer "user_id", null: false |
| 45 | + end |
| 46 | + end |
| 47 | + EOF |
| 48 | + class Comment < ApplicationRecord |
| 49 | + belongs_to :post |
| 50 | + belongs_to :user |
| 51 | + end |
| 52 | + EOF |
| 53 | + let(:warnings) { [ |
| 54 | + '/db/schema.rb#6: always add db index comments => ["post_id"]', |
| 55 | + '/db/schema.rb#7: always add db index comments => ["user_id"]' |
| 56 | + ] } |
| 57 | + |
| 58 | + include_examples 'warnable' |
| 59 | + end |
| 60 | + |
| 61 | + context 'polymorphic index exists' do |
| 62 | + let(:rewriter_name) { 'rails_best_practices/always_add_db_index' } |
| 63 | + let(:file_paths) { %w[db/schema.rb app/models/picture.rb] } |
| 64 | + let(:test_contents) { [<<~EOF.strip, <<~EOF.strip] } |
| 65 | + ActiveRecord::Schema[7.1].define(version: 2024_04_22_081242) do |
| 66 | + enable_extension "plpgsql" |
| 67 | +
|
| 68 | + create_table "pictures", force: :cascade do |t| |
| 69 | + t.string "name", null: false |
| 70 | + t.bigint "imageable_id", null: false |
| 71 | + t.string "imageable_type", null: false |
| 72 | + t.index ["imageable_type", "imageable_id"], name: "index_pictures_on_imageable_type_and_imageable_id" |
| 73 | + end |
| 74 | + end |
| 75 | + EOF |
| 76 | + class Picture < ApplicationRecord |
| 77 | + belongs_to :imageable, polymorphic: true |
| 78 | + end |
| 79 | + EOF |
| 80 | + let(:warnings) { [] } |
| 81 | + |
| 82 | + include_examples 'warnable' |
| 83 | + end |
| 84 | + |
| 85 | + context 'polymorphic index is missing' do |
| 86 | + let(:rewriter_name) { 'rails_best_practices/always_add_db_index' } |
| 87 | + let(:file_paths) { %w[db/schema.rb app/models/picture.rb] } |
| 88 | + let(:test_contents) { [<<~EOF.strip, <<~EOF.strip] } |
| 89 | + ActiveRecord::Schema[7.1].define(version: 2024_04_22_081242) do |
| 90 | + enable_extension "plpgsql" |
| 91 | +
|
| 92 | + create_table "pictures", force: :cascade do |t| |
| 93 | + t.string "name", null: false |
| 94 | + t.bigint "imageable_id", null: false |
| 95 | + t.string "imageable_type", null: false |
| 96 | + end |
| 97 | + end |
| 98 | + EOF |
| 99 | + class Picture < ApplicationRecord |
| 100 | + belongs_to :imageable, polymorphic: true |
| 101 | + end |
| 102 | + EOF |
| 103 | + let(:warnings) { [ |
| 104 | + '/db/schema.rb#6: always add db index pictures => ["imageable_type", "imageable_id"]', |
| 105 | + ] } |
| 106 | + |
| 107 | + include_examples 'warnable' |
| 108 | + end |
| 109 | + |
| 110 | + context 'foreign_key option index is missing' do |
| 111 | + let(:rewriter_name) { 'rails_best_practices/always_add_db_index' } |
| 112 | + let(:file_paths) { %w[db/schema.rb app/models/comment.rb] } |
| 113 | + let(:test_contents) { [<<~EOF.strip, <<~EOF.strip] } |
| 114 | + ActiveRecord::Schema[7.1].define(version: 2024_04_22_081242) do |
| 115 | + enable_extension "plpgsql" |
| 116 | +
|
| 117 | + create_table "comments", force: :cascade do |t| |
| 118 | + t.string "content", null: false |
| 119 | + t.integer "user_id", null: false |
| 120 | + end |
| 121 | + end |
| 122 | + EOF |
| 123 | + class Comment < ApplicationRecord |
| 124 | + belongs_to :commentor, foreign_key: :user_id |
| 125 | + end |
| 126 | + EOF |
| 127 | + let(:warnings) { [ |
| 128 | + '/db/schema.rb#6: always add db index comments => ["user_id"]' |
| 129 | + ] } |
| 130 | + |
| 131 | + include_examples 'warnable' |
| 132 | + end |
| 133 | +end |
0 commit comments