<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Testing Rails migrations at GitLab > 原文:[https://docs.gitlab.com/ee/development/testing_guide/testing_migrations_guide.html](https://docs.gitlab.com/ee/development/testing_guide/testing_migrations_guide.html) * [When to write a migration test](#when-to-write-a-migration-test) * [How does it work?](#how-does-it-work) * [Testing an `ActiveRecord::Migration` class](#testing-an-activerecordmigration-class) * [Test helpers](#test-helpers) * [`table`](#table) * [`migrate!`](#migrate) * [`reversible_migration`](#reversible_migration) * [Example database migration test](#example-database-migration-test) * [Testing a non-`ActiveRecord::Migration` class](#testing-a-non-activerecordmigration-class) * [Example background migration test](#example-background-migration-test) # Testing Rails migrations at GitLab[](#testing-rails-migrations-at-gitlab "Permalink") 為了可靠地檢查 Rails 遷移,我們需要針對數據庫架構對其進行測試. ## When to write a migration test[](#when-to-write-a-migration-test "Permalink") * 后期遷移( `/db/post_migrate` )和后臺遷移( `lib/gitlab/background_migration` ) **必須**執行遷移測試. * 如果您的遷移是數據遷移,那么它**必須**具有遷移測試. * 如有必要,其他遷移可能會進行遷移測試. ## How does it work?[](#how-does-it-work "Permalink") 在測試簽名中添加`:migration`標記,可以在[`spec/support/migration.rb`](https://gitlab.com/gitlab-org/gitlab/-/blob/f81fa6ab1dd788b70ef44b85aaba1f31ffafae7d/spec/support/migration.rb)掛鉤`before`和`after`運行一些自定義 RSpec. `before`鉤子會將所有遷移還原到尚未遷移被測試的遷移點. 換句話說,我們的自定義 RSpec 掛鉤將找到以前的遷移,并將數據庫**向下**遷移到以前的遷移版本. 使用這種方法,您可以針對數據庫架構測試遷移. 一個`after`鉤子將數據庫遷移**并** reinstitute 最新的架構版本,因此該方法不會影響后續的規格,并確保適當的隔離. ## Testing an `ActiveRecord::Migration` class[](#testing-an-activerecordmigration-class "Permalink") 要測試`ActiveRecord::Migration`類(即常規遷移`db/migrate`或遷移后`db/post_migrate` ),您將需要手動`require`遷移文件,因為它不會隨 Rails 自動加載. 例: ``` require Rails.root.join('db', 'post_migrate', '20170526185842_migrate_pipeline_stages.rb') ``` ### Test helpers[](#test-helpers "Permalink") #### `table`[](#table "Permalink") 使用`table`助手為`table`創建一個臨時的`ActiveRecord::Base`派生模型. [FactoryBot](best_practices.html#factories) **不**應用于為遷移規范創建數據. 例如,要在`projects`表中創建一條記錄: ``` project = table(:projects).create!(id: 1, name: 'gitlab1', path: 'gitlab1') ``` #### `migrate!`[](#migrate "Permalink") 使用`migrate!` 幫助程序來運行正在測試的遷移. 它不僅將運行遷移,還將在`schema_migrations`表中增加模式版本. 這是必要的,因為在`after`鉤中,我們會觸發其余的遷移,并且我們需要知道從哪里開始. 例: ``` it 'migrates successfully' do # ... pre-migration expectations migrate! # ... post-migration expectations end ``` #### `reversible_migration`[](#reversible_migration "Permalink") Use the `reversible_migration` helper to test migrations with either a `change` or both `up` and `down` hooks. This will test that the state of the application and its data after the migration becomes reversed is the same as it was before the migration ran in the first place. The helper: 1. 在**向上**遷移之前運行`before`期望. 2. Migrates **up**. 3. 運行`after`期望. 4. Migrates **down**. 5. `before`運行`before`期望. Example: ``` reversible_migration do |migration| migration.before -> { # ... pre-migration expectations } migration.after -> { # ... post-migration expectations } end ``` ### Example database migration test[](#example-database-migration-test "Permalink") 該規范測試了[`db/post_migrate/20170526185842_migrate_pipeline_stages.rb`](https://gitlab.com/gitlab-org/gitlab-foss/blob/v11.6.5/db/post_migrate/20170526185842_migrate_pipeline_stages.rb)遷移. 您可以在[`spec/migrations/migrate_pipeline_stages_spec.rb`](https://gitlab.com/gitlab-org/gitlab-foss/blob/v11.6.5/spec/migrations/migrate_pipeline_stages_spec.rb)找到完整的規范. ``` require 'spec_helper' require Rails.root.join('db', 'post_migrate', '20170526185842_migrate_pipeline_stages.rb') RSpec.describe MigratePipelineStages do # Create test data - pipeline and CI/CD jobs. let(:jobs) { table(:ci_builds) } let(:stages) { table(:ci_stages) } let(:pipelines) { table(:ci_pipelines) } let(:projects) { table(:projects) } before do projects.create!(id: 123, name: 'gitlab1', path: 'gitlab1') pipelines.create!(id: 1, project_id: 123, ref: 'master', sha: 'adf43c3a') jobs.create!(id: 1, commit_id: 1, project_id: 123, stage_idx: 2, stage: 'build') jobs.create!(id: 2, commit_id: 1, project_id: 123, stage_idx: 1, stage: 'test') end # Test just the up migration. it 'correctly migrates pipeline stages' do expect(stages.count).to be_zero migrate! expect(stages.count).to eq 2 expect(stages.all.pluck(:name)).to match_array %w[test build] end # Test a reversible migration. it 'correctly migrates up and down pipeline stages' do reversible_migration do |migration| # Expectations will run before the up migration, # and then again after the down migration migration.before -> { expect(stages.count).to be_zero } # Expectations will run after the up migration. migration.after -> { expect(stages.count).to eq 2 expect(stages.all.pluck(:name)).to match_array %w[test build] } end end ``` ## Testing a non-`ActiveRecord::Migration` class[](#testing-a-non-activerecordmigration-class "Permalink") 要測試非`ActiveRecord::Migration`測試(后臺遷移),您將需要手動提供所需的架構版本. 請向要在其中切換數據庫架構的上下文中添加`schema`標簽. 如果未設置,則`schema`默認為`:latest` . Example: ``` describe SomeClass, schema: 20170608152748 do # ... end ``` ### Example background migration test[](#example-background-migration-test "Permalink") 該規范測試了[`lib/gitlab/background_migration/archive_legacy_traces.rb`](https://gitlab.com/gitlab-org/gitlab-foss/blob/v11.6.5/lib/gitlab/background_migration/archive_legacy_traces.rb)背景遷移. 您可以在[`spec/lib/gitlab/background_migration/archive_legacy_traces_spec.rb`](https://gitlab.com/gitlab-org/gitlab-foss/blob/v11.6.5/spec/lib/gitlab/background_migration/archive_legacy_traces_spec.rb)找到完整的規范 ``` require 'spec_helper' describe Gitlab::BackgroundMigration::ArchiveLegacyTraces, schema: 20180529152628 do include TraceHelpers let(:namespaces) { table(:namespaces) } let(:projects) { table(:projects) } let(:builds) { table(:ci_builds) } let(:job_artifacts) { table(:ci_job_artifacts) } before do namespaces.create!(id: 123, name: 'gitlab1', path: 'gitlab1') projects.create!(id: 123, name: 'gitlab1', path: 'gitlab1', namespace_id: 123) @build = builds.create!(id: 1, project_id: 123, status: 'success', type: 'Ci::Build') end context 'when trace file exists at the right place' do before do create_legacy_trace(@build, 'trace in file') end it 'correctly archive legacy traces' do expect(job_artifacts.count).to eq(0) expect(File.exist?(legacy_trace_path(@build))).to be_truthy described_class.new.perform(1, 1) expect(job_artifacts.count).to eq(1) expect(File.exist?(legacy_trace_path(@build))).to be_falsy expect(File.read(archived_trace_path(job_artifacts.first))).to eq('trace in file') end end end ``` **注意:**由于我們使用刪除數據庫清除策略,因此這些測試不在數據庫事務中運行. 不要依賴存在的事務.
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看