Schema Validation Gem: Add support to pg adapter
What does this MR do and why?
Adds support to PG gem connections
How to set up and validate locally
- Define some basic objects:
database_model = Gitlab::Database.database_base_models[Gitlab::Database::MAIN_DATABASE_NAME]
structure_sql = Gitlab::Schema::Validation::Sources::StructureSql.new(Rails.root.join('db/structure.sql'))
validators = Gitlab::Schema::Validation::Validators::Base.all_validators
- Generate the inconsistencies report, using the
ActiveRecord
adapter:
database = Gitlab::Schema::Validation::Sources::Database.new(database_model.connection)
ar_inconsistencies = Gitlab::Schema::Validation::Runner.new(structure_sql, database, validators: validators).execute.map(&:error_message)
- Generate the inconsistencies report, using the
PG
adapter:
db_config = database_model.connection.load_balancer.configuration.db_config.configuration_hash
connection = PG.connect(host: db_config[:host], port: db_config[:port], dbname: db_config[:database])
database = Gitlab::Schema::Validation::Sources::Database.new(connection)
pg_inconsistencies = Gitlab::Schema::Validation::Runner.new(structure_sql, database, validators: validators).execute.map(&:error_message)
- Both results must be equal:
ar_inconsistencies == pg_inconsistencies # => true
- Raises an error if the connection adapter is not supported:
Gitlab::Schema::Validation::Sources::Database.new({})
=> Gitlab::Schema::Validation::Sources::AdapterNotSupportedError: Hash is not supported
MR acceptance checklist
This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.
-
I have evaluated the MR acceptance checklist for this MR.
Related to #419123 (closed)
Edited by Leonardo da Rosa