philomena/priv/repo/seeds.exs

118 lines
2.7 KiB
Elixir
Raw Normal View History

2019-08-15 02:32:32 +02:00
# Script for populating the database. You can run it as:
#
# mix run priv/repo/seeds.exs
#
# Inside the script, you can read and write to any of your
# repositories directly:
#
# Philomena.Repo.insert!(%Philomena.SomeSchema{})
#
# We recommend using the bang functions (`insert!`, `update!`
# and so on) as they will fail if something goes wrong.
2019-12-02 16:58:12 +01:00
2020-01-11 05:20:19 +01:00
alias Philomena.{
Repo,
Comments.Comment,
Filters.Filter,
Forums.Forum,
Galleries.Gallery,
Posts.Post,
Images.Image,
Reports.Report,
2021-01-18 19:00:35 +01:00
Filters.Filter,
2020-01-11 05:20:19 +01:00
Roles.Role,
Tags.Tag,
2020-04-05 01:33:03 +02:00
Users.User,
StaticPages.StaticPage
2020-01-11 05:20:19 +01:00
}
alias PhilomenaQuery.Search
alias Philomena.Users
2019-12-02 16:58:12 +01:00
alias Philomena.Tags
2021-01-18 19:00:35 +01:00
alias Philomena.Filters
2019-12-02 16:58:12 +01:00
import Ecto.Query
IO.puts("---- Creating search indices")
2020-01-11 05:20:19 +01:00
2021-01-18 19:00:35 +01:00
for model <- [Image, Comment, Gallery, Tag, Post, Report, Filter] do
Search.delete_index!(model)
Search.create_index!(model)
2019-12-02 18:26:45 +01:00
end
2019-12-02 16:58:12 +01:00
resources =
"priv/repo/seeds.json"
|> File.read!()
|> Jason.decode!()
2020-01-11 05:20:19 +01:00
IO.puts("---- Generating rating tags")
2019-12-02 16:58:12 +01:00
for tag_name <- resources["rating_tags"] do
%Tag{category: "rating"}
|> Tag.creation_changeset(%{name: tag_name})
|> Repo.insert(on_conflict: :nothing)
end
2020-01-11 05:20:19 +01:00
IO.puts("---- Generating system filters")
2019-12-02 16:58:12 +01:00
for filter_def <- resources["system_filters"] do
spoilered_tag_list = Enum.join(filter_def["spoilered"], ",")
hidden_tag_list = Enum.join(filter_def["hidden"], ",")
%Filter{system: true}
|> Filter.changeset(%{
name: filter_def["name"],
description: filter_def["description"],
spoilered_tag_list: spoilered_tag_list,
hidden_tag_list: hidden_tag_list
})
|> Repo.insert(on_conflict: :nothing)
2021-01-18 19:00:35 +01:00
|> case do
{:ok, filter} ->
Filters.reindex_filter(filter)
{:error, changeset} ->
IO.inspect(changeset.errors)
end
2019-12-02 16:58:12 +01:00
end
2020-01-11 05:20:19 +01:00
IO.puts("---- Generating forums")
2019-12-02 16:58:12 +01:00
for forum_def <- resources["forums"] do
%Forum{}
|> Forum.changeset(forum_def)
|> Repo.insert(on_conflict: :nothing)
end
2020-01-11 05:20:19 +01:00
IO.puts("---- Generating users")
2019-12-02 16:58:12 +01:00
for user_def <- resources["users"] do
{:ok, user} = Users.register_user(user_def)
user
|> Repo.preload([:roles])
|> User.confirm_changeset()
|> User.update_changeset(%{role: user_def["role"]}, [])
|> Repo.update!()
2019-12-02 16:58:12 +01:00
end
2020-01-11 05:20:19 +01:00
IO.puts("---- Generating roles")
2019-12-16 03:21:14 +01:00
for role_def <- resources["roles"] do
%Role{name: role_def["name"], resource_type: role_def["resource_type"]}
|> Role.changeset(%{})
|> Repo.insert(on_conflict: :nothing)
end
2020-04-05 01:33:03 +02:00
IO.puts("---- Generating static pages")
for page_def <- resources["pages"] do
%StaticPage{title: page_def["title"], slug: page_def["slug"], body: page_def["body"]}
|> StaticPage.changeset(%{})
|> Repo.insert(on_conflict: :nothing)
end
2020-01-11 05:20:19 +01:00
IO.puts("---- Indexing content")
Search.reindex(Tag |> preload(^Tags.indexing_preloads()), Tag)
2019-12-02 16:58:12 +01:00
2020-01-11 05:20:19 +01:00
IO.puts("---- Done.")