Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
pleroma
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
Container registry
Model registry
Operate
Environments
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
jeff
pleroma
Commits
a11ca87f
Commit
a11ca87f
authored
6 years ago
by
rinpatch
Browse files
Options
Downloads
Patches
Plain Diff
Add a migration to remove embeded objects
parent
83589ca6
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
lib/mix/tasks/compact_database.ex
+0
-57
0 additions, 57 deletions
lib/mix/tasks/compact_database.ex
priv/repo/migrations/20190418072951_remove_embeded_objects.exs
+10
-0
10 additions, 0 deletions
...repo/migrations/20190418072951_remove_embeded_objects.exs
with
10 additions
and
57 deletions
lib/mix/tasks/compact_database.ex
deleted
100644 → 0
+
0
−
57
View file @
83589ca6
defmodule
Mix
.
Tasks
.
CompactDatabase
do
@moduledoc
"""
Compact the database by flattening the object graph.
"""
require
Logger
use
Mix
.
Task
import
Ecto
.
Query
alias
Pleroma
.
Activity
alias
Pleroma
.
Repo
defp
maybe_compact
(%
Activity
{
data:
%{
"object"
=>
%{
"id"
=>
object_id
}}}
=
activity
)
do
data
=
activity
.
data
|>
Map
.
put
(
"object"
,
object_id
)
{
:ok
,
activity
}
=
Activity
.
change
(
activity
,
%{
data:
data
})
|>
Repo
.
update
()
{
:ok
,
activity
}
end
defp
maybe_compact
(%
Activity
{}
=
activity
),
do
:
{
:ok
,
activity
}
defp
activity_query
(
min_id
,
max_id
)
do
from
(
a
in
Activity
,
where:
fragment
(
"?->>'type' = 'Create'"
,
a
.
data
),
where:
a
.
id
>=
^
min_id
,
where:
a
.
id
<
^
max_id
)
end
def
run
(
_args
)
do
Application
.
ensure_all_started
(
:pleroma
)
max
=
Repo
.
aggregate
(
Activity
,
:max
,
:id
)
Logger
.
info
(
"Considering
#{
max
}
activities"
)
chunks
=
0
..
round
(
max
/
100
)
Enum
.
each
(
chunks
,
fn
i
->
min
=
i
*
100
max
=
min
+
100
activity_query
(
min
,
max
)
|>
Repo
.
all
()
|>
Enum
.
each
(
&
maybe_compact
/
1
)
IO
.
write
(
"."
)
end
)
Logger
.
info
(
"Finished."
)
end
end
This diff is collapsed.
Click to expand it.
priv/repo/migrations/20190418072951_remove_embeded_objects.exs
0 → 100644
+
10
−
0
View file @
a11ca87f
defmodule
Pleroma
.
Repo
.
Migrations
.
RemoveEmbededObjects
do
use
Ecto
.
Migration
# TODO: bench on a real DB and add clippy if it takes too long
def
change
do
execute
"""
update activities set data = jsonb_set(data, '{object}'::text[], data->'object'->'id') where data->>'type' = 'Create' and data->'object'->>'id' is not null;
"""
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