Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
28abc4fc
Commit
28abc4fc
authored
Sep 26, 2019
by
Luke Duncalfe
Committed by
Mayra Cabrera
Sep 26, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding reversible_migration migration helper
Can be used to test the reversibility of migrations.
parent
46db8bf1
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
85 additions
and
1 deletion
+85
-1
doc/development/testing_guide/testing_migrations_guide.md
doc/development/testing_guide/testing_migrations_guide.md
+50
-1
spec/support/helpers/migrations_helpers.rb
spec/support/helpers/migrations_helpers.rb
+35
-0
No files found.
doc/development/testing_guide/testing_migrations_guide.md
View file @
28abc4fc
...
...
@@ -44,6 +44,10 @@ autoloaded with Rails. Example:
require
Rails
.
root
.
join
(
'db'
,
'post_migrate'
,
'20170526185842_migrate_pipeline_stages.rb'
)
```
### Test helpers
#### `table`
Use the
`table`
helper to create a temporary
`ActiveRecord::Base`
-derived model
for a table.
[
FactoryBot
](
https://docs.gitlab.com/ee/development/testing_guide/best_practices.html#factories
)
**should not**
be used to create data for migration specs. For example, to
...
...
@@ -53,6 +57,8 @@ create a record in the `projects` table:
project
=
table
(
:projects
).
create!
(
id:
1
,
name:
'gitlab1'
,
path:
'gitlab1'
)
```
#### `migrate!`
Use the
`migrate!`
helper to run the migration that is under test. It will not only
run the migration, but will also bump the schema version in the
`schema_migrations`
table. It is necessary because in the
`after`
hook we trigger the rest of
...
...
@@ -68,6 +74,33 @@ it 'migrates successfully' do
end
```
#### `reversible_migration`
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.
Runs the
`before`
expectations before the
**up**
migration.
1.
Migrates
**up**
.
1.
Runs the
`after`
expectations.
1.
Migrates
**down**
.
1.
Runs the
`before`
expectations a second time.
Example:
```
ruby
reversible_migration
do
|
migration
|
migration
.
before
->
{
# ... pre-migration expectations
}
migration
.
after
->
{
# ... post-migration expectations
}
end
```
### Example database migration test
This spec tests the
...
...
@@ -93,7 +126,7 @@ describe MigratePipelineStages, :migration do
jobs
.
create!
(
id:
2
,
commit_id:
1
,
project_id:
123
,
stage_idx:
1
,
stage:
'test'
)
end
# Test the up migration.
# Test
just
the up migration.
it
'correctly migrates pipeline stages'
do
expect
(
stages
.
count
).
to
be_zero
...
...
@@ -102,6 +135,22 @@ describe MigratePipelineStages, :migration do
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
```
...
...
spec/support/helpers/migrations_helpers.rb
View file @
28abc4fc
...
...
@@ -132,6 +132,41 @@ module MigrationsHelpers
migration
.
name
==
described_class
.
name
end
end
class
ReversibleMigrationTest
attr_reader
:before_up
,
:after_up
def
initialize
@before_up
=
->
{}
@after_up
=
->
{}
end
def
before
(
expectations
)
@before_up
=
expectations
self
end
def
after
(
expectations
)
@after_up
=
expectations
self
end
end
def
reversible_migration
(
&
block
)
tests
=
yield
(
ReversibleMigrationTest
.
new
)
tests
.
before_up
.
call
migrate!
tests
.
after_up
.
call
schema_migrate_down!
tests
.
before_up
.
call
end
end
MigrationsHelpers
.
prepend_if_ee
(
'EE::MigrationsHelpers'
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment