Skip to content

Commit

Permalink
Review database structures (#1167)
Browse files Browse the repository at this point in the history
* Add new tooling for making sure db schema up to snuff
* Flip MissingIndexChecker to true

* Resolve RedundantIndexChecker on score
  • Loading branch information
epugh authored Jan 10, 2025
1 parent 8c72f7b commit 9aad230
Show file tree
Hide file tree
Showing 33 changed files with 573 additions and 19 deletions.
444 changes: 444 additions & 0 deletions .database_consistency.todo.yml

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions .database_consistency.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Ignore false positive from Rails' ActionText and ActiveStorage
ActionText::RichText:
enabled: false
ActiveStorage::Attachment:
enabled: false
ActiveStorage::Blob:
enabled: false
ActiveStorage::VariantRecord:
enabled: false
Ahoy::Event:
enabled: false
Ahoy::Visit:
enabled: false
3 changes: 1 addition & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ group :development do
# Add speed badges [https://github.com/MiniProfiler/rack-mini-profiler]
# gem 'rack-mini-profiler'

# # Highlight the fine-grained location where an error occurred [https://github.com/ruby/error_highlight]

gem 'database_consistency', '~> 2.0', require: false
gem 'debugbar'
gem 'derailed_benchmarks'
gem 'letter_opener'
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ GEM
csv (3.3.2)
d3-rails (3.5.17)
railties (>= 3.1)
database_consistency (2.0.3)
activerecord (>= 3.2)
date (3.4.1)
debug (1.10.0)
irb (~> 1.10)
Expand Down Expand Up @@ -563,6 +565,7 @@ DEPENDENCIES
capybara
colorize
d3-rails (~> 3.5.5)
database_consistency (~> 2.0)
debug
debugbar
derailed_benchmarks
Expand Down
4 changes: 4 additions & 0 deletions app/models/announcement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
# updated_at :datetime not null
# author_id :integer
#
# Indexes
#
# index_announcements_author_id (author_id)
#
class Announcement < ApplicationRecord
belongs_to :author, class_name: 'User'
has_many :announcement_viewed, dependent: :destroy
Expand Down
4 changes: 4 additions & 0 deletions app/models/announcement_viewed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
# announcement_id :integer
# user_id :integer
#
# Indexes
#
# index_announcement_viewed_announcement_id (announcement_id)
#
class AnnouncementViewed < ApplicationRecord
self.table_name = 'announcement_viewed'
belongs_to :user
Expand Down
1 change: 1 addition & 0 deletions app/models/api_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# Indexes
#
# index_api_keys_on_token_digest (token_digest)
# index_api_keys_user_id (user_id)
#
class ApiKey < ApplicationRecord
HMAC_SECRET_KEY = Rails.application.secret_key_base
Expand Down
1 change: 1 addition & 0 deletions app/models/book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# Indexes
#
# index_books_on_selection_strategy_id (selection_strategy_id)
# index_books_owner_id (owner_id)
#
# Foreign Keys
#
Expand Down
3 changes: 2 additions & 1 deletion app/models/case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
#
# Indexes
#
# user_id (owner_id)
# index_cases_book_id (book_id)
# user_id (owner_id)
#
# Foreign Keys
#
Expand Down
4 changes: 4 additions & 0 deletions app/models/permission.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
# updated_at :datetime not null
# user_id :integer
#
# Indexes
#
# index_permissions_user_id (user_id)
#

class Permission < ApplicationRecord
# Associations
Expand Down
8 changes: 4 additions & 4 deletions app/models/score.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
#
# Indexes
#
# case_id (case_id)
# index_case_scores_on_annotation_id (annotation_id)
# support_last_score (updated_at,created_at,id)
# user_id (user_id)
# case_id (case_id)
# index_case_scores_annotation_id (annotation_id) UNIQUE
# support_last_score (updated_at,created_at,id)
# user_id (user_id)
#
# Foreign Keys
#
Expand Down
4 changes: 4 additions & 0 deletions app/models/scorer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
# updated_at :datetime not null
# owner_id :integer
#
# Indexes
#
# index_scorers_owner_id (owner_id)
#

require 'scale_serializer'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddAnnouncementsAuthorIdIndex < ActiveRecord::Migration[8.0]
def change
add_index :announcements, :author_id, name: :index_announcements_author_id
end
end
5 changes: 5 additions & 0 deletions db/migrate/20250110133916_add_api_keys_user_id_index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddApiKeysUserIdIndex < ActiveRecord::Migration[8.0]
def change
add_index :api_keys, :user_id, name: :index_api_keys_user_id
end
end
5 changes: 5 additions & 0 deletions db/migrate/20250110133917_add_books_owner_id_index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddBooksOwnerIdIndex < ActiveRecord::Migration[8.0]
def change
add_index :books, :owner_id, name: :index_books_owner_id
end
end
5 changes: 5 additions & 0 deletions db/migrate/20250110133953_add_cases_book_id_index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCasesBookIdIndex < ActiveRecord::Migration[8.0]
def change
add_index :cases, :book_id, name: :index_cases_book_id
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddAnnouncementViewedAnnouncementIdIndex < ActiveRecord::Migration[8.0]
def change
add_index :announcement_viewed, :announcement_id, name: :index_announcement_viewed_announcement_id
end
end
5 changes: 5 additions & 0 deletions db/migrate/20250110133955_add_scorers_owner_id_index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddScorersOwnerIdIndex < ActiveRecord::Migration[8.0]
def change
add_index :scorers, :owner_id, name: :index_scorers_owner_id
end
end
5 changes: 5 additions & 0 deletions db/migrate/20250110133956_add_permissions_user_id_index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddPermissionsUserIdIndex < ActiveRecord::Migration[8.0]
def change
add_index :permissions, :user_id, name: :index_permissions_user_id
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCaseScoresAnnotationIdIndex < ActiveRecord::Migration[8.0]
def change
add_index :case_scores, :annotation_id, name: :index_case_scores_annotation_id, unique: true
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemoveIndexCaseScoresOnAnnotationIdIndex < ActiveRecord::Migration[8.0]
def change
remove_index 'case_scores', name: 'index_case_scores_on_annotation_id'
end
end
11 changes: 9 additions & 2 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions test/fixtures/announcements.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
# updated_at :datetime not null
# author_id :integer
#
# Indexes
#
# index_announcements_author_id (author_id)
#

live_announcement:
text: We have a new Scorer! 🥳
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/books.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# Indexes
#
# index_books_on_selection_strategy_id (selection_strategy_id)
# index_books_owner_id (owner_id)
#
# Foreign Keys
#
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/cases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
#
# Indexes
#
# user_id (owner_id)
# index_cases_book_id (book_id)
# user_id (owner_id)
#
# Foreign Keys
#
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/scorers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
# updated_at :datetime not null
# owner_id :integer
#
# Indexes
#
# index_scorers_owner_id (owner_id)
#

quepid_default_scorer:
code: pass();
Expand Down
8 changes: 4 additions & 4 deletions test/fixtures/scores.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
#
# Indexes
#
# case_id (case_id)
# index_case_scores_on_annotation_id (annotation_id)
# support_last_score (updated_at,created_at,id)
# user_id (user_id)
# case_id (case_id)
# index_case_scores_annotation_id (annotation_id) UNIQUE
# support_last_score (updated_at,created_at,id)
# user_id (user_id)
#
# Foreign Keys
#
Expand Down
4 changes: 4 additions & 0 deletions test/models/announcement_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
# updated_at :datetime not null
# author_id :integer
#
# Indexes
#
# index_announcements_author_id (author_id)
#
require 'test_helper'

class AnnouncementTest < ActiveSupport::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/models/book_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# Indexes
#
# index_books_on_selection_strategy_id (selection_strategy_id)
# index_books_owner_id (owner_id)
#
# Foreign Keys
#
Expand Down
3 changes: 2 additions & 1 deletion test/models/case_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
#
# Indexes
#
# user_id (owner_id)
# index_cases_book_id (book_id)
# user_id (owner_id)
#
# Foreign Keys
#
Expand Down
4 changes: 4 additions & 0 deletions test/models/permission_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
# updated_at :datetime not null
# user_id :integer
#
# Indexes
#
# index_permissions_user_id (user_id)
#

require 'test_helper'

Expand Down
8 changes: 4 additions & 4 deletions test/models/score_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
#
# Indexes
#
# case_id (case_id)
# index_case_scores_on_annotation_id (annotation_id)
# support_last_score (updated_at,created_at,id)
# user_id (user_id)
# case_id (case_id)
# index_case_scores_annotation_id (annotation_id) UNIQUE
# support_last_score (updated_at,created_at,id)
# user_id (user_id)
#
# Foreign Keys
#
Expand Down
4 changes: 4 additions & 0 deletions test/models/scorer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
# updated_at :datetime not null
# owner_id :integer
#
# Indexes
#
# index_scorers_owner_id (owner_id)
#

require 'test_helper'

Expand Down

0 comments on commit 9aad230

Please sign in to comment.