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
0
Merge Requests
0
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
Léo-Paul Géneau
gitlab-ce
Commits
1dadfff3
Commit
1dadfff3
authored
Feb 08, 2019
by
Stan Hu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move terminate_all_connections into setup Rake task
parent
5488e8a4
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
21 additions
and
35 deletions
+21
-35
lib/gitlab/database.rb
lib/gitlab/database.rb
+0
-22
lib/tasks/gitlab/setup.rake
lib/tasks/gitlab/setup.rake
+21
-1
spec/lib/gitlab/database_spec.rb
spec/lib/gitlab/database_spec.rb
+0
-12
No files found.
lib/gitlab/database.rb
View file @
1dadfff3
...
...
@@ -235,28 +235,6 @@ module Gitlab
connection
.
schema_cache
.
data_source_exists?
(
table_name
)
end
# WARNING: Only run this if you know what you're doing.
#
# If there are any clients connected to the DB, PostgreSQL won't let
# you drop the database. It's possible that Sidekiq, Unicorn, or
# some other client will be hanging onto a connection, preventing
# the DROP DATABASE from working. To workaround this problem, this
# method terminates all the connections so that a subsequent DROP
# will work.
def
self
.
terminate_all_connections
return
false
unless
Gitlab
::
Database
.
postgresql?
cmd
=
<<~
SQL
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE datname = current_database()
AND pid <> pg_backend_pid();
SQL
connection
.
execute
(
cmd
)
&
.
result_status
==
PG
::
PGRES_TUPLES_OK
rescue
ActiveRecord
::
NoDatabaseError
end
private_class_method
:connection
def
self
.
database_version
...
...
lib/tasks/gitlab/setup.rake
View file @
1dadfff3
...
...
@@ -28,7 +28,7 @@ namespace :gitlab do
# In production, we might want to prevent ourselves from shooting
# ourselves in the foot, so let's only do this in a test or
# development environment.
Gitlab
::
Database
.
terminate_all_connections
unless
Rails
.
env
.
production?
terminate_all_connections
unless
Rails
.
env
.
production?
Rake
::
Task
[
"db:reset"
].
invoke
Rake
::
Task
[
"add_limits_mysql"
].
invoke
...
...
@@ -38,4 +38,24 @@ namespace :gitlab do
puts
"Quitting..."
.
color
(
:red
)
exit
1
end
# If there are any clients connected to the DB, PostgreSQL won't let
# you drop the database. It's possible that Sidekiq, Unicorn, or
# some other client will be hanging onto a connection, preventing
# the DROP DATABASE from working. To workaround this problem, this
# method terminates all the connections so that a subsequent DROP
# will work.
def
self
.
terminate_all_connections
return
false
unless
Gitlab
::
Database
.
postgresql?
cmd
=
<<~
SQL
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE datname = current_database()
AND pid <> pg_backend_pid();
SQL
ActiveRecord
::
Base
.
connection
.
execute
(
cmd
)
&
.
result_status
==
PG
::
PGRES_TUPLES_OK
rescue
ActiveRecord
::
NoDatabaseError
end
end
spec/lib/gitlab/database_spec.rb
View file @
1dadfff3
...
...
@@ -410,18 +410,6 @@ describe Gitlab::Database do
end
end
describe
'.terminate_all_connections'
do
it
'returns true for PostgreSQL'
,
:postgresql
do
expect
(
described_class
.
terminate_all_connections
).
to
be_truthy
end
it
'returns false for MySQL'
do
expect
(
described_class
).
to
receive
(
:postgresql?
).
and_return
(
false
)
expect
(
described_class
.
terminate_all_connections
).
to
be_falsey
end
end
describe
'#true_value'
do
it
'returns correct value for PostgreSQL'
do
expect
(
described_class
).
to
receive
(
:postgresql?
).
and_return
(
true
)
...
...
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