Skip to content

Commit c5bc18d

Browse files
committed
Merge branch 'docs-fix-migrations-readme' into 'master'
Improvements for spec migration readme See merge request gitlab-org/gitlab-ce!24440
2 parents 3b04de8 + bd961ad commit c5bc18d

File tree

1 file changed

+60
-16
lines changed

1 file changed

+60
-16
lines changed

spec/migrations/README.md

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,39 +22,33 @@ migrate the database **down** to the previous migration version.
2222
With this approach you can test a migration against a database schema that this
2323
migration has been written for.
2424

25-
Use `migrate!` helper to run the migration that is under test.
26-
2725
The `after` hook will migrate the database **up** and reinstitutes the latest
2826
schema version, so that the process does not affect subsequent specs and
2927
ensures proper isolation.
3028

31-
## Testing a class that is not an ActiveRecord::Migration
32-
33-
In order to test a class that is not a migration itself, you will need to
34-
manually provide a required schema version. Please add a `schema` tag to a
35-
context that you want to switch the database schema within.
36-
37-
Example: `describe SomeClass, :migration, schema: 20170608152748`.
38-
3929
## Available helpers
4030

4131
Use `table` helper to create a temporary `ActiveRecord::Base` derived model
4232
for a table.
4333

44-
Use `migrate!` helper to run the migration that is under test. It will not only
34+
See `spec/support/helpers/migrations_helpers.rb` for all the available helpers.
35+
36+
## Testing a class that is an ActiveRecord::Migration
37+
38+
In order to test a class that is an `ActiveRecord::Migration`, you will need to
39+
manually `require` the migration file because it is not autoloaded with Rails.
40+
41+
Use `migrate!` helper to run the migration that is under test. It will not only
4542
run migration, but will also bump the schema version in the `schema_migrations`
4643
table. It is necessary because in the `after` hook we trigger the rest of
4744
the migrations, and we need to know where to start.
4845

49-
See `spec/support/migrations_helpers.rb` for all the available helpers.
46+
### Example
5047

51-
## An example
48+
This spec tests the [`db/post_migrate/20170526185842_migrate_pipeline_stages.rb`](https://gitlab.com/gitlab-org/gitlab-ce/blob/v11.6.5/db/post_migrate/20170526185842_migrate_pipeline_stages.rb) migration. You can find the complete spec on [`spec/migrations/migrate_pipeline_stages_spec.rb`](https://gitlab.com/gitlab-org/gitlab-ce/blob/v11.6.5/spec/migrations/migrate_pipeline_stages_spec.rb).
5249

5350
```ruby
5451
require 'spec_helper'
55-
56-
# Load a migration class.
57-
5852
require Rails.root.join('db', 'post_migrate', '20170526185842_migrate_pipeline_stages.rb')
5953

6054
describe MigratePipelineStages, :migration do
@@ -86,6 +80,56 @@ describe MigratePipelineStages, :migration do
8680
end
8781
```
8882

83+
## Testing a class that is not an ActiveRecord::Migration
84+
85+
To test a class that is not an `ActiveRecord::Migration` (a background migration),
86+
you will need to manually provide a required schema version. Please add a
87+
schema tag to a context that you want to switch the database schema within.
88+
89+
Example: `describe SomeClass, :migration, schema: 20170608152748`.
90+
91+
### Example
92+
93+
This spec tests the [`lib/gitlab/background_migration/archive_legacy_traces.rb`](https://gitlab.com/gitlab-org/gitlab-ce/blob/v11.6.5/lib/gitlab/background_migration/archive_legacy_traces.rb)
94+
background migration. You can find the complete spec on
95+
[`spec/lib/gitlab/background_migration/archive_legacy_traces_spec.rb`](https://gitlab.com/gitlab-org/gitlab-ce/blob/v11.6.5/spec/lib/gitlab/background_migration/archive_legacy_traces_spec.rb)
96+
97+
```ruby
98+
require 'spec_helper'
99+
100+
describe Gitlab::BackgroundMigration::ArchiveLegacyTraces, :migration, schema: 20180529152628 do
101+
include TraceHelpers
102+
103+
let(:namespaces) { table(:namespaces) }
104+
let(:projects) { table(:projects) }
105+
let(:builds) { table(:ci_builds) }
106+
let(:job_artifacts) { table(:ci_job_artifacts) }
107+
108+
before do
109+
namespaces.create!(id: 123, name: 'gitlab1', path: 'gitlab1')
110+
projects.create!(id: 123, name: 'gitlab1', path: 'gitlab1', namespace_id: 123)
111+
@build = builds.create!(id: 1, project_id: 123, status: 'success', type: 'Ci::Build')
112+
end
113+
114+
context 'when trace file exsits at the right place' do
115+
before do
116+
create_legacy_trace(@build, 'trace in file')
117+
end
118+
119+
it 'correctly archive legacy traces' do
120+
expect(job_artifacts.count).to eq(0)
121+
expect(File.exist?(legacy_trace_path(@build))).to be_truthy
122+
123+
described_class.new.perform(1, 1)
124+
125+
expect(job_artifacts.count).to eq(1)
126+
expect(File.exist?(legacy_trace_path(@build))).to be_falsy
127+
expect(File.read(archived_trace_path(job_artifacts.first))).to eq('trace in file')
128+
end
129+
end
130+
end
131+
```
132+
89133
## Best practices
90134

91135
1. Note that this type of tests do not run within the transaction, we use

0 commit comments

Comments
 (0)