Skip to content

Adds triggers to Database Schema validations

What does this MR do and why?

This first MR aims to refactor the index implementation to support other types of Schema Objects (like triggers, functions, etc)

We're checking for:

  • Schema objects were definition between the DB and db/structure.sql is different;
  • Schema objects are present in db/structure.sql and missing in the DB
  • Schema objects are present in DB and missing db/structure.sql

A rake task will use these classes in the future.

Screenshots or screen recordings

Screenshots are required for UI changes, and strongly recommended for all other merge requests.

How to set up and validate locally

Testing DifferentDefinitionTriggers
  1. Change some existent trigger definition in structure.sql, like CREATE TRIGGER projects_loose_fk_trigger AFTER DELETE ON projects REFERENCING OLD TABLE AS old_table FOR EACH STATEMENT EXECUTE FUNCTION my_function();
  2. Check if the projects_loose_fk_trigger trigger is present in DifferentDefinitionTriggers output:
structure_sql = Gitlab::Database::SchemaValidation::StructureSql.new(Rails.root.join('db/structure.sql'), 'public')
database = Gitlab::Database::SchemaValidation::Database.new(ActiveRecord::Base.connection)
Gitlab::Database::SchemaValidation::Validators::DifferentDefinitionTriggers.new(structure_sql, database).execute

=> [#<struct ... type="different_definition_triggers", object_name="projects_loose_fk_trigger", statement="CREATE TRIGGER projects_loose_fk_trigger...">]
Testing ExtraTriggers (that are present in the Database and not in the structure.sql)
  1. Remove some existent trigger definition from structure.sql, like CREATE TRIGGER projects_loose_fk_trigger
  2. Check if the projects_loose_fk_trigger trigger is present in ExtraTriggers output:
structure_sql = Gitlab::Database::SchemaValidation::StructureSql.new(Rails.root.join('db/structure.sql'), 'public')
database = Gitlab::Database::SchemaValidation::Database.new(ActiveRecord::Base.connection)
Gitlab::Database::SchemaValidation::Validators::ExtraTriggers.new(structure_sql, database).execute

=> [#<struct ... type="extra_triggers", object_name="projects_loose_fk_trigger", statement="CREATE TRIGGER projects_loose_fk_trigger ...">]
Testing MissingTriggers (that are present in the structure.sql and not in the Database)
  1. Drop projects_loose_fk_trigger from database: DROP TRIGGER projects_loose_fk_trigger ON projects;
  2. Check if the projects_loose_fk_trigger trigger is present in ExtraTriggers output:
structure_sql = Gitlab::Database::SchemaValidation::StructureSql.new(Rails.root.join('db/structure.sql'), 'public')
database = Gitlab::Database::SchemaValidation::Database.new(ActiveRecord::Base.connection)
Gitlab::Database::SchemaValidation::Validators::MissingTriggers.new(structure_sql, database).execute

=> [#<struct ... type="missing_triggers", object_name="projects_loose_fk_trigger", statement="CREATE TRIGGER projects_loose_fk_trigger ...">]

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #388015 (closed)

Edited by Leonardo da Rosa

Merge request reports

Loading