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
e8e21745
Commit
e8e21745
authored
Nov 14, 2020
by
Matija Čupić
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create artifact expiry backfill migration
parent
a09aa50a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
139 additions
and
0 deletions
+139
-0
lib/gitlab/background_migration/backfill_artifact_expiry_date.rb
...lab/background_migration/backfill_artifact_expiry_date.rb
+57
-0
spec/lib/gitlab/background_migration/backfill_artifact_expiry_date_spec.rb
...ackground_migration/backfill_artifact_expiry_date_spec.rb
+82
-0
No files found.
lib/gitlab/background_migration/backfill_artifact_expiry_date.rb
0 → 100644
View file @
e8e21745
# frozen_string_literal: true
module
Gitlab
module
BackgroundMigration
# Backfill expire_at for a range of Ci::JobArtifact
class
BackfillArtifactExpiryDate
include
Gitlab
::
Utils
::
StrongMemoize
BATCH_SIZE
=
1_000
DEFAULT_EXPIRATION_SWITCH_DATE
=
Date
.
new
(
2020
,
6
,
22
).
freeze
OLD_ARTIFACT_AGE
=
15
.
months
OLD_ARTIFACT_EXPIRY_OFFSET
=
3
.
months
RECENT_ARTIFACT_EXPIRY_OFFSET
=
1
.
year
# Ci::JobArtifact model
class
Ci::JobArtifact
<
ActiveRecord
::
Base
include
::
EachBatch
self
.
table_name
=
'ci_job_artifacts'
scope
:between
,
->
(
start_id
,
end_id
)
{
where
(
id:
start_id
..
end_id
)
}
scope
:before_default_expiration_switch
,
->
{
where
(
'created_at < ?'
,
DEFAULT_EXPIRATION_SWITCH_DATE
)
}
scope
:without_expiry_date
,
->
{
where
(
expire_at:
nil
)
}
scope
:old
,
->
{
where
(
self
.
arel_table
[
:created_at
].
lt
(
OLD_ARTIFACT_AGE
.
ago
))
}
scope
:recent
,
->
{
where
(
self
.
arel_table
[
:created_at
].
gt
(
OLD_ARTIFACT_AGE
.
ago
))
}
end
def
perform
(
start_id
,
end_id
)
Ci
::
JobArtifact
.
between
(
start_id
,
end_id
)
.
without_expiry_date
.
before_default_expiration_switch
.
each_batch
(
of:
BATCH_SIZE
)
do
|
batch
|
batch
.
old
.
update_all
(
expire_at:
old_artifact_expiry_date
)
batch
.
recent
.
update_all
(
expire_at:
recent_artifact_expiry_date
)
end
end
private
def
offset_date
strong_memoize
(
:offset_date
)
do
current_date
=
Time
.
current
target_date
=
Time
.
zone
.
local
(
current_date
.
year
,
current_date
.
month
,
22
,
0
,
0
,
0
)
current_date
.
day
<
22
?
target_date
:
target_date
.
next_month
end
end
def
old_artifact_expiry_date
offset_date
+
OLD_ARTIFACT_EXPIRY_OFFSET
end
def
recent_artifact_expiry_date
offset_date
+
RECENT_ARTIFACT_EXPIRY_OFFSET
end
end
end
end
spec/lib/gitlab/background_migration/backfill_artifact_expiry_date_spec.rb
0 → 100644
View file @
e8e21745
# frozen_string_literal: true
require
'spec_helper'
RSpec
.
describe
Gitlab
::
BackgroundMigration
::
BackfillArtifactExpiryDate
,
:migration
,
schema:
20201111152859
do
subject
(
:perform
)
{
migration
.
perform
(
1
,
99
)
}
let
(
:migration
)
{
described_class
.
new
}
let
(
:artifact_outside_id_range
)
{
create_artifact!
(
id:
100
,
created_at:
1
.
year
.
ago
,
expire_at:
nil
)
}
let
(
:artifact_outside_date_range
)
{
create_artifact!
(
id:
40
,
created_at:
Time
.
current
,
expire_at:
nil
)
}
let
(
:old_artifact
)
{
create_artifact!
(
id:
10
,
created_at:
16
.
months
.
ago
,
expire_at:
nil
)
}
let
(
:recent_artifact
)
{
create_artifact!
(
id:
20
,
created_at:
1
.
year
.
ago
,
expire_at:
nil
)
}
let
(
:artifact_with_expiry
)
{
create_artifact!
(
id:
30
,
created_at:
1
.
year
.
ago
,
expire_at:
Time
.
current
+
1
.
day
)
}
before
do
table
(
:namespaces
).
create!
(
id:
1
,
name:
'the-namespace'
,
path:
'the-path'
)
table
(
:projects
).
create!
(
id:
1
,
name:
'the-project'
,
namespace_id:
1
)
table
(
:ci_builds
).
create!
(
id:
1
,
allow_failure:
false
)
end
context
'when current date is before the 22nd'
do
before
do
travel_to
(
Time
.
zone
.
local
(
2020
,
1
,
1
,
0
,
0
,
0
))
end
it
'backfills the expiry date for old artifacts'
do
expect
(
old_artifact
.
reload
.
expire_at
).
to
eq
(
nil
)
perform
expect
(
old_artifact
.
reload
.
expire_at
).
to
be_within
(
1
.
minute
).
of
(
Time
.
zone
.
local
(
2020
,
4
,
22
,
0
,
0
,
0
))
end
it
'backfills the expiry date for recent artifacts'
do
expect
(
recent_artifact
.
reload
.
expire_at
).
to
eq
(
nil
)
perform
expect
(
recent_artifact
.
reload
.
expire_at
).
to
be_within
(
1
.
minute
).
of
(
Time
.
zone
.
local
(
2021
,
1
,
22
,
0
,
0
,
0
))
end
end
context
'when current date is after the 22nd'
do
before
do
travel_to
(
Time
.
zone
.
local
(
2020
,
1
,
23
,
0
,
0
,
0
))
end
it
'backfills the expiry date for old artifacts'
do
expect
(
old_artifact
.
reload
.
expire_at
).
to
eq
(
nil
)
perform
expect
(
old_artifact
.
reload
.
expire_at
).
to
be_within
(
1
.
minute
).
of
(
Time
.
zone
.
local
(
2020
,
5
,
22
,
0
,
0
,
0
))
end
it
'backfills the expiry date for recent artifacts'
do
expect
(
recent_artifact
.
reload
.
expire_at
).
to
eq
(
nil
)
perform
expect
(
recent_artifact
.
reload
.
expire_at
).
to
be_within
(
1
.
minute
).
of
(
Time
.
zone
.
local
(
2021
,
2
,
22
,
0
,
0
,
0
))
end
end
it
'does not touch artifacts with expiry date'
do
expect
{
perform
}.
not_to
change
{
artifact_with_expiry
.
reload
.
expire_at
}
end
it
'does not touch artifacts outside id range'
do
expect
{
perform
}.
not_to
change
{
artifact_outside_id_range
.
reload
.
expire_at
}
end
it
'does not touch artifacts outside date range'
do
expect
{
perform
}.
not_to
change
{
artifact_outside_date_range
.
reload
.
expire_at
}
end
private
def
create_artifact!
(
**
args
)
table
(
:ci_job_artifacts
).
create!
(
**
args
,
project_id:
1
,
job_id:
1
,
file_type:
1
)
end
end
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