diff options
25 files changed, 142 insertions, 131 deletions
diff --git a/app/services/ci/list_config_variables_service.rb b/app/services/ci/list_config_variables_service.rb index c791a89b804..5320c5be0e9 100644 --- a/app/services/ci/list_config_variables_service.rb +++ b/app/services/ci/list_config_variables_service.rb @@ -29,7 +29,7 @@ module Ci user: current_user, sha: sha).execute - result.valid? ? result.variables_with_data : {} + result.valid? ? result.root_variables_with_data : {} end # Required for ReactiveCaching, it is also used in `reactive_cache_worker_finder` diff --git a/app/services/ci/play_build_service.rb b/app/services/ci/play_build_service.rb index fbf2aad1991..b7aec57f3e3 100644 --- a/app/services/ci/play_build_service.rb +++ b/app/services/ci/play_build_service.rb @@ -5,21 +5,27 @@ module Ci def execute(build, job_variables_attributes = nil) check_access!(build, job_variables_attributes) - # Try to enqueue the build, otherwise create a duplicate. - # - if build.enqueue - build.tap do |build| - build.update!(user: current_user, job_variables_attributes: job_variables_attributes || []) - - AfterRequeueJobService.new(project, current_user).execute(build) - end + if build.can_enqueue? + build.user = current_user + build.job_variables_attributes = job_variables_attributes || [] + build.enqueue! + + AfterRequeueJobService.new(project, current_user).execute(build) + + build else - Ci::RetryJobService.new(project, current_user).execute(build)[:job] + retry_build(build) end + rescue StateMachines::InvalidTransition + retry_build(build.reset) end private + def retry_build(build) + Ci::RetryJobService.new(project, current_user).execute(build)[:job] + end + def check_access!(build, job_variables_attributes) raise Gitlab::Access::AccessDeniedError unless can?(current_user, :play_job, build) diff --git a/app/services/packages/maven/metadata/base_create_xml_service.rb b/app/services/packages/maven/metadata/base_create_xml_service.rb index 4d5cab4978e..3b0d93e1dfb 100644 --- a/app/services/packages/maven/metadata/base_create_xml_service.rb +++ b/app/services/packages/maven/metadata/base_create_xml_service.rb @@ -8,13 +8,16 @@ module Packages INDENT_SPACE = 2 - def initialize(metadata_content:, package:) + def initialize(metadata_content:, package:, logger: nil) @metadata_content = metadata_content @package = package + @logger = logger || Gitlab::AppJsonLogger end private + attr_reader :logger + def xml_doc strong_memoize(:xml_doc) do Nokogiri::XML(@metadata_content) do |config| diff --git a/app/services/packages/maven/metadata/create_versions_xml_service.rb b/app/services/packages/maven/metadata/create_versions_xml_service.rb index 13b6efa8650..c2ac7fea703 100644 --- a/app/services/packages/maven/metadata/create_versions_xml_service.rb +++ b/app/services/packages/maven/metadata/create_versions_xml_service.rb @@ -67,6 +67,12 @@ module Packages def update_release return false if release_coherent? + unless release_xml_node.present? + log_malformed_content('Missing release tag') + + return false + end + if release_from_database release_xml_node.content = release_from_database else @@ -159,6 +165,15 @@ module Packages non_snapshot_versions_from_database.last end end + + def log_malformed_content(reason) + logger.warn( + message: 'A malformed metadata file has been encountered', + reason: reason, + project_id: @package.project_id, + package_id: @package.id + ) + end end end end diff --git a/doc/.vale/gitlab/SubstitutionSuggestions.yml b/doc/.vale/gitlab/SubstitutionSuggestions.yml index 4b77def065d..ce8868a82b9 100644 --- a/doc/.vale/gitlab/SubstitutionSuggestions.yml +++ b/doc/.vale/gitlab/SubstitutionSuggestions.yml @@ -16,7 +16,8 @@ swap: docs: '"documentation"' e-mail: '"email"' GLFM: '"GitLab Flavored Markdown"' - it is recommended: '"we recommend"' + it is recommended: '"you should"' + we recommend: '"you should"' navigate: go OAuth2: '"OAuth 2.0"' once that: '"after that"' diff --git a/doc/ci/yaml/index.md b/doc/ci/yaml/index.md index e06abe1dc69..7c322370e6a 100644 --- a/doc/ci/yaml/index.md +++ b/doc/ci/yaml/index.md @@ -3938,14 +3938,15 @@ Use `trigger` to declare that a job is a "trigger job" which starts a Trigger jobs can use only a limited set of GitLab CI/CD configuration keywords. The keywords available for use in trigger jobs are: -- [`trigger`](#trigger). -- [`stage`](#stage). - [`allow_failure`](#allow_failure). -- [`rules`](#rules). -- [`only` and `except`](#only--except). -- [`when`](#when) (only with a value of `on_success`, `on_failure`, or `always`). - [`extends`](#extends). - [`needs`](#needs), but not [`needs:project`](#needsproject). +- [`only` and `except`](#only--except). +- [`rules`](#rules). +- [`stage`](#stage). +- [`trigger`](#trigger). +- [`variables`](#variables). +- [`when`](#when) (only with a value of `on_success`, `on_failure`, or `always`). **Keyword type**: Job keyword. You can use it only as part of a job. diff --git a/doc/development/application_limits.md b/doc/development/application_limits.md index edf159a116a..b64d25ccf64 100644 --- a/doc/development/application_limits.md +++ b/doc/development/application_limits.md @@ -38,7 +38,7 @@ It's recommended to create two separate migration script files. desired limit using `create_or_update_plan_limit` migration helper, such as: ```ruby - class InsertProjectHooksPlanLimits < Gitlab::Database::Migration[1.0] + class InsertProjectHooksPlanLimits < Gitlab::Database::Migration[2.0] def up create_or_update_plan_limit('project_hooks', 'default', 0) create_or_update_plan_limit('project_hooks', 'free', 10) diff --git a/doc/development/cascading_settings.md b/doc/development/cascading_settings.md index 22f146c3f5a..1a0f0ec5b5f 100644 --- a/doc/development/cascading_settings.md +++ b/doc/development/cascading_settings.md @@ -38,7 +38,7 @@ Settings are not cascading by default. To define a cascading setting, take the f `application_settings`. ```ruby - class AddDelayedProjectRemovalCascadingSetting < Gitlab::Database::Migration[1.0] + class AddDelayedProjectRemovalCascadingSetting < Gitlab::Database::Migration[2.0] include Gitlab::Database::MigrationHelpers::CascadingNamespaceSettings enable_lock_retries! diff --git a/doc/development/database/background_migrations.md b/doc/development/database/background_migrations.md index 8e6f29b9eb8..fe62bbc6b14 100644 --- a/doc/development/database/background_migrations.md +++ b/doc/development/database/background_migrations.md @@ -236,7 +236,7 @@ Next we need a post-deployment migration that schedules the migration for existing data. ```ruby -class ScheduleExtractIntegrationsUrl < Gitlab::Database::Migration[1.0] +class ScheduleExtractIntegrationsUrl < Gitlab::Database::Migration[2.0] disable_ddl_transaction! MIGRATION = 'ExtractIntegrationsUrl' @@ -263,7 +263,7 @@ jobs and manually run on any un-migrated rows. Such a migration would look like this: ```ruby -class ConsumeRemainingExtractIntegrationsUrlJobs < Gitlab::Database::Migration[1.0] +class ConsumeRemainingExtractIntegrationsUrlJobs < Gitlab::Database::Migration[2.0] disable_ddl_transaction! def up diff --git a/doc/development/database/loose_foreign_keys.md b/doc/development/database/loose_foreign_keys.md index abf66368548..962cd2602bc 100644 --- a/doc/development/database/loose_foreign_keys.md +++ b/doc/development/database/loose_foreign_keys.md @@ -192,7 +192,7 @@ trigger needs to be configured only once. If the model already has at least one `loose_foreign_key` definition, then this step can be skipped: ```ruby -class TrackProjectRecordChanges < Gitlab::Database::Migration[1.0] +class TrackProjectRecordChanges < Gitlab::Database::Migration[2.0] include Gitlab::Database::MigrationHelpers::LooseForeignKeyHelpers enable_lock_retries! @@ -227,7 +227,7 @@ trigger. If the foreign key is deleted earlier, there is a good chance of introducing data inconsistency which needs manual cleanup: ```ruby -class RemoveProjectsCiPipelineFk < Gitlab::Database::Migration[1.0] +class RemoveProjectsCiPipelineFk < Gitlab::Database::Migration[2.0] disable_ddl_transaction! def up @@ -258,7 +258,7 @@ records in the database. Migration for removing the trigger: ```ruby -class UnTrackProjectRecordChanges < Gitlab::Database::Migration[1.0] +class UnTrackProjectRecordChanges < Gitlab::Database::Migration[2.0] include Gitlab::Database::MigrationHelpers::LooseForeignKeyHelpers enable_lock_retries! @@ -278,7 +278,7 @@ table however, there is still a chance for having leftover pending records in th must be removed with an inline data migration. ```ruby -class RemoveLeftoverProjectDeletions < Gitlab::Database::Migration[1.0] +class RemoveLeftoverProjectDeletions < Gitlab::Database::Migration[2.0] disable_ddl_transaction! def up diff --git a/doc/development/database/not_null_constraints.md b/doc/development/database/not_null_constraints.md index dccaff2df00..53ab9a83d60 100644 --- a/doc/development/database/not_null_constraints.md +++ b/doc/development/database/not_null_constraints.md @@ -25,7 +25,7 @@ For example, consider a migration that creates a table with two `NOT NULL` colum `db/migrate/20200401000001_create_db_guides.rb`: ```ruby -class CreateDbGuides < Gitlab::Database::Migration[1.0] +class CreateDbGuides < Gitlab::Database::Migration[2.0] def change create_table :db_guides do |t| t.bigint :stars, default: 0, null: false @@ -44,7 +44,7 @@ For example, consider a migration that adds a new `NOT NULL` column `active` to `db/migrate/20200501000001_add_active_to_db_guides.rb`: ```ruby -class AddExtendedTitleToSprints < Gitlab::Database::Migration[1.0] +class AddExtendedTitleToSprints < Gitlab::Database::Migration[2.0] def change add_column :db_guides, :active, :boolean, default: true, null: false end @@ -116,7 +116,7 @@ with `validate: false` in a post-deployment migration, `db/post_migrate/20200501000001_add_not_null_constraint_to_epics_description.rb`: ```ruby -class AddNotNullConstraintToEpicsDescription < Gitlab::Database::Migration[1.0] +class AddNotNullConstraintToEpicsDescription < Gitlab::Database::Migration[2.0] disable_ddl_transaction! def up @@ -147,7 +147,7 @@ so we add a post-deployment migration for the 13.0 milestone (current), `db/post_migrate/20200501000002_cleanup_epics_with_null_description.rb`: ```ruby -class CleanupEpicsWithNullDescription < Gitlab::Database::Migration[1.0] +class CleanupEpicsWithNullDescription < Gitlab::Database::Migration[2.0] # With BATCH_SIZE=1000 and epics.count=29500 on GitLab.com # - 30 iterations will be run # - each requires on average ~150ms @@ -185,7 +185,7 @@ migration helper in a final post-deployment migration, `db/post_migrate/20200601000001_validate_not_null_constraint_on_epics_description.rb`: ```ruby -class ValidateNotNullConstraintOnEpicsDescription < Gitlab::Database::Migration[1.0] +class ValidateNotNullConstraintOnEpicsDescription < Gitlab::Database::Migration[2.0] disable_ddl_transaction! def up diff --git a/doc/development/database/single_table_inheritance.md b/doc/development/database/single_table_inheritance.md index ad0101e1594..32de1fdea35 100644 --- a/doc/development/database/single_table_inheritance.md +++ b/doc/development/database/single_table_inheritance.md @@ -47,7 +47,7 @@ This ensures that the migration loads the columns for the migration in isolation and the helper disables STI by default. ```ruby -class EnqueueSomeBackgroundMigration < Gitlab::Database::Migration[1.0] +class EnqueueSomeBackgroundMigration < Gitlab::Database::Migration[2.0] disable_ddl_transaction! def up diff --git a/doc/development/database/table_partitioning.md b/doc/development/database/table_partitioning.md index bf12329473d..24a7cbe0f87 100644 --- a/doc/development/database/table_partitioning.md +++ b/doc/development/database/table_partitioning.md @@ -173,7 +173,7 @@ An example migration of partitioning the `audit_events` table by its `created_at` column would look like: ```ruby -class PartitionAuditEvents < Gitlab::Database::Migration[1.0] +class PartitionAuditEvents < Gitlab::Database::Migration[2.0] include Gitlab::Database::PartitioningMigrationHelpers def up @@ -200,7 +200,7 @@ into the partitioned copy. Continuing the above example, the migration would look like: ```ruby -class BackfillPartitionAuditEvents < Gitlab::Database::Migration[1.0] +class BackfillPartitionAuditEvents < Gitlab::Database::Migration[2.0] include Gitlab::Database::PartitioningMigrationHelpers def up @@ -233,7 +233,7 @@ failed jobs. Once again, continuing the example, this migration would look like: ```ruby -class CleanupPartitionedAuditEventsBackfill < Gitlab::Database::Migration[1.0] +class CleanupPartitionedAuditEventsBackfill < Gitlab::Database::Migration[2.0] include Gitlab::Database::PartitioningMigrationHelpers def up diff --git a/doc/development/documentation/styleguide/word_list.md b/doc/development/documentation/styleguide/word_list.md index 65ad8dea688..ce9fd671a39 100644 --- a/doc/development/documentation/styleguide/word_list.md +++ b/doc/development/documentation/styleguide/word_list.md @@ -720,25 +720,18 @@ Do not use **navigate**. Use **go** instead. For example: ## need to, should -Try to avoid **needs to**, because it's wordy. Avoid **should** when you can be more specific. If something is required, use **must**. +Try to avoid **needs to**, because it's wordy. If something is recommended, use **should** instead. If something is required, use **must**. Use: -- You must set the variable. -- Set the variable. +- You should set the variable. (recommended) +- You must set the variable. (required) +- Set the variable. (required) Instead of: - You need to set the variable. -**Should** is acceptable for recommended actions or items, or in cases where an event may not -happen. For example: - -- Although you can configure the installation manually, you should use the express configuration to - avoid complications. -- You should see a success message in the console. Contact support if an error message appears - instead. - ## note that Do not use **note that** because it's wordy. @@ -1173,7 +1166,7 @@ Instead of: - We created a feature for you to add widgets. -One exception: You can use **we recommend** instead of **it is recommended** or **GitLab recommends**. ([Vale](../testing.md#vale) rule: [`SubstitutionSuggestions.yml`](https://gitlab.com/gitlab-org/gitlab/-/blob/master/doc/.vale/gitlab/SubstitutionSuggestions.yml)) +([Vale](../testing.md#vale) rule: [`SubstitutionSuggestions.yml`](https://gitlab.com/gitlab-org/gitlab/-/blob/master/doc/.vale/gitlab/SubstitutionSuggestions.yml)) ## whitelist diff --git a/doc/development/prometheus_metrics.md b/doc/development/prometheus_metrics.md index c2caa354567..d3d809c5386 100644 --- a/doc/development/prometheus_metrics.md +++ b/doc/development/prometheus_metrics.md @@ -36,7 +36,7 @@ After you add or change an existing common metric, you must [re-run the import s Or, you can create a database migration: ```ruby -class ImportCommonMetrics < Gitlab::Database::Migration[1.0] +class ImportCommonMetrics < Gitlab::Database::Migration[2.0] def up ::Gitlab::DatabaseImporters::CommonMetrics::Importer.new.execute end diff --git a/doc/development/sql.md b/doc/development/sql.md index 5829d27b8ee..cdc952eb08b 100644 --- a/doc/development/sql.md +++ b/doc/development/sql.md @@ -103,7 +103,7 @@ transaction. Transactions for migrations can be disabled using the following pattern: ```ruby -class MigrationName < Gitlab::Database::Migration[1.0] +class MigrationName < Gitlab::Database::Migration[2.0] disable_ddl_transaction! end ``` @@ -111,7 +111,7 @@ end For example: ```ruby -class AddUsersLowerUsernameEmailIndexes < Gitlab::Database::Migration[1.0] +class AddUsersLowerUsernameEmailIndexes < Gitlab::Database::Migration[2.0] disable_ddl_transaction! def up diff --git a/doc/topics/autodevops/customize.md b/doc/topics/autodevops/customize.md index 0d4afc3c464..3206e9e7df3 100644 --- a/doc/topics/autodevops/customize.md +++ b/doc/topics/autodevops/customize.md @@ -400,6 +400,7 @@ applications. | `AUTO_DEVOPS_CHART_REPOSITORY_USERNAME` | Used to set a username to connect to the Helm repository. Defaults to no credentials. Also set `AUTO_DEVOPS_CHART_REPOSITORY_PASSWORD`. | | `AUTO_DEVOPS_CHART_REPOSITORY_PASSWORD` | Used to set a password to connect to the Helm repository. Defaults to no credentials. Also set `AUTO_DEVOPS_CHART_REPOSITORY_USERNAME`. | | `AUTO_DEVOPS_CHART_REPOSITORY_PASS_CREDENTIALS` | From GitLab 14.2, set to a non-empty value to enable forwarding of the Helm repository credentials to the chart server when the chart artifacts are on a different host than repository. | +| `AUTO_DEVOPS_COMMON_NAME` | From GitLab 15.5, set to a valid domain name to customize the common name used for the TLS certificate. Defaults to `le-$CI_PROJECT_ID.$KUBE_INGRESS_BASE_DOMAIN`. Set to `false` to not set this alternative host on the Ingress. | | `AUTO_DEVOPS_DEPLOY_DEBUG` | From GitLab 13.1, if this variable is present, Helm outputs debug logs. | | `AUTO_DEVOPS_ALLOW_TO_FORCE_DEPLOY_V<N>` | From [auto-deploy-image](https://gitlab.com/gitlab-org/cluster-integration/auto-deploy-image) v1.0.0, if this variable is present, a new major version of chart is forcibly deployed. For more information, see [Ignore warnings and continue deploying](upgrading_auto_deploy_dependencies.md#ignore-warnings-and-continue-deploying). | | `BUILDPACK_URL` | Buildpack's full URL. [Must point to a URL supported by Pack or Herokuish](#custom-buildpacks). | diff --git a/lib/gitlab/ci/yaml_processor/result.rb b/lib/gitlab/ci/yaml_processor/result.rb index 5c3864362da..4063bd87033 100644 --- a/lib/gitlab/ci/yaml_processor/result.rb +++ b/lib/gitlab/ci/yaml_processor/result.rb @@ -6,12 +6,17 @@ module Gitlab module Ci class YamlProcessor class Result - attr_reader :errors, :warnings + attr_reader :errors, :warnings, + :root_variables, :root_variables_with_data, + :stages, :jobs, + :workflow_rules, :workflow_name def initialize(ci_config: nil, errors: [], warnings: []) @ci_config = ci_config @errors = errors || [] @warnings = warnings || [] + + assign_valid_attributes if valid? end def valid? @@ -32,34 +37,10 @@ module Gitlab end end - def workflow_rules - @workflow_rules ||= @ci_config.workflow_rules - end - - def workflow_name - @workflow_name ||= @ci_config.workflow_name&.strip - end - - def root_variables - @root_variables ||= transform_to_array(@ci_config.variables) - end - - def jobs - @jobs ||= @ci_config.normalized_jobs - end - - def stages - @stages ||= @ci_config.stages - end - def included_templates @included_templates ||= @ci_config.included_templates end - def variables_with_data - @ci_config.variables_with_data - end - def yaml_variables_for(job_name) job = jobs[job_name] @@ -82,6 +63,17 @@ module Gitlab private + def assign_valid_attributes + @root_variables = transform_to_array(@ci_config.variables) + @root_variables_with_data = @ci_config.variables_with_data + + @stages = @ci_config.stages + @jobs = @ci_config.normalized_jobs + + @workflow_rules = @ci_config.workflow_rules + @workflow_name = @ci_config.workflow_name&.strip + end + def stage_builds_attributes(stage) jobs.values .select { |job| job[:stage] == stage } @@ -129,14 +121,10 @@ module Gitlab start_in: job[:start_in], trigger: job[:trigger], bridge_needs: job.dig(:needs, :bridge)&.first, - release: release(job) + release: job[:release] }.compact }.compact end - def release(job) - job[:release] - end - def transform_to_array(variables) ::Gitlab::Ci::Variables::Helpers.transform_to_array(variables) end diff --git a/spec/migrations/change_public_projects_cost_factor_spec.rb b/spec/migrations/change_public_projects_cost_factor_spec.rb index 039edda750b..cec0f242b6c 100644 --- a/spec/migrations/change_public_projects_cost_factor_spec.rb +++ b/spec/migrations/change_public_projects_cost_factor_spec.rb @@ -53,6 +53,8 @@ RSpec.describe ChangePublicProjectsCostFactor, migration: :gitlab_ci do expect(shared_2.public_projects_minutes_cost_factor).to eq(0) expect(shared_3.public_projects_minutes_cost_factor).to eq(1) expect(group_1.public_projects_minutes_cost_factor).to eq(0) + + schema_migrate_up! end end end diff --git a/spec/services/ci/play_build_service_spec.rb b/spec/services/ci/play_build_service_spec.rb index 85ef8b60af4..fc07801b672 100644 --- a/spec/services/ci/play_build_service_spec.rb +++ b/spec/services/ci/play_build_service_spec.rb @@ -87,11 +87,15 @@ RSpec.describe Ci::PlayBuildService, '#execute' do expect(build.reload.job_variables.map(&:key)).to contain_exactly('first', 'second') end - context 'when variables are invalid' do + context 'and variables are invalid' do let(:job_variables) { [{}] } - it 'raises an error' do - expect { subject }.to raise_error(ActiveRecord::RecordInvalid) + it 'resets the attributes of the build' do + build.update!(job_variables_attributes: [{ key: 'old', value: 'old variable' }]) + + subject + + expect(build.job_variables.map(&:key)).to contain_exactly('old') end end diff --git a/spec/services/packages/maven/metadata/create_versions_xml_service_spec.rb b/spec/services/packages/maven/metadata/create_versions_xml_service_spec.rb index 39c6feb5d12..70c2bbad87a 100644 --- a/spec/services/packages/maven/metadata/create_versions_xml_service_spec.rb +++ b/spec/services/packages/maven/metadata/create_versions_xml_service_spec.rb @@ -65,6 +65,23 @@ RSpec.describe ::Packages::Maven::Metadata::CreateVersionsXmlService do let(:versions_in_database) { versions_in_xml + additional_versions } it_behaves_like 'returning an xml with versions in the database' + + context 'with an xml without a release version' do + let(:version_release) { nil } + + it_behaves_like 'returning an xml with versions in the database' + + it 'logs a warn with the reason' do + expect(Gitlab::AppJsonLogger).to receive(:warn).with( + message: 'A malformed metadata file has been encountered', + reason: 'Missing release tag', + project_id: package.project_id, + package_id: package.id + ) + + subject + end + end end end diff --git a/spec/support/database/multiple_databases.rb b/spec/support/database/multiple_databases.rb index 25c3b6e74ce..96bdab5171d 100644 --- a/spec/support/database/multiple_databases.rb +++ b/spec/support/database/multiple_databases.rb @@ -2,15 +2,6 @@ module Database module MultipleDatabases - def run_and_cleanup(example) - # Each example may call `migrate!`, so we must ensure we are migrated down every time - schema_migrate_down! - - example.run - - delete_from_all_tables!(except: deletion_except_tables) - end - def skip_if_multiple_databases_not_setup skip 'Skipping because multiple databases not set up' unless Gitlab::Database.has_config?(:ci) end @@ -31,21 +22,6 @@ module Database model.establish_connection(new_db_config) end - def ensure_schema_and_empty_tables - # Ensure all schemas for both databases are migrated back - Gitlab::Database.database_base_models.each do |_, base_model| - with_reestablished_active_record_base do - reconfigure_db_connection( - model: ActiveRecord::Base, - config_model: base_model - ) - - schema_migrate_up! - delete_from_all_tables!(except: deletion_except_tables) - end - end - end - # The usage of this method switches temporarily used `connection_handler` # allowing full manipulation of ActiveRecord::Base connections without # having side effects like: @@ -133,15 +109,7 @@ RSpec.configure do |config| end end - config.append_after(:context, :migration) do - break if recreate_databases_and_seed_if_needed - - ensure_schema_and_empty_tables - end - config.around(:each, :migration) do |example| - self.class.use_transactional_tests = false - migration_schema = example.metadata[:migration] migration_schema = :gitlab_main if migration_schema == true base_model = Gitlab::Database.schemas_to_base_models.fetch(migration_schema).first @@ -154,13 +122,11 @@ RSpec.configure do |config| config_model: base_model ) - run_and_cleanup(example) + example.run end else - run_and_cleanup(example) + example.run end - - self.class.use_transactional_tests = true end end diff --git a/spec/support/database_cleaner.rb b/spec/support/database_cleaner.rb index 7bd1f0c5dfa..f8ddf3e66a5 100644 --- a/spec/support/database_cleaner.rb +++ b/spec/support/database_cleaner.rb @@ -13,6 +13,19 @@ RSpec.configure do |config| DatabaseCleaner.clean_with(:deletion) end + config.append_after(:context, :migration) do + delete_from_all_tables!(except: ['work_item_types']) + + # Postgres maximum number of columns in a table is 1600 (https://github.com/postgres/postgres/blob/de41869b64d57160f58852eab20a27f248188135/src/include/access/htup_details.h#L23-L47). + # We drop and recreate the database if any table has more than 1200 columns, just to be safe. + if any_connection_class_with_more_than_allowed_columns? + recreate_all_databases! + + # Seed required data as recreating DBs will delete it + TestEnv.seed_db + end + end + config.around(:each, :delete) do |example| self.class.use_transactional_tests = false @@ -22,4 +35,14 @@ RSpec.configure do |config| self.class.use_transactional_tests = true end + + config.around(:each, :migration) do |example| + self.class.use_transactional_tests = false + + example.run + + delete_from_all_tables!(except: ['work_item_types']) + + self.class.use_transactional_tests = true + end end diff --git a/spec/support/db_cleaner.rb b/spec/support/db_cleaner.rb index 24cdbe04fc2..2a1302f971a 100644 --- a/spec/support/db_cleaner.rb +++ b/spec/support/db_cleaner.rb @@ -78,19 +78,6 @@ module DbCleaner puts "Databases re-creation done in #{Gitlab::Metrics::System.monotonic_time - start}" end - def recreate_databases_and_seed_if_needed - # Postgres maximum number of columns in a table is 1600 (https://github.com/postgres/postgres/blob/de41869b64d57160f58852eab20a27f248188135/src/include/access/htup_details.h#L23-L47). - # We drop and recreate the database if any table has more than 1200 columns, just to be safe. - return false unless any_connection_class_with_more_than_allowed_columns? - - recreate_all_databases! - - # Seed required data as recreating DBs will delete it - TestEnv.seed_db - - true - end - def force_disconnect_all_connections! cmd = <<~SQL SELECT pg_terminate_backend(pg_stat_activity.pid) diff --git a/spec/support/migration.rb b/spec/support/migration.rb index 24e2fc2ff31..3c359af886d 100644 --- a/spec/support/migration.rb +++ b/spec/support/migration.rb @@ -19,9 +19,13 @@ RSpec.configure do |config| # Each example may call `migrate!`, so we must ensure we are migrated down every time config.before(:each, :migration) do use_fake_application_settings + + schema_migrate_down! end config.after(:context, :migration) do + schema_migrate_up! + Gitlab::CurrentSettings.clear_in_memory_application_settings! end end |