Commit 6a3d332a authored by Peter Leitzen's avatar Peter Leitzen

RSpec: Infer migration filename also for spec files with schema version

Prior this commit you had to pass a filename to `require_migration!`
in cases where your spec included "schema version" in its filename.

With this commit the helper method is able to infer migration filename
even in these scenarios.
parent 84b103c1
......@@ -61,14 +61,35 @@ Since the migration files are not autoloaded by Rails, you must 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 filename.
For example, if your spec file is named as `populate_foo_column_spec.rb` then the
helper method tries to load `${schema_version}_populate_foo_column.rb` migration file.
In GitLab 14.4 and later, you can use `require_migration!` to load migration files from spec files
that contain the schema version in the filename (for example,
`2021101412150000_populate_foo_column_spec.rb`).
In case there is no pattern between your spec file and the actual migration file,
you can provide the migration filename without the schema version, like so:
```ruby
# frozen_string_literal: true
require 'spec_helper'
require_migration!
RSpec.describe PopulateFooColumn do
...
end
```
In some cases, you must require multiple migration files to use them in your specs. Here, there's no
pattern between your spec file and the other migration file. You can provide the migration filename
like so:
```ruby
require_migration!('populate_foo_column')
# frozen_string_literal: true
require 'spec_helper'
require_migration!
require_migration!('populate_bar_column')
RSpec.describe PopulateFooColumn do
...
end
```
#### `table`
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('add_timestamp_softwarelicensespolicy')
require_migration!
RSpec.describe AddTimestampSoftwarelicensespolicy do
let(:software_licenses_policy) { table(:software_license_policies) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('backfill_project_settings')
require_migration!
RSpec.describe BackfillProjectSettings, :sidekiq, schema: 20200114113341 do
let(:projects) { table(:projects) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('remove_invalid_jira_data')
require_migration!
RSpec.describe RemoveInvalidJiraData do
let(:jira_tracker_data) { table(:jira_tracker_data) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('remove_invalid_issue_tracker_data')
require_migration!
RSpec.describe RemoveInvalidIssueTrackerData do
let(:issue_tracker_data) { table(:issue_tracker_data) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('reschedule_migrate_issue_trackers_data')
require_migration!
RSpec.describe RescheduleMigrateIssueTrackersData do
let(:services) { table(:services) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('remove_orphaned_chat_names')
require_migration!
RSpec.describe RemoveOrphanedChatNames, schema: 20200313202430 do
let(:projects) { table(:projects) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('backfill_deployment_clusters_from_deployments')
require_migration!
RSpec.describe BackfillDeploymentClustersFromDeployments, :migration, :sidekiq, schema: 20200227140242 do
describe '#up' do
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('change_variable_interpolation_format_in_common_metrics')
require_migration!
RSpec.describe ChangeVariableInterpolationFormatInCommonMetrics, :migration do
let(:prometheus_metrics) { table(:prometheus_metrics) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('dedup_mr_metrics')
require_migration!
RSpec.describe DedupMrMetrics, :migration, schema: 20200526013844 do
let(:namespaces) { table(:namespaces) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('update_index_approval_rule_name_for_code_owners_rule_type')
require_migration!
RSpec.describe UpdateIndexApprovalRuleNameForCodeOwnersRuleType do
let(:migration) { described_class.new }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('backfill_namespace_settings')
require_migration!
RSpec.describe BackfillNamespaceSettings, :sidekiq, schema: 20200703124823 do
let(:namespaces) { table(:namespaces) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('adjust_unique_index_alert_management_alerts')
require_migration!
RSpec.describe AdjustUniqueIndexAlertManagementAlerts, :migration do
let(:migration) { described_class.new }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('replace_unique_index_on_cycle_analytics_stages')
require_migration!
RSpec.describe ReplaceUniqueIndexOnCycleAnalyticsStages, :migration, schema: 20200727142337 do
let(:namespaces) { table(:namespaces) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('add_o_auth_paths_to_protected_paths')
require_migration!
RSpec.describe AddOAuthPathsToProtectedPaths do
subject(:migration) { described_class.new }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('create_missing_vulnerabilities_issue_links')
require_migration!
RSpec.describe CreateMissingVulnerabilitiesIssueLinks, :migration do
let(:namespace) { table(:namespaces).create!(name: 'user', path: 'user') }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('schedule_migration_to_hashed_storage')
require_migration!
RSpec.describe ScheduleMigrationToHashedStorage, :sidekiq do
describe '#up' do
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('create_initial_versions_for_pre_versioning_terraform_states')
require_migration!
RSpec.describe CreateInitialVersionsForPreVersioningTerraformStates do
let(:namespace) { table(:namespaces).create!(name: 'terraform', path: 'terraform') }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('drop_backfill_jira_tracker_deployment_type_jobs')
require_migration!
RSpec.describe DropBackfillJiraTrackerDeploymentTypeJobs, :sidekiq, :redis, schema: 2020_10_14_205300 do
subject(:migration) { described_class.new }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('migrate_services_to_http_integrations')
require_migration!
RSpec.describe MigrateServicesToHttpIntegrations do
let!(:namespace) { table(:namespaces).create!(name: 'namespace', path: 'namespace') }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('backfill_jira_tracker_deployment_type2')
require_migration!
RSpec.describe BackfillJiraTrackerDeploymentType2, :sidekiq, schema: 20201028182809 do
let(:services) { table(:services) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('cleanup_transfered_projects_shared_runners')
require_migration!
RSpec.describe CleanupTransferedProjectsSharedRunners, :sidekiq, schema: 20201110161542 do
let(:namespaces) { table(:namespaces) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('schedule_remove_duplicate_vulnerabilities_findings')
require_migration!
RSpec.describe ScheduleRemoveDuplicateVulnerabilitiesFindings, :migration do
let(:namespace) { table(:namespaces).create!(name: 'user', path: 'user') }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('schedule_recalculate_uuid_on_vulnerabilities_occurrences')
require_migration!
RSpec.describe ScheduleRecalculateUuidOnVulnerabilitiesOccurrences, :migration do
let(:namespace) { table(:namespaces).create!(name: 'user', path: 'user') }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('remove_duplicate_services2')
require_migration!
RSpec.describe RemoveDuplicateServices2 do
let_it_be(:namespaces) { table(:namespaces) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('alter_vsa_issue_first_mentioned_in_commit_value')
require_migration!
RSpec.describe AlterVsaIssueFirstMentionedInCommitValue, schema: 20210114033715 do
let(:group_stages) { table(:analytics_cycle_analytics_group_stages) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('remove_bad_dependency_proxy_manifests')
require_migration!
RSpec.describe RemoveBadDependencyProxyManifests, schema: 20210128140157 do
let_it_be(:namespaces) { table(:namespaces) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('backfill_updated_at_after_repository_storage_move')
require_migration!
RSpec.describe BackfillUpdatedAtAfterRepositoryStorageMove, :sidekiq do
let_it_be(:projects) { table(:projects) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('add_environment_scope_to_group_variables')
require_migration!
RSpec.describe AddEnvironmentScopeToGroupVariables do
let(:migration) { described_class.new }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('dedup_issue_metrics')
require_migration!
RSpec.describe DedupIssueMetrics, :migration, schema: 20210205104425 do
let(:namespaces) { table(:namespaces) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('backfill_total_tuple_count_for_batched_migrations')
require_migration!
RSpec.describe BackfillTotalTupleCountForBatchedMigrations, :migration, schema: 20210406140057 do
let_it_be(:table_name) { 'projects' }
......
......@@ -2,7 +2,7 @@
require 'spec_helper'
require_migration!('reschedule_artifact_expiry_backfill_again')
require_migration!
RSpec.describe RescheduleArtifactExpiryBackfillAgain, :migration do
let(:migration_class) { Gitlab::BackgroundMigration::BackfillArtifactExpiryDate }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('schedule_update_jira_tracker_data_deployment_type_based_on_url')
require_migration!
RSpec.describe ScheduleUpdateJiraTrackerDataDeploymentTypeBasedOnUrl, :migration do
let(:services_table) { table(:services) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('schedule_drop_invalid_vulnerabilities')
require_migration!
RSpec.describe ScheduleDropInvalidVulnerabilities, :migration do
let_it_be(:namespace) { table(:namespaces).create!(name: 'user', path: 'user') }
......
......@@ -2,7 +2,7 @@
#
require 'spec_helper'
require_migration!('copy_adoption_snapshot_namespace')
require_migration!
RSpec.describe CopyAdoptionSnapshotNamespace, :migration, schema: 20210430124630 do
let(:namespaces_table) { table(:namespaces) }
......
......@@ -2,7 +2,7 @@
require 'spec_helper'
require_migration!('copy_adoption_segments_namespace')
require_migration!
RSpec.describe CopyAdoptionSegmentsNamespace, :migration do
let(:namespaces_table) { table(:namespaces) }
......
......@@ -2,7 +2,7 @@
require 'spec_helper'
require_migration!('add_project_value_stream_id_to_project_stages')
require_migration!
RSpec.describe AddProjectValueStreamIdToProjectStages, schema: 20210503105022 do
let(:stages) { table(:analytics_cycle_analytics_project_stages) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('schedule_drop_invalid_vulnerabilities2')
require_migration!
RSpec.describe ScheduleDropInvalidVulnerabilities2, :migration do
let_it_be(:background_migration_jobs) { table(:background_migration_jobs) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('schedule_cleanup_orphaned_lfs_objects_projects')
require_migration!
RSpec.describe ScheduleCleanupOrphanedLfsObjectsProjects, schema: 20210511165250 do
let(:lfs_objects_projects) { table(:lfs_objects_projects) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('fix_total_stage_in_vsa')
require_migration!
RSpec.describe FixTotalStageInVsa, :migration, schema: 20210518001450 do
let(:namespaces) { table(:namespaces) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('group_protected_environments_add_index_and_constraint')
require_migration!
RSpec.describe GroupProtectedEnvironmentsAddIndexAndConstraint do
let(:migration) { described_class.new }
......
......@@ -2,7 +2,7 @@
require 'spec_helper'
require_migration!('remove_builds_email_service_from_services')
require_migration!
RSpec.describe RemoveBuildsEmailServiceFromServices do
let(:namespaces) { table(:namespaces) }
......
......@@ -2,7 +2,7 @@
require 'spec_helper'
require_migration!('delete_legacy_operations_feature_flags')
require_migration!
RSpec.describe DeleteLegacyOperationsFeatureFlags do
let(:namespace) { table(:namespaces).create!(name: 'foo', path: 'bar') }
......
......@@ -2,7 +2,7 @@
require 'spec_helper'
require_migration!('cascade_delete_freeze_periods')
require_migration!
RSpec.describe CascadeDeleteFreezePeriods do
let(:namespace) { table(:namespaces).create!(name: 'deploy_freeze', path: 'deploy_freeze') }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration! 'reschedule_merge_request_diff_users_background_migration'
require_migration!
RSpec.describe RescheduleMergeRequestDiffUsersBackgroundMigration, :migration do
let(:migration) { described_class.new }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('update_issuable_slas_where_issue_closed')
require_migration!
RSpec.describe UpdateIssuableSlasWhereIssueClosed, :migration do
let(:namespaces) { table(:namespaces) }
......
......@@ -2,7 +2,7 @@
require 'spec_helper'
require_migration!('operations_feature_flags_correct_flexible_rollout_values')
require_migration!
RSpec.describe OperationsFeatureFlagsCorrectFlexibleRolloutValues, :migration do
let_it_be(:strategies) { table(:operations_strategies) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('create_base_work_item_types')
require_migration!
RSpec.describe CreateBaseWorkItemTypes, :migration do
let!(:work_item_types) { table(:work_item_types) }
......
......@@ -2,7 +2,7 @@
require 'spec_helper'
require_migration!('update_trial_plans_ci_daily_pipeline_schedule_triggers')
require_migration!
RSpec.describe UpdateTrialPlansCiDailyPipelineScheduleTriggers, :migration do
let!(:plans) { table(:plans) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('update_external_project_bots')
require_migration!
RSpec.describe UpdateExternalProjectBots, :migration do
def create_user(**extra_options)
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('backfill_projects_with_coverage')
require_migration!
RSpec.describe BackfillProjectsWithCoverage do
let(:projects) { table(:projects) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('drop_temporary_columns_and_triggers_for_ci_builds_runner_session')
require_migration!
RSpec.describe DropTemporaryColumnsAndTriggersForCiBuildsRunnerSession, :migration do
let(:ci_builds_runner_session_table) { table(:ci_builds_runner_session) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('upsert_base_work_item_types')
require_migration!
RSpec.describe UpsertBaseWorkItemTypes, :migration do
let!(:work_item_types) { table(:work_item_types) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('drop_temporary_columns_and_triggers_for_ci_build_needs')
require_migration!
RSpec.describe DropTemporaryColumnsAndTriggersForCiBuildNeeds do
let(:ci_build_needs_table) { table(:ci_build_needs) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('drop_temporary_columns_and_triggers_for_ci_build_trace_chunks')
require_migration!
RSpec.describe DropTemporaryColumnsAndTriggersForCiBuildTraceChunks do
let(:ci_build_trace_chunks_table) { table(:ci_build_trace_chunks) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('drop_temporary_columns_and_triggers_for_taggings')
require_migration!
RSpec.describe DropTemporaryColumnsAndTriggersForTaggings do
let(:taggings_table) { table(:taggings) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('cleanup_bigint_conversion_for_ci_builds_metadata')
require_migration!
RSpec.describe CleanupBigintConversionForCiBuildsMetadata do
let(:ci_builds_metadata) { table(:ci_builds_metadata) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('finalize_ci_builds_bigint_conversion')
require_migration!
RSpec.describe FinalizeCiBuildsBigintConversion, :migration, schema: 20210907182359 do
context 'with an unexpected FK fk_3f0c88d7dc' do
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('update_report_type_for_existing_approval_project_rules')
require_migration!
RSpec.describe UpdateReportTypeForExistingApprovalProjectRules, :migration do
using RSpec::Parameterized::TableSyntax
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('cleanup_orphan_project_access_tokens')
require_migration!
RSpec.describe CleanupOrphanProjectAccessTokens, :migration do
def create_user(**extra_options)
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('cleanup_bigint_conversion_for_ci_builds')
require_migration!
RSpec.describe CleanupBigintConversionForCiBuilds do
let(:ci_builds) { table(:ci_builds) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('drop_int4_columns_for_ci_job_artifacts')
require_migration!
RSpec.describe DropInt4ColumnsForCiJobArtifacts do
let(:ci_job_artifacts) { table(:ci_job_artifacts) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('drop_int4_column_for_ci_sources_pipelines')
require_migration!
RSpec.describe DropInt4ColumnForCiSourcesPipelines do
let(:ci_sources_pipelines) { table(:ci_sources_pipelines) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('drop_int4_column_for_events')
require_migration!
RSpec.describe DropInt4ColumnForEvents do
let(:events) { table(:events) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('drop_int4_column_for_push_event_payloads')
require_migration!
RSpec.describe DropInt4ColumnForPushEventPayloads do
let(:push_event_payloads) { table(:push_event_payloads) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('schedule_populate_topics_total_projects_count_cache')
require_migration!
RSpec.describe SchedulePopulateTopicsTotalProjectsCountCache do
let(:topics) { table(:topics) }
......
......@@ -12,8 +12,8 @@
# To solve this problem, use SchemaVersionFinder to set schema one version prior to AddNotNullConstraintToUserOnGroupImportStates
require 'spec_helper'
require_migration!('add_not_null_constraint_to_user_on_group_import_states')
require_migration!
require_migration!('add_not_null_constraint_to_user_on_group_import_states')
RSpec.describe CleanupGroupImportStatesWithNullUserId, :migration,
schema: MigrationHelpers::SchemaVersionFinder.migration_prior(AddNotNullConstraintToUserOnGroupImportStates) do
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('cleanup_move_container_registry_enabled_to_project_feature')
require_migration!
RSpec.describe CleanupMoveContainerRegistryEnabledToProjectFeature, :migration do
let(:namespace) { table(:namespaces).create!(name: 'gitlab', path: 'gitlab-org') }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('cleanup_optimistic_locking_nulls_pt2_fixed')
require_migration!
RSpec.describe CleanupOptimisticLockingNullsPt2Fixed, :migration, schema: 20200219193117 do
test_tables = %w(ci_stages ci_builds ci_pipelines).freeze
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('cleanup_optimistic_locking_nulls')
require_migration!
RSpec.describe CleanupOptimisticLockingNulls do
let(:epics) { table(:epics) }
......
......@@ -2,8 +2,8 @@
require 'spec_helper'
require_migration!('add_projects_foreign_key_to_namespaces')
require_migration!
require_migration!('add_projects_foreign_key_to_namespaces')
# In order to test the CleanupProjectsWithMissingNamespace migration, we need
# to first create an orphaned project (one with an invalid namespace_id)
......
# frozen_string_literal: true
require 'spec_helper'
require_migration! 'cleanup_remaining_orphan_invites'
require_migration!
RSpec.describe CleanupRemainingOrphanInvites, :migration do
def create_member(**extra_attributes)
......
# frozen_string_literal: true
#
require 'spec_helper'
require_migration!('fix_projects_without_prometheus_service')
require_migration!
RSpec.describe FixProjectsWithoutPrometheusService, :migration do
let(:namespace) { table(:namespaces).create!(name: 'gitlab', path: 'gitlab-org') }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration! 'orphaned_invite_tokens_cleanup'
require_migration!
RSpec.describe OrphanedInviteTokensCleanup, :migration do
def create_member(**extra_attributes)
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!
require_migration!
require_migration!('ensure_deprecated_jenkins_service_records_removal')
RSpec.shared_examples 'remove DeprecatedJenkinsService records' do
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!('remove_duplicate_labels_from_group')
require_migration!
RSpec.describe RemoveDuplicateLabelsFromGroup do
let(:labels_table) { table(:labels) }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration! 'slice_merge_request_diff_commit_migrations'
require_migration!
RSpec.describe SliceMergeRequestDiffCommitMigrations, :migration do
let(:migration) { described_class.new }
......
# frozen_string_literal: true
require 'spec_helper'
require_migration! 'steal_merge_request_diff_commit_users_migration'
require_migration!
RSpec.describe StealMergeRequestDiffCommitUsersMigration, :migration do
let(:migration) { described_class.new }
......
......@@ -15,7 +15,7 @@ class RequireMigration
end
MIGRATION_FOLDERS = %w[db/migrate db/post_migrate].freeze
SPEC_FILE_PATTERN = %r{.+/(?<file_name>.+)_spec\.rb}.freeze
SPEC_FILE_PATTERN = %r{.+/(?:\d+_)?(?<file_name>.+)_spec\.rb}.freeze
class << self
def require_migration!(file_name)
......@@ -26,10 +26,12 @@ class RequireMigration
end
def search_migration_file(file_name)
migration_file_pattern = /\A\d+_#{file_name}\.rb\z/
migration_folders.flat_map do |path|
migration_path = Rails.root.join(path).to_s
Find.find(migration_path).select { |m| File.basename(m).match? /\A\d+_#{file_name}\.rb\z/ }
Find.find(migration_path).select { |m| migration_file_pattern.match? File.basename(m) }
end
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