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
bb0df486
Commit
bb0df486
authored
Dec 18, 2021
by
Lee Tickett
Committed by
Steve Abrams
Dec 18, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Backfill / update timelogs.spent_at where NULL
parent
7499e720
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
142 additions
and
0 deletions
+142
-0
db/post_migrate/20211215090620_schedule_update_timelogs_null_spent_at.rb
.../20211215090620_schedule_update_timelogs_null_spent_at.rb
+23
-0
db/schema_migrations/20211215090620
db/schema_migrations/20211215090620
+1
-0
lib/gitlab/background_migration/update_timelogs_null_spent_at.rb
...lab/background_migration/update_timelogs_null_spent_at.rb
+34
-0
spec/lib/gitlab/background_migration/update_timelogs_null_spent_at_spec.rb
...ackground_migration/update_timelogs_null_spent_at_spec.rb
+40
-0
spec/migrations/schedule_update_timelogs_null_spent_at_spec.rb
...migrations/schedule_update_timelogs_null_spent_at_spec.rb
+44
-0
No files found.
db/post_migrate/20211215090620_schedule_update_timelogs_null_spent_at.rb
0 → 100644
View file @
bb0df486
# frozen_string_literal: true
class
ScheduleUpdateTimelogsNullSpentAt
<
Gitlab
::
Database
::
Migration
[
1.0
]
DOWNTIME
=
false
BATCH_SIZE
=
5_000
DELAY_INTERVAL
=
2
.
minutes
MIGRATION
=
'UpdateTimelogsNullSpentAt'
disable_ddl_transaction!
def
up
queue_background_migration_jobs_by_range_at_intervals
(
define_batchable_model
(
'timelogs'
).
where
(
spent_at:
nil
),
MIGRATION
,
DELAY_INTERVAL
,
batch_size:
BATCH_SIZE
)
end
def
down
# no-op
end
end
db/schema_migrations/20211215090620
0 → 100644
View file @
bb0df486
43ae6290e11e3944b23ce2865b5c466a29c9ba3cfd2e0b58bd834568414b5bf2
\ No newline at end of file
lib/gitlab/background_migration/update_timelogs_null_spent_at.rb
0 → 100644
View file @
bb0df486
# frozen_string_literal: true
module
Gitlab
module
BackgroundMigration
# Class to populate spent_at for timelogs
class
UpdateTimelogsNullSpentAt
include
Gitlab
::
Database
::
DynamicModelHelpers
BATCH_SIZE
=
100
def
perform
(
start_id
,
stop_id
)
define_batchable_model
(
'timelogs'
).
where
(
spent_at:
nil
,
id:
start_id
..
stop_id
).
each_batch
(
of:
100
)
do
|
subbatch
|
batch_start
,
batch_end
=
subbatch
.
pluck
(
'min(id), max(id)'
).
first
update_timelogs
(
batch_start
,
batch_end
)
end
end
def
update_timelogs
(
batch_start
,
batch_stop
)
execute
(
<<~
SQL
)
UPDATE timelogs
SET spent_at = created_at
WHERE spent_at IS NULL
AND timelogs.id BETWEEN
#{
batch_start
}
AND
#{
batch_stop
}
;
SQL
end
def
execute
(
sql
)
@connection
||=
::
ActiveRecord
::
Base
.
connection
@connection
.
execute
(
sql
)
end
end
end
end
spec/lib/gitlab/background_migration/update_timelogs_null_spent_at_spec.rb
0 → 100644
View file @
bb0df486
# frozen_string_literal: true
require
'spec_helper'
RSpec
.
describe
Gitlab
::
BackgroundMigration
::
UpdateTimelogsNullSpentAt
,
schema:
20211215090620
do
let_it_be
(
:previous_time
)
{
10
.
days
.
ago
}
let_it_be
(
:namespace
)
{
table
(
:namespaces
).
create!
(
name:
'namespace'
,
path:
'namespace'
)
}
let_it_be
(
:project
)
{
table
(
:projects
).
create!
(
namespace_id:
namespace
.
id
)
}
let_it_be
(
:issue
)
{
table
(
:issues
).
create!
(
project_id:
project
.
id
)
}
let_it_be
(
:merge_request
)
{
table
(
:merge_requests
).
create!
(
target_project_id:
project
.
id
,
source_branch:
'master'
,
target_branch:
'feature'
)
}
let_it_be
(
:timelog1
)
{
create_timelog!
(
issue_id:
issue
.
id
)
}
let_it_be
(
:timelog2
)
{
create_timelog!
(
merge_request_id:
merge_request
.
id
)
}
let_it_be
(
:timelog3
)
{
create_timelog!
(
issue_id:
issue
.
id
,
spent_at:
previous_time
)
}
let_it_be
(
:timelog4
)
{
create_timelog!
(
merge_request_id:
merge_request
.
id
,
spent_at:
previous_time
)
}
subject
(
:background_migration
)
{
described_class
.
new
}
before_all
do
table
(
:timelogs
).
where
.
not
(
id:
[
timelog3
.
id
,
timelog4
.
id
]).
update_all
(
spent_at:
nil
)
end
describe
'#perform'
do
it
'sets correct spent_at'
do
background_migration
.
perform
(
timelog1
.
id
,
timelog4
.
id
)
expect
(
timelog1
.
reload
.
spent_at
).
to
be_like_time
(
timelog1
.
created_at
)
expect
(
timelog2
.
reload
.
spent_at
).
to
be_like_time
(
timelog2
.
created_at
)
expect
(
timelog3
.
reload
.
spent_at
).
to
be_like_time
(
previous_time
)
expect
(
timelog4
.
reload
.
spent_at
).
to
be_like_time
(
previous_time
)
expect
(
timelog3
.
reload
.
spent_at
).
not_to
be_like_time
(
timelog3
.
created_at
)
expect
(
timelog4
.
reload
.
spent_at
).
not_to
be_like_time
(
timelog4
.
created_at
)
end
end
private
def
create_timelog!
(
**
args
)
table
(
:timelogs
).
create!
(
**
args
,
time_spent:
1
)
end
end
spec/migrations/schedule_update_timelogs_null_spent_at_spec.rb
0 → 100644
View file @
bb0df486
# frozen_string_literal: true
require
'spec_helper'
require_migration!
RSpec
.
describe
ScheduleUpdateTimelogsNullSpentAt
do
let_it_be
(
:namespace
)
{
table
(
:namespaces
).
create!
(
name:
'namespace'
,
path:
'namespace'
)
}
let_it_be
(
:project
)
{
table
(
:projects
).
create!
(
namespace_id:
namespace
.
id
)
}
let_it_be
(
:issue
)
{
table
(
:issues
).
create!
(
project_id:
project
.
id
)
}
let_it_be
(
:merge_request
)
{
table
(
:merge_requests
).
create!
(
target_project_id:
project
.
id
,
source_branch:
'master'
,
target_branch:
'feature'
)
}
let_it_be
(
:timelog1
)
{
create_timelog!
(
merge_request_id:
merge_request
.
id
)
}
let_it_be
(
:timelog2
)
{
create_timelog!
(
merge_request_id:
merge_request
.
id
)
}
let_it_be
(
:timelog3
)
{
create_timelog!
(
merge_request_id:
merge_request
.
id
)
}
let_it_be
(
:timelog4
)
{
create_timelog!
(
issue_id:
issue
.
id
)
}
let_it_be
(
:timelog5
)
{
create_timelog!
(
issue_id:
issue
.
id
)
}
before_all
do
table
(
:timelogs
).
where
.
not
(
id:
timelog3
.
id
).
update_all
(
spent_at:
nil
)
end
it
'correctly schedules background migrations'
do
stub_const
(
"
#{
described_class
}
::BATCH_SIZE"
,
2
)
Sidekiq
::
Testing
.
fake!
do
freeze_time
do
migrate!
expect
(
described_class
::
MIGRATION
)
.
to
be_scheduled_delayed_migration
(
2
.
minutes
,
timelog1
.
id
,
timelog2
.
id
)
expect
(
described_class
::
MIGRATION
)
.
to
be_scheduled_delayed_migration
(
4
.
minutes
,
timelog4
.
id
,
timelog5
.
id
)
expect
(
BackgroundMigrationWorker
.
jobs
.
size
).
to
eq
(
2
)
end
end
end
private
def
create_timelog!
(
**
args
)
table
(
:timelogs
).
create!
(
**
args
,
time_spent:
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