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
5be29637
Commit
5be29637
authored
Mar 18, 2020
by
Vladimir Shushlin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove deployment refs on environment destruction
+ specs
parent
be3938ee
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
59 additions
and
3 deletions
+59
-3
app/models/deployment.rb
app/models/deployment.rb
+23
-2
app/models/environment.rb
app/models/environment.rb
+5
-1
spec/models/deployment_spec.rb
spec/models/deployment_spec.rb
+22
-0
spec/models/environment_spec.rb
spec/models/environment_spec.rb
+9
-0
No files found.
app/models/deployment.rb
View file @
5be29637
...
...
@@ -7,6 +7,7 @@ class Deployment < ApplicationRecord
include
UpdatedAtFilterable
include
Importable
include
Gitlab
::
Utils
::
StrongMemoize
include
FastDestroyAll
belongs_to
:project
,
required:
true
belongs_to
:environment
,
required:
true
...
...
@@ -113,6 +114,26 @@ class Deployment < ApplicationRecord
success
.
find_by!
(
iid:
iid
)
end
class
<<
self
##
# FastDestroyAll concerns
def
begin_fast_destroy
preload
(
:project
).
find_each
.
map
do
|
deployment
|
[
deployment
.
project
,
deployment
.
ref_path
]
end
end
##
# FastDestroyAll concerns
def
finalize_fast_destroy
(
params
)
by_project
=
params
.
group_by
(
&
:shift
)
by_project
.
each
do
|
project
,
ref_paths
|
project
.
repository
.
delete_refs
(
*
ref_paths
.
flatten
)
end
end
end
def
commit
project
.
commit
(
sha
)
end
...
...
@@ -280,12 +301,12 @@ class Deployment < ApplicationRecord
errors
.
add
(
:ref
,
_
(
'The branch or tag does not exist'
))
end
private
def
ref_path
File
.
join
(
environment
.
ref_path
,
'deployments'
,
iid
.
to_s
)
end
private
def
legacy_finished_at
self
.
created_at
if
success?
&&
!
read_attribute
(
:finished_at
)
end
...
...
app/models/environment.rb
View file @
5be29637
...
...
@@ -3,6 +3,7 @@
class
Environment
<
ApplicationRecord
include
Gitlab
::
Utils
::
StrongMemoize
include
ReactiveCaching
include
FastDestroyAll
::
Helpers
self
.
reactive_cache_refresh_interval
=
1
.
minute
self
.
reactive_cache_lifetime
=
55
.
seconds
...
...
@@ -10,7 +11,10 @@ class Environment < ApplicationRecord
belongs_to
:project
,
required:
true
has_many
:deployments
,
->
{
visible
},
dependent: :destroy
# rubocop:disable Cop/ActiveRecordDependent
use_fast_destroy
:all_deployments
has_many
:all_deployments
,
class_name:
'Deployment'
has_many
:deployments
,
->
{
visible
}
has_many
:successful_deployments
,
->
{
success
},
class_name:
'Deployment'
has_many
:active_deployments
,
->
{
active
},
class_name:
'Deployment'
has_many
:prometheus_alerts
,
inverse_of: :environment
...
...
spec/models/deployment_spec.rb
View file @
5be29637
...
...
@@ -623,4 +623,26 @@ describe Deployment do
expect
(
deploy
.
errors
[
:ref
]).
not_to
be_empty
end
end
describe
'.fast_destroy_all'
do
it
'cleans path_refs for destroyed environments'
do
project
=
create
(
:project
,
:repository
)
environment
=
create
(
:environment
,
project:
project
)
destroyed_deployments
=
create_list
(
:deployment
,
2
,
:success
,
environment:
environment
,
project:
project
)
other_deployments
=
create_list
(
:deployment
,
2
,
:success
,
environment:
environment
,
project:
project
)
(
destroyed_deployments
+
other_deployments
).
each
(
&
:create_ref
)
described_class
.
where
(
id:
destroyed_deployments
.
map
(
&
:id
)).
fast_destroy_all
destroyed_deployments
.
each
do
|
deployment
|
expect
(
project
.
commit
(
deployment
.
ref_path
)).
to
be_nil
end
other_deployments
.
each
do
|
deployment
|
expect
(
project
.
commit
(
deployment
.
ref_path
)).
not_to
be_nil
end
end
end
end
spec/models/environment_spec.rb
View file @
5be29637
...
...
@@ -1301,4 +1301,13 @@ describe Environment, :use_clean_rails_memory_store_caching do
end
end
end
describe
'#destroy'
do
it
'remove the deployment refs from gitaly'
do
deployment
=
create
(
:deployment
,
:success
,
environment:
environment
,
project:
project
)
deployment
.
create_ref
expect
{
environment
.
destroy
}.
to
change
{
project
.
commit
(
deployment
.
ref_path
)
}.
to
(
nil
)
end
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