Skip to content

Commit

Permalink
Add support of disabling per database connection (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
djezzzl committed Jul 5, 2023
1 parent 33c2fce commit d6f736a
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 13 deletions.
4 changes: 4 additions & 0 deletions lib/database_consistency/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def adapter
end
end

def database_name(model)
model.connection_db_config.name.to_s if model.respond_to?(:connection_db_config)
end

def postgresql?
adapter == 'postgresql'
end
Expand Down
5 changes: 3 additions & 2 deletions lib/database_consistency/processors/associations_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ class AssociationsProcessor < BaseProcessor

private

def check
def check # rubocop:disable Metrics/AbcSize
Helper.models.flat_map do |model|
next unless configuration.enabled?(model.name.to_s)
next unless configuration.enabled?('DatabaseConsistencyDatabases', Helper.database_name(model)) &&
configuration.enabled?(model.name.to_s)

Helper.first_level_associations(model).flat_map do |association|
enabled_checkers.flat_map do |checker_class|
Expand Down
5 changes: 3 additions & 2 deletions lib/database_consistency/processors/columns_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ class ColumnsProcessor < BaseProcessor

private

def check
def check # rubocop:disable Metrics/AbcSize
Helper.parent_models.flat_map do |model|
next unless configuration.enabled?(model.name.to_s)
next unless configuration.enabled?('DatabaseConsistencyDatabases', Helper.database_name(model)) &&
configuration.enabled?(model.name.to_s)

model.columns.flat_map do |column|
enabled_checkers.flat_map do |checker_class|
Expand Down
5 changes: 3 additions & 2 deletions lib/database_consistency/processors/enums_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ class EnumsProcessor < BaseProcessor

private

def check
def check # rubocop:disable Metrics/AbcSize
Helper.models.flat_map do |model|
next unless configuration.enabled?(model.name.to_s)
next unless configuration.enabled?('DatabaseConsistencyDatabases', Helper.database_name(model)) &&
configuration.enabled?(model.name.to_s)

model.defined_enums.keys.flat_map do |enum|
enabled_checkers.flat_map do |checker_class|
Expand Down
3 changes: 2 additions & 1 deletion lib/database_consistency/processors/indexes_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class IndexesProcessor < BaseProcessor

def check # rubocop:disable Metrics/AbcSize
Helper.parent_models.flat_map do |model|
next unless configuration.enabled?(model.name.to_s)
next unless configuration.enabled?('DatabaseConsistencyDatabases', Helper.database_name(model)) &&
configuration.enabled?(model.name.to_s)

indexes = model.connection.indexes(model.table_name)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ class ValidatorsFractionsProcessor < BaseProcessor
private

# @return [Array<Hash>]
def check
def check # rubocop:disable Metrics/AbcSize
Helper.parent_models.flat_map do |model|
next unless configuration.enabled?(model.name.to_s)
next unless configuration.enabled?('DatabaseConsistencyDatabases', Helper.database_name(model)) &&
configuration.enabled?(model.name.to_s)

model._validators.flat_map do |attribute, validators|
next unless attribute
Expand Down
5 changes: 3 additions & 2 deletions lib/database_consistency/processors/validators_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ class ValidatorsProcessor < BaseProcessor
private

# @return [Array<Hash>]
def check # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def check # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity
Helper.parent_models.flat_map do |model|
next unless configuration.enabled?(model.name.to_s)
next unless configuration.enabled?('DatabaseConsistencyDatabases', Helper.database_name(model)) &&
configuration.enabled?(model.name.to_s)

model.validators.flat_map do |validator|
next unless validator.respond_to?(:attributes)
Expand Down
6 changes: 6 additions & 0 deletions rails7-example/.database_consistency.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ DatabaseConsistencySettings:
color: true
log_level: DEBUG

# Configures database connections.
DatabaseConsistencyDatabases:
# Database connection name listed in database.yml.
secondary:
enabled: false # disables any check for +secondary+ database.

# Everything is enabled by default.
DatabaseConsistencyCheckers:
All:
Expand Down
2 changes: 2 additions & 0 deletions rails7-example/app/models/secondary/company.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Secondary::Company < SecondaryRecord
end
8 changes: 8 additions & 0 deletions rails7-example/app/models/secondary_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class SecondaryRecord < ActiveRecord::Base
self.abstract_class = true

connects_to database: {
writing: :secondary,
reading: :secondary
}
end
9 changes: 7 additions & 2 deletions rails7-example/config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ default: &default
timeout: 5000

development:
<<: *default
database: db/development.sqlite3
primary:
<<: *default
database: db/development.sqlite3
secondary:
<<: *default
database: db/development_secondary.sqlite3
migrations_paths: db/migrate_secondary

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class CreateCompanies < ActiveRecord::Migration[6.0]
def change
create_table :companies do |t|
t.string :name, null: false
end
end
end
18 changes: 18 additions & 0 deletions rails7-example/db/secondary_schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2019_10_12_163944) do
create_table "companies", force: :cascade do |t|
t.string "name", null: false
end

end

0 comments on commit d6f736a

Please sign in to comment.