Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mastodon
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Pierre Boudes
mastodon
Commits
886af5ce
Unverified
Commit
886af5ce
authored
6 years ago
by
Eugen Rochko
Committed by
GitHub
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Add fallback for PostgreSQL without upsert in CopyStatusStats (#8903)
Fix #8590
parent
2fb692ea
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
db/migrate/20180812173710_copy_status_stats.rb
+41
-9
41 additions, 9 deletions
db/migrate/20180812173710_copy_status_stats.rb
with
41 additions
and
9 deletions
db/migrate/20180812173710_copy_status_stats.rb
+
41
−
9
View file @
886af5ce
...
...
@@ -3,15 +3,10 @@ class CopyStatusStats < ActiveRecord::Migration[5.2]
def
up
safety_assured
do
Status
.
unscoped
.
select
(
'id'
).
find_in_batches
(
batch_size:
5_000
)
do
|
statuses
|
execute
<<-
SQL
.
squish
INSERT INTO status_stats (status_id, reblogs_count, favourites_count, created_at, updated_at)
SELECT id, reblogs_count, favourites_count, created_at, updated_at
FROM statuses
WHERE id IN (
#{
statuses
.
map
(
&
:id
).
join
(
', '
)
}
)
ON CONFLICT (status_id) DO UPDATE
SET reblogs_count = EXCLUDED.reblogs_count, favourites_count = EXCLUDED.favourites_count
SQL
if
supports_upsert?
up_fast
else
up_slow
end
end
end
...
...
@@ -19,4 +14,41 @@ class CopyStatusStats < ActiveRecord::Migration[5.2]
def
down
# Nothing
end
private
def
supports_upsert?
version
=
select_one
(
"SELECT current_setting('server_version_num') AS v"
)[
'v'
].
to_i
version
>=
90500
end
def
up_fast
say
'Upsert is available, importing counters using the fast method'
Status
.
unscoped
.
select
(
'id'
).
find_in_batches
(
batch_size:
5_000
)
do
|
statuses
|
execute
<<-
SQL
.
squish
INSERT INTO status_stats (status_id, reblogs_count, favourites_count, created_at, updated_at)
SELECT id, reblogs_count, favourites_count, created_at, updated_at
FROM statuses
WHERE id IN (
#{
statuses
.
map
(
&
:id
).
join
(
', '
)
}
)
ON CONFLICT (status_id) DO UPDATE
SET reblogs_count = EXCLUDED.reblogs_count, favourites_count = EXCLUDED.favourites_count
SQL
end
end
def
up_slow
say
'Upsert is not available in PostgreSQL below 9.5, falling back to slow import of counters'
# We cannot use bulk INSERT or overarching transactions here because of possible
# uniqueness violations that we need to skip over
Status
.
unscoped
.
select
(
'id, reblogs_count, favourites_count, created_at, updated_at'
).
find_each
do
|
status
|
begin
params
=
[[
nil
,
status
.
id
],
[
nil
,
status
.
reblogs_count
],
[
nil
,
status
.
favourites_count
],
[
nil
,
status
.
created_at
],
[
nil
,
status
.
updated_at
]]
exec_insert
(
'INSERT INTO status_stats (status_id, reblogs_count, favourites_count, created_at, updated_at) VALUES ($1, $2, $3, $4, $5)'
,
nil
,
params
)
rescue
ActiveRecord
::
RecordNotUnique
next
end
end
end
end
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment