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
9e273eae
Commit
9e273eae
authored
Jan 27, 2020
by
Andreas Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add migration to create missing project features
Relates to:
https://gitlab.com/gitlab-org/gitlab/issues/34367
parent
4505da2c
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
128 additions
and
0 deletions
+128
-0
changelogs/unreleased/ab-projects-without-feature.yml
changelogs/unreleased/ab-projects-without-feature.yml
+5
-0
db/post_migrate/20200127111840_fix_projects_without_project_feature.rb
...te/20200127111840_fix_projects_without_project_feature.rb
+21
-0
lib/gitlab/background_migration/fix_projects_without_project_feature.rb
...kground_migration/fix_projects_without_project_feature.rb
+63
-0
spec/lib/gitlab/background_migration/fix_projects_without_project_feature_spec.rb
...nd_migration/fix_projects_without_project_feature_spec.rb
+39
-0
No files found.
changelogs/unreleased/ab-projects-without-feature.yml
0 → 100644
View file @
9e273eae
---
title
:
Ensure all Project records have a corresponding ProjectFeature record
merge_request
:
23766
author
:
type
:
fixed
db/post_migrate/20200127111840_fix_projects_without_project_feature.rb
0 → 100644
View file @
9e273eae
# frozen_string_literal: true
class
FixProjectsWithoutProjectFeature
<
ActiveRecord
::
Migration
[
5.2
]
include
Gitlab
::
Database
::
MigrationHelpers
BATCH_SIZE
=
100_000
disable_ddl_transaction!
class
Project
<
ActiveRecord
::
Base
include
EachBatch
end
def
up
queue_background_migration_jobs_by_range_at_intervals
(
Project
,
'FixProjectsWithoutProjectFeature'
,
2
.
minutes
,
batch_size:
BATCH_SIZE
)
end
def
down
# no-op
end
end
lib/gitlab/background_migration/fix_projects_without_project_feature.rb
0 → 100644
View file @
9e273eae
# frozen_string_literal: true
module
Gitlab
module
BackgroundMigration
# Project model for this migration
class
Project
<
ActiveRecord
::
Base
include
EachBatch
end
ENABLED
=
20
INSERT_BATCH_SIZE
=
10_000
# This migration creates missing project_features records
# for the projects within the given range of ids
class
FixProjectsWithoutProjectFeature
def
perform
(
from_id
,
to_id
)
Project
.
transaction
do
projects
=
Project
.
where
(
<<~
SQL
,
from_id
,
to_id
)
projects.id BETWEEN ? AND ?
AND NOT EXISTS (
SELECT 1 FROM project_features
WHERE project_features.project_id = projects.id
)
SQL
projects
.
each_batch
(
of:
INSERT_BATCH_SIZE
)
do
|
batch
|
insert_missing_records
(
batch
)
end
end
end
private
def
insert_missing_records
(
projects
)
features
=
projects
.
map
do
|
project
|
record
=
{
project_id:
project
.
id
,
merge_requests_access_level:
ENABLED
,
issues_access_level:
ENABLED
,
wiki_access_level:
ENABLED
,
snippets_access_level:
ENABLED
,
builds_access_level:
ENABLED
,
repository_access_level:
ENABLED
,
pages_access_level:
ENABLED
,
forking_access_level:
ENABLED
}
record
[
'created_at'
]
=
record
[
'updated_at'
]
=
Time
.
now
.
to_s
(
:db
)
record
end
Gitlab
::
Database
.
bulk_insert
(
:project_features
,
features
,
on_conflict: :do_nothing
)
logger
.
info
(
message:
"FixProjectsWithoutProjectFeature: created missing project_features for Projects
#{
projects
.
map
(
&
:id
).
join
(
', '
)
}
"
)
end
def
logger
@logger
||=
Gitlab
::
BackgroundMigration
::
Logger
.
build
end
end
end
end
spec/lib/gitlab/background_migration/fix_projects_without_project_feature_spec.rb
0 → 100644
View file @
9e273eae
# frozen_string_literal: true
require
'spec_helper'
describe
Gitlab
::
BackgroundMigration
::
FixProjectsWithoutProjectFeature
,
:migration
,
schema:
2020_01_27_111840
do
let
(
:namespaces
)
{
table
(
:namespaces
)
}
let
(
:projects
)
{
table
(
:projects
)
}
let
(
:project_features
)
{
table
(
:project_features
)
}
let
(
:namespace
)
{
namespaces
.
create
(
name:
'foo'
,
path:
'foo'
)
}
let!
(
:project
)
{
projects
.
create!
(
namespace_id:
namespace
.
id
)
}
let!
(
:projects_without_feature
)
{
[
projects
.
create!
(
namespace_id:
namespace
.
id
),
projects
.
create!
(
namespace_id:
namespace
.
id
)]
}
before
do
project_features
.
create
({
project_id:
project
.
id
,
pages_access_level:
20
})
end
subject
{
described_class
.
new
.
perform
(
*
project_range
)
}
def
project_feature_records
ActiveRecord
::
Base
.
connection
.
select_all
(
'SELECT project_id FROM project_features ORDER BY project_id'
).
map
{
|
e
|
e
[
'project_id'
]
}
end
def
project_range
ActiveRecord
::
Base
.
connection
.
select_one
(
'SELECT MIN(id), MAX(id) FROM projects'
).
values
end
it
'creates a default ProjectFeature for projects without it'
do
expect
{
subject
}.
to
change
{
project_feature_records
}.
from
([
project
.
id
]).
to
([
project
.
id
,
*
projects_without_feature
.
map
(
&
:id
)])
end
it
'sets created_at/updated_at timestamps'
do
subject
offenders
=
ActiveRecord
::
Base
.
connection
.
select_all
(
'SELECT 1 FROM project_features WHERE created_at IS NULL or updated_at IS NULL'
)
expect
(
offenders
).
to
be_empty
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