Commit 00ad28c9 authored by Mehmet Emin INAC's avatar Mehmet Emin INAC Committed by Mayra Cabrera

Implement `require_migration!` helper method

The `require_migration!` helper method can be used to require migration
files in our specs.
parent fe915758
......@@ -37,15 +37,37 @@ ensures proper isolation.
To test an `ActiveRecord::Migration` class (i.e., a
regular migration `db/migrate` or a post-migration `db/post_migrate`), you
will need to manually `require` the migration file because it is not
autoloaded with Rails. Example:
will need to load the migration file by using the `require_migration!` helper
method because it is not autoloaded by Rails.
Example:
```ruby
require Rails.root.join('db', 'post_migrate', '20170526185842_migrate_pipeline_stages.rb')
require 'spec_helper'
require_migration!
RSpec.describe ...
```
### Test helpers
#### `require_migration!`
Since the migration files are not autoloaded by Rails, you will need to manually
load the migration file. To do so, you can use the `require_migration!` helper method
which can automatically load the correct migration file based on the spec file name.
For example, if your spec file is named as `populate_foo_column_spec.rb` then the
helper method will try to load `${schema_version}_populate_foo_column.rb` migration file.
In case there is no pattern between your spec file and the actual migration file,
you can provide the migration file name without the schema version, like so:
```ruby
require_migration!('populate_foo_column')
```
#### `table`
Use the `table` helper to create a temporary `ActiveRecord::Base`-derived model
......@@ -110,7 +132,8 @@ migration. You can find the complete spec in
```ruby
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20170526185842_migrate_pipeline_stages.rb')
require_migration!
RSpec.describe MigratePipelineStages do
# Create test data - pipeline and CI/CD jobs.
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20191108202723_add_unique_constraint_to_software_licenses.rb')
require_migration!
RSpec.describe AddUniqueConstraintToSoftwareLicenses do
let(:migration) { described_class.new }
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20200117194850_backfill_operations_feature_flags_iid.rb')
require_migration!
RSpec.describe BackfillOperationsFeatureFlagsIid do
let(:namespaces) { table(:namespaces) }
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20190917173107_backfill_software_licenses_spdx_identifiers.rb')
require_migration!
RSpec.describe BackfillSoftwareLicensesSpdxIdentifiers do
let(:software_licenses) { table(:software_licenses) }
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20191030223057_backfill_version_author_and_created_at.rb')
require_migration!
RSpec.describe BackfillVersionAuthorAndCreatedAt do
let_it_be(:migration_name) { described_class::MIGRATION.to_s.demodulize }
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20191125024005_cleanup_deploy_access_levels_for_removed_groups.rb')
require_migration!
RSpec.describe CleanupDeployAccessLevelsForRemovedGroups do
let(:namespaces) { table(:namespaces) }
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20191008143850_fix_any_approver_rule_for_projects.rb')
require_migration!
RSpec.describe FixAnyApproverRuleForProjects do
let(:namespaces) { table(:namespaces) }
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('ee', 'db', 'geo', 'migrate', '20180322062741_migrate_ci_job_artifacts_to_separate_registry.rb')
require_migration!
RSpec.describe MigrateCiJobArtifactsToSeparateRegistry, :geo do
let(:file_registry) { table(:file_registry) }
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20200124110831_migrate_design_notes_mentions_to_db')
require_migration!
RSpec.describe MigrateDesignNotesMentionsToDb, :sidekiq do
let(:users) { table(:users) }
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20191115115043_migrate_epic_mentions_to_db')
require_migration!
RSpec.describe MigrateEpicMentionsToDb, :migration do
let(:users) { table(:users) }
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20191115115522_migrate_epic_notes_mentions_to_db')
require_migration!
RSpec.describe MigrateEpicNotesMentionsToDb, :migration do
let(:users) { table(:users) }
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20200310215714_migrate_saml_identities_to_scim_identities.rb')
require_migration!
RSpec.describe MigrateSamlIdentitiesToScimIdentities, :migration do
let(:group1) { table(:namespaces).create!(name: 'group1', path: 'group1') }
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20200506154421_migrate_scim_identities_to_saml_for_new_users.rb')
require_migration!
RSpec.describe MigrateScimIdentitiesToSamlForNewUsers, :migration do
let(:group1) { table(:namespaces).create!(name: 'group1', path: 'group1') }
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20200519201128_migrate_vulnerability_dismissal_feedback.rb')
require_migration!
RSpec.describe MigrateVulnerabilityDismissalFeedback, :migration, :sidekiq do
let(:users) { table(:users) }
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20200416111111_migrate_vulnerability_dismissals.rb')
require_migration!
RSpec.describe MigrateVulnerabilityDismissals, :migration, :sidekiq do
let(:users) { table(:users) }
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20191022113635_nullify_feature_flag_plaintext_tokens.rb')
require_migration!
RSpec.describe NullifyFeatureFlagPlaintextTokens do
let(:namespaces) { table(:namespaces) }
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20200113151354_remove_creations_in_gitlab_subscription_histories.rb')
require_migration!
RSpec.describe RemoveCreationsInGitlabSubscriptionHistories do
before do
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20200207185149_schedule_fix_orphan_promoted_issues.rb')
require_migration!
RSpec.describe ScheduleFixOrphanPromotedIssues do
let(:projects) { table(:projects) }
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20200518114540_schedule_fix_ruby_object_in_audit_events.rb')
require_migration!
RSpec.describe ScheduleFixRubyObjectInAuditEvents do
let(:audit_events) { table(:audit_events) }
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20190905091831_schedule_merge_request_any_approval_rule_migration.rb')
require_migration!
RSpec.describe ScheduleMergeRequestAnyApprovalRuleMigration do
let(:namespaces) { table(:namespaces) }
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20190905091812_schedule_project_any_approval_rule_migration.rb')
require_migration!
RSpec.describe ScheduleProjectAnyApprovalRuleMigration do
let(:namespaces) { table(:namespaces) }
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20191105094625_set_report_type_for_vulnerabilities.rb')
require_migration!
RSpec.describe SetReportTypeForVulnerabilities do
let(:confidence_levels) do
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20191114173624_set_resolved_state_on_vulnerabilities.rb')
require_migration!
RSpec.describe SetResolvedStateOnVulnerabilities do
let(:confidence_levels) do
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20190909141517_update_cs_vulnerability_confidence_column.rb')
require_migration!
RSpec.describe UpdateCsVulnerabilityConfidenceColumn do
let(:vulnerabilities) { table(:vulnerability_occurrences) }
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20200227140242_update_occurrence_severity_column.rb')
require_migration!
RSpec.describe UpdateOccurrenceSeverityColumn do
let(:vulnerabilities) { table(:vulnerability_occurrences) }
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20200506085748_update_undefined_confidence_from_occurrences.rb')
require_migration!
RSpec.describe UpdateUndefinedConfidenceFromOccurrences, :migration do
let(:vulnerabilities) { table(:vulnerability_occurrences) }
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20200511092714_update_undefined_confidence_from_vulnerabilities.rb')
require_migration!
RSpec.describe UpdateUndefinedConfidenceFromVulnerabilities, :migration do
let(:vulnerabilities) { table(:vulnerabilities) }
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20200302142052_update_vulnerability_severity_column.rb')
require_migration!
RSpec.describe UpdateVulnerabilitySeverityColumn, :migration do
let(:vulnerabilities) { table(:vulnerabilities) }
......
# frozen_string_literal: true
require 'find'
class RequireMigration
MIGRATION_FOLDERS = %w(db/migrate db/post_migrate ee/db/geo/migrate ee/db/geo/post_migrate).freeze
SPEC_FILE_PATTERN = /.+\/(?<file_name>.+)_spec\.rb/.freeze
class << self
def require_migration!(file_name)
file_paths = search_migration_file(file_name)
require file_paths.first
end
def search_migration_file(file_name)
MIGRATION_FOLDERS.flat_map do |path|
migration_path = Rails.root.join(path).to_s
Find.find(migration_path).grep(/\d+_#{file_name}\.rb/)
end
end
end
end
def require_migration!(file_name = nil)
location_info = caller_locations.first.path.match(RequireMigration::SPEC_FILE_PATTERN)
file_name ||= location_info[:file_name]
RequireMigration.require_migration!(file_name)
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment