Skip to content

Commit

Permalink
Merge pull request #25 from groupher/add-db-prefix
Browse files Browse the repository at this point in the history
refactor(databaes): add db prefix
  • Loading branch information
mydearxym authored Apr 5, 2024
2 parents 6f6cad9 + 497f5e5 commit f385a4d
Show file tree
Hide file tree
Showing 130 changed files with 1,029 additions and 339 deletions.
5 changes: 4 additions & 1 deletion lib/groupher_server/accounts/models/achievement.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ defmodule GroupherServer.Accounts.Model.Achievement do
use Ecto.Schema
import Ecto.Changeset

alias Helper.Constant.DBPrefix
alias GroupherServer.Accounts.Model.{User, SourceContribute}

@schema_prefix DBPrefix.account()

@required_fields ~w(user_id)a
@optional_fields ~w(articles_upvotes_count articles_collects_count contents_watched_count followers_count reputation donate_member senior_member sponsor_member)a

@type t :: %Achievement{}
schema "user_achievements" do
schema "achievements" do
belongs_to(:user, User)

field(:articles_upvotes_count, :integer, default: 0)
Expand Down
3 changes: 3 additions & 0 deletions lib/groupher_server/accounts/models/collect_folder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ defmodule GroupherServer.Accounts.Model.CollectFolder do
use Accessible
import Ecto.Changeset

alias Helper.Constant.DBPrefix
alias GroupherServer.{Accounts, CMS}
alias Accounts.Model.{User, Embeds}
alias CMS.Model.ArticleCollect

@schema_prefix DBPrefix.account()

@required_fields ~w(user_id title)a
@optional_fields ~w(index total_count private desc last_updated)a

Expand Down
3 changes: 3 additions & 0 deletions lib/groupher_server/accounts/models/customization.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ defmodule GroupherServer.Accounts.Model.Customization do
import Helper.Utils, only: [get_config: 2]
import Ecto.Changeset

alias Helper.Constant.DBPrefix
alias GroupherServer.Accounts.Model.User

@schema_prefix DBPrefix.account()

@required_fields ~w(user_id)a
@optional_fields ~w(theme sidebar_layout sidebar_communities_index community_chart brainwash_free banner_layout contents_layout content_divider content_hover mark_viewed display_density)a

Expand Down
4 changes: 4 additions & 0 deletions lib/groupher_server/accounts/models/github_user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ defmodule GroupherServer.Accounts.Model.GithubUser do

use Ecto.Schema
import Ecto.Changeset

alias Helper.Constant.DBPrefix
alias GroupherServer.Accounts.Model.User

@schema_prefix DBPrefix.account()

@type t :: %GithubUser{}
schema "github_users" do
belongs_to(:user, User)
Expand Down
5 changes: 4 additions & 1 deletion lib/groupher_server/accounts/models/social.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ defmodule GroupherServer.Accounts.Model.Social do
use Ecto.Schema
import Ecto.Changeset

alias Helper.Constant.DBPrefix
alias GroupherServer.Accounts.Model.User

@schema_prefix DBPrefix.account()

@required_fields ~w(user_id)a
@optional_fields ~w(github twitter blog company zhihu dribble huaban douban pinterest)a

@type t :: %Social{}
schema "user_socials" do
schema "socials" do
belongs_to(:user, User)

field(:github, :string)
Expand Down
12 changes: 8 additions & 4 deletions lib/groupher_server/accounts/models/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ defmodule GroupherServer.Accounts.Model.User do
# import GroupherServerWeb.Schema.Helper.Fields
import Ecto.Changeset

alias Helper.Constant.DBPrefix

alias GroupherServer.Accounts.Model.{
Achievement,
Embeds,
Expand All @@ -24,6 +26,8 @@ defmodule GroupherServer.Accounts.Model.User do

alias GroupherServer.CMS.Model.{Passport, CommunitySubscriber}

@schema_prefix DBPrefix.account()

@required_fields ~w(nickname avatar)a
@optional_fields ~w(login nickname bio shortbio remote_ip sex location email subscribed_communities_count)a

Expand Down Expand Up @@ -52,13 +56,13 @@ defmodule GroupherServer.Accounts.Model.User do
has_one(:github_profile, GithubUser)
has_one(:cms_passport, Passport)

has_many(:followers, {"users_followers", UserFollower})
has_many(:followings, {"users_followings", UserFollowing})
has_many(:followers, UserFollower)
has_many(:followings, UserFollowing)

has_many(:subscribed_communities, {"communities_subscribers", CommunitySubscriber})
has_many(:subscribed_communities, CommunitySubscriber)
field(:subscribed_communities_count, :integer, default: 0)

has_many(:collect_folder, {"collect_folders", CollectFolder})
has_many(:collect_folder, CollectFolder)

# field(:sponsor_member, :boolean)
# field(:paid_member, :boolean)
Expand Down
5 changes: 4 additions & 1 deletion lib/groupher_server/accounts/models/user_follower.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ defmodule GroupherServer.Accounts.Model.UserFollower do

use Ecto.Schema
import Ecto.Changeset

alias Helper.Constant.DBPrefix
alias GroupherServer.Accounts.Model.User

@schema_prefix DBPrefix.account()
@required_fields ~w(user_id follower_id)a

@type t :: %UserFollower{}
schema "users_followers" do
schema "followers" do
belongs_to(:user, User, foreign_key: :user_id)
belongs_to(:follower, User, foreign_key: :follower_id)

Expand Down
5 changes: 4 additions & 1 deletion lib/groupher_server/accounts/models/user_following.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ defmodule GroupherServer.Accounts.Model.UserFollowing do

use Ecto.Schema
import Ecto.Changeset

alias Helper.Constant.DBPrefix
alias GroupherServer.Accounts.Model.User

@schema_prefix DBPrefix.account()
@required_fields ~w(user_id following_id)a

@type t :: %UserFollowing{}
schema "users_followings" do
schema "followings" do
belongs_to(:user, User, foreign_key: :user_id)
belongs_to(:following, User, foreign_key: :following_id)

Expand Down
21 changes: 10 additions & 11 deletions lib/groupher_server/cms/delegates/article_crud.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ defmodule GroupherServer.CMS.Delegate.ArticleCRUD do
import Helper.ErrorCode
import ShortMaps

alias Helper.{Later, ORM, QueryBuilder, Converter}
alias Helper.{Later, ORM, QueryBuilder, Converter, Constant}
alias GroupherServer.{Accounts, CMS, Email, Repo, Statistics}

alias Accounts.Model.User
alias CMS.Model.{Author, Community, PinnedArticle, Embeds, Post}
alias CMS.Constant

alias CMS.Delegate.{
ArticleCommunity,
Expand All @@ -50,12 +49,12 @@ defmodule GroupherServer.CMS.Delegate.ArticleCRUD do
@default_user_meta Accounts.Model.Embeds.UserMeta.default_meta()
@remove_article_hint "The content does not comply with the community norms"

@audit_legal Constant.pending(:legal)
@audit_illegal Constant.pending(:illegal)
@audit_failed Constant.pending(:audit_failed)
@audit_legal Constant.CMS.pending(:legal)
@audit_illegal Constant.CMS.pending(:illegal)
@audit_failed Constant.CMS.pending(:audit_failed)

@article_cat Constant.article_cat()
@article_state Constant.article_state()
@article_cat Constant.CMS.article_cat()
@article_state Constant.CMS.article_state()

@doc """
read articles for un-logined user
Expand Down Expand Up @@ -108,18 +107,18 @@ defmodule GroupherServer.CMS.Delegate.ArticleCRUD do
end

defp covert_cat_state_ifneed(%Post{cat: cat, state: state} = article) when is_nil(state) do
cat_value = Constant.article_cat_value(cat)
cat_value = Constant.CMS.article_cat_value(cat)
article |> Map.merge(%{cat: cat_value})
end

defp covert_cat_state_ifneed(%Post{cat: cat, state: state} = article) when is_nil(cat) do
state_value = Constant.article_state_value(state)
state_value = Constant.CMS.article_state_value(state)
article |> Map.merge(%{state: state_value})
end

defp covert_cat_state_ifneed(%Post{cat: cat, state: state} = article) do
cat_value = Constant.article_cat_value(cat)
state_value = Constant.article_state_value(state)
cat_value = Constant.CMS.article_cat_value(cat)
state_value = Constant.CMS.article_state_value(state)

article |> Map.merge(%{cat: cat_value, state: state_value})
end
Expand Down
1 change: 0 additions & 1 deletion lib/groupher_server/cms/delegates/comment_action.ex
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
sync_embed_replies(comment)
end)
|> Multi.run(:after_hooks, fn _, _ ->
# IO.inspect(comment, label: "for upvote comment")
# Hooks.SubscribeCommunity.handle(comment, from_user)
Later.run({Hooks.SubscribeCommunity, :handle, [comment, from_user]})
Later.run({Hooks.Notify, :handle, [:upvote, comment, from_user]})
Expand Down
13 changes: 6 additions & 7 deletions lib/groupher_server/cms/delegates/comment_crud.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ defmodule GroupherServer.CMS.Delegate.CommentCRUD do
alias Accounts.Model.User
alias CMS.Model.{Post, Comment, PinnedComment, Embeds}

alias CMS.Constant
alias CMS.Delegate.Hooks
alias Helper.{Later, ORM, QueryBuilder, Converter}
alias Helper.{Later, ORM, QueryBuilder, Converter, Constant}

alias Ecto.Multi

Expand All @@ -36,12 +35,12 @@ defmodule GroupherServer.CMS.Delegate.CommentCRUD do

@default_user_meta Accounts.Model.Embeds.UserMeta.default_meta()

@audit_legal Constant.pending(:legal)
@audit_illegal Constant.pending(:illegal)
@audit_failed Constant.pending(:audit_failed)
@audit_legal Constant.CMS.pending(:legal)
@audit_illegal Constant.CMS.pending(:illegal)
@audit_failed Constant.CMS.pending(:audit_failed)

@article_cat Constant.article_cat()
@article_state Constant.article_state()
@article_cat Constant.CMS.article_cat()
@article_state Constant.CMS.article_state()

def comments_state(thread, article_id) do
filter = %{page: 1, size: 20}
Expand Down
12 changes: 5 additions & 7 deletions lib/groupher_server/cms/delegates/community_crud.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityCRUD do
import GroupherServer.CMS.Helper.Matcher
import ShortMaps

alias Helper.{ORM, QueryBuilder, OSS}
alias Helper.{ORM, QueryBuilder, OSS, Constant}
alias GroupherServer.{Accounts, CMS, Repo}

alias Accounts.Model.User
Expand All @@ -27,8 +27,6 @@ defmodule GroupherServer.CMS.Delegate.CommunityCRUD do
Thread
}

alias CMS.Constant

@default_meta Embeds.CommunityMeta.default_meta()
@default_dashboard CommunityDashboard.default()
@default_community_settings %{meta: @default_meta, dashboard: @default_dashboard}
Expand All @@ -37,10 +35,10 @@ defmodule GroupherServer.CMS.Delegate.CommunityCRUD do
@community_default_threads get_config(:general, :community_default_threads)

@default_user_meta Accounts.Model.Embeds.UserMeta.default_meta()
@community_normal Constant.pending(:normal)
@community_applying Constant.pending(:applying)
@community_normal Constant.CMS.pending(:normal)
@community_applying Constant.CMS.pending(:applying)

@default_apply_category Constant.apply_category(:web)
@default_apply_category Constant.CMS.apply_category(:web)

@default_read_opt [inc_views: true]

Expand Down Expand Up @@ -329,7 +327,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityCRUD do
join: c in assoc(a, :communities),
where: a.mark_delete == false and c.id == ^community.id
)
|> ORM.count()
|> ORM.count(prefix: Constant.DBPrefix.cms())

meta = Map.put(community.meta, :"#{plural(thread)}_count", thread_article_count)

Expand Down
22 changes: 19 additions & 3 deletions lib/groupher_server/cms/helper/macros.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,18 @@ defmodule GroupherServer.CMS.Helper.Macros do
alias GroupherServer.{CMS, Accounts}

alias Accounts.Model.User
alias CMS.Model.{Embeds, Author, Community, Comment, ArticleTag, ArticleUpvote, ArticleCollect}

alias CMS.Model.{
Embeds,
Author,
Community,
Comment,
CommunityJoinBlog,
ArticleTag,
ArticleUpvote,
ArticleCollect,
ArticleJoinTag
}

@article_threads get_config(:article, :threads)

Expand Down Expand Up @@ -230,7 +241,11 @@ defmodule GroupherServer.CMS.Helper.Macros do
many_to_many(
:communities,
Community,
join_through: unquote("communities_join_#{plural(thread)}"),
# NOTE: can not use "communities_join_[article]s" here because it need to set schema_prefix
# unfortunatelly, we need to manually default community_join_[article]
# join_through: unquote("communities_join_#{plural(thread)}"),
join_through:
unquote(Module.concat(CMS.Model, "CommunityJoin#{Recase.to_title(to_string(thread))}")),
on_replace: :delete
)
end
Expand All @@ -248,7 +263,8 @@ defmodule GroupherServer.CMS.Helper.Macros do
many_to_many(
:article_tags,
ArticleTag,
join_through: "articles_join_tags",
# NOTE: can not use "articles_join_tags" here because it need to set schema_prefix
join_through: ArticleJoinTag,
join_keys: Keyword.new([{unquote(:"#{thread}_id"), :id}]) ++ [article_tag_id: :id],
# :delete_all will only remove data from the join source
on_delete: :delete_all,
Expand Down
5 changes: 4 additions & 1 deletion lib/groupher_server/cms/models/Changelog.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ defmodule GroupherServer.CMS.Model.Changelog do
alias GroupherServer.CMS
alias CMS.Model.Embeds

alias Helper.Constant.DBPrefix
alias Helper.HTML

@schema_prefix DBPrefix.cms()

@timestamps_opts [type: :utc_datetime_usec]

@required_fields ~w(title digest)a
Expand All @@ -21,7 +24,7 @@ defmodule GroupherServer.CMS.Model.Changelog do
@article_cast_fields

@type t :: %Changelog{}
schema "cms_changelogs" do
schema "changelogs" do
article_tags_field(:changelog)
article_communities_field(:changelog)
general_article_fields(:changelog)
Expand Down
3 changes: 3 additions & 0 deletions lib/groupher_server/cms/models/abuse_report.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ defmodule GroupherServer.CMS.Model.AbuseReport do

alias GroupherServer.{Accounts, CMS}

alias Helper.Constant.DBPrefix
alias Accounts.Model.User
alias CMS.Model.{Comment, Embeds}

@schema_prefix DBPrefix.cms()

@article_threads get_config(:article, :threads)

# @required_fields ~w(comment_id user_id recived_user_id)a
Expand Down
3 changes: 3 additions & 0 deletions lib/groupher_server/cms/models/article_collect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ defmodule GroupherServer.CMS.Model.ArticleCollect do
import GroupherServer.CMS.Helper.Macros
import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1]

alias Helper.Constant.DBPrefix
alias GroupherServer.Accounts
alias Accounts.Model.{User, CollectFolder}

@schema_prefix DBPrefix.cms()

@article_threads get_config(:article, :threads)

@required_fields ~w(user_id)a
Expand Down
4 changes: 4 additions & 0 deletions lib/groupher_server/cms/models/article_document.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ defmodule GroupherServer.CMS.Model.ArticleDocument do
import Ecto.Changeset
import Helper.Utils, only: [get_config: 2]

alias Helper.Constant.DBPrefix

@schema_prefix DBPrefix.cms()

@timestamps_opts [type: :utc_datetime_usec]

@max_body_length get_config(:article, :max_length)
Expand Down
Loading

0 comments on commit f385a4d

Please sign in to comment.