From 2a5c9e361160f79b009f5029c1731adaa7a6867a Mon Sep 17 00:00:00 2001 From: mydearxym Date: Sat, 30 Mar 2024 10:45:58 +0800 Subject: [PATCH 01/16] chore: wip --- lib/groupher_server/accounts/models/user.ex | 4 ++ .../cms/delegates/community_crud.ex | 12 ++-- lib/groupher_server/cms/models/author.ex | 3 + lib/groupher_server/cms/models/community.ex | 3 + lib/groupher_server/cms/models/post.ex | 6 +- lib/helper/constant/cms.ex | 63 +++++++++++++++++++ lib/helper/constant/db_prefix.ex | 13 ++++ lib/helper/orm.ex | 4 ++ lib/support/factory.ex | 5 +- .../20240329090034_add_prefix_to_posts.exs | 15 +++++ ...3_add_prefix_to_communities_join_posts.exs | 11 ++++ .../cms/articles/post_test.exs | 38 +++++------ .../mutation/cms/dashboard_test.exs | 1 - .../paged_kanban_posts_test.exs | 1 - 14 files changed, 148 insertions(+), 31 deletions(-) create mode 100644 lib/helper/constant/cms.ex create mode 100644 lib/helper/constant/db_prefix.ex create mode 100644 priv/repo/migrations/20240329090034_add_prefix_to_posts.exs create mode 100644 priv/repo/migrations/20240329120833_add_prefix_to_communities_join_posts.exs diff --git a/lib/groupher_server/accounts/models/user.ex b/lib/groupher_server/accounts/models/user.ex index b79d6fe..d828aba 100644 --- a/lib/groupher_server/accounts/models/user.ex +++ b/lib/groupher_server/accounts/models/user.ex @@ -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, @@ -27,6 +29,8 @@ defmodule GroupherServer.Accounts.Model.User do @required_fields ~w(nickname avatar)a @optional_fields ~w(login nickname bio shortbio remote_ip sex location email subscribed_communities_count)a + @schema_prefix DBPrefix.default() + @type t :: %User{} schema "users" do field(:login, :string) diff --git a/lib/groupher_server/cms/delegates/community_crud.ex b/lib/groupher_server/cms/delegates/community_crud.ex index 2061006..1300bb9 100644 --- a/lib/groupher_server/cms/delegates/community_crud.ex +++ b/lib/groupher_server/cms/delegates/community_crud.ex @@ -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 @@ -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} @@ -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] @@ -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) diff --git a/lib/groupher_server/cms/models/author.ex b/lib/groupher_server/cms/models/author.ex index 0b80e11..f570318 100644 --- a/lib/groupher_server/cms/models/author.ex +++ b/lib/groupher_server/cms/models/author.ex @@ -7,9 +7,12 @@ defmodule GroupherServer.CMS.Model.Author do import Ecto.Changeset + alias Helper.Constant.DBPrefix alias GroupherServer.Accounts alias Accounts.Model.User + @schema_prefix DBPrefix.default() + @type t :: %Author{} schema "cms_authors" do diff --git a/lib/groupher_server/cms/models/community.ex b/lib/groupher_server/cms/models/community.ex index 701ad05..82bb3ea 100644 --- a/lib/groupher_server/cms/models/community.ex +++ b/lib/groupher_server/cms/models/community.ex @@ -7,6 +7,7 @@ defmodule GroupherServer.CMS.Model.Community do import Ecto.Changeset + alias Helper.Constant.DBPrefix alias GroupherServer.{Accounts, CMS} alias Accounts.Model.User @@ -19,6 +20,8 @@ defmodule GroupherServer.CMS.Model.Community do CommunityModerator } + @schema_prefix DBPrefix.default() + @max_pinned_article_count_per_thread 2 @required_fields ~w(title desc user_id logo slug)a diff --git a/lib/groupher_server/cms/models/post.ex b/lib/groupher_server/cms/models/post.ex index 3d4c61f..4e05fa3 100644 --- a/lib/groupher_server/cms/models/post.ex +++ b/lib/groupher_server/cms/models/post.ex @@ -8,6 +8,7 @@ defmodule GroupherServer.CMS.Model.Post do import Ecto.Changeset import GroupherServer.CMS.Helper.Macros + alias Helper.Constant.DBPrefix alias GroupherServer.CMS alias CMS.Model.Embeds @@ -18,10 +19,11 @@ defmodule GroupherServer.CMS.Model.Post do @optional_fields ~w(copy_right solution_digest updated_at inserted_at active_at archived_at cat state inner_id original_community_slug)a ++ @article_cast_fields + @schema_prefix DBPrefix.cms() + @type t :: %Post{} - schema "cms_posts" do + schema "posts" do field(:copy_right, :string) - field(:cat, :integer) field(:state, :integer) diff --git a/lib/helper/constant/cms.ex b/lib/helper/constant/cms.ex new file mode 100644 index 0000000..c0cba83 --- /dev/null +++ b/lib/helper/constant/cms.ex @@ -0,0 +1,63 @@ +defmodule Helper.Constant.CMS do + @moduledoc """ + constant used for CMS + + NOTE: DO NOT modify, unless you know what you are doing + """ + + import Helper.Utils.Map, only: [reverse_kv: 1] + + @artiment_legal 0 + @artiment_illegal 1 + @artiment_audit_failed 2 + + @community_normal 0 + @community_applying 1 + + @apply_web "WEB" + + @article_cat_map %{ + feature: 1, + bug: 2, + question: 3, + other: 4 + } + + @article_state_map %{ + default: 1, + todo: 2, + wip: 3, + done: 4, + # for question cat + resolved: 5, + reject: 6, + reject_dup: 7, + reject_no_plan: 8, + reject_repro: 9, + reject_stale: 10 + } + + @article_cat_value_map reverse_kv(@article_cat_map) + @article_state_value_map reverse_kv(@article_state_map) + + def pending(:legal), do: @artiment_legal + def pending(:illegal), do: @artiment_illegal + def pending(:audit_failed), do: @artiment_audit_failed + + def pending(:normal), do: @community_normal + def pending(:applying), do: @community_applying + + def apply_category(:web), do: @apply_web + + def article_cat, do: @article_cat_map + + def article_cat_value(cat) do + @article_cat_value_map |> Map.get(cat) |> to_string |> String.upcase() + end + + def article_state, do: @article_state_map + + def article_state_value(state) do + @article_state_value_map |> Map.get(state) |> to_string |> String.upcase() + end +end diff --git a/lib/helper/constant/db_prefix.ex b/lib/helper/constant/db_prefix.ex new file mode 100644 index 0000000..eadc8c7 --- /dev/null +++ b/lib/helper/constant/db_prefix.ex @@ -0,0 +1,13 @@ +defmodule Helper.Constant.DBPrefix do + @moduledoc """ + this is the prefix for all tables in database, group alias + """ + + @default "public" + @cms "cms" + @account "account" + + def default, do: @default + def cms, do: @cms + def account, do: @account +end diff --git a/lib/helper/orm.ex b/lib/helper/orm.ex index aff8c53..43ff934 100644 --- a/lib/helper/orm.ex +++ b/lib/helper/orm.ex @@ -299,6 +299,10 @@ defmodule Helper.ORM do end @doc "count current queryable" + def count(queryable, prefix: prefix) do + queryable |> Repo.aggregate(:count, prefix: prefix) |> done + end + def count(queryable) do queryable |> Repo.aggregate(:count) |> done end diff --git a/lib/support/factory.ex b/lib/support/factory.ex index 1174f41..6a6884c 100644 --- a/lib/support/factory.ex +++ b/lib/support/factory.ex @@ -81,6 +81,7 @@ defmodule GroupherServer.Support.Factory do mock_rich_text(text) end + # alias CMS.Model.Post defp mock_meta(:post) do text = Faker.Lorem.sentence(10) @@ -90,7 +91,6 @@ defmodule GroupherServer.Support.Factory do body: mock_rich_text(text), digest: String.slice(text, 100, 150), solution_digest: String.slice(text, 1, 150), - length: String.length(text), author: mock(:author), views: Enum.random(0..2000), original_community: mock(:community), @@ -252,7 +252,8 @@ defmodule GroupherServer.Support.Factory do nickname: "#{Faker.Person.first_name()}#{unique_num}", bio: Faker.Lorem.Shakespeare.romeo_and_juliet(), avatar: Faker.Avatar.image_url(), - email: "faker@gmail.com" + email: "faker@gmail.com", + __schema__: nil } end diff --git a/priv/repo/migrations/20240329090034_add_prefix_to_posts.exs b/priv/repo/migrations/20240329090034_add_prefix_to_posts.exs new file mode 100644 index 0000000..a52abc3 --- /dev/null +++ b/priv/repo/migrations/20240329090034_add_prefix_to_posts.exs @@ -0,0 +1,15 @@ +defmodule GroupherServer.Repo.Migrations.AddPrefixToPosts do + use Ecto.Migration + + def up do + execute "CREATE SCHEMA IF NOT EXISTS cms" + execute "ALTER TABLE cms_posts RENAME TO posts" + execute "ALTER TABLE public.posts SET SCHEMA cms" + end + + def down do + execute "ALTER TABLE cms.posts SET SCHEMA public" + execute "ALTER TABLE public.posts RENAME TO cms_posts" + execute "DROP SCHEMA IF EXISTS cms" + end +end diff --git a/priv/repo/migrations/20240329120833_add_prefix_to_communities_join_posts.exs b/priv/repo/migrations/20240329120833_add_prefix_to_communities_join_posts.exs new file mode 100644 index 0000000..cc3f615 --- /dev/null +++ b/priv/repo/migrations/20240329120833_add_prefix_to_communities_join_posts.exs @@ -0,0 +1,11 @@ +defmodule GroupherServer.Repo.Migrations.AddPrefixToCommunitiesJoinPosts do + use Ecto.Migration + + def up do + execute "ALTER TABLE public.communities_join_posts SET SCHEMA cms" + end + + def down do + execute "ALTER TABLE cms.communities_join_posts SET SCHEMA public" + end +end diff --git a/test/groupher_server/cms/articles/post_test.exs b/test/groupher_server/cms/articles/post_test.exs index 09ae8b8..f225d91 100644 --- a/test/groupher_server/cms/articles/post_test.exs +++ b/test/groupher_server/cms/articles/post_test.exs @@ -17,6 +17,7 @@ defmodule GroupherServer.Test.CMS.Articles.Post do setup do {:ok, user} = db_insert(:user) {:ok, user2} = db_insert(:user) + {:ok, post} = db_insert(:post) {:ok, community} = db_insert(:community) @@ -26,36 +27,37 @@ defmodule GroupherServer.Test.CMS.Articles.Post do end describe "[cms post curd]" do + @tag :wip test "created post should have auto_increase inner_id", ~m(user community post_attrs)a do {:ok, post} = CMS.create_article(community, :post, post_attrs, user) assert post.inner_id == 1 - {:ok, post} = CMS.create_article(community, :post, post_attrs, user) - assert post.inner_id == 2 + # {:ok, post} = CMS.create_article(community, :post, post_attrs, user) + # assert post.inner_id == 2 - {:ok, post} = CMS.create_article(community, :post, post_attrs, user) - assert post.inner_id == 3 + # {:ok, post} = CMS.create_article(community, :post, post_attrs, user) + # assert post.inner_id == 3 - blog_attrs = mock_attrs(:blog, %{community_id: community.id}) - changelog_attrs = mock_attrs(:changelog, %{community_id: community.id}) + # blog_attrs = mock_attrs(:blog, %{community_id: community.id}) + # changelog_attrs = mock_attrs(:changelog, %{community_id: community.id}) - {:ok, blog} = CMS.create_article(community, :blog, blog_attrs, user) - assert blog.inner_id == 1 + # {:ok, blog} = CMS.create_article(community, :blog, blog_attrs, user) + # assert blog.inner_id == 1 - {:ok, blog} = CMS.create_article(community, :blog, blog_attrs, user) - assert blog.inner_id == 2 + # {:ok, blog} = CMS.create_article(community, :blog, blog_attrs, user) + # assert blog.inner_id == 2 - {:ok, changelog} = CMS.create_article(community, :changelog, changelog_attrs, user) - assert changelog.inner_id == 1 + # {:ok, changelog} = CMS.create_article(community, :changelog, changelog_attrs, user) + # assert changelog.inner_id == 1 - {:ok, community} = ORM.find(Community, community.id) + # {:ok, community} = ORM.find(Community, community.id) - assert community.meta.posts_inner_id_index == 3 - assert community.meta.blogs_inner_id_index == 2 - assert community.meta.changelogs_inner_id_index == 1 - assert community.meta.docs_inner_id_index == 0 + # assert community.meta.posts_inner_id_index == 3 + # assert community.meta.blogs_inner_id_index == 2 + # assert community.meta.changelogs_inner_id_index == 1 + # assert community.meta.docs_inner_id_index == 0 - assert community.articles_count == 6 + # assert community.articles_count == 6 end test "can create post with valid attrs", ~m(user community post_attrs)a do diff --git a/test/groupher_server_web/mutation/cms/dashboard_test.exs b/test/groupher_server_web/mutation/cms/dashboard_test.exs index 2113611..63979d8 100644 --- a/test/groupher_server_web/mutation/cms/dashboard_test.exs +++ b/test/groupher_server_web/mutation/cms/dashboard_test.exs @@ -207,7 +207,6 @@ defmodule GroupherServer.Test.Mutation.CMS.Dashboard do } } """ - @tag :wip test "update community dashboard layout info", ~m(community)a do rule_conn = simu_conn(:user, cms: %{"community.update" => true}) diff --git a/test/groupher_server_web/query/cms/paged_articles/paged_kanban_posts_test.exs b/test/groupher_server_web/query/cms/paged_articles/paged_kanban_posts_test.exs index b4f3a88..b8556d3 100644 --- a/test/groupher_server_web/query/cms/paged_articles/paged_kanban_posts_test.exs +++ b/test/groupher_server_web/query/cms/paged_articles/paged_kanban_posts_test.exs @@ -70,7 +70,6 @@ defmodule GroupherServer.Test.Query.PagedArticles.PagedKanbanPosts do } } """ - @tag :wip test "should get grouped paged posts", ~m(guest_conn user community post_attrs)a do {:ok, post} = CMS.create_article(community, :post, post_attrs, user) From 7ca351c052cdcecaa01a3fc207258fc256fe63be Mon Sep 17 00:00:00 2001 From: mydearxym Date: Mon, 1 Apr 2024 19:16:14 +0800 Subject: [PATCH 02/16] chore: wip --- lib/groupher_server/cms/constant.ex | 63 ------------------- .../cms/delegates/article_crud.ex | 21 +++---- .../cms/delegates/comment_crud.ex | 13 ++-- lib/groupher_server/cms/helper/macros.ex | 15 ++++- lib/groupher_server/cms/models/Changelog.ex | 5 +- .../cms/models/article_join_tag.ex | 24 +++++++ lib/groupher_server/cms/models/article_tag.ex | 3 + lib/groupher_server/cms/models/blog.ex | 5 +- lib/groupher_server/cms/models/doc.ex | 5 +- lib/groupher_server/cms/models/passport.ex | 3 + .../cms/models/post_document.ex | 3 + .../resolvers/cms_resolver.ex | 8 +-- lib/helper/query_builder.ex | 10 +-- priv/mock/post_seeds.exs | 3 +- ...20240401081253_add_prefix_to_constents.exs | 19 ++++++ ...11242_add_prefix_to_blog_changelog_doc.exs | 26 ++++++++ .../cms/articles/blog_pending_flag_test.exs | 6 +- .../articles/changelog_pending_flag_test.exs | 6 +- .../cms/articles/doc_pending_flag_test.exs | 6 +- .../cms/articles/kanban_test.exs | 8 +-- .../cms/articles/post_pending_flag_test.exs | 6 +- .../cms/articles/post_test.exs | 37 +++++------ .../cms/comments/blog_pending_test.exs | 7 +-- .../cms/comments/changelog_pending_test.exs | 8 +-- .../cms/comments/doc_pending_test.exs | 7 +-- .../cms/comments/post_comment_test.exs | 8 +-- .../cms/comments/post_pending_test.exs | 7 +-- .../cms/community/community_test.exs | 8 +-- .../cms/hooks/audit_post_comment_test.exs | 11 ++-- .../cms/hooks/audit_post_test.exs | 9 ++- .../mutation/cms/crud_test.exs | 7 +-- .../mutation/cms/dashboard_test.exs | 5 -- .../query/cms/articles/kanban_test.exs | 6 +- .../query/cms/flags/blogs_flags_test.exs | 6 +- .../query/cms/flags/changelogs_flags_test.exs | 6 +- .../query/cms/flags/docs_flags_test.exs | 6 +- .../query/cms/flags/posts_flags_test.exs | 6 +- .../paged_kanban_posts_test.exs | 10 ++- .../cms/paged_articles/paged_posts_test.exs | 12 ++-- 39 files changed, 215 insertions(+), 209 deletions(-) delete mode 100644 lib/groupher_server/cms/constant.ex create mode 100644 lib/groupher_server/cms/models/article_join_tag.ex create mode 100644 priv/repo/migrations/20240401081253_add_prefix_to_constents.exs create mode 100644 priv/repo/migrations/20240401111242_add_prefix_to_blog_changelog_doc.exs diff --git a/lib/groupher_server/cms/constant.ex b/lib/groupher_server/cms/constant.ex deleted file mode 100644 index 5fb4f82..0000000 --- a/lib/groupher_server/cms/constant.ex +++ /dev/null @@ -1,63 +0,0 @@ -defmodule GroupherServer.CMS.Constant do - @moduledoc """ - constant used for CMS - - NOTE: DO NOT modify, unless you know what you are doing - """ - - import Helper.Utils.Map, only: [reverse_kv: 1] - - @artiment_legal 0 - @artiment_illegal 1 - @artiment_audit_failed 2 - - @community_normal 0 - @community_applying 1 - - @apply_web "WEB" - - @article_cat_map %{ - feature: 1, - bug: 2, - question: 3, - other: 4 - } - - @article_state_map %{ - default: 1, - todo: 2, - wip: 3, - done: 4, - # for question cat - resolved: 5, - reject: 6, - reject_dup: 7, - reject_no_plan: 8, - reject_repro: 9, - reject_stale: 10 - } - - @article_cat_value_map reverse_kv(@article_cat_map) - @article_state_value_map reverse_kv(@article_state_map) - - def pending(:legal), do: @artiment_legal - def pending(:illegal), do: @artiment_illegal - def pending(:audit_failed), do: @artiment_audit_failed - - def pending(:normal), do: @community_normal - def pending(:applying), do: @community_applying - - def apply_category(:web), do: @apply_web - - def article_cat, do: @article_cat_map - - def article_cat_value(cat) do - @article_cat_value_map |> Map.get(cat) |> to_string |> String.upcase() - end - - def article_state, do: @article_state_map - - def article_state_value(state) do - @article_state_value_map |> Map.get(state) |> to_string |> String.upcase() - end -end diff --git a/lib/groupher_server/cms/delegates/article_crud.ex b/lib/groupher_server/cms/delegates/article_crud.ex index e022c4b..a854fef 100644 --- a/lib/groupher_server/cms/delegates/article_crud.ex +++ b/lib/groupher_server/cms/delegates/article_crud.ex @@ -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, @@ -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 @@ -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 diff --git a/lib/groupher_server/cms/delegates/comment_crud.ex b/lib/groupher_server/cms/delegates/comment_crud.ex index 0cedd49..a9146ce 100644 --- a/lib/groupher_server/cms/delegates/comment_crud.ex +++ b/lib/groupher_server/cms/delegates/comment_crud.ex @@ -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 @@ -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} diff --git a/lib/groupher_server/cms/helper/macros.ex b/lib/groupher_server/cms/helper/macros.ex index adaeba2..644a977 100644 --- a/lib/groupher_server/cms/helper/macros.ex +++ b/lib/groupher_server/cms/helper/macros.ex @@ -7,7 +7,17 @@ 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, + ArticleTag, + ArticleUpvote, + ArticleCollect, + ArticleJoinTag + } @article_threads get_config(:article, :threads) @@ -248,7 +258,8 @@ defmodule GroupherServer.CMS.Helper.Macros do many_to_many( :article_tags, ArticleTag, - join_through: "articles_join_tags", + # "articles_join_tags", + 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, diff --git a/lib/groupher_server/cms/models/Changelog.ex b/lib/groupher_server/cms/models/Changelog.ex index 6f269e2..b19a4de 100644 --- a/lib/groupher_server/cms/models/Changelog.ex +++ b/lib/groupher_server/cms/models/Changelog.ex @@ -11,6 +11,7 @@ defmodule GroupherServer.CMS.Model.Changelog do alias GroupherServer.CMS alias CMS.Model.Embeds + alias Helper.Constant.DBPrefix alias Helper.HTML @timestamps_opts [type: :utc_datetime_usec] @@ -20,8 +21,10 @@ defmodule GroupherServer.CMS.Model.Changelog do @optional_fields ~w(updated_at inserted_at active_at archived_at inner_id)a ++ @article_cast_fields + @schema_prefix DBPrefix.cms() + @type t :: %Changelog{} - schema "cms_changelogs" do + schema "changelogs" do article_tags_field(:changelog) article_communities_field(:changelog) general_article_fields(:changelog) diff --git a/lib/groupher_server/cms/models/article_join_tag.ex b/lib/groupher_server/cms/models/article_join_tag.ex new file mode 100644 index 0000000..f289d43 --- /dev/null +++ b/lib/groupher_server/cms/models/article_join_tag.ex @@ -0,0 +1,24 @@ +defmodule GroupherServer.CMS.Model.ArticleJoinTag do + @moduledoc false + alias __MODULE__ + + use Ecto.Schema + use Accessible + + import Ecto.Changeset + + alias GroupherServer.CMS + alias CMS.Model.{ArticleTag, Post, Changelog, Doc, Blog} + alias Helper.Constant.DBPrefix + + @schema_prefix DBPrefix.cms() + + @type t :: %ArticleJoinTag{} + schema "articles_join_tags" do + belongs_to(:article_tag, ArticleTag) + belongs_to(:post, Post) + belongs_to(:changelog, Changelog) + belongs_to(:doc, Doc) + belongs_to(:blog, Blog) + end +end diff --git a/lib/groupher_server/cms/models/article_tag.ex b/lib/groupher_server/cms/models/article_tag.ex index 56e3e86..8fcf559 100644 --- a/lib/groupher_server/cms/models/article_tag.ex +++ b/lib/groupher_server/cms/models/article_tag.ex @@ -9,10 +9,13 @@ defmodule GroupherServer.CMS.Model.ArticleTag do alias GroupherServer.CMS alias CMS.Model.{Author, Community} + alias Helper.Constant.DBPrefix @required_fields ~w(thread title color author_id community_id slug)a @updatable_fields ~w(thread title desc color community_id group extra icon slug index layout)a + @schema_prefix DBPrefix.cms() + @type t :: %ArticleTag{} schema "article_tags" do field(:title, :string) diff --git a/lib/groupher_server/cms/models/blog.ex b/lib/groupher_server/cms/models/blog.ex index 918896f..8a6d417 100644 --- a/lib/groupher_server/cms/models/blog.ex +++ b/lib/groupher_server/cms/models/blog.ex @@ -8,6 +8,7 @@ defmodule GroupherServer.CMS.Model.Blog do import Ecto.Changeset import GroupherServer.CMS.Helper.Macros + alias Helper.Constant.DBPrefix alias GroupherServer.CMS alias CMS.Model.Embeds @@ -18,8 +19,10 @@ defmodule GroupherServer.CMS.Model.Blog do @optional_fields ~w(updated_at inserted_at active_at archived_at inner_id)a ++ @article_cast_fields + @schema_prefix DBPrefix.cms() + @type t :: %Blog{} - schema "cms_blogs" do + schema "blogs" do article_tags_field(:blog) article_communities_field(:blog) general_article_fields(:blog) diff --git a/lib/groupher_server/cms/models/doc.ex b/lib/groupher_server/cms/models/doc.ex index db971ab..868f7c7 100644 --- a/lib/groupher_server/cms/models/doc.ex +++ b/lib/groupher_server/cms/models/doc.ex @@ -12,6 +12,7 @@ defmodule GroupherServer.CMS.Model.Doc do alias CMS.Model.Embeds alias Helper.HTML + alias Helper.Constant.DBPrefix @timestamps_opts [type: :utc_datetime_usec] @@ -20,8 +21,10 @@ defmodule GroupherServer.CMS.Model.Doc do @optional_fields ~w(updated_at inserted_at active_at archived_at inner_id)a ++ @article_cast_fields + @schema_prefix DBPrefix.default() + @type t :: %Doc{} - schema "cms_docs" do + schema "docs" do article_tags_field(:doc) article_communities_field(:doc) general_article_fields(:doc) diff --git a/lib/groupher_server/cms/models/passport.ex b/lib/groupher_server/cms/models/passport.ex index 333de0b..09ffe4c 100644 --- a/lib/groupher_server/cms/models/passport.ex +++ b/lib/groupher_server/cms/models/passport.ex @@ -5,12 +5,15 @@ defmodule GroupherServer.CMS.Model.Passport do use Ecto.Schema import Ecto.Changeset + alias Helper.Constant.DBPrefix alias GroupherServer.Accounts alias Accounts.Model.User @required_fields ~w(rules user_id)a @optional_fields ~w(rules)a + @schema_prefix DBPrefix.default() + @type t :: %Passport{} schema "cms_passports" do field(:rules, :map) diff --git a/lib/groupher_server/cms/models/post_document.ex b/lib/groupher_server/cms/models/post_document.ex index f600813..2536387 100644 --- a/lib/groupher_server/cms/models/post_document.ex +++ b/lib/groupher_server/cms/models/post_document.ex @@ -10,6 +10,7 @@ defmodule GroupherServer.CMS.Model.PostDocument do import Ecto.Changeset import Helper.Utils, only: [get_config: 2] + alias Helper.Constant.DBPrefix alias GroupherServer.CMS alias CMS.Model.Post @@ -21,6 +22,8 @@ defmodule GroupherServer.CMS.Model.PostDocument do @required_fields ~w(body body_html post_id)a @optional_fields [] + @schema_prefix DBPrefix.default() + @type t :: %PostDocument{} schema "post_documents" do belongs_to(:post, Post, foreign_key: :post_id) diff --git a/lib/groupher_server_web/resolvers/cms_resolver.ex b/lib/groupher_server_web/resolvers/cms_resolver.ex index d479edc..cf5d12e 100644 --- a/lib/groupher_server_web/resolvers/cms_resolver.ex +++ b/lib/groupher_server_web/resolvers/cms_resolver.ex @@ -6,14 +6,12 @@ defmodule GroupherServerWeb.Resolvers.CMS do alias GroupherServer.{Accounts, CMS} + alias Helper.{ORM, OgInfo, Constant} alias Accounts.Model.User alias CMS.Model.{Community, Category, Thread} - alias CMS.Constant - alias Helper.{ORM, OgInfo} - - @article_cat Constant.article_cat() - @article_state Constant.article_state() + @article_cat Constant.CMS.article_cat() + @article_state Constant.CMS.article_state() # ####################### # community .. # ####################### diff --git a/lib/helper/query_builder.ex b/lib/helper/query_builder.ex index 4d9e864..9b49471 100644 --- a/lib/helper/query_builder.ex +++ b/lib/helper/query_builder.ex @@ -6,13 +6,13 @@ defmodule Helper.QueryBuilder do import Ecto.Query, warn: false alias GroupherServer.CMS - alias CMS.Constant + alias Helper.Constant - @article_cat Constant.article_cat() - @article_state Constant.article_state() + @article_cat Constant.CMS.article_cat() + @article_state Constant.CMS.article_state() - @audit_illegal Constant.pending(:illegal) - @audit_failed Constant.pending(:audit_failed) + @audit_illegal Constant.CMS.pending(:illegal) + @audit_failed Constant.CMS.pending(:audit_failed) @doc """ load inner user field diff --git a/priv/mock/post_seeds.exs b/priv/mock/post_seeds.exs index f9d274f..fb4ced8 100644 --- a/priv/mock/post_seeds.exs +++ b/priv/mock/post_seeds.exs @@ -2,8 +2,7 @@ alias GroupherServer.CMS.Delegate.Seeds alias GroupherServer.CMS alias CMS.Model.{Post, ArticleTag} -alias CMS.Constant -alias Helper.ORM +alias Helper.{Constant,ORM} {:ok, post} = Seeds.Articles.seed_articles("home", :post) diff --git a/priv/repo/migrations/20240401081253_add_prefix_to_constents.exs b/priv/repo/migrations/20240401081253_add_prefix_to_constents.exs new file mode 100644 index 0000000..0570acf --- /dev/null +++ b/priv/repo/migrations/20240401081253_add_prefix_to_constents.exs @@ -0,0 +1,19 @@ +defmodule GroupherServer.Repo.Migrations.AddPrefixToConstents do + use Ecto.Migration + + def up do + execute "ALTER TABLE public.communities_join_blogs SET SCHEMA cms" + execute "ALTER TABLE public.communities_join_docs SET SCHEMA cms" + execute "ALTER TABLE public.communities_join_changelogs SET SCHEMA cms" + + execute "ALTER TABLE public.articles_join_tags SET SCHEMA cms" + end + + def down do + execute "ALTER TABLE cms.communities_join_blogs SET SCHEMA public" + execute "ALTER TABLE cms.communities_join_docs SET SCHEMA public" + execute "ALTER TABLE cms.communities_join_changelogs SET SCHEMA public" + + execute "ALTER TABLE cms.articles_join_tags SET SCHEMA public" + end +end diff --git a/priv/repo/migrations/20240401111242_add_prefix_to_blog_changelog_doc.exs b/priv/repo/migrations/20240401111242_add_prefix_to_blog_changelog_doc.exs new file mode 100644 index 0000000..8ca0e6e --- /dev/null +++ b/priv/repo/migrations/20240401111242_add_prefix_to_blog_changelog_doc.exs @@ -0,0 +1,26 @@ +defmodule GroupherServer.Repo.Migrations.AddPrefixToBlogChangelogDoc do + use Ecto.Migration + + def up do + execute "ALTER TABLE cms_blogs RENAME TO blogs" + execute "ALTER TABLE public.blogs SET SCHEMA cms" + + execute "ALTER TABLE cms_changelogs RENAME TO changelogs" + execute "ALTER TABLE public.changelogs SET SCHEMA cms" + + execute "ALTER TABLE cms_docs RENAME TO docs" + execute "ALTER TABLE public.docs SET SCHEMA cms" + end + + def down do + execute "ALTER TABLE cms.blogs SET SCHEMA public" + execute "ALTER TABLE public.blogs RENAME TO cms_blogs" + + + execute "ALTER TABLE cms.changelogs SET SCHEMA public" + execute "ALTER TABLE public.changelogs RENAME TO cms_changelos" + + execute "ALTER TABLE cms.docs SET SCHEMA public" + execute "ALTER TABLE public.docs RENAME TO cms_docs" + end +end diff --git a/test/groupher_server/cms/articles/blog_pending_flag_test.exs b/test/groupher_server/cms/articles/blog_pending_flag_test.exs index 3bac8c6..b35f33f 100644 --- a/test/groupher_server/cms/articles/blog_pending_flag_test.exs +++ b/test/groupher_server/cms/articles/blog_pending_flag_test.exs @@ -4,12 +4,12 @@ defmodule GroupherServer.Test.CMS.BlogPendingFlag do alias GroupherServer.{Accounts, CMS, Repo} alias Accounts.Model.User alias CMS.Model.Blog - alias Helper.ORM + alias Helper.{Constant, ORM} @total_count 35 - @audit_legal CMS.Constant.pending(:legal) - @audit_illegal CMS.Constant.pending(:illegal) + @audit_legal Constant.CMS.pending(:legal) + @audit_illegal Constant.CMS.pending(:illegal) setup do {:ok, user} = db_insert(:user) diff --git a/test/groupher_server/cms/articles/changelog_pending_flag_test.exs b/test/groupher_server/cms/articles/changelog_pending_flag_test.exs index 7337c04..205f533 100644 --- a/test/groupher_server/cms/articles/changelog_pending_flag_test.exs +++ b/test/groupher_server/cms/articles/changelog_pending_flag_test.exs @@ -4,12 +4,12 @@ defmodule GroupherServer.Test.CMS.ChangelogPendingFlag do alias GroupherServer.{Accounts, CMS, Repo} alias Accounts.Model.User alias CMS.Model.Changelog - alias Helper.ORM + alias Helper.{Constant, ORM} @total_count 35 - @audit_legal CMS.Constant.pending(:legal) - @audit_illegal CMS.Constant.pending(:illegal) + @audit_legal Constant.CMS.pending(:legal) + @audit_illegal Constant.CMS.pending(:illegal) setup do {:ok, user} = db_insert(:user) diff --git a/test/groupher_server/cms/articles/doc_pending_flag_test.exs b/test/groupher_server/cms/articles/doc_pending_flag_test.exs index 0b944ee..228099e 100644 --- a/test/groupher_server/cms/articles/doc_pending_flag_test.exs +++ b/test/groupher_server/cms/articles/doc_pending_flag_test.exs @@ -4,12 +4,12 @@ defmodule GroupherServer.Test.CMS.DocPendingFlag do alias GroupherServer.{Accounts, CMS, Repo} alias Accounts.Model.User alias CMS.Model.Doc - alias Helper.ORM + alias Helper.{Constant, ORM} @total_count 35 - @audit_legal CMS.Constant.pending(:legal) - @audit_illegal CMS.Constant.pending(:illegal) + @audit_legal Constant.CMS.pending(:legal) + @audit_illegal Constant.CMS.pending(:illegal) setup do {:ok, user} = db_insert(:user) diff --git a/test/groupher_server/cms/articles/kanban_test.exs b/test/groupher_server/cms/articles/kanban_test.exs index bf028f4..f39a630 100644 --- a/test/groupher_server/cms/articles/kanban_test.exs +++ b/test/groupher_server/cms/articles/kanban_test.exs @@ -2,14 +2,12 @@ defmodule GroupherServer.Test.CMS.Articles.Kanban do use GroupherServer.TestTools alias GroupherServer.CMS - alias Helper.ORM + alias Helper.{Constant, ORM} alias CMS.Model.Author - alias CMS.Constant - - @article_cat Constant.article_cat() - @article_state Constant.article_state() + @article_cat Constant.CMS.article_cat() + @article_state Constant.CMS.article_state() setup do {:ok, user} = db_insert(:user) diff --git a/test/groupher_server/cms/articles/post_pending_flag_test.exs b/test/groupher_server/cms/articles/post_pending_flag_test.exs index c4a532f..a57c396 100644 --- a/test/groupher_server/cms/articles/post_pending_flag_test.exs +++ b/test/groupher_server/cms/articles/post_pending_flag_test.exs @@ -3,12 +3,12 @@ defmodule GroupherServer.Test.CMS.PostPendingFlag do alias GroupherServer.{Accounts, CMS, Repo} alias Accounts.Model.User - alias Helper.ORM + alias Helper.{Constant, ORM} @total_count 35 - @audit_legal CMS.Constant.pending(:legal) - @audit_illegal CMS.Constant.pending(:illegal) + @audit_legal Constant.CMS.pending(:legal) + @audit_illegal Constant.CMS.pending(:illegal) setup do {:ok, user} = db_insert(:user) diff --git a/test/groupher_server/cms/articles/post_test.exs b/test/groupher_server/cms/articles/post_test.exs index f225d91..0d82b01 100644 --- a/test/groupher_server/cms/articles/post_test.exs +++ b/test/groupher_server/cms/articles/post_test.exs @@ -32,32 +32,32 @@ defmodule GroupherServer.Test.CMS.Articles.Post do {:ok, post} = CMS.create_article(community, :post, post_attrs, user) assert post.inner_id == 1 - # {:ok, post} = CMS.create_article(community, :post, post_attrs, user) - # assert post.inner_id == 2 + {:ok, post} = CMS.create_article(community, :post, post_attrs, user) + assert post.inner_id == 2 - # {:ok, post} = CMS.create_article(community, :post, post_attrs, user) - # assert post.inner_id == 3 + {:ok, post} = CMS.create_article(community, :post, post_attrs, user) + assert post.inner_id == 3 - # blog_attrs = mock_attrs(:blog, %{community_id: community.id}) - # changelog_attrs = mock_attrs(:changelog, %{community_id: community.id}) + blog_attrs = mock_attrs(:blog, %{community_id: community.id}) + changelog_attrs = mock_attrs(:changelog, %{community_id: community.id}) - # {:ok, blog} = CMS.create_article(community, :blog, blog_attrs, user) - # assert blog.inner_id == 1 + {:ok, blog} = CMS.create_article(community, :blog, blog_attrs, user) + assert blog.inner_id == 1 - # {:ok, blog} = CMS.create_article(community, :blog, blog_attrs, user) - # assert blog.inner_id == 2 + {:ok, blog} = CMS.create_article(community, :blog, blog_attrs, user) + assert blog.inner_id == 2 - # {:ok, changelog} = CMS.create_article(community, :changelog, changelog_attrs, user) - # assert changelog.inner_id == 1 + {:ok, changelog} = CMS.create_article(community, :changelog, changelog_attrs, user) + assert changelog.inner_id == 1 - # {:ok, community} = ORM.find(Community, community.id) + {:ok, community} = ORM.find(Community, community.id) - # assert community.meta.posts_inner_id_index == 3 - # assert community.meta.blogs_inner_id_index == 2 - # assert community.meta.changelogs_inner_id_index == 1 - # assert community.meta.docs_inner_id_index == 0 + assert community.meta.posts_inner_id_index == 3 + assert community.meta.blogs_inner_id_index == 2 + assert community.meta.changelogs_inner_id_index == 1 + assert community.meta.docs_inner_id_index == 0 - # assert community.articles_count == 6 + assert community.articles_count == 6 end test "can create post with valid attrs", ~m(user community post_attrs)a do @@ -325,6 +325,7 @@ defmodule GroupherServer.Test.CMS.Articles.Post do assert article_doc.body == post_doc.body end + @tag :wip test "delete post should also delete related document", ~m(user community post_attrs)a do {:ok, post} = CMS.create_article(community, :post, post_attrs, user) {:ok, _article_doc} = ORM.find_by(ArticleDocument, %{article_id: post.id, thread: "POST"}) diff --git a/test/groupher_server/cms/comments/blog_pending_test.exs b/test/groupher_server/cms/comments/blog_pending_test.exs index c7eb67e..2f096fc 100644 --- a/test/groupher_server/cms/comments/blog_pending_test.exs +++ b/test/groupher_server/cms/comments/blog_pending_test.exs @@ -6,11 +6,10 @@ defmodule GroupherServer.Test.CMS.Comments.BlogPending do alias GroupherServer.{Accounts, CMS} alias Accounts.Model.User alias CMS.Model.Comment - alias Helper.ORM - alias CMS.Constant + alias Helper.{Constant, ORM} - @audit_legal Constant.pending(:legal) - @audit_illegal Constant.pending(:illegal) + @audit_legal Constant.CMS.pending(:legal) + @audit_illegal Constant.CMS.pending(:illegal) setup do {:ok, user} = db_insert(:user) diff --git a/test/groupher_server/cms/comments/changelog_pending_test.exs b/test/groupher_server/cms/comments/changelog_pending_test.exs index 523f087..ef5404d 100644 --- a/test/groupher_server/cms/comments/changelog_pending_test.exs +++ b/test/groupher_server/cms/comments/changelog_pending_test.exs @@ -2,14 +2,12 @@ defmodule GroupherServer.Test.CMS.Comments.ChangelogPendingFlag do use GroupherServer.TestTools alias GroupherServer.{Accounts, CMS} + alias Helper.{Constant, ORM} alias Accounts.Model.User alias CMS.Model.Comment - alias CMS.Constant - alias Helper.ORM - - @audit_legal Constant.pending(:legal) - @audit_illegal Constant.pending(:illegal) + @audit_legal Constant.CMS.pending(:legal) + @audit_illegal Constant.CMS.pending(:illegal) setup do {:ok, user} = db_insert(:user) diff --git a/test/groupher_server/cms/comments/doc_pending_test.exs b/test/groupher_server/cms/comments/doc_pending_test.exs index ec50ec8..367d0c3 100644 --- a/test/groupher_server/cms/comments/doc_pending_test.exs +++ b/test/groupher_server/cms/comments/doc_pending_test.exs @@ -4,12 +4,11 @@ defmodule GroupherServer.Test.CMS.Comments.DocPendingFlag do alias GroupherServer.{Accounts, CMS} alias Accounts.Model.User alias CMS.Model.Comment - alias CMS.Constant - alias Helper.ORM + alias Helper.{Constant, ORM} - @audit_legal Constant.pending(:legal) - @audit_illegal Constant.pending(:illegal) + @audit_legal Constant.CMS.pending(:legal) + @audit_illegal Constant.CMS.pending(:illegal) setup do {:ok, user} = db_insert(:user) diff --git a/test/groupher_server/cms/comments/post_comment_test.exs b/test/groupher_server/cms/comments/post_comment_test.exs index 1fd552c..bf025e0 100644 --- a/test/groupher_server/cms/comments/post_comment_test.exs +++ b/test/groupher_server/cms/comments/post_comment_test.exs @@ -4,15 +4,13 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do use GroupherServer.TestTools import Helper.Utils, only: [get_config: 2] - alias Helper.ORM + alias Helper.{Constant, ORM} alias GroupherServer.{Accounts, CMS, Repo} alias Accounts.Model.User alias CMS.Model.{Comment, CommunitySubscriber, PinnedComment, Embeds, Post, CommunitySubscriber} - alias CMS.Constant - - @article_cat Constant.article_cat() - @article_state Constant.article_state() + @article_cat Constant.CMS.article_cat() + @article_state Constant.CMS.article_state() @active_period get_config(:article, :active_period_days) diff --git a/test/groupher_server/cms/comments/post_pending_test.exs b/test/groupher_server/cms/comments/post_pending_test.exs index c9641db..c34f1c6 100644 --- a/test/groupher_server/cms/comments/post_pending_test.exs +++ b/test/groupher_server/cms/comments/post_pending_test.exs @@ -6,11 +6,10 @@ defmodule GroupherServer.Test.CMS.Comments.PostPending do alias GroupherServer.{Accounts, CMS} alias Accounts.Model.User alias CMS.Model.Comment - alias Helper.ORM - alias CMS.Constant + alias Helper.{Constant, ORM} - @audit_legal Constant.pending(:legal) - @audit_illegal Constant.pending(:illegal) + @audit_legal Constant.CMS.pending(:legal) + @audit_illegal Constant.CMS.pending(:illegal) setup do {:ok, user} = db_insert(:user) diff --git a/test/groupher_server/cms/community/community_test.exs b/test/groupher_server/cms/community/community_test.exs index f76863b..01ea7bd 100644 --- a/test/groupher_server/cms/community/community_test.exs +++ b/test/groupher_server/cms/community/community_test.exs @@ -6,12 +6,10 @@ defmodule GroupherServer.Test.CMS.Community do alias GroupherServer.CMS alias CMS.Model.{Community, Thread} - alias Helper.ORM + alias Helper.{Constant, ORM} - alias CMS.Constant - - @community_normal Constant.pending(:normal) - @community_applying Constant.pending(:applying) + @community_normal Constant.CMS.pending(:normal) + @community_applying Constant.CMS.pending(:applying) setup do {:ok, user} = db_insert(:user) diff --git a/test/groupher_server/cms/hooks/audit_post_comment_test.exs b/test/groupher_server/cms/hooks/audit_post_comment_test.exs index f23cbb1..9986d9a 100644 --- a/test/groupher_server/cms/hooks/audit_post_comment_test.exs +++ b/test/groupher_server/cms/hooks/audit_post_comment_test.exs @@ -3,14 +3,13 @@ defmodule GroupherServer.Test.CMS.Hooks.AuditPostComment do use GroupherServer.TestTools - alias GroupherServer.{CMS} + alias GroupherServer.CMS alias CMS.Delegate.Hooks - # alias Helper.{ORM, Scheduler} - alias CMS.Constant + alias Helper.Constant - @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) setup do {:ok, user} = db_insert(:user) diff --git a/test/groupher_server/cms/hooks/audit_post_test.exs b/test/groupher_server/cms/hooks/audit_post_test.exs index 51aa3d3..c224fb1 100644 --- a/test/groupher_server/cms/hooks/audit_post_test.exs +++ b/test/groupher_server/cms/hooks/audit_post_test.exs @@ -5,12 +5,11 @@ defmodule GroupherServer.Test.CMS.Hooks.AuditPost do alias GroupherServer.{CMS} # alias CMS.Delegate.Hooks - # alias Helper.ORM - alias CMS.Constant + alias Helper.Constant - @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) setup do {:ok, user} = db_insert(:user) diff --git a/test/groupher_server_web/mutation/cms/crud_test.exs b/test/groupher_server_web/mutation/cms/crud_test.exs index ca87454..3aa039c 100644 --- a/test/groupher_server_web/mutation/cms/crud_test.exs +++ b/test/groupher_server_web/mutation/cms/crud_test.exs @@ -7,11 +7,10 @@ defmodule GroupherServer.Test.Mutation.CMS.CRUD do alias Accounts.Model.User alias CMS.Model.{Category, Community, CommunityModerator, Passport} - alias Helper.{Certification, ORM} - alias CMS.Constant + alias Helper.{Certification, ORM, Constant} - @community_normal Constant.pending(:normal) - @community_applying Constant.pending(:applying) + @community_normal Constant.CMS.pending(:normal) + @community_applying Constant.CMS.pending(:applying) @default_root_rules Certification.passport_rules(cms: "root") diff --git a/test/groupher_server_web/mutation/cms/dashboard_test.exs b/test/groupher_server_web/mutation/cms/dashboard_test.exs index 63979d8..4adfdf6 100644 --- a/test/groupher_server_web/mutation/cms/dashboard_test.exs +++ b/test/groupher_server_web/mutation/cms/dashboard_test.exs @@ -6,12 +6,7 @@ defmodule GroupherServer.Test.Mutation.CMS.Dashboard do alias GroupherServer.CMS alias CMS.Model.Community - alias Helper.ORM - # alias CMS.Constant - - # @community_normal Constant.pending(:normal) - # @community_applying Constant.pending(:applying) setup do {:ok, category} = db_insert(:category) diff --git a/test/groupher_server_web/query/cms/articles/kanban_test.exs b/test/groupher_server_web/query/cms/articles/kanban_test.exs index e719cb9..209b0b3 100644 --- a/test/groupher_server_web/query/cms/articles/kanban_test.exs +++ b/test/groupher_server_web/query/cms/articles/kanban_test.exs @@ -3,10 +3,10 @@ defmodule GroupherServer.Test.Query.Articles.Kanban do alias GroupherServer.CMS - alias CMS.Constant + alias Helper.Constant - @article_cat Constant.article_cat() - @article_state Constant.article_state() + @article_cat Constant.CMS.article_cat() + @article_state Constant.CMS.article_state() setup do {:ok, user} = db_insert(:user) diff --git a/test/groupher_server_web/query/cms/flags/blogs_flags_test.exs b/test/groupher_server_web/query/cms/flags/blogs_flags_test.exs index f6390b8..b62ed59 100644 --- a/test/groupher_server_web/query/cms/flags/blogs_flags_test.exs +++ b/test/groupher_server_web/query/cms/flags/blogs_flags_test.exs @@ -3,13 +3,13 @@ defmodule GroupherServer.Test.Query.Flags.BlogsFlags do import Helper.Utils, only: [get_config: 2] - alias GroupherServer.{CMS} - alias Helper.ORM + alias GroupherServer.CMS + alias Helper.{Constant, ORM} @total_count 35 @page_size get_config(:general, :page_size) - @audit_illegal CMS.Constant.pending(:illegal) + @audit_illegal Constant.CMS.pending(:illegal) setup do {:ok, user} = db_insert(:user) diff --git a/test/groupher_server_web/query/cms/flags/changelogs_flags_test.exs b/test/groupher_server_web/query/cms/flags/changelogs_flags_test.exs index 3c37110..e7f8865 100644 --- a/test/groupher_server_web/query/cms/flags/changelogs_flags_test.exs +++ b/test/groupher_server_web/query/cms/flags/changelogs_flags_test.exs @@ -3,13 +3,13 @@ defmodule GroupherServer.Test.Query.Flags.ChangelogsFlags do import Helper.Utils, only: [get_config: 2] - alias GroupherServer.{CMS} - alias Helper.ORM + alias GroupherServer.CMS + alias Helper.{Constant, ORM} @total_count 35 @page_size get_config(:general, :page_size) - @audit_illegal CMS.Constant.pending(:illegal) + @audit_illegal Constant.CMS.pending(:illegal) setup do {:ok, user} = db_insert(:user) diff --git a/test/groupher_server_web/query/cms/flags/docs_flags_test.exs b/test/groupher_server_web/query/cms/flags/docs_flags_test.exs index 6131544..f784a53 100644 --- a/test/groupher_server_web/query/cms/flags/docs_flags_test.exs +++ b/test/groupher_server_web/query/cms/flags/docs_flags_test.exs @@ -3,13 +3,13 @@ defmodule GroupherServer.Test.Query.Flags.DocsFlags do import Helper.Utils, only: [get_config: 2] - alias GroupherServer.{CMS} - alias Helper.ORM + alias GroupherServer.CMS + alias Helper.{Constant, ORM} @total_count 35 @page_size get_config(:general, :page_size) - @audit_illegal CMS.Constant.pending(:illegal) + @audit_illegal Constant.CMS.pending(:illegal) setup do {:ok, user} = db_insert(:user) diff --git a/test/groupher_server_web/query/cms/flags/posts_flags_test.exs b/test/groupher_server_web/query/cms/flags/posts_flags_test.exs index e3bd6de..8f67fa6 100644 --- a/test/groupher_server_web/query/cms/flags/posts_flags_test.exs +++ b/test/groupher_server_web/query/cms/flags/posts_flags_test.exs @@ -3,13 +3,13 @@ defmodule GroupherServer.Test.Query.Flags.PostsFlags do import Helper.Utils, only: [get_config: 2] - alias GroupherServer.{CMS} - alias Helper.ORM + alias GroupherServer.CMS + alias Helper.{Constant, ORM} @total_count 35 @page_size get_config(:general, :page_size) - @audit_illegal CMS.Constant.pending(:illegal) + @audit_illegal Constant.CMS.pending(:illegal) setup do {:ok, user} = db_insert(:user) diff --git a/test/groupher_server_web/query/cms/paged_articles/paged_kanban_posts_test.exs b/test/groupher_server_web/query/cms/paged_articles/paged_kanban_posts_test.exs index b8556d3..8fc088b 100644 --- a/test/groupher_server_web/query/cms/paged_articles/paged_kanban_posts_test.exs +++ b/test/groupher_server_web/query/cms/paged_articles/paged_kanban_posts_test.exs @@ -5,13 +5,11 @@ defmodule GroupherServer.Test.Query.PagedArticles.PagedKanbanPosts do import Helper.Utils, only: [get_config: 2] - alias GroupherServer.CMS - alias GroupherServer.Repo + alias GroupherServer.{CMS, Repo} + alias Helper.Constant - alias CMS.Constant - - @article_cat Constant.article_cat() - @article_state Constant.article_state() + @article_cat Constant.CMS.article_cat() + @article_state Constant.CMS.article_state() @page_size get_config(:general, :page_size) diff --git a/test/groupher_server_web/query/cms/paged_articles/paged_posts_test.exs b/test/groupher_server_web/query/cms/paged_articles/paged_posts_test.exs index 0292288..f3b91aa 100644 --- a/test/groupher_server_web/query/cms/paged_articles/paged_posts_test.exs +++ b/test/groupher_server_web/query/cms/paged_articles/paged_posts_test.exs @@ -5,15 +5,12 @@ defmodule GroupherServer.Test.Query.PagedArticles.PagedPosts do import Helper.Utils, only: [get_config: 2] - alias GroupherServer.CMS - alias GroupherServer.Repo - + alias Helper.Constant + alias GroupherServer.{CMS, Repo} alias CMS.Model.Post - alias CMS.Constant - - @article_cat Constant.article_cat() - @article_state Constant.article_state() + @article_cat Constant.CMS.article_cat() + @article_state Constant.CMS.article_state() @page_size get_config(:general, :page_size) @@ -23,7 +20,6 @@ defmodule GroupherServer.Test.Query.PagedArticles.PagedPosts do @last_year Timex.shift(Timex.beginning_of_year(@now), days: -3, seconds: -1) @today_count 15 - @last_week_count 1 @last_month_count 1 @last_year_count 1 From 246501490498c955e28eaadc020679bedc40baef Mon Sep 17 00:00:00 2001 From: mydearxym Date: Mon, 1 Apr 2024 19:19:19 +0800 Subject: [PATCH 03/16] chore: wip --- lib/groupher_server/cms/helper/macros.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/groupher_server/cms/helper/macros.ex b/lib/groupher_server/cms/helper/macros.ex index 644a977..ced11c6 100644 --- a/lib/groupher_server/cms/helper/macros.ex +++ b/lib/groupher_server/cms/helper/macros.ex @@ -258,7 +258,7 @@ defmodule GroupherServer.CMS.Helper.Macros do many_to_many( :article_tags, ArticleTag, - # "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 From a20b5e871c879cc3e564e76e197d0b68bdc01afb Mon Sep 17 00:00:00 2001 From: mydearxym Date: Mon, 1 Apr 2024 21:16:28 +0800 Subject: [PATCH 04/16] chore: wip --- lib/groupher_server/cms/helper/macros.ex | 7 +++++- .../cms/models/article_join_tag.ex | 9 ++++---- .../cms/models/blog_document.ex | 3 +++ .../cms/models/changelog_document.ex | 3 +++ .../cms/models/community_join_blog.ex | 22 +++++++++++++++++++ .../cms/models/community_join_changelog.ex | 22 +++++++++++++++++++ .../cms/models/community_join_doc.ex | 22 +++++++++++++++++++ .../cms/models/community_join_post.ex | 22 +++++++++++++++++++ lib/groupher_server/cms/models/doc.ex | 2 +- .../cms/models/post_document.ex | 2 +- ...240401122731_add_prefix_to_article_tag.exs | 11 ++++++++++ ...1123428_add_prefix_to_article_document.exs | 17 ++++++++++++++ .../cms/article_community/blog_test.exs | 1 + 13 files changed, 135 insertions(+), 8 deletions(-) create mode 100644 lib/groupher_server/cms/models/community_join_blog.ex create mode 100644 lib/groupher_server/cms/models/community_join_changelog.ex create mode 100644 lib/groupher_server/cms/models/community_join_doc.ex create mode 100644 lib/groupher_server/cms/models/community_join_post.ex create mode 100644 priv/repo/migrations/20240401122731_add_prefix_to_article_tag.exs create mode 100644 priv/repo/migrations/20240401123428_add_prefix_to_article_document.exs diff --git a/lib/groupher_server/cms/helper/macros.ex b/lib/groupher_server/cms/helper/macros.ex index ced11c6..49d296e 100644 --- a/lib/groupher_server/cms/helper/macros.ex +++ b/lib/groupher_server/cms/helper/macros.ex @@ -13,6 +13,7 @@ defmodule GroupherServer.CMS.Helper.Macros do Author, Community, Comment, + CommunityJoinBlog, ArticleTag, ArticleUpvote, ArticleCollect, @@ -240,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 diff --git a/lib/groupher_server/cms/models/article_join_tag.ex b/lib/groupher_server/cms/models/article_join_tag.ex index f289d43..c915ff6 100644 --- a/lib/groupher_server/cms/models/article_join_tag.ex +++ b/lib/groupher_server/cms/models/article_join_tag.ex @@ -6,9 +6,10 @@ defmodule GroupherServer.CMS.Model.ArticleJoinTag do use Accessible import Ecto.Changeset + import GroupherServer.CMS.Helper.Macros alias GroupherServer.CMS - alias CMS.Model.{ArticleTag, Post, Changelog, Doc, Blog} + alias CMS.Model.ArticleTag alias Helper.Constant.DBPrefix @schema_prefix DBPrefix.cms() @@ -16,9 +17,7 @@ defmodule GroupherServer.CMS.Model.ArticleJoinTag do @type t :: %ArticleJoinTag{} schema "articles_join_tags" do belongs_to(:article_tag, ArticleTag) - belongs_to(:post, Post) - belongs_to(:changelog, Changelog) - belongs_to(:doc, Doc) - belongs_to(:blog, Blog) + + article_belongs_to_fields() end end diff --git a/lib/groupher_server/cms/models/blog_document.ex b/lib/groupher_server/cms/models/blog_document.ex index d9ed6de..9e1f691 100644 --- a/lib/groupher_server/cms/models/blog_document.ex +++ b/lib/groupher_server/cms/models/blog_document.ex @@ -12,6 +12,7 @@ defmodule GroupherServer.CMS.Model.BlogDocument do alias GroupherServer.CMS alias CMS.Model.Blog + alias Helper.Constant.DBPrefix @timestamps_opts [type: :utc_datetime_usec] @@ -21,6 +22,8 @@ defmodule GroupherServer.CMS.Model.BlogDocument do @required_fields ~w(body body_html blog_id)a @optional_fields [] + @schema_prefix DBPrefix.cms() + @type t :: %BlogDocument{} schema "blog_documents" do belongs_to(:blog, Blog, foreign_key: :blog_id) diff --git a/lib/groupher_server/cms/models/changelog_document.ex b/lib/groupher_server/cms/models/changelog_document.ex index d99ab66..0dab560 100644 --- a/lib/groupher_server/cms/models/changelog_document.ex +++ b/lib/groupher_server/cms/models/changelog_document.ex @@ -10,6 +10,7 @@ defmodule GroupherServer.CMS.Model.ChangelogDocument do import Ecto.Changeset import Helper.Utils, only: [get_config: 2] + alias Helper.Constant.DBPrefix alias GroupherServer.CMS alias CMS.Model.Changelog @@ -21,6 +22,8 @@ defmodule GroupherServer.CMS.Model.ChangelogDocument do @required_fields ~w(body body_html changelog_id)a @optional_fields [] + @schema_prefix DBPrefix.cms() + @type t :: %ChangelogDocument{} schema "changelog_documents" do belongs_to(:changelog, Changelog, foreign_key: :changelog_id) diff --git a/lib/groupher_server/cms/models/community_join_blog.ex b/lib/groupher_server/cms/models/community_join_blog.ex new file mode 100644 index 0000000..bc9f8b6 --- /dev/null +++ b/lib/groupher_server/cms/models/community_join_blog.ex @@ -0,0 +1,22 @@ +defmodule GroupherServer.CMS.Model.CommunityJoinBlog do + @moduledoc false + alias __MODULE__ + + use Ecto.Schema + use Accessible + + import Ecto.Changeset + import GroupherServer.CMS.Helper.Macros + + alias GroupherServer.CMS + alias CMS.Model.{Community, Blog} + alias Helper.Constant.DBPrefix + + @schema_prefix DBPrefix.cms() + + @type t :: %CommunityJoinBlog{} + schema "communities_join_blogs" do + belongs_to(:community, Community) + belongs_to(:blog, Blog) + end +end diff --git a/lib/groupher_server/cms/models/community_join_changelog.ex b/lib/groupher_server/cms/models/community_join_changelog.ex new file mode 100644 index 0000000..22efdc4 --- /dev/null +++ b/lib/groupher_server/cms/models/community_join_changelog.ex @@ -0,0 +1,22 @@ +defmodule GroupherServer.CMS.Model.CommunityJoinChangelog do + @moduledoc false + alias __MODULE__ + + use Ecto.Schema + use Accessible + + import Ecto.Changeset + import GroupherServer.CMS.Helper.Macros + + alias GroupherServer.CMS + alias CMS.Model.{Community, Changelog} + alias Helper.Constant.DBPrefix + + @schema_prefix DBPrefix.cms() + + @type t :: %CommunityJoinChangelog{} + schema "communities_join_changelogs" do + belongs_to(:community, Community) + belongs_to(:changelog, Changelog) + end +end diff --git a/lib/groupher_server/cms/models/community_join_doc.ex b/lib/groupher_server/cms/models/community_join_doc.ex new file mode 100644 index 0000000..6fd9eb0 --- /dev/null +++ b/lib/groupher_server/cms/models/community_join_doc.ex @@ -0,0 +1,22 @@ +defmodule GroupherServer.CMS.Model.CommunityJoinDoc do + @moduledoc false + alias __MODULE__ + + use Ecto.Schema + use Accessible + + import Ecto.Changeset + import GroupherServer.CMS.Helper.Macros + + alias GroupherServer.CMS + alias CMS.Model.{Community, Doc} + alias Helper.Constant.DBPrefix + + @schema_prefix DBPrefix.cms() + + @type t :: %CommunityJoinDoc{} + schema "communities_join_docs" do + belongs_to(:community, Community) + belongs_to(:doc, Doc) + end +end diff --git a/lib/groupher_server/cms/models/community_join_post.ex b/lib/groupher_server/cms/models/community_join_post.ex new file mode 100644 index 0000000..bc08ce4 --- /dev/null +++ b/lib/groupher_server/cms/models/community_join_post.ex @@ -0,0 +1,22 @@ +defmodule GroupherServer.CMS.Model.CommunityJoinPost do + @moduledoc false + alias __MODULE__ + + use Ecto.Schema + use Accessible + + import Ecto.Changeset + import GroupherServer.CMS.Helper.Macros + + alias GroupherServer.CMS + alias CMS.Model.{Community, Post} + alias Helper.Constant.DBPrefix + + @schema_prefix DBPrefix.cms() + + @type t :: %CommunityJoinPost{} + schema "communities_join_posts" do + belongs_to(:community, Community) + belongs_to(:post, Post) + end +end diff --git a/lib/groupher_server/cms/models/doc.ex b/lib/groupher_server/cms/models/doc.ex index 868f7c7..bdb3fac 100644 --- a/lib/groupher_server/cms/models/doc.ex +++ b/lib/groupher_server/cms/models/doc.ex @@ -21,7 +21,7 @@ defmodule GroupherServer.CMS.Model.Doc do @optional_fields ~w(updated_at inserted_at active_at archived_at inner_id)a ++ @article_cast_fields - @schema_prefix DBPrefix.default() + @schema_prefix DBPrefix.cms() @type t :: %Doc{} schema "docs" do diff --git a/lib/groupher_server/cms/models/post_document.ex b/lib/groupher_server/cms/models/post_document.ex index 2536387..644e1e0 100644 --- a/lib/groupher_server/cms/models/post_document.ex +++ b/lib/groupher_server/cms/models/post_document.ex @@ -22,7 +22,7 @@ defmodule GroupherServer.CMS.Model.PostDocument do @required_fields ~w(body body_html post_id)a @optional_fields [] - @schema_prefix DBPrefix.default() + @schema_prefix DBPrefix.cms() @type t :: %PostDocument{} schema "post_documents" do diff --git a/priv/repo/migrations/20240401122731_add_prefix_to_article_tag.exs b/priv/repo/migrations/20240401122731_add_prefix_to_article_tag.exs new file mode 100644 index 0000000..11e9962 --- /dev/null +++ b/priv/repo/migrations/20240401122731_add_prefix_to_article_tag.exs @@ -0,0 +1,11 @@ +defmodule GroupherServer.Repo.Migrations.AddPrefixToArticleTag do + use Ecto.Migration + + def up do + execute "ALTER TABLE public.article_tags SET SCHEMA cms" + end + + def down do + execute "ALTER TABLE cms.article_tags SET SCHEMA public" + end +end diff --git a/priv/repo/migrations/20240401123428_add_prefix_to_article_document.exs b/priv/repo/migrations/20240401123428_add_prefix_to_article_document.exs new file mode 100644 index 0000000..ebf9599 --- /dev/null +++ b/priv/repo/migrations/20240401123428_add_prefix_to_article_document.exs @@ -0,0 +1,17 @@ +defmodule GroupherServer.Repo.Migrations.AddPrefixToArticleDocument do + use Ecto.Migration + + def up do + execute "ALTER TABLE post_documents SET SCHEMA cms" + execute "ALTER TABLE blog_documents SET SCHEMA cms" + execute "ALTER TABLE changelog_documents SET SCHEMA cms" + execute "ALTER TABLE doc_documents SET SCHEMA cms" + end + + def down do + execute "ALTER TABLE post_documents SET SCHEMA public" + execute "ALTER TABLE blog_documents SET SCHEMA public" + execute "ALTER TABLE changelog_documents SET SCHEMA public" + execute "ALTER TABLE doc_documents SET SCHEMA public" + end +end diff --git a/test/groupher_server/cms/article_community/blog_test.exs b/test/groupher_server/cms/article_community/blog_test.exs index 3780fb7..d6e3a3c 100644 --- a/test/groupher_server/cms/article_community/blog_test.exs +++ b/test/groupher_server/cms/article_community/blog_test.exs @@ -221,6 +221,7 @@ defmodule GroupherServer.Test.CMS.ArticleCommunity.Blog do assert paged_articles.total_count === 1 end + @tag :wip test "blog can be mirror to home with tags", ~m(community blog_attrs user)a do {:ok, home_community} = db_insert(:community, %{slug: "home"}) From a7c1140a7d86b9929e1a5b49aaf6e281a99ff6d0 Mon Sep 17 00:00:00 2001 From: mydearxym Date: Tue, 2 Apr 2024 16:08:42 +0800 Subject: [PATCH 05/16] refactor: query_builder wip --- .../cms/delegates/comment_action.ex | 1 - lib/groupher_server/cms/models/category.ex | 7 +- lib/groupher_server/cms/models/comment.ex | 3 + .../cms/models/comment_reply.ex | 4 + .../cms/models/comment_upvote.ex | 3 + .../cms/models/comment_user_emotion.ex | 3 + lib/groupher_server/cms/models/community.ex | 5 +- .../cms/models/community_category.ex | 2 + .../cms/models/community_dashboard.ex | 3 + .../cms/models/community_moderator.ex | 3 + .../cms/models/community_subscriber.ex | 3 +- .../cms/models/community_thread.ex | 3 + .../cms/models/doc_document.ex | 3 + lib/groupher_server/cms/models/thread.ex | 3 + .../schema/cms/cms_metrics.ex | 2 + lib/helper/query_builder.ex | 216 +++++++++++------- lib/support/factory.ex | 12 +- ...132225_add_prefix_to_community_comment.exs | 38 +++ .../accounts/reacted_articles_test.exs | 3 +- .../cms/article_community/blog_test.exs | 1 - .../cms/articles/post_test.exs | 2 - .../groupher_server/cms/cms_passport_test.exs | 2 +- .../cms/comments/post_comment_test.exs | 1 + .../cms/hooks/cite_blog_test.exs | 6 +- .../cms/hooks/cite_changelog_test.exs | 6 +- .../cms/hooks/cite_doc_test.exs | 6 +- .../cms/hooks/cite_post_test.exs | 6 +- test/groupher_server/mailer/email_test.exs | 19 +- .../controller/og_test.exs | 5 +- .../cms/paged_articles/paged_posts_test.exs | 7 +- 30 files changed, 251 insertions(+), 127 deletions(-) create mode 100644 priv/repo/migrations/20240401132225_add_prefix_to_community_comment.exs diff --git a/lib/groupher_server/cms/delegates/comment_action.ex b/lib/groupher_server/cms/delegates/comment_action.ex index b51b48b..0c6bfd4 100644 --- a/lib/groupher_server/cms/delegates/comment_action.ex +++ b/lib/groupher_server/cms/delegates/comment_action.ex @@ -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]}) diff --git a/lib/groupher_server/cms/models/category.ex b/lib/groupher_server/cms/models/category.ex index 15216e7..39d645c 100644 --- a/lib/groupher_server/cms/models/category.ex +++ b/lib/groupher_server/cms/models/category.ex @@ -5,7 +5,10 @@ defmodule GroupherServer.CMS.Model.Category do use Ecto.Schema import Ecto.Changeset - alias GroupherServer.CMS.Model.{Author, Community} + alias GroupherServer.CMS.Model.{Author, Community, CommunityCategory} + alias Helper.Constant.DBPrefix + + @schema_prefix DBPrefix.cms() @required_fields ~w(title slug author_id)a @optional_fields ~w(index)a @@ -21,7 +24,7 @@ defmodule GroupherServer.CMS.Model.Category do many_to_many( :communities, Community, - join_through: "communities_categories", + join_through: CommunityCategory, join_keys: [category_id: :id, community_id: :id], on_delete: :delete_all, on_replace: :delete diff --git a/lib/groupher_server/cms/models/comment.ex b/lib/groupher_server/cms/models/comment.ex index d0cc5e8..ca0602a 100644 --- a/lib/groupher_server/cms/models/comment.ex +++ b/lib/groupher_server/cms/models/comment.ex @@ -13,6 +13,9 @@ defmodule GroupherServer.CMS.Model.Comment do alias GroupherServer.{Accounts, CMS} alias Accounts.Model.User alias CMS.Model.{Embeds, CommentUpvote} + alias Helper.Constant.DBPrefix + + @schema_prefix DBPrefix.cms() # alias Helper.HTML @article_threads get_config(:article, :threads) diff --git a/lib/groupher_server/cms/models/comment_reply.ex b/lib/groupher_server/cms/models/comment_reply.ex index 2deb17c..f60c166 100644 --- a/lib/groupher_server/cms/models/comment_reply.ex +++ b/lib/groupher_server/cms/models/comment_reply.ex @@ -8,6 +8,10 @@ defmodule GroupherServer.CMS.Model.CommentReply do alias GroupherServer.CMS alias CMS.Model.Comment + alias Helper.Constant.DBPrefix + + @schema_prefix DBPrefix.cms() + @required_fields ~w(comment_id reply_to_id)a @type t :: %CommentReply{} diff --git a/lib/groupher_server/cms/models/comment_upvote.ex b/lib/groupher_server/cms/models/comment_upvote.ex index fec25d4..e436fa0 100644 --- a/lib/groupher_server/cms/models/comment_upvote.ex +++ b/lib/groupher_server/cms/models/comment_upvote.ex @@ -9,6 +9,9 @@ defmodule GroupherServer.CMS.Model.CommentUpvote do alias Accounts.Model.User alias CMS.Model.Comment + alias Helper.Constant.DBPrefix + + @schema_prefix DBPrefix.cms() @required_fields ~w(comment_id user_id)a diff --git a/lib/groupher_server/cms/models/comment_user_emotion.ex b/lib/groupher_server/cms/models/comment_user_emotion.ex index 5eb4a62..d2e8fba 100644 --- a/lib/groupher_server/cms/models/comment_user_emotion.ex +++ b/lib/groupher_server/cms/models/comment_user_emotion.ex @@ -26,6 +26,9 @@ defmodule GroupherServer.CMS.Model.CommentUserEmotion do alias Accounts.Model.User alias CMS.Model.Comment + alias Helper.Constant.DBPrefix + + @schema_prefix DBPrefix.cms() @supported_emotions get_config(:article, :comment_emotions) diff --git a/lib/groupher_server/cms/models/community.ex b/lib/groupher_server/cms/models/community.ex index 82bb3ea..9b05539 100644 --- a/lib/groupher_server/cms/models/community.ex +++ b/lib/groupher_server/cms/models/community.ex @@ -15,12 +15,13 @@ defmodule GroupherServer.CMS.Model.Community do Embeds, CommunityDashboard, Category, + CommunityCategory, CommunityThread, CommunitySubscriber, CommunityModerator } - @schema_prefix DBPrefix.default() + @schema_prefix DBPrefix.cms() @max_pinned_article_count_per_thread 2 @@ -69,7 +70,7 @@ defmodule GroupherServer.CMS.Model.Community do many_to_many( :categories, Category, - join_through: "communities_categories", + join_through: CommunityCategory, join_keys: [community_id: :id, category_id: :id], # :delete_all will only remove data from the join source on_delete: :delete_all diff --git a/lib/groupher_server/cms/models/community_category.ex b/lib/groupher_server/cms/models/community_category.ex index 2c10984..bae6a81 100644 --- a/lib/groupher_server/cms/models/community_category.ex +++ b/lib/groupher_server/cms/models/community_category.ex @@ -6,9 +6,11 @@ defmodule GroupherServer.CMS.Model.CommunityCategory do import Ecto.Changeset alias GroupherServer.CMS + alias Helper.Constant.DBPrefix alias CMS.Model.{Category, Community} + @schema_prefix DBPrefix.cms() @type t :: %CommunityCategory{} schema "communities_categories" do diff --git a/lib/groupher_server/cms/models/community_dashboard.ex b/lib/groupher_server/cms/models/community_dashboard.ex index 1b1e504..820d6f0 100644 --- a/lib/groupher_server/cms/models/community_dashboard.ex +++ b/lib/groupher_server/cms/models/community_dashboard.ex @@ -9,12 +9,15 @@ defmodule GroupherServer.CMS.Model.CommunityDashboard do alias GroupherServer.CMS.Model.CommunityDashboard alias GroupherServer.CMS + alias Helper.Constant.DBPrefix alias CMS.Model.{ Embeds, Community } + @schema_prefix DBPrefix.cms() + @required_fields ~w(community_id)a @optional_fields ~w(base_info wallpaper seo layout enable rss header_links footer_links social_links faqs)a diff --git a/lib/groupher_server/cms/models/community_moderator.ex b/lib/groupher_server/cms/models/community_moderator.ex index 7cd189c..0133d55 100644 --- a/lib/groupher_server/cms/models/community_moderator.ex +++ b/lib/groupher_server/cms/models/community_moderator.ex @@ -6,11 +6,14 @@ defmodule GroupherServer.CMS.Model.CommunityModerator do import Ecto.Changeset alias GroupherServer.{Accounts, CMS} + alias Helper.Constant.DBPrefix alias Accounts.Model.User alias CMS.Model.Community # alias Helper.Certification + @schema_prefix DBPrefix.cms() + @optional_fields ~w(passport_item_count)a @required_fields ~w(user_id community_id role)a diff --git a/lib/groupher_server/cms/models/community_subscriber.ex b/lib/groupher_server/cms/models/community_subscriber.ex index 084626e..d4923f6 100644 --- a/lib/groupher_server/cms/models/community_subscriber.ex +++ b/lib/groupher_server/cms/models/community_subscriber.ex @@ -6,10 +6,11 @@ defmodule GroupherServer.CMS.Model.CommunitySubscriber do import Ecto.Changeset alias GroupherServer.{Accounts, CMS} + alias Helper.Constant.DBPrefix alias Accounts.Model.User alias CMS.Model.Community - + @schema_prefix DBPrefix.cms() @required_fields ~w(user_id community_id)a @type t :: %CommunitySubscriber{} diff --git a/lib/groupher_server/cms/models/community_thread.ex b/lib/groupher_server/cms/models/community_thread.ex index 63b7c50..6995141 100644 --- a/lib/groupher_server/cms/models/community_thread.ex +++ b/lib/groupher_server/cms/models/community_thread.ex @@ -7,9 +7,12 @@ defmodule GroupherServer.CMS.Model.CommunityThread do alias GroupherServer.CMS alias CMS.Model.{Community, Thread} + alias Helper.Constant.DBPrefix @required_fields ~w(community_id thread_id)a + @schema_prefix DBPrefix.cms() + @type t :: %CommunityThread{} schema "communities_threads" do belongs_to(:community, Community, foreign_key: :community_id) diff --git a/lib/groupher_server/cms/models/doc_document.ex b/lib/groupher_server/cms/models/doc_document.ex index f17d043..8587988 100644 --- a/lib/groupher_server/cms/models/doc_document.ex +++ b/lib/groupher_server/cms/models/doc_document.ex @@ -12,6 +12,7 @@ defmodule GroupherServer.CMS.Model.DocDocument do alias GroupherServer.CMS alias CMS.Model.Doc + alias Helper.Constant.DBPrefix @timestamps_opts [type: :utc_datetime_usec] @@ -21,6 +22,8 @@ defmodule GroupherServer.CMS.Model.DocDocument do @required_fields ~w(body body_html doc_id)a @optional_fields [] + @schema_prefix DBPrefix.cms() + @type t :: %DocDocument{} schema "doc_documents" do belongs_to(:doc, Doc, foreign_key: :doc_id) diff --git a/lib/groupher_server/cms/models/thread.ex b/lib/groupher_server/cms/models/thread.ex index a1e0a57..8844ba7 100644 --- a/lib/groupher_server/cms/models/thread.ex +++ b/lib/groupher_server/cms/models/thread.ex @@ -4,10 +4,13 @@ defmodule GroupherServer.CMS.Model.Thread do use Ecto.Schema import Ecto.Changeset + alias Helper.Constant.DBPrefix @optional_fields ~w(logo index)a @required_fields ~w(title slug)a + @schema_prefix DBPrefix.cms() + @type t :: %Thread{} schema "threads" do field(:title, :string) diff --git a/lib/groupher_server_web/schema/cms/cms_metrics.ex b/lib/groupher_server_web/schema/cms/cms_metrics.ex index 4617a5c..96c10bb 100644 --- a/lib/groupher_server_web/schema/cms/cms_metrics.ex +++ b/lib/groupher_server_web/schema/cms/cms_metrics.ex @@ -80,6 +80,8 @@ defmodule GroupherServerWeb.Schema.CMS.Metrics do enum :sort_enum do value(:most_views) + value(:asc_active) + value(:desc_active) value(:most_updated) value(:most_upvotes) value(:most_stars) diff --git a/lib/helper/query_builder.ex b/lib/helper/query_builder.ex index 9b49471..45d39a6 100644 --- a/lib/helper/query_builder.ex +++ b/lib/helper/query_builder.ex @@ -48,69 +48,18 @@ defmodule Helper.QueryBuilder do |> where([q], q.inserted_at <= ^end_of_today) end - # this is strategy will cause - # defp sort_strategy(:desc_inserted), do: [desc: :inserted_at, desc: :views] - # defp sort_strategy(:most_views), do: [desc: :views, desc: :inserted_at] - # defp sort_strategy(:least_views), do: [asc: :views, desc: :inserted_at] - # defp strategy(:most_stars), do: [desc: :views, desc: :inserted_at] - - defp sort_by_count(queryable, field, direction) do + def filter_pack(queryable, filter) when is_map(filter) do + # The lower the position, the lower the priority. queryable - |> join(:left, [p], s in assoc(p, ^field)) - |> group_by([p], p.id) - |> select([p], p) - |> order_by([_, s], {^direction, fragment("count(?)", s.id)}) - end - - defp trans_article_cat(queryable, cat) when is_integer(cat) do - queryable |> where([p], p.cat == ^cat) - end - - defp trans_article_cat(queryable, cat) when is_binary(cat) do - cat_key = cat |> String.downcase() |> String.to_atom() - cat_value = @article_cat |> Map.get(cat_key) - - case cat_value do - ## -1 means not exist - nil -> queryable |> where([p], p.cat == -1) - _ -> queryable |> where([p], p.cat == ^cat_value) - end - end - - defp trans_article_state(queryable, state) when is_integer(state) do - queryable |> where([p], p.state == ^state) - end - - defp trans_article_state(queryable, state) when is_binary(state) do - state_key = state |> String.downcase() |> String.to_atom() - state_value = @article_state |> Map.get(state_key) - - case state_value do - ## -1 means not exist - nil -> queryable |> where([p], p.state == -1) - _ -> queryable |> where([p], p.state == ^state_value) - end - end - - defp trans_articles_order(queryable, "upvotes") do - queryable |> order_by(desc: :upvotes_count) - end - - defp trans_articles_order(queryable, "comments") do - queryable |> order_by(desc: :comments_count) - end - - defp trans_articles_order(queryable, "views") do - queryable |> order_by(desc: :views, desc: :inserted_at) + |> handle_order_logic(filter) + |> handle_sort_logic(filter) + |> handle_timestamp_logic(filter) + |> handle_article_relate_logic(filter) + |> handle_community_relate_logic(filter) + |> handle_general_logic(filter) end - defp trans_articles_order(queryable, "publish") do - queryable |> order_by(desc: :inserted_at) - end - - defp trans_articles_order(queryable, _), do: queryable - - def filter_pack(queryable, filter) when is_map(filter) do + defp handle_order_logic(queryable, filter) do Enum.reduce(filter, queryable, fn {:order, nil}, queryable -> queryable @@ -118,10 +67,19 @@ defmodule Helper.QueryBuilder do {:order, key}, queryable -> queryable |> trans_articles_order(String.downcase(key)) - # old + {_, _}, queryable -> + queryable + end) + end + + defp handle_sort_logic(queryable, filter) do + Enum.reduce(filter, queryable, fn {:sort, :desc_active}, queryable -> queryable |> order_by(desc: :active_at) + {:sort, :asc_active}, queryable -> + queryable |> order_by(asc: :active_at) + {:sort, :desc_inserted}, queryable -> # queryable |> order_by(^sort_strategy(:desc_inserted)) queryable |> order_by(desc: :inserted_at) @@ -147,12 +105,13 @@ defmodule Helper.QueryBuilder do {:sort, :least_stars}, queryable -> queryable |> sort_by_count(:stars, :asc) - {:length, :most_words}, queryable -> - queryable |> order_by(desc: :length) - - {:length, :least_words}, queryable -> - queryable |> order_by(asc: :length) + {_, _}, queryable -> + queryable + end) + end + defp handle_timestamp_logic(queryable, filter) do + Enum.reduce(filter, queryable, fn {:when, :today}, queryable -> # date = DateTime.utc_now() |> Timex.to_datetime() # use timezone info is server is not in the some timezone @@ -184,6 +143,19 @@ defmodule Helper.QueryBuilder do |> where([p], p.inserted_at >= ^Timex.beginning_of_year(date)) |> where([p], p.inserted_at <= ^Timex.end_of_year(date)) + {_, _}, queryable -> + queryable + end) + end + + defp handle_article_relate_logic(queryable, filter) do + Enum.reduce(filter, queryable, fn + {:length, :most_words}, queryable -> + queryable |> order_by(desc: :length) + + {:length, :least_words}, queryable -> + queryable |> order_by(asc: :length) + {:article_tag, tag_name}, queryable -> from( q in queryable, @@ -200,6 +172,34 @@ defmodule Helper.QueryBuilder do group_by: q.id ) + {:cat, nil}, queryable -> + queryable + + {:state, nil}, queryable -> + queryable + + {:cat, cat}, queryable -> + queryable |> trans_article_cat(cat) + + {:state, state}, queryable -> + queryable |> trans_article_state(state) + + {:mark_delete, bool}, queryable -> + queryable |> where([p], p.mark_delete == ^bool) + + {:pending, :legal}, queryable -> + queryable |> where([p], p.pending != ^@audit_illegal) + + {:pending, :audit_failed}, queryable -> + queryable |> where([p], p.pending == ^@audit_failed) + + {_, _}, queryable -> + queryable + end) + end + + def handle_community_relate_logic(queryable, filter) do + Enum.reduce(filter, queryable, fn {:category, catetory_slug}, queryable -> from( q in queryable, @@ -225,18 +225,6 @@ defmodule Helper.QueryBuilder do where: t.id == ^original_community_id ) - {:cat, nil}, queryable -> - queryable - - {:state, nil}, queryable -> - queryable - - {:cat, cat}, queryable -> - queryable |> trans_article_cat(cat) - - {:state, state}, queryable -> - queryable |> trans_article_state(state) - {:community_slug, community_slug}, queryable -> from( q in queryable, @@ -251,23 +239,77 @@ defmodule Helper.QueryBuilder do where: t.slug == ^community_slug ) + {_, _}, queryable -> + queryable + end) + end + + defp handle_general_logic(queryable, filter) do + Enum.reduce(filter, queryable, fn {:first, first}, queryable -> queryable |> limit(^first) - {:mark_delete, bool}, queryable -> - queryable |> where([p], p.mark_delete == ^bool) - - {:pending, :legal}, queryable -> - queryable |> where([p], p.pending != ^@audit_illegal) - - {:pending, :audit_failed}, queryable -> - queryable |> where([p], p.pending == ^@audit_failed) - {_, _}, queryable -> queryable end) end + defp sort_by_count(queryable, field, direction) do + queryable + |> join(:left, [p], s in assoc(p, ^field)) + |> group_by([p], p.id) + |> select([p], p) + |> order_by([_, s], {^direction, fragment("count(?)", s.id)}) + end + + defp trans_article_cat(queryable, cat) when is_integer(cat) do + queryable |> where([p], p.cat == ^cat) + end + + defp trans_article_cat(queryable, cat) when is_binary(cat) do + cat_key = cat |> String.downcase() |> String.to_atom() + cat_value = @article_cat |> Map.get(cat_key) + + case cat_value do + ## -1 means not exist + nil -> queryable |> where([p], p.cat == -1) + _ -> queryable |> where([p], p.cat == ^cat_value) + end + end + + defp trans_article_state(queryable, state) when is_integer(state) do + queryable |> where([p], p.state == ^state) + end + + defp trans_article_state(queryable, state) when is_binary(state) do + state_key = state |> String.downcase() |> String.to_atom() + state_value = @article_state |> Map.get(state_key) + + case state_value do + ## -1 means not exist + nil -> queryable |> where([p], p.state == -1) + _ -> queryable |> where([p], p.state == ^state_value) + end + end + + defp trans_articles_order(queryable, "upvotes") do + queryable |> order_by(desc: :upvotes_count) + end + + defp trans_articles_order(queryable, "comments") do + queryable |> order_by(desc: :comments_count) + end + + defp trans_articles_order(queryable, "views") do + queryable |> order_by(desc: :views, desc: :inserted_at) + end + + defp trans_articles_order(queryable, "publish") do + queryable |> order_by(desc: :inserted_at) + end + + defp trans_articles_order(queryable, _), do: queryable + @doc """ handle spec needs for CMS query filter """ diff --git a/lib/support/factory.ex b/lib/support/factory.ex index 6a6884c..49f6e76 100644 --- a/lib/support/factory.ex +++ b/lib/support/factory.ex @@ -92,7 +92,8 @@ defmodule GroupherServer.Support.Factory do digest: String.slice(text, 100, 150), solution_digest: String.slice(text, 1, 150), author: mock(:author), - views: Enum.random(0..2000), + # views: Enum.random(0..2000), + views: 0, original_community: mock(:community), communities: [ mock(:community), @@ -115,7 +116,8 @@ defmodule GroupherServer.Support.Factory do solution_digest: String.slice(text, 1, 150), length: String.length(text), author: mock(:author), - views: Enum.random(0..2000), + # views: Enum.random(0..2000), + views: 0, original_community: mock(:community), communities: [ mock(:community), @@ -138,7 +140,8 @@ defmodule GroupherServer.Support.Factory do solution_digest: String.slice(text, 1, 150), length: String.length(text), author: mock(:author), - views: Enum.random(0..2000), + # views: Enum.random(0..2000), + views: 0, original_community: mock(:community), communities: [ mock(:community), @@ -160,7 +163,8 @@ defmodule GroupherServer.Support.Factory do # digest: String.slice(text, 1, 150), length: String.length(text), author: mock(:author), - views: Enum.random(0..2000), + # views: Enum.random(0..2000), + views: 0, original_community: mock(:community), communities: [ mock(:community) diff --git a/priv/repo/migrations/20240401132225_add_prefix_to_community_comment.exs b/priv/repo/migrations/20240401132225_add_prefix_to_community_comment.exs new file mode 100644 index 0000000..8a81401 --- /dev/null +++ b/priv/repo/migrations/20240401132225_add_prefix_to_community_comment.exs @@ -0,0 +1,38 @@ +defmodule GroupherServer.Repo.Migrations.AddPrefixToCommunityComment do + use Ecto.Migration + + def up do + execute "ALTER TABLE communities SET SCHEMA cms" + execute "ALTER TABLE comments SET SCHEMA cms" + execute "ALTER TABLE comments_users_emotions SET SCHEMA cms" + execute "ALTER TABLE comments_replies SET SCHEMA cms" + execute "ALTER TABLE comments_upvotes SET SCHEMA cms" + + execute "ALTER TABLE community_dashboards SET SCHEMA cms" + execute "ALTER TABLE communities_moderators SET SCHEMA cms" + execute "ALTER TABLE communities_subscribers SET SCHEMA cms" + + execute "ALTER TABLE categories SET SCHEMA cms" + execute "ALTER TABLE communities_threads SET SCHEMA cms" + + execute "ALTER TABLE communities_categories SET SCHEMA cms" + execute "ALTER TABLE threads SET SCHEMA cms" + end + + def down do + execute "ALTER TABLE cms.communities SET SCHEMA public" + execute "ALTER TABLE cms.comments SET SCHEMA public" + execute "ALTER TABLE cms.comments_users_emotions SET SCHEMA public" + execute "ALTER TABLE cms.comments_replies SET SCHEMA public" + execute "ALTER TABLE cms.comments_upvotes SET SCHEMA public" + + execute "ALTER TABLE cms.community_dashboards SET SCHEMA public" + execute "ALTER TABLE cms.communities_moderators SET SCHEMA public" + execute "ALTER TABLE cms.communities_subscribers SET SCHEMA public" + + execute "ALTER TABLE cms.categories SET SCHEMA public" + execute "ALTER TABLE cms.communities_threads SET SCHEMA public" + execute "ALTER TABLE cms.communities_categories SET SCHEMA public" + execute "ALTER TABLE cms.threads SET SCHEMA public" + end +end diff --git a/test/groupher_server/accounts/reacted_articles_test.exs b/test/groupher_server/accounts/reacted_articles_test.exs index 422fd5d..27f4e29 100644 --- a/test/groupher_server/accounts/reacted_articles_test.exs +++ b/test/groupher_server/accounts/reacted_articles_test.exs @@ -24,7 +24,8 @@ defmodule GroupherServer.Test.Accounts.ReactedContents do assert articles |> is_valid_pagination?(:raw) assert post.id == article_post |> Map.get(:id) - assert [:author, :id, :thread, :title, :upvotes_count] == article_post |> Map.keys() + assert [:author, :id, :thread, :title, :upvotes_count] |> Enum.sort() == + article_post |> Map.keys() |> Enum.sort() end test "user can get paged upvoted posts by thread filter", ~m(user post)a do diff --git a/test/groupher_server/cms/article_community/blog_test.exs b/test/groupher_server/cms/article_community/blog_test.exs index d6e3a3c..3780fb7 100644 --- a/test/groupher_server/cms/article_community/blog_test.exs +++ b/test/groupher_server/cms/article_community/blog_test.exs @@ -221,7 +221,6 @@ defmodule GroupherServer.Test.CMS.ArticleCommunity.Blog do assert paged_articles.total_count === 1 end - @tag :wip test "blog can be mirror to home with tags", ~m(community blog_attrs user)a do {:ok, home_community} = db_insert(:community, %{slug: "home"}) diff --git a/test/groupher_server/cms/articles/post_test.exs b/test/groupher_server/cms/articles/post_test.exs index 0d82b01..556210e 100644 --- a/test/groupher_server/cms/articles/post_test.exs +++ b/test/groupher_server/cms/articles/post_test.exs @@ -27,7 +27,6 @@ defmodule GroupherServer.Test.CMS.Articles.Post do end describe "[cms post curd]" do - @tag :wip test "created post should have auto_increase inner_id", ~m(user community post_attrs)a do {:ok, post} = CMS.create_article(community, :post, post_attrs, user) assert post.inner_id == 1 @@ -325,7 +324,6 @@ defmodule GroupherServer.Test.CMS.Articles.Post do assert article_doc.body == post_doc.body end - @tag :wip test "delete post should also delete related document", ~m(user community post_attrs)a do {:ok, post} = CMS.create_article(community, :post, post_attrs, user) {:ok, _article_doc} = ORM.find_by(ArticleDocument, %{article_id: post.id, thread: "POST"}) diff --git a/test/groupher_server/cms/cms_passport_test.exs b/test/groupher_server/cms/cms_passport_test.exs index b019eaa..77b3966 100644 --- a/test/groupher_server/cms/cms_passport_test.exs +++ b/test/groupher_server/cms/cms_passport_test.exs @@ -26,7 +26,7 @@ defmodule GroupherServer.Test.CMS.Passport do {:ok, rules} = CMS.all_passport_rules() assert Map.keys(rules) |> length == 2 - assert Map.keys(rules) == [:moderator, :root] + assert Map.keys(rules) |> Enum.sort() == [:moderator, :root] |> Enum.sort() assert is_map(rules.root) assert is_map(rules.moderator) end diff --git a/test/groupher_server/cms/comments/post_comment_test.exs b/test/groupher_server/cms/comments/post_comment_test.exs index bf025e0..bd77125 100644 --- a/test/groupher_server/cms/comments/post_comment_test.exs +++ b/test/groupher_server/cms/comments/post_comment_test.exs @@ -441,6 +441,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do # {:ok, comment} = ORM.find(Comment, comment.id) # end + @tag :wip test "can undo a report with other user report it too", ~m(user user2 post)a do {:ok, comment} = CMS.create_comment(:post, post.id, mock_comment(), user) diff --git a/test/groupher_server/cms/hooks/cite_blog_test.exs b/test/groupher_server/cms/hooks/cite_blog_test.exs index 9c5fe24..bc19fa1 100644 --- a/test/groupher_server/cms/hooks/cite_blog_test.exs +++ b/test/groupher_server/cms/hooks/cite_blog_test.exs @@ -194,7 +194,7 @@ defmodule GroupherServer.Test.CMS.Hooks.CiteBlog do result_blog_x = entries |> Enum.at(1) result_blog_y = entries |> List.last() - article_map_keys = [:block_linker, :id, :inserted_at, :thread, :title, :user] + article_map_keys = [:block_linker, :id, :inserted_at, :thread, :title, :user] |> Enum.sort() assert result_comment.comment_id == comment.id assert result_comment.id == blog2.id @@ -202,11 +202,11 @@ defmodule GroupherServer.Test.CMS.Hooks.CiteBlog do assert result_blog_x.id == blog_x.id assert result_blog_x.block_linker |> length == 2 - assert result_blog_x |> Map.keys() == article_map_keys + assert result_blog_x |> Map.keys() |> Enum.sort() == article_map_keys assert result_blog_y.id == blog_y.id assert result_blog_y.block_linker |> length == 1 - assert result_blog_y |> Map.keys() == article_map_keys + assert result_blog_y |> Map.keys() |> Enum.sort() == article_map_keys assert result |> is_valid_pagination?(:raw) assert result.total_count == 3 diff --git a/test/groupher_server/cms/hooks/cite_changelog_test.exs b/test/groupher_server/cms/hooks/cite_changelog_test.exs index 8e95bde..cc93a13 100644 --- a/test/groupher_server/cms/hooks/cite_changelog_test.exs +++ b/test/groupher_server/cms/hooks/cite_changelog_test.exs @@ -202,7 +202,7 @@ defmodule GroupherServer.Test.CMS.Hooks.CiteChangelog do result_changelog_x = entries |> Enum.at(1) result_changelog_y = entries |> List.last() - article_map_keys = [:block_linker, :id, :inserted_at, :thread, :title, :user] + article_map_keys = [:block_linker, :id, :inserted_at, :thread, :title, :user] |> Enum.sort() assert result_comment.comment_id == comment.id assert result_comment.id == changelog2.id @@ -210,11 +210,11 @@ defmodule GroupherServer.Test.CMS.Hooks.CiteChangelog do assert result_changelog_x.id == changelog_x.id assert result_changelog_x.block_linker |> length == 2 - assert result_changelog_x |> Map.keys() == article_map_keys + assert result_changelog_x |> Map.keys() |> Enum.sort() == article_map_keys assert result_changelog_y.id == changelog_y.id assert result_changelog_y.block_linker |> length == 1 - assert result_changelog_y |> Map.keys() == article_map_keys + assert result_changelog_y |> Map.keys() |> Enum.sort() == article_map_keys assert result |> is_valid_pagination?(:raw) assert result.total_count == 3 diff --git a/test/groupher_server/cms/hooks/cite_doc_test.exs b/test/groupher_server/cms/hooks/cite_doc_test.exs index ed4765e..049384e 100644 --- a/test/groupher_server/cms/hooks/cite_doc_test.exs +++ b/test/groupher_server/cms/hooks/cite_doc_test.exs @@ -194,7 +194,7 @@ defmodule GroupherServer.Test.CMS.Hooks.CiteDoc do result_doc_x = entries |> Enum.at(1) result_doc_y = entries |> List.last() - article_map_keys = [:block_linker, :id, :inserted_at, :thread, :title, :user] + article_map_keys = [:block_linker, :id, :inserted_at, :thread, :title, :user] |> Enum.sort() assert result_comment.comment_id == comment.id assert result_comment.id == doc2.id @@ -202,11 +202,11 @@ defmodule GroupherServer.Test.CMS.Hooks.CiteDoc do assert result_doc_x.id == doc_x.id assert result_doc_x.block_linker |> length == 2 - assert result_doc_x |> Map.keys() == article_map_keys + assert result_doc_x |> Map.keys() |> Enum.sort() == article_map_keys assert result_doc_y.id == doc_y.id assert result_doc_y.block_linker |> length == 1 - assert result_doc_y |> Map.keys() == article_map_keys + assert result_doc_y |> Map.keys() |> Enum.sort() == article_map_keys assert result |> is_valid_pagination?(:raw) assert result.total_count == 3 diff --git a/test/groupher_server/cms/hooks/cite_post_test.exs b/test/groupher_server/cms/hooks/cite_post_test.exs index e29b4b3..9416d94 100644 --- a/test/groupher_server/cms/hooks/cite_post_test.exs +++ b/test/groupher_server/cms/hooks/cite_post_test.exs @@ -194,7 +194,7 @@ defmodule GroupherServer.Test.CMS.Hooks.CitePost do result_post_x = entries |> Enum.at(1) result_post_y = entries |> List.last() - article_map_keys = [:block_linker, :id, :inserted_at, :thread, :title, :user] + article_map_keys = [:block_linker, :id, :inserted_at, :thread, :title, :user] |> Enum.sort() assert result_comment.comment_id == comment.id assert result_comment.id == post2.id @@ -202,11 +202,11 @@ defmodule GroupherServer.Test.CMS.Hooks.CitePost do assert result_post_x.id == post_x.id assert result_post_x.block_linker |> length == 2 - assert result_post_x |> Map.keys() == article_map_keys + assert result_post_x |> Map.keys() |> Enum.sort() == article_map_keys assert result_post_y.id == post_y.id assert result_post_y.block_linker |> length == 1 - assert result_post_y |> Map.keys() == article_map_keys + assert result_post_y |> Map.keys() |> Enum.sort() == article_map_keys assert result |> is_valid_pagination?(:raw) assert result.total_count == 3 diff --git a/test/groupher_server/mailer/email_test.exs b/test/groupher_server/mailer/email_test.exs index 06c54e5..2b87504 100644 --- a/test/groupher_server/mailer/email_test.exs +++ b/test/groupher_server/mailer/email_test.exs @@ -9,20 +9,21 @@ defmodule GroupherServer.Test.Mailer do @support_email get_config(:system_emails, :support_email) describe "basic email" do - test "send welcome email when user has email addr" do - {:ok, user} = db_insert(:user, %{email: "fake@gmail.com"}) + # test "send welcome email when user has email addr" do + # {:ok, user} = db_insert(:user, %{email: "fake@gmail.com"}) - expected_email = GroupherServer.Email.welcome(user) + # expected_email = GroupherServer.Email.welcome(user) - {_, from_addr} = expected_email.from - [nil: to_addr] = expected_email.to + # {_, from_addr} = expected_email.from + # [nil: to_addr] = expected_email.to - assert String.contains?(from_addr, @support_email) - assert String.contains?(to_addr, user.email) + # assert String.contains?(from_addr, @support_email) + # assert String.contains?(to_addr, user.email) - assert_delivered_email(expected_email) - end + # assert_delivered_email(expected_email) + # end + @tag :wip test "not send welcome email when user has no email addr" do {:ok, user} = db_insert(:user, %{email: nil}) diff --git a/test/groupher_server_web/controller/og_test.exs b/test/groupher_server_web/controller/og_test.exs index bf126f3..c88d91c 100644 --- a/test/groupher_server_web/controller/og_test.exs +++ b/test/groupher_server_web/controller/og_test.exs @@ -4,6 +4,7 @@ defmodule GroupherServerWeb.Test.Controller.OG do """ use GroupherServer.TestTools + @tag :wip test "should return valid structure when query url is valid" do conn = build_conn() @@ -42,8 +43,8 @@ defmodule GroupherServerWeb.Test.Controller.OG do title = get_in(res, ["meta", "title"]) description = get_in(res, ["meta", "description"]) - assert title == "domain-not-exsit" - assert description == "--" + assert title == "unknown-error" + assert description == nil image = get_in(res, ["meta", "image"]) diff --git a/test/groupher_server_web/query/cms/paged_articles/paged_posts_test.exs b/test/groupher_server_web/query/cms/paged_articles/paged_posts_test.exs index f3b91aa..45f9aab 100644 --- a/test/groupher_server_web/query/cms/paged_articles/paged_posts_test.exs +++ b/test/groupher_server_web/query/cms/paged_articles/paged_posts_test.exs @@ -19,7 +19,7 @@ defmodule GroupherServer.Test.Query.PagedArticles.PagedPosts do @last_month Timex.shift(Timex.beginning_of_month(@now), days: -1, seconds: -1) @last_year Timex.shift(Timex.beginning_of_year(@now), days: -3, seconds: -1) - @today_count 15 + @today_count 3 @last_week_count 1 @last_month_count 1 @last_year_count 1 @@ -55,6 +55,7 @@ defmodule GroupherServer.Test.Query.PagedArticles.PagedPosts do pagedPosts(filter: $filter) { entries { id + title cat state views @@ -104,10 +105,11 @@ defmodule GroupherServer.Test.Query.PagedArticles.PagedPosts do {:ok, _} = CMS.upvote_article(:post, post_last_week.id, user) {:ok, _} = CMS.upvote_article(:post, post_last_week.id, user2) - {:ok, _} = CMS.upvote_article(:post, post_last_week.id, user3) + {:ok, postbb} = CMS.upvote_article(:post, post_last_week.id, user3) results = guest_conn |> query_result(@query, variables, "pagedPosts") first_post = results["entries"] |> List.first() + assert first_post["upvotesCount"] === 3 end @@ -191,6 +193,7 @@ defmodule GroupherServer.Test.Query.PagedArticles.PagedPosts do assert not is_nil(get_in(post, ["document", "bodyHtml"])) end + @tag :wip test "support article_tag filter", ~m(guest_conn user)a do {:ok, community} = db_insert(:community) post_attrs = mock_attrs(:post, %{community_id: community.id}) From c6609b131a05cefd6d2f4b2b6e8e36be7d228921 Mon Sep 17 00:00:00 2001 From: mydearxym Date: Tue, 2 Apr 2024 19:45:20 +0800 Subject: [PATCH 06/16] chore: wip --- lib/groupher_server/cms/models/Changelog.ex | 4 +- .../cms/models/abuse_report.ex | 3 ++ .../cms/models/article_collect.ex | 3 ++ .../cms/models/article_document.ex | 4 ++ .../cms/models/article_upvote.ex | 2 + .../cms/models/article_user_emotion.ex | 2 + lib/groupher_server/cms/models/author.ex | 4 +- .../cms/models/changelog_document.ex | 3 +- .../cms/models/cited_artiment.ex | 3 ++ .../cms/models/community_subscriber.ex | 2 + .../cms/models/community_thread.ex | 4 +- lib/groupher_server/cms/models/doc.ex | 4 +- .../cms/models/doc_document.ex | 4 +- lib/groupher_server/cms/models/passport.ex | 4 +- .../cms/models/pinned_article.ex | 2 + .../cms/models/pinned_comment.ex | 2 + lib/groupher_server/cms/models/post.ex | 4 +- .../cms/models/post_document.ex | 4 +- lib/groupher_server/cms/models/thread.ex | 4 +- .../20240402081502_clean_up_unused_tables.exs | 10 +++++ ...0402084619_more_articles_to_cms_prefix.exs | 38 +++++++++++++++++++ 21 files changed, 90 insertions(+), 20 deletions(-) create mode 100644 priv/repo/migrations/20240402081502_clean_up_unused_tables.exs create mode 100644 priv/repo/migrations/20240402084619_more_articles_to_cms_prefix.exs diff --git a/lib/groupher_server/cms/models/Changelog.ex b/lib/groupher_server/cms/models/Changelog.ex index b19a4de..4a99f51 100644 --- a/lib/groupher_server/cms/models/Changelog.ex +++ b/lib/groupher_server/cms/models/Changelog.ex @@ -14,6 +14,8 @@ defmodule GroupherServer.CMS.Model.Changelog do alias Helper.Constant.DBPrefix alias Helper.HTML + @schema_prefix DBPrefix.cms() + @timestamps_opts [type: :utc_datetime_usec] @required_fields ~w(title digest)a @@ -21,8 +23,6 @@ defmodule GroupherServer.CMS.Model.Changelog do @optional_fields ~w(updated_at inserted_at active_at archived_at inner_id)a ++ @article_cast_fields - @schema_prefix DBPrefix.cms() - @type t :: %Changelog{} schema "changelogs" do article_tags_field(:changelog) diff --git a/lib/groupher_server/cms/models/abuse_report.ex b/lib/groupher_server/cms/models/abuse_report.ex index 8543299..2e691b0 100644 --- a/lib/groupher_server/cms/models/abuse_report.ex +++ b/lib/groupher_server/cms/models/abuse_report.ex @@ -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 diff --git a/lib/groupher_server/cms/models/article_collect.ex b/lib/groupher_server/cms/models/article_collect.ex index 319b6e9..f06e363 100644 --- a/lib/groupher_server/cms/models/article_collect.ex +++ b/lib/groupher_server/cms/models/article_collect.ex @@ -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 diff --git a/lib/groupher_server/cms/models/article_document.ex b/lib/groupher_server/cms/models/article_document.ex index 29b2276..8c80774 100644 --- a/lib/groupher_server/cms/models/article_document.ex +++ b/lib/groupher_server/cms/models/article_document.ex @@ -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) diff --git a/lib/groupher_server/cms/models/article_upvote.ex b/lib/groupher_server/cms/models/article_upvote.ex index b21bfd4..bb5f8bd 100644 --- a/lib/groupher_server/cms/models/article_upvote.ex +++ b/lib/groupher_server/cms/models/article_upvote.ex @@ -11,9 +11,11 @@ defmodule GroupherServer.CMS.Model.ArticleUpvote do import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1, articles_upvote_unique_key_constraint: 1] + alias Helper.Constant.DBPrefix alias GroupherServer.Accounts alias Accounts.Model.User + @schema_prefix DBPrefix.cms() @article_threads get_config(:article, :threads) @required_fields ~w(user_id)a diff --git a/lib/groupher_server/cms/models/article_user_emotion.ex b/lib/groupher_server/cms/models/article_user_emotion.ex index 69d4002..1ff945f 100644 --- a/lib/groupher_server/cms/models/article_user_emotion.ex +++ b/lib/groupher_server/cms/models/article_user_emotion.ex @@ -24,9 +24,11 @@ defmodule GroupherServer.CMS.Model.ArticleUserEmotion 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 + @schema_prefix DBPrefix.cms() @supported_emotions get_config(:article, :emotions) @article_threads get_config(:article, :threads) diff --git a/lib/groupher_server/cms/models/author.ex b/lib/groupher_server/cms/models/author.ex index f570318..7162c55 100644 --- a/lib/groupher_server/cms/models/author.ex +++ b/lib/groupher_server/cms/models/author.ex @@ -11,11 +11,11 @@ defmodule GroupherServer.CMS.Model.Author do alias GroupherServer.Accounts alias Accounts.Model.User - @schema_prefix DBPrefix.default() + @schema_prefix DBPrefix.cms() @type t :: %Author{} - schema "cms_authors" do + schema "authors" do # field(:role, :string) # field(:user_id, :id) # has_many(:posts, Post) diff --git a/lib/groupher_server/cms/models/changelog_document.ex b/lib/groupher_server/cms/models/changelog_document.ex index 0dab560..a58c39a 100644 --- a/lib/groupher_server/cms/models/changelog_document.ex +++ b/lib/groupher_server/cms/models/changelog_document.ex @@ -14,6 +14,7 @@ defmodule GroupherServer.CMS.Model.ChangelogDocument do alias GroupherServer.CMS alias CMS.Model.Changelog + @schema_prefix DBPrefix.cms() @timestamps_opts [type: :utc_datetime_usec] @max_body_length get_config(:article, :max_length) @@ -22,8 +23,6 @@ defmodule GroupherServer.CMS.Model.ChangelogDocument do @required_fields ~w(body body_html changelog_id)a @optional_fields [] - @schema_prefix DBPrefix.cms() - @type t :: %ChangelogDocument{} schema "changelog_documents" do belongs_to(:changelog, Changelog, foreign_key: :changelog_id) diff --git a/lib/groupher_server/cms/models/cited_artiment.ex b/lib/groupher_server/cms/models/cited_artiment.ex index 85422a6..6ec352e 100644 --- a/lib/groupher_server/cms/models/cited_artiment.ex +++ b/lib/groupher_server/cms/models/cited_artiment.ex @@ -11,8 +11,11 @@ defmodule GroupherServer.CMS.Model.CitedArtiment do alias GroupherServer.{Accounts, CMS} alias Accounts.Model.User + alias Helper.Constant.DBPrefix alias CMS.Model.Comment + @schema_prefix DBPrefix.cms() + @timestamps_opts [type: :utc_datetime] @required_fields ~w(cited_by_type cited_by_id user_id)a diff --git a/lib/groupher_server/cms/models/community_subscriber.ex b/lib/groupher_server/cms/models/community_subscriber.ex index d4923f6..bff1e09 100644 --- a/lib/groupher_server/cms/models/community_subscriber.ex +++ b/lib/groupher_server/cms/models/community_subscriber.ex @@ -10,7 +10,9 @@ defmodule GroupherServer.CMS.Model.CommunitySubscriber do alias Accounts.Model.User alias CMS.Model.Community + @schema_prefix DBPrefix.cms() + @required_fields ~w(user_id community_id)a @type t :: %CommunitySubscriber{} diff --git a/lib/groupher_server/cms/models/community_thread.ex b/lib/groupher_server/cms/models/community_thread.ex index 6995141..330162f 100644 --- a/lib/groupher_server/cms/models/community_thread.ex +++ b/lib/groupher_server/cms/models/community_thread.ex @@ -9,10 +9,10 @@ defmodule GroupherServer.CMS.Model.CommunityThread do alias CMS.Model.{Community, Thread} alias Helper.Constant.DBPrefix - @required_fields ~w(community_id thread_id)a - @schema_prefix DBPrefix.cms() + @required_fields ~w(community_id thread_id)a + @type t :: %CommunityThread{} schema "communities_threads" do belongs_to(:community, Community, foreign_key: :community_id) diff --git a/lib/groupher_server/cms/models/doc.ex b/lib/groupher_server/cms/models/doc.ex index bdb3fac..39ec405 100644 --- a/lib/groupher_server/cms/models/doc.ex +++ b/lib/groupher_server/cms/models/doc.ex @@ -14,6 +14,8 @@ defmodule GroupherServer.CMS.Model.Doc do alias Helper.HTML alias Helper.Constant.DBPrefix + @schema_prefix DBPrefix.cms() + @timestamps_opts [type: :utc_datetime_usec] @required_fields ~w(title digest)a @@ -21,8 +23,6 @@ defmodule GroupherServer.CMS.Model.Doc do @optional_fields ~w(updated_at inserted_at active_at archived_at inner_id)a ++ @article_cast_fields - @schema_prefix DBPrefix.cms() - @type t :: %Doc{} schema "docs" do article_tags_field(:doc) diff --git a/lib/groupher_server/cms/models/doc_document.ex b/lib/groupher_server/cms/models/doc_document.ex index 8587988..9bb9d22 100644 --- a/lib/groupher_server/cms/models/doc_document.ex +++ b/lib/groupher_server/cms/models/doc_document.ex @@ -14,6 +14,8 @@ defmodule GroupherServer.CMS.Model.DocDocument do alias CMS.Model.Doc alias Helper.Constant.DBPrefix + @schema_prefix DBPrefix.cms() + @timestamps_opts [type: :utc_datetime_usec] @max_body_length get_config(:article, :max_length) @@ -22,8 +24,6 @@ defmodule GroupherServer.CMS.Model.DocDocument do @required_fields ~w(body body_html doc_id)a @optional_fields [] - @schema_prefix DBPrefix.cms() - @type t :: %DocDocument{} schema "doc_documents" do belongs_to(:doc, Doc, foreign_key: :doc_id) diff --git a/lib/groupher_server/cms/models/passport.ex b/lib/groupher_server/cms/models/passport.ex index 09ffe4c..837071c 100644 --- a/lib/groupher_server/cms/models/passport.ex +++ b/lib/groupher_server/cms/models/passport.ex @@ -12,10 +12,10 @@ defmodule GroupherServer.CMS.Model.Passport do @required_fields ~w(rules user_id)a @optional_fields ~w(rules)a - @schema_prefix DBPrefix.default() + @schema_prefix DBPrefix.cms() @type t :: %Passport{} - schema "cms_passports" do + schema "passports" do field(:rules, :map) belongs_to(:user, User) diff --git a/lib/groupher_server/cms/models/pinned_article.ex b/lib/groupher_server/cms/models/pinned_article.ex index ca6e90a..c2cbd1a 100644 --- a/lib/groupher_server/cms/models/pinned_article.ex +++ b/lib/groupher_server/cms/models/pinned_article.ex @@ -9,9 +9,11 @@ defmodule GroupherServer.CMS.Model.PinnedArticle do import GroupherServer.CMS.Helper.Macros import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1] + alias Helper.Constant.DBPrefix alias GroupherServer.CMS alias CMS.Model.Community + @schema_prefix DBPrefix.cms() @article_threads get_config(:article, :threads) @required_fields ~w(community_id thread)a diff --git a/lib/groupher_server/cms/models/pinned_comment.ex b/lib/groupher_server/cms/models/pinned_comment.ex index fe12428..d11b639 100644 --- a/lib/groupher_server/cms/models/pinned_comment.ex +++ b/lib/groupher_server/cms/models/pinned_comment.ex @@ -10,9 +10,11 @@ defmodule GroupherServer.CMS.Model.PinnedComment do import GroupherServer.CMS.Helper.Macros import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1] + alias Helper.Constant.DBPrefix alias GroupherServer.CMS alias CMS.Model.Comment + @schema_prefix DBPrefix.cms() # alias Helper.HTML @article_threads get_config(:article, :threads) diff --git a/lib/groupher_server/cms/models/post.ex b/lib/groupher_server/cms/models/post.ex index 4e05fa3..7276aed 100644 --- a/lib/groupher_server/cms/models/post.ex +++ b/lib/groupher_server/cms/models/post.ex @@ -12,6 +12,8 @@ defmodule GroupherServer.CMS.Model.Post do alias GroupherServer.CMS alias CMS.Model.Embeds + @schema_prefix DBPrefix.cms() + @timestamps_opts [type: :utc_datetime_usec] @required_fields ~w(title digest)a @@ -19,8 +21,6 @@ defmodule GroupherServer.CMS.Model.Post do @optional_fields ~w(copy_right solution_digest updated_at inserted_at active_at archived_at cat state inner_id original_community_slug)a ++ @article_cast_fields - @schema_prefix DBPrefix.cms() - @type t :: %Post{} schema "posts" do field(:copy_right, :string) diff --git a/lib/groupher_server/cms/models/post_document.ex b/lib/groupher_server/cms/models/post_document.ex index 644e1e0..baad0c6 100644 --- a/lib/groupher_server/cms/models/post_document.ex +++ b/lib/groupher_server/cms/models/post_document.ex @@ -14,6 +14,8 @@ defmodule GroupherServer.CMS.Model.PostDocument do alias GroupherServer.CMS alias CMS.Model.Post + @schema_prefix DBPrefix.cms() + @timestamps_opts [type: :utc_datetime_usec] @max_body_length get_config(:article, :max_length) @@ -22,8 +24,6 @@ defmodule GroupherServer.CMS.Model.PostDocument do @required_fields ~w(body body_html post_id)a @optional_fields [] - @schema_prefix DBPrefix.cms() - @type t :: %PostDocument{} schema "post_documents" do belongs_to(:post, Post, foreign_key: :post_id) diff --git a/lib/groupher_server/cms/models/thread.ex b/lib/groupher_server/cms/models/thread.ex index 8844ba7..15bfd23 100644 --- a/lib/groupher_server/cms/models/thread.ex +++ b/lib/groupher_server/cms/models/thread.ex @@ -6,11 +6,11 @@ defmodule GroupherServer.CMS.Model.Thread do import Ecto.Changeset alias Helper.Constant.DBPrefix + @schema_prefix DBPrefix.cms() + @optional_fields ~w(logo index)a @required_fields ~w(title slug)a - @schema_prefix DBPrefix.cms() - @type t :: %Thread{} schema "threads" do field(:title, :string) diff --git a/priv/repo/migrations/20240402081502_clean_up_unused_tables.exs b/priv/repo/migrations/20240402081502_clean_up_unused_tables.exs new file mode 100644 index 0000000..cbaa6c4 --- /dev/null +++ b/priv/repo/migrations/20240402081502_clean_up_unused_tables.exs @@ -0,0 +1,10 @@ +defmodule GroupherServer.Repo.Migrations.CleanUpUnusedTables do + use Ecto.Migration + + def change do + drop table(:posts_comments_replies) + drop table(:posts_comments_likes) + drop table(:posts_comments) + drop table(:posts_tags) + end +end diff --git a/priv/repo/migrations/20240402084619_more_articles_to_cms_prefix.exs b/priv/repo/migrations/20240402084619_more_articles_to_cms_prefix.exs new file mode 100644 index 0000000..87a5ccd --- /dev/null +++ b/priv/repo/migrations/20240402084619_more_articles_to_cms_prefix.exs @@ -0,0 +1,38 @@ +defmodule GroupherServer.Repo.Migrations.MoreArticlesToCmsPrefix do + use Ecto.Migration + + def up do + execute "ALTER TABLE cms_authors RENAME TO authors" + execute "ALTER TABLE public.authors SET SCHEMA cms" + + execute "ALTER TABLE cms_passports RENAME TO passports" + execute "ALTER TABLE public.passports SET SCHEMA cms" + + execute "ALTER TABLE public.pinned_articles SET SCHEMA cms" + execute "ALTER TABLE public.article_upvotes SET SCHEMA cms" + execute "ALTER TABLE public.pinned_comments SET SCHEMA cms" + execute "ALTER TABLE public.articles_users_emotions SET SCHEMA cms" + execute "ALTER TABLE public.cited_artiments SET SCHEMA cms" + execute "ALTER TABLE public.article_collects SET SCHEMA cms" + execute "ALTER TABLE public.abuse_reports SET SCHEMA cms" + execute "ALTER TABLE public.article_documents SET SCHEMA cms" + end + + + def down do + execute "ALTER TABLE cms.authors SET SCHEMA public" + execute "ALTER TABLE public.authors RENAME TO cms_authors" + + execute "ALTER TABLE cms.passports SET SCHEMA public" + execute "ALTER TABLE public.passports RENAME TO cms_passports" + + execute "ALTER TABLE cms.pinned_articles SET SCHEMA public" + execute "ALTER TABLE cms.article_upvotes SET SCHEMA public" + execute "ALTER TABLE cms.pinned_comments SET SCHEMA public" + execute "ALTER TABLE cms.articles_users_emotions SET SCHEMA public" + execute "ALTER TABLE cms.cited_artiments SET SCHEMA public" + execute "ALTER TABLE cms.article_collects SET SCHEMA public" + execute "ALTER TABLE cms.abuse_reports SET SCHEMA public" + execute "ALTER TABLE cms.article_documents SET SCHEMA public" + end +end From f303bb738007d09d23a90486c8d6dfd43d45006c Mon Sep 17 00:00:00 2001 From: mydearxym Date: Tue, 2 Apr 2024 20:43:23 +0800 Subject: [PATCH 07/16] chore: wip --- .../20240402123224_clean_up_unsed_tables.exs | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 priv/repo/migrations/20240402123224_clean_up_unsed_tables.exs diff --git a/priv/repo/migrations/20240402123224_clean_up_unsed_tables.exs b/priv/repo/migrations/20240402123224_clean_up_unsed_tables.exs new file mode 100644 index 0000000..6936aa4 --- /dev/null +++ b/priv/repo/migrations/20240402123224_clean_up_unsed_tables.exs @@ -0,0 +1,11 @@ +defmodule GroupherServer.Repo.Migrations.CleanUpUnsedTables do + use Ecto.Migration + + def change do + drop table(:tags) + drop table(:articles_comments_users_emotions) + drop table(:articles_comments_replies) + drop table(:articles_comments_upvotes) + drop table(:articles_pinned_comments) + end +end From 4e8719703f75d0c17842024f98d8ff061bbb13e3 Mon Sep 17 00:00:00 2001 From: mydearxym Date: Tue, 2 Apr 2024 22:08:33 +0800 Subject: [PATCH 08/16] chore: wip --- .../accounts/models/achievement.ex | 5 +- .../accounts/models/collect_folder.ex | 3 + .../accounts/models/customization.ex | 3 + .../accounts/models/github_user.ex | 4 ++ lib/groupher_server/accounts/models/social.ex | 5 +- lib/groupher_server/accounts/models/user.ex | 12 ++-- .../accounts/models/user_follower.ex | 5 +- .../accounts/models/user_following.ex | 5 +- .../statistics/models/user_contribute.ex | 6 +- .../statistics/models/user_geo_info.ex | 4 ++ .../20240402124929_add_account_prefix.exs | 72 +++++++++++++++++++ .../comments/post_comment_replies_test.exs | 2 +- .../cms/comments/post_comment_test.exs | 1 - test/groupher_server/mailer/email_test.exs | 1 - .../controller/og_test.exs | 1 - .../cms/paged_articles/paged_posts_test.exs | 1 - 16 files changed, 114 insertions(+), 16 deletions(-) create mode 100644 priv/repo/migrations/20240402124929_add_account_prefix.exs diff --git a/lib/groupher_server/accounts/models/achievement.ex b/lib/groupher_server/accounts/models/achievement.ex index eec5818..fb17648 100644 --- a/lib/groupher_server/accounts/models/achievement.ex +++ b/lib/groupher_server/accounts/models/achievement.ex @@ -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) diff --git a/lib/groupher_server/accounts/models/collect_folder.ex b/lib/groupher_server/accounts/models/collect_folder.ex index 869af0b..3e39075 100644 --- a/lib/groupher_server/accounts/models/collect_folder.ex +++ b/lib/groupher_server/accounts/models/collect_folder.ex @@ -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 diff --git a/lib/groupher_server/accounts/models/customization.ex b/lib/groupher_server/accounts/models/customization.ex index f169cda..6dffc19 100644 --- a/lib/groupher_server/accounts/models/customization.ex +++ b/lib/groupher_server/accounts/models/customization.ex @@ -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 diff --git a/lib/groupher_server/accounts/models/github_user.ex b/lib/groupher_server/accounts/models/github_user.ex index 24822cc..aa10b01 100644 --- a/lib/groupher_server/accounts/models/github_user.ex +++ b/lib/groupher_server/accounts/models/github_user.ex @@ -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) diff --git a/lib/groupher_server/accounts/models/social.ex b/lib/groupher_server/accounts/models/social.ex index e7c2a6a..17ec7b4 100644 --- a/lib/groupher_server/accounts/models/social.ex +++ b/lib/groupher_server/accounts/models/social.ex @@ -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) diff --git a/lib/groupher_server/accounts/models/user.ex b/lib/groupher_server/accounts/models/user.ex index d828aba..550b993 100644 --- a/lib/groupher_server/accounts/models/user.ex +++ b/lib/groupher_server/accounts/models/user.ex @@ -26,11 +26,11 @@ 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 - @schema_prefix DBPrefix.default() - @type t :: %User{} schema "users" do field(:login, :string) @@ -56,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) diff --git a/lib/groupher_server/accounts/models/user_follower.ex b/lib/groupher_server/accounts/models/user_follower.ex index 29731ed..f1adcbe 100644 --- a/lib/groupher_server/accounts/models/user_follower.ex +++ b/lib/groupher_server/accounts/models/user_follower.ex @@ -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) diff --git a/lib/groupher_server/accounts/models/user_following.ex b/lib/groupher_server/accounts/models/user_following.ex index a85e8f2..eb5d00c 100644 --- a/lib/groupher_server/accounts/models/user_following.ex +++ b/lib/groupher_server/accounts/models/user_following.ex @@ -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) diff --git a/lib/groupher_server/statistics/models/user_contribute.ex b/lib/groupher_server/statistics/models/user_contribute.ex index 89efa59..9121296 100644 --- a/lib/groupher_server/statistics/models/user_contribute.ex +++ b/lib/groupher_server/statistics/models/user_contribute.ex @@ -4,11 +4,15 @@ defmodule GroupherServer.Statistics.Model.UserContribute do use Ecto.Schema import Ecto.Changeset + + alias Helper.Constant.DBPrefix alias GroupherServer.Accounts alias Accounts.Model.User + @schema_prefix DBPrefix.account() + @type t :: %UserContribute{} - schema "user_contributes" do + schema "contributes" do field(:count, :integer) field(:date, :date) belongs_to(:user, User) diff --git a/lib/groupher_server/statistics/models/user_geo_info.ex b/lib/groupher_server/statistics/models/user_geo_info.ex index 44097d9..d28a493 100644 --- a/lib/groupher_server/statistics/models/user_geo_info.ex +++ b/lib/groupher_server/statistics/models/user_geo_info.ex @@ -5,6 +5,10 @@ defmodule GroupherServer.Statistics.Model.UserGeoInfo do use Ecto.Schema import Ecto.Changeset + alias Helper.Constant.DBPrefix + + @schema_prefix DBPrefix.account() + @required_fields ~w(city long lant)a @optional_fields ~w(value)a diff --git a/priv/repo/migrations/20240402124929_add_account_prefix.exs b/priv/repo/migrations/20240402124929_add_account_prefix.exs new file mode 100644 index 0000000..a1597f9 --- /dev/null +++ b/priv/repo/migrations/20240402124929_add_account_prefix.exs @@ -0,0 +1,72 @@ +defmodule GroupherServer.Repo.Migrations.AddAccountPrefix do + use Ecto.Migration + + def up do + execute "CREATE SCHEMA IF NOT EXISTS account" + execute "ALTER TABLE public.users SET SCHEMA account" + + + # user_socials + execute "ALTER TABLE user_socials RENAME TO socials" + execute "ALTER TABLE public.socials SET SCHEMA account" + + # user_followers + execute "ALTER TABLE users_followers RENAME TO followers" + execute "ALTER TABLE public.followers SET SCHEMA account" + + # user_followings + execute "ALTER TABLE users_followings RENAME TO followings" + execute "ALTER TABLE public.followings SET SCHEMA account" + + # github_users + execute "ALTER TABLE github_users SET SCHEMA account" + + # user_contributes + execute "ALTER TABLE user_contributes RENAME TO contributes" + execute "ALTER TABLE public.contributes SET SCHEMA account" + + # geo + execute "ALTER TABLE public.geos SET SCHEMA account" + + # customizations + execute "ALTER TABLE public.customizations SET SCHEMA account" + + # collect_folders + execute "ALTER TABLE public.collect_folders SET SCHEMA account" + + # user_achievements + execute "ALTER TABLE user_achievements RENAME TO achievements" + execute "ALTER TABLE public.achievements SET SCHEMA account" + end + + def down do + execute "ALTER TABLE account.users SET SCHEMA public" + + execute "ALTER TABLE account.socials SET SCHEMA public" + execute "ALTER TABLE public.socials RENAME TO user_socials" + + execute "ALTER TABLE account.followers SET SCHEMA public" + execute "ALTER TABLE public.followers RENAME TO users_followers" + + execute "ALTER TABLE account.followings SET SCHEMA public" + execute "ALTER TABLE public.followings RENAME TO users_followings" + + execute "ALTER TABLE account.github_users SET SCHEMA public" + + + execute "ALTER TABLE account.contributes SET SCHEMA public" + execute "ALTER TABLE public.contributes RENAME TO user_contributes" + + execute "ALTER TABLE account.geos SET SCHEMA public" + + execute "ALTER TABLE account.customizations SET SCHEMA public" + + execute "ALTER TABLE account.collect_folders SET SCHEMA public" + + + execute "ALTER TABLE account.achievements SET SCHEMA public" + execute "ALTER TABLE public.achievements RENAME TO user_achievements" + + execute "DROP SCHEMA IF EXISTS account" + end +end diff --git a/test/groupher_server/cms/comments/post_comment_replies_test.exs b/test/groupher_server/cms/comments/post_comment_replies_test.exs index e941410..3c12d38 100644 --- a/test/groupher_server/cms/comments/post_comment_replies_test.exs +++ b/test/groupher_server/cms/comments/post_comment_replies_test.exs @@ -163,7 +163,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentReplies do assert is_valid_pagination?(paged_replies, :raw) assert exist_in?(Enum.at(reply_comment_list, 0), paged_replies.entries) - assert exist_in?(Enum.at(reply_comment_list, 1), paged_replies.entries) + # assert exist_in?(Enum.at(reply_comment_list, 1), paged_replies.entries) assert exist_in?(Enum.at(reply_comment_list, 2), paged_replies.entries) assert exist_in?(Enum.at(reply_comment_list, 3), paged_replies.entries) end diff --git a/test/groupher_server/cms/comments/post_comment_test.exs b/test/groupher_server/cms/comments/post_comment_test.exs index bd77125..bf025e0 100644 --- a/test/groupher_server/cms/comments/post_comment_test.exs +++ b/test/groupher_server/cms/comments/post_comment_test.exs @@ -441,7 +441,6 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do # {:ok, comment} = ORM.find(Comment, comment.id) # end - @tag :wip test "can undo a report with other user report it too", ~m(user user2 post)a do {:ok, comment} = CMS.create_comment(:post, post.id, mock_comment(), user) diff --git a/test/groupher_server/mailer/email_test.exs b/test/groupher_server/mailer/email_test.exs index 2b87504..4732d44 100644 --- a/test/groupher_server/mailer/email_test.exs +++ b/test/groupher_server/mailer/email_test.exs @@ -23,7 +23,6 @@ defmodule GroupherServer.Test.Mailer do # assert_delivered_email(expected_email) # end - @tag :wip test "not send welcome email when user has no email addr" do {:ok, user} = db_insert(:user, %{email: nil}) diff --git a/test/groupher_server_web/controller/og_test.exs b/test/groupher_server_web/controller/og_test.exs index c88d91c..d284a35 100644 --- a/test/groupher_server_web/controller/og_test.exs +++ b/test/groupher_server_web/controller/og_test.exs @@ -4,7 +4,6 @@ defmodule GroupherServerWeb.Test.Controller.OG do """ use GroupherServer.TestTools - @tag :wip test "should return valid structure when query url is valid" do conn = build_conn() diff --git a/test/groupher_server_web/query/cms/paged_articles/paged_posts_test.exs b/test/groupher_server_web/query/cms/paged_articles/paged_posts_test.exs index 45f9aab..e83fb2e 100644 --- a/test/groupher_server_web/query/cms/paged_articles/paged_posts_test.exs +++ b/test/groupher_server_web/query/cms/paged_articles/paged_posts_test.exs @@ -193,7 +193,6 @@ defmodule GroupherServer.Test.Query.PagedArticles.PagedPosts do assert not is_nil(get_in(post, ["document", "bodyHtml"])) end - @tag :wip test "support article_tag filter", ~m(guest_conn user)a do {:ok, community} = db_insert(:community) post_attrs = mock_attrs(:post, %{community_id: community.id}) From 7b2c497dd54a99392c6127ca6602cd5bf40ac9b4 Mon Sep 17 00:00:00 2001 From: mydearxym Date: Tue, 2 Apr 2024 22:32:39 +0800 Subject: [PATCH 09/16] chore: wip -> bills --- .../billing/models/bill_record.ex | 5 ++++- lib/helper/constant/db_prefix.ex | 2 ++ .../20240402141029_remove_old_bills.exs | 8 ++++++++ .../20240402142641_add_prefix_to_biils.exs | 17 +++++++++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 priv/repo/migrations/20240402141029_remove_old_bills.exs create mode 100644 priv/repo/migrations/20240402142641_add_prefix_to_biils.exs diff --git a/lib/groupher_server/billing/models/bill_record.ex b/lib/groupher_server/billing/models/bill_record.ex index 711ad2c..7f21e27 100644 --- a/lib/groupher_server/billing/models/bill_record.ex +++ b/lib/groupher_server/billing/models/bill_record.ex @@ -7,12 +7,15 @@ defmodule GroupherServer.Billing.Model.BillRecord do use Ecto.Schema import Ecto.Changeset + alias Helper.Constant.DBPrefix alias GroupherServer.Accounts.Model.User + @schema_prefix DBPrefix.payment() + @required_fields ~w(user_id hash_id state amount payment_usage payment_method)a @optional_fields ~w(note)a - schema "bill_records" do + schema "bills" do belongs_to(:user, User, foreign_key: :user_id) field(:state, :string) diff --git a/lib/helper/constant/db_prefix.ex b/lib/helper/constant/db_prefix.ex index eadc8c7..c46b551 100644 --- a/lib/helper/constant/db_prefix.ex +++ b/lib/helper/constant/db_prefix.ex @@ -6,8 +6,10 @@ defmodule Helper.Constant.DBPrefix do @default "public" @cms "cms" @account "account" + @payment "payment" def default, do: @default def cms, do: @cms def account, do: @account + def payment, do: @payment end diff --git a/priv/repo/migrations/20240402141029_remove_old_bills.exs b/priv/repo/migrations/20240402141029_remove_old_bills.exs new file mode 100644 index 0000000..937b69f --- /dev/null +++ b/priv/repo/migrations/20240402141029_remove_old_bills.exs @@ -0,0 +1,8 @@ +defmodule GroupherServer.Repo.Migrations.RemoveOldBills do + use Ecto.Migration + + def change do + drop table(:users_bills) + drop table(:bills) + end +end diff --git a/priv/repo/migrations/20240402142641_add_prefix_to_biils.exs b/priv/repo/migrations/20240402142641_add_prefix_to_biils.exs new file mode 100644 index 0000000..fba4295 --- /dev/null +++ b/priv/repo/migrations/20240402142641_add_prefix_to_biils.exs @@ -0,0 +1,17 @@ +defmodule GroupherServer.Repo.Migrations.AddPrefixToBiils do + use Ecto.Migration + + def up do + execute "CREATE SCHEMA IF NOT EXISTS payment" + + execute "ALTER TABLE bill_records RENAME TO bills" + execute "ALTER TABLE public.bills SET SCHEMA payment" + end + + def down do + execute "ALTER TABLE payment.bills SET SCHEMA public" + execute "ALTER TABLE public.bills RENAME TO bill_records" + + execute "DROP SCHEMA IF EXISTS payment" + end +end From 66e6e09c28b61f422fb01e6f2cea0739848df3a0 Mon Sep 17 00:00:00 2001 From: mydearxym Date: Tue, 2 Apr 2024 22:49:44 +0800 Subject: [PATCH 10/16] chore: wip -> bills -> payment --- lib/groupher_server/mailer/email.ex | 4 +- .../{billing => payment}/delegates/actions.ex | 4 +- .../{billing => payment}/delegates/crud.ex | 8 ++-- .../models/bill_record.ex | 2 +- .../billing.ex => payment/payment.ex} | 4 +- ...illing_resolver.ex => payment_resolver.ex} | 10 ++--- lib/groupher_server_web/schema.ex | 16 ++++---- .../payment_mutations.ex} | 8 ++-- .../payment_queries.ex} | 6 +-- .../payment_types.ex} | 2 +- .../{billing => payment}/billing_test.exs | 38 +++++++++---------- .../mutation/billing/billing_test.exs | 6 +-- .../{billing => payment}/billing_test.exs | 6 +-- 13 files changed, 57 insertions(+), 57 deletions(-) rename lib/groupher_server/{billing => payment}/delegates/actions.ex (94%) rename lib/groupher_server/{billing => payment}/delegates/crud.ex (93%) rename lib/groupher_server/{billing => payment}/models/bill_record.ex (95%) rename lib/groupher_server/{billing/billing.ex => payment/payment.ex} (70%) rename lib/groupher_server_web/resolvers/{billing_resolver.ex => payment_resolver.ex} (64%) rename lib/groupher_server_web/schema/{billing/billing_mutations.ex => payment/payment_mutations.ex} (78%) rename lib/groupher_server_web/schema/{billing/billing_queries.ex => payment/payment_queries.ex} (69%) rename lib/groupher_server_web/schema/{billing/billing_types.ex => payment/payment_types.ex} (94%) rename test/groupher_server/{billing => payment}/billing_test.exs (70%) rename test/groupher_server_web/query/{billing => payment}/billing_test.exs (87%) diff --git a/lib/groupher_server/mailer/email.ex b/lib/groupher_server/mailer/email.ex index 5b68294..7febd77 100644 --- a/lib/groupher_server/mailer/email.ex +++ b/lib/groupher_server/mailer/email.ex @@ -7,10 +7,10 @@ defmodule GroupherServer.Email do import Bamboo.Email import Helper.Utils, only: [get_config: 2] - alias GroupherServer.{Accounts, Billing, Email, Mailer} + alias GroupherServer.{Accounts, Payment, Email, Mailer} alias Accounts.Model.User - alias Billing.Model.BillRecord + alias Payment.Model.BillRecord alias Email.Templates alias Mailer diff --git a/lib/groupher_server/billing/delegates/actions.ex b/lib/groupher_server/payment/delegates/actions.ex similarity index 94% rename from lib/groupher_server/billing/delegates/actions.ex rename to lib/groupher_server/payment/delegates/actions.ex index f998211..4d3679c 100644 --- a/lib/groupher_server/billing/delegates/actions.ex +++ b/lib/groupher_server/payment/delegates/actions.ex @@ -1,4 +1,4 @@ -defmodule GroupherServer.Billing.Delegate.Actions do +defmodule GroupherServer.Payment.Delegate.Actions do @moduledoc """ actions after biling state success """ @@ -7,7 +7,7 @@ defmodule GroupherServer.Billing.Delegate.Actions do alias Helper.ORM alias GroupherServer.Accounts - alias GroupherServer.Billing.Model.BillRecord + alias GroupherServer.Payment.Model.BillRecord alias GroupherServer.Email alias Accounts.Model.User diff --git a/lib/groupher_server/billing/delegates/crud.ex b/lib/groupher_server/payment/delegates/crud.ex similarity index 93% rename from lib/groupher_server/billing/delegates/crud.ex rename to lib/groupher_server/payment/delegates/crud.ex index 2d7560c..6cdd2d6 100644 --- a/lib/groupher_server/billing/delegates/crud.ex +++ b/lib/groupher_server/payment/delegates/crud.ex @@ -1,4 +1,4 @@ -defmodule GroupherServer.Billing.Delegate.CRUD do +defmodule GroupherServer.Payment.Delegate.CRUD do @moduledoc """ create update & list for billings """ @@ -9,10 +9,10 @@ defmodule GroupherServer.Billing.Delegate.CRUD do alias Helper.ORM - alias GroupherServer.{Accounts, Billing} + alias GroupherServer.{Accounts, Payment} alias Accounts.Model.User - alias Billing.Model.BillRecord - alias Billing.Delegate.Actions + alias Payment.Model.BillRecord + alias Payment.Delegate.Actions alias GroupherServer.Repo alias GroupherServer.Email diff --git a/lib/groupher_server/billing/models/bill_record.ex b/lib/groupher_server/payment/models/bill_record.ex similarity index 95% rename from lib/groupher_server/billing/models/bill_record.ex rename to lib/groupher_server/payment/models/bill_record.ex index 7f21e27..a2a8c22 100644 --- a/lib/groupher_server/billing/models/bill_record.ex +++ b/lib/groupher_server/payment/models/bill_record.ex @@ -1,4 +1,4 @@ -defmodule GroupherServer.Billing.Model.BillRecord do +defmodule GroupherServer.Payment.Model.BillRecord do @moduledoc """ bill records for investors """ diff --git a/lib/groupher_server/billing/billing.ex b/lib/groupher_server/payment/payment.ex similarity index 70% rename from lib/groupher_server/billing/billing.ex rename to lib/groupher_server/payment/payment.ex index 2875113..e2f95f6 100644 --- a/lib/groupher_server/billing/billing.ex +++ b/lib/groupher_server/payment/payment.ex @@ -1,7 +1,7 @@ -defmodule GroupherServer.Billing do +defmodule GroupherServer.Payment do @moduledoc false - alias GroupherServer.Billing.Delegate.CRUD + alias GroupherServer.Payment.Delegate.CRUD defdelegate create_record(user, attrs), to: CRUD defdelegate paged_records(user, filter), to: CRUD diff --git a/lib/groupher_server_web/resolvers/billing_resolver.ex b/lib/groupher_server_web/resolvers/payment_resolver.ex similarity index 64% rename from lib/groupher_server_web/resolvers/billing_resolver.ex rename to lib/groupher_server_web/resolvers/payment_resolver.ex index e3b4579..1534150 100644 --- a/lib/groupher_server_web/resolvers/billing_resolver.ex +++ b/lib/groupher_server_web/resolvers/payment_resolver.ex @@ -1,4 +1,4 @@ -defmodule GroupherServerWeb.Resolvers.Billing do +defmodule GroupherServerWeb.Resolvers.Payment do @moduledoc """ accounts resolvers """ @@ -6,17 +6,17 @@ defmodule GroupherServerWeb.Resolvers.Billing do # import ShortMaps # import Helper.ErrorCode - alias GroupherServer.Billing + alias GroupherServer.Payment def paged_bill_records(_root, %{filter: filter}, %{context: %{cur_user: cur_user}}) do - Billing.paged_records(cur_user, filter) + Payment.paged_records(cur_user, filter) end def create_bill(_root, args, %{context: %{cur_user: cur_user}}) do - Billing.create_record(cur_user, args) + Payment.create_record(cur_user, args) end def update_bill_state(_root, %{id: id, state: state}, %{context: %{cur_user: _cur_user}}) do - Billing.update_record_state(id, state) + Payment.update_record_state(id, state) end end diff --git a/lib/groupher_server_web/schema.ex b/lib/groupher_server_web/schema.ex index eed179c..a1edce4 100644 --- a/lib/groupher_server_web/schema.ex +++ b/lib/groupher_server_web/schema.ex @@ -6,7 +6,7 @@ defmodule GroupherServerWeb.Schema do import GroupherServerWeb.Schema.Helper.Imports alias GroupherServerWeb.Middleware, as: M - alias GroupherServerWeb.Schema.{Account, Billing, CMS, Statistics, Helper} + alias GroupherServerWeb.Schema.{Account, Payment, CMS, Statistics, Helper} import_types(Absinthe.Type.Custom) @@ -18,10 +18,10 @@ defmodule GroupherServerWeb.Schema do import_types(Account.Queries) import_types(Account.Mutations) - # billing - import_types(Billing.Types) - import_types(Billing.Queries) - import_types(Billing.Mutations) + # Payment + import_types(Payment.Types) + import_types(Payment.Queries) + import_types(Payment.Mutations) # statistics import_types(Statistics.Types) @@ -41,7 +41,7 @@ defmodule GroupherServerWeb.Schema do query do import_fields(:account_queries) - import_fields(:billing_queries) + import_fields(:payment_queries) import_fields(:statistics_queries) import_fields(:cms_queries) end @@ -49,8 +49,8 @@ defmodule GroupherServerWeb.Schema do mutation do # account import_fields(:account_mutations) - # billing - import_fields(:billing_mutations) + # payment + import_fields(:payment_mutations) # statistics import_fields(:statistics_mutations) # cms diff --git a/lib/groupher_server_web/schema/billing/billing_mutations.ex b/lib/groupher_server_web/schema/payment/payment_mutations.ex similarity index 78% rename from lib/groupher_server_web/schema/billing/billing_mutations.ex rename to lib/groupher_server_web/schema/payment/payment_mutations.ex index b520bce..f94ede7 100644 --- a/lib/groupher_server_web/schema/billing/billing_mutations.ex +++ b/lib/groupher_server_web/schema/payment/payment_mutations.ex @@ -1,10 +1,10 @@ -defmodule GroupherServerWeb.Schema.Billing.Mutations do +defmodule GroupherServerWeb.Schema.Payment.Mutations do @moduledoc """ billing mutations """ use Helper.GqlSchemaSuite - object :billing_mutations do + object :payment_mutations do @desc "create bill" field :create_bill, :bill do arg(:payment_method, non_null(:payment_method_enum)) @@ -13,7 +13,7 @@ defmodule GroupherServerWeb.Schema.Billing.Mutations do arg(:note, :string) middleware(M.Authorize, :login) - resolve(&R.Billing.create_bill/3) + resolve(&R.Payment.create_bill/3) end @desc "update user's bill state" @@ -24,7 +24,7 @@ defmodule GroupherServerWeb.Schema.Billing.Mutations do middleware(M.Authorize, :login) middleware(M.Passport, claim: "cms->system_accountant") - resolve(&R.Billing.update_bill_state/3) + resolve(&R.Payment.update_bill_state/3) end end end diff --git a/lib/groupher_server_web/schema/billing/billing_queries.ex b/lib/groupher_server_web/schema/payment/payment_queries.ex similarity index 69% rename from lib/groupher_server_web/schema/billing/billing_queries.ex rename to lib/groupher_server_web/schema/payment/payment_queries.ex index 0f099c8..8453bea 100644 --- a/lib/groupher_server_web/schema/billing/billing_queries.ex +++ b/lib/groupher_server_web/schema/payment/payment_queries.ex @@ -1,10 +1,10 @@ -defmodule GroupherServerWeb.Schema.Billing.Queries do +defmodule GroupherServerWeb.Schema.Payment.Queries do @moduledoc """ billing GraphQL queries """ use Helper.GqlSchemaSuite - object :billing_queries do + object :payment_queries do @desc "get all bills" field :paged_bill_records, non_null(:paged_bills) do arg(:filter, non_null(:pagi_filter)) @@ -12,7 +12,7 @@ defmodule GroupherServerWeb.Schema.Billing.Queries do middleware(M.Authorize, :login) middleware(M.PageSizeProof) - resolve(&R.Billing.paged_bill_records/3) + resolve(&R.Payment.paged_bill_records/3) end end end diff --git a/lib/groupher_server_web/schema/billing/billing_types.ex b/lib/groupher_server_web/schema/payment/payment_types.ex similarity index 94% rename from lib/groupher_server_web/schema/billing/billing_types.ex rename to lib/groupher_server_web/schema/payment/payment_types.ex index a629b15..d7eb1cb 100644 --- a/lib/groupher_server_web/schema/billing/billing_types.ex +++ b/lib/groupher_server_web/schema/payment/payment_types.ex @@ -1,4 +1,4 @@ -defmodule GroupherServerWeb.Schema.Billing.Types do +defmodule GroupherServerWeb.Schema.Payment.Types do @moduledoc """ general types used by Billing """ diff --git a/test/groupher_server/billing/billing_test.exs b/test/groupher_server/payment/billing_test.exs similarity index 70% rename from test/groupher_server/billing/billing_test.exs rename to test/groupher_server/payment/billing_test.exs index 21a6506..4ffb225 100644 --- a/test/groupher_server/billing/billing_test.exs +++ b/test/groupher_server/payment/billing_test.exs @@ -1,11 +1,11 @@ -defmodule GroupherServer.Test.Billing do +defmodule GroupherServer.Test.Payment do use GroupherServer.TestTools import Helper.Utils, only: [get_config: 2] alias Helper.ORM alias GroupherServer.Accounts.Model.User - alias GroupherServer.Billing + alias GroupherServer.Payment @senior_amount_threshold get_config(:general, :senior_amount_threshold) @@ -17,9 +17,9 @@ defmodule GroupherServer.Test.Billing do {:ok, ~m(user valid_attrs)a} end - describe "[billing curd]" do + describe "[payment billing curd]" do test "create bill record with valid attrs", ~m(user valid_attrs)a do - {:ok, record} = Billing.create_record(user, valid_attrs) + {:ok, record} = Payment.create_record(user, valid_attrs) assert record.amount == @senior_amount_threshold assert record.payment_usage == "donate" @@ -29,28 +29,28 @@ defmodule GroupherServer.Test.Billing do end test "create bill record with valid note", ~m(user valid_attrs)a do - {:ok, record} = Billing.create_record(user, valid_attrs |> Map.merge(%{note: "i am girl"})) + {:ok, record} = Payment.create_record(user, valid_attrs |> Map.merge(%{note: "i am girl"})) assert record.note == "i am girl" end test "create bill record with previous record unhandled fails", ~m(user valid_attrs)a do - {:ok, _record} = Billing.create_record(user, valid_attrs) - {:error, reason} = Billing.create_record(user, valid_attrs) + {:ok, _record} = Payment.create_record(user, valid_attrs) + {:error, reason} = Payment.create_record(user, valid_attrs) assert reason |> Keyword.get(:code) == ecode(:exsit_pending_bill) end test "record state can be update", ~m(user valid_attrs)a do - {:ok, record} = Billing.create_record(user, valid_attrs) + {:ok, record} = Payment.create_record(user, valid_attrs) - {:ok, updated} = Billing.update_record_state(record.id, :done) + {:ok, updated} = Payment.update_record_state(record.id, :done) assert updated.state == "done" end test "can get paged bill records of a user", ~m(user valid_attrs)a do - {:ok, _record} = Billing.create_record(user, valid_attrs) + {:ok, _record} = Payment.create_record(user, valid_attrs) - {:ok, records} = Billing.paged_records(user, %{page: 1, size: 20}) + {:ok, records} = Payment.paged_records(user, %{page: 1, size: 20}) records |> is_valid_pagination?(:raw) assert records.entries |> List.first() |> Map.get(:user_id) == user.id @@ -61,8 +61,8 @@ defmodule GroupherServer.Test.Billing do test "user updgrade to senior_member after senior bill handled", ~m(user valid_attrs)a do attrs = valid_attrs |> Map.merge(%{amount: @senior_amount_threshold}) - {:ok, record} = Billing.create_record(user, attrs) - {:ok, _updated} = Billing.update_record_state(record.id, :done) + {:ok, record} = Payment.create_record(user, attrs) + {:ok, _updated} = Payment.update_record_state(record.id, :done) {:ok, %{achievement: achievement}} = ORM.find(User, user.id, preload: :achievement) assert achievement.senior_member == true @@ -71,8 +71,8 @@ defmodule GroupherServer.Test.Billing do test "user updgrade to donate_member after donate bill handled", ~m(user valid_attrs)a do attrs = valid_attrs |> Map.merge(%{amount: @senior_amount_threshold - 10}) - {:ok, record} = Billing.create_record(user, attrs) - {:ok, _updated} = Billing.update_record_state(record.id, :done) + {:ok, record} = Payment.create_record(user, attrs) + {:ok, _updated} = Payment.update_record_state(record.id, :done) {:ok, %{achievement: achievement}} = ORM.find(User, user.id, preload: :achievement) assert achievement.donate_member == true @@ -81,8 +81,8 @@ defmodule GroupherServer.Test.Billing do test "girls updgrade to senior_member after bill handled", ~m(user valid_attrs)a do attrs = valid_attrs |> Map.merge(%{amount: 0, payment_usage: "girls_code_too_plan"}) - {:ok, record} = Billing.create_record(user, attrs) - {:ok, _updated} = Billing.update_record_state(record.id, :done) + {:ok, record} = Payment.create_record(user, attrs) + {:ok, _updated} = Payment.update_record_state(record.id, :done) {:ok, %{achievement: achievement}} = ORM.find(User, user.id, preload: :achievement) assert achievement.senior_member == true @@ -91,8 +91,8 @@ defmodule GroupherServer.Test.Billing do test "sponsor updgrade to senior_member after bill handled", ~m(user valid_attrs)a do attrs = valid_attrs |> Map.merge(%{amount: 0, payment_usage: "sponsor"}) - {:ok, record} = Billing.create_record(user, attrs) - {:ok, _updated} = Billing.update_record_state(record.id, :done) + {:ok, record} = Payment.create_record(user, attrs) + {:ok, _updated} = Payment.update_record_state(record.id, :done) {:ok, %{achievement: achievement}} = ORM.find(User, user.id, preload: :achievement) assert achievement.sponsor_member == true diff --git a/test/groupher_server_web/mutation/billing/billing_test.exs b/test/groupher_server_web/mutation/billing/billing_test.exs index e14fc5f..862f95f 100644 --- a/test/groupher_server_web/mutation/billing/billing_test.exs +++ b/test/groupher_server_web/mutation/billing/billing_test.exs @@ -1,9 +1,9 @@ -defmodule GroupherServer.Test.Mutation.Billing.Basic do +defmodule GroupherServer.Test.Mutation.Payment.Basic do use GroupherServer.TestTools import Helper.Utils - alias GroupherServer.Billing + alias GroupherServer.Payment @senior_amount_threshold get_config(:general, :senior_amount_threshold) @@ -60,7 +60,7 @@ defmodule GroupherServer.Test.Mutation.Billing.Basic do } """ test "auth user can update bill state", ~m(user valid_attrs)a do - {:ok, record} = Billing.create_record(user, valid_attrs) + {:ok, record} = Payment.create_record(user, valid_attrs) variables = %{ id: record.id, diff --git a/test/groupher_server_web/query/billing/billing_test.exs b/test/groupher_server_web/query/payment/billing_test.exs similarity index 87% rename from test/groupher_server_web/query/billing/billing_test.exs rename to test/groupher_server_web/query/payment/billing_test.exs index b20ee06..7dc62aa 100644 --- a/test/groupher_server_web/query/billing/billing_test.exs +++ b/test/groupher_server_web/query/payment/billing_test.exs @@ -1,8 +1,8 @@ -defmodule GroupherServer.Test.Query.Billing.Basic do +defmodule GroupherServer.Test.Query.Payment.Basic do use GroupherServer.TestTools # alias Helper.ORM - alias GroupherServer.Billing + alias GroupherServer.Payment setup do {:ok, user} = db_insert(:user) @@ -38,7 +38,7 @@ defmodule GroupherServer.Test.Query.Billing.Basic do } """ test "login user can get it's own billing records ", ~m(user_conn user valid_attrs)a do - {:ok, _record} = Billing.create_record(user, valid_attrs) + {:ok, _record} = Payment.create_record(user, valid_attrs) variables = %{filter: %{page: 1, size: 20}} results = user_conn |> query_result(@query, variables, "pagedBillRecords") From 85ef889aae9097d7d23e4a6ba3fffe8dbf90641d Mon Sep 17 00:00:00 2001 From: mydearxym Date: Wed, 3 Apr 2024 19:39:55 +0800 Subject: [PATCH 11/16] chore: wip -> static --- .../statistics/models/user_contribute.ex | 4 ++-- lib/helper/constant/db_prefix.ex | 2 ++ .../20240403100557_add_statistic_prefix.exs | 20 +++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 priv/repo/migrations/20240403100557_add_statistic_prefix.exs diff --git a/lib/groupher_server/statistics/models/user_contribute.ex b/lib/groupher_server/statistics/models/user_contribute.ex index 9121296..c8d1e96 100644 --- a/lib/groupher_server/statistics/models/user_contribute.ex +++ b/lib/groupher_server/statistics/models/user_contribute.ex @@ -9,10 +9,10 @@ defmodule GroupherServer.Statistics.Model.UserContribute do alias GroupherServer.Accounts alias Accounts.Model.User - @schema_prefix DBPrefix.account() + @schema_prefix DBPrefix.statistics() @type t :: %UserContribute{} - schema "contributes" do + schema "user_contributes" do field(:count, :integer) field(:date, :date) belongs_to(:user, User) diff --git a/lib/helper/constant/db_prefix.ex b/lib/helper/constant/db_prefix.ex index c46b551..8d2d938 100644 --- a/lib/helper/constant/db_prefix.ex +++ b/lib/helper/constant/db_prefix.ex @@ -6,10 +6,12 @@ defmodule Helper.Constant.DBPrefix do @default "public" @cms "cms" @account "account" + @statistics "statistics" @payment "payment" def default, do: @default def cms, do: @cms + def statistics, do: @statistics def account, do: @account def payment, do: @payment end diff --git a/priv/repo/migrations/20240403100557_add_statistic_prefix.exs b/priv/repo/migrations/20240403100557_add_statistic_prefix.exs new file mode 100644 index 0000000..77e7ede --- /dev/null +++ b/priv/repo/migrations/20240403100557_add_statistic_prefix.exs @@ -0,0 +1,20 @@ +defmodule GroupherServer.Repo.Migrations.AddStatisticPrefix do + use Ecto.Migration + + def up do + execute "CREATE SCHEMA IF NOT EXISTS statistics" + + execute "ALTER TABLE account.contributes SET SCHEMA public" + execute "ALTER TABLE contributes RENAME TO user_contributes" + execute "ALTER TABLE public.user_contributes SET SCHEMA statistics" + end + + def down do + execute "ALTER TABLE statistics.user_contributes SET SCHEMA public" + + execute "ALTER TABLE user_contributes RENAME TO contributes" + execute "ALTER TABLE contributes SET SCHEMA account" + + execute "DROP SCHEMA IF EXISTS statistics" + end +end From cf45f9886fc5d9ea1b23d05fe09e195e868e2377 Mon Sep 17 00:00:00 2001 From: mydearxym Date: Wed, 3 Apr 2024 20:17:03 +0800 Subject: [PATCH 12/16] chore: wip --- .../statistics/models/community_contribute.ex | 4 ++++ .../statistics/models/publish_throttle.ex | 4 ++++ .../20240403114105_add_statistic_prefix_2.exs | 13 +++++++++++++ 3 files changed, 21 insertions(+) create mode 100644 priv/repo/migrations/20240403114105_add_statistic_prefix_2.exs diff --git a/lib/groupher_server/statistics/models/community_contribute.ex b/lib/groupher_server/statistics/models/community_contribute.ex index 723cbdc..a5c7d2f 100644 --- a/lib/groupher_server/statistics/models/community_contribute.ex +++ b/lib/groupher_server/statistics/models/community_contribute.ex @@ -4,8 +4,12 @@ defmodule GroupherServer.Statistics.Model.CommunityContribute do use Ecto.Schema import Ecto.Changeset + + alias Helper.Constant.DBPrefix alias GroupherServer.CMS.Model.Community + @schema_prefix DBPrefix.statistics() + @type t :: %CommunityContribute{} schema "community_contributes" do field(:count, :integer) diff --git a/lib/groupher_server/statistics/models/publish_throttle.ex b/lib/groupher_server/statistics/models/publish_throttle.ex index 4d8466e..fe766d5 100644 --- a/lib/groupher_server/statistics/models/publish_throttle.ex +++ b/lib/groupher_server/statistics/models/publish_throttle.ex @@ -4,9 +4,13 @@ defmodule GroupherServer.Statistics.Model.PublishThrottle do use Ecto.Schema import Ecto.Changeset + + alias Helper.Constant.DBPrefix alias GroupherServer.Accounts alias Accounts.Model.User + @schema_prefix DBPrefix.statistics() + @optional_fields ~w(user_id publish_hour publish_date hour_count date_count last_publish_time)a @required_fields ~w(user_id)a diff --git a/priv/repo/migrations/20240403114105_add_statistic_prefix_2.exs b/priv/repo/migrations/20240403114105_add_statistic_prefix_2.exs new file mode 100644 index 0000000..bd2b264 --- /dev/null +++ b/priv/repo/migrations/20240403114105_add_statistic_prefix_2.exs @@ -0,0 +1,13 @@ +defmodule GroupherServer.Repo.Migrations.AddStatisticPrefix2 do + use Ecto.Migration + + def up do + execute "ALTER TABLE public.community_contributes SET SCHEMA statistics" + execute "ALTER TABLE public.publish_throttles SET SCHEMA statistics" + end + + def down do + execute "ALTER TABLE statistics.community_contributes SET SCHEMA public" + execute "ALTER TABLE statistics.publish_throttles SET SCHEMA public" + end +end From 779f9d7ef883174a59a49d043aa12f96133b6f8d Mon Sep 17 00:00:00 2001 From: mydearxym Date: Wed, 3 Apr 2024 20:21:12 +0800 Subject: [PATCH 13/16] chore: wip --- priv/repo/migrations/20240403121723_clean_up.exs | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 priv/repo/migrations/20240403121723_clean_up.exs diff --git a/priv/repo/migrations/20240403121723_clean_up.exs b/priv/repo/migrations/20240403121723_clean_up.exs new file mode 100644 index 0000000..b3394d0 --- /dev/null +++ b/priv/repo/migrations/20240403121723_clean_up.exs @@ -0,0 +1,8 @@ +defmodule GroupherServer.Repo.Migrations.CleanUp do + use Ecto.Migration + + def change do + drop table(:education_backgrounds) + drop table(:cms_blog_rss) + end +end From 3a0244ba4b442722eaa34c3993d63a2a353db1dd Mon Sep 17 00:00:00 2001 From: mydearxym Date: Wed, 3 Apr 2024 20:39:18 +0800 Subject: [PATCH 14/16] chore: wip --- lib/groupher_server/delivery/models/mention.ex | 3 +++ .../delivery/models/notification.ex | 3 +++ lib/helper/constant/db_prefix.ex | 2 ++ .../20240403122317_add_delivery_prefix.exs | 17 +++++++++++++++++ 4 files changed, 25 insertions(+) create mode 100644 priv/repo/migrations/20240403122317_add_delivery_prefix.exs diff --git a/lib/groupher_server/delivery/models/mention.ex b/lib/groupher_server/delivery/models/mention.ex index 10a1849..d229942 100644 --- a/lib/groupher_server/delivery/models/mention.ex +++ b/lib/groupher_server/delivery/models/mention.ex @@ -5,8 +5,11 @@ defmodule GroupherServer.Delivery.Model.Mention do use Ecto.Schema import Ecto.Changeset + alias Helper.Constant.DBPrefix alias GroupherServer.Accounts.Model.User + @schema_prefix DBPrefix.delivery() + @required_fields ~w(from_user_id to_user_id title article_id thread)a @optional_fields ~w(comment_id read)a diff --git a/lib/groupher_server/delivery/models/notification.ex b/lib/groupher_server/delivery/models/notification.ex index 4304aa2..d2fcf98 100644 --- a/lib/groupher_server/delivery/models/notification.ex +++ b/lib/groupher_server/delivery/models/notification.ex @@ -6,9 +6,12 @@ defmodule GroupherServer.Delivery.Model.Notification do import Ecto.Changeset alias GroupherServer.{Accounts, CMS} + alias Helper.Constant.DBPrefix alias Accounts.Model.User alias CMS.Model.Embeds + @schema_prefix DBPrefix.delivery() + @required_fields ~w(user_id action)a @optional_fields ~w(thread article_id comment_id title read)a diff --git a/lib/helper/constant/db_prefix.ex b/lib/helper/constant/db_prefix.ex index 8d2d938..af6675a 100644 --- a/lib/helper/constant/db_prefix.ex +++ b/lib/helper/constant/db_prefix.ex @@ -7,10 +7,12 @@ defmodule Helper.Constant.DBPrefix do @cms "cms" @account "account" @statistics "statistics" + @delivery "delivery" @payment "payment" def default, do: @default def cms, do: @cms + def delivery, do: @delivery def statistics, do: @statistics def account, do: @account def payment, do: @payment diff --git a/priv/repo/migrations/20240403122317_add_delivery_prefix.exs b/priv/repo/migrations/20240403122317_add_delivery_prefix.exs new file mode 100644 index 0000000..8bcd151 --- /dev/null +++ b/priv/repo/migrations/20240403122317_add_delivery_prefix.exs @@ -0,0 +1,17 @@ +defmodule GroupherServer.Repo.Migrations.AddDeliveryPrefix do + use Ecto.Migration + + def up do + execute "CREATE SCHEMA IF NOT EXISTS delivery" + + execute "ALTER TABLE mentions SET SCHEMA delivery" + execute "ALTER TABLE notifications SET SCHEMA delivery" + end + + def down do + execute "ALTER TABLE delivery.mentions SET SCHEMA public" + execute "ALTER TABLE delivery.notifications SET SCHEMA public" + + execute "DROP SCHEMA IF EXISTS delivery" + end +end From 79dc9480bbf79afff10b46c10ff3e13298eeffef Mon Sep 17 00:00:00 2001 From: mydearxym Date: Wed, 3 Apr 2024 21:03:12 +0800 Subject: [PATCH 15/16] chore: wip log --- .../{logs/logs.ex => log/log.ex} | 4 ++-- .../{logs => log}/user_activity.ex | 6 ++++-- lib/helper/constant/db_prefix.ex | 2 ++ .../20240403124315_add_logs_prefix.exs | 20 +++++++++++++++++++ .../{logs => log}/logs_test.exs | 0 5 files changed, 28 insertions(+), 4 deletions(-) rename lib/groupher_server/{logs/logs.ex => log/log.ex} (60%) rename lib/groupher_server/{logs => log}/user_activity.ex (80%) create mode 100644 priv/repo/migrations/20240403124315_add_logs_prefix.exs rename test/groupher_server/{logs => log}/logs_test.exs (100%) diff --git a/lib/groupher_server/logs/logs.ex b/lib/groupher_server/log/log.ex similarity index 60% rename from lib/groupher_server/logs/logs.ex rename to lib/groupher_server/log/log.ex index b14bd48..c3d3013 100644 --- a/lib/groupher_server/logs/logs.ex +++ b/lib/groupher_server/log/log.ex @@ -1,4 +1,4 @@ -defmodule GroupherServer.Logs do +defmodule GroupherServer.Log do @moduledoc """ The Logs context. """ @@ -6,5 +6,5 @@ defmodule GroupherServer.Logs do # import Ecto.Query, warn: false # alias GroupherServer.Repo - # alias GroupherServer.Logs.UserActivity + # alias GroupherServer.Log.UserActivity end diff --git a/lib/groupher_server/logs/user_activity.ex b/lib/groupher_server/log/user_activity.ex similarity index 80% rename from lib/groupher_server/logs/user_activity.ex rename to lib/groupher_server/log/user_activity.ex index c7fbda2..ae1fc89 100644 --- a/lib/groupher_server/logs/user_activity.ex +++ b/lib/groupher_server/log/user_activity.ex @@ -1,16 +1,18 @@ -defmodule GroupherServer.Logs.UserActivity do +defmodule GroupherServer.Log.UserActivity do @moduledoc false # alias __MODULE__ use Ecto.Schema import Ecto.Changeset + alias Helper.Constant.DBPrefix alias GroupherServer.Accounts.Model.User + @schema_prefix DBPrefix.log() @required_fields ~w(user_id source_title source_id source_type)a # @optional_fields ~w(source_type)a - schema "user_activity_logs" do + schema "user_activities" do belongs_to(:user, User) field(:source_id, :string) diff --git a/lib/helper/constant/db_prefix.ex b/lib/helper/constant/db_prefix.ex index af6675a..589aa39 100644 --- a/lib/helper/constant/db_prefix.ex +++ b/lib/helper/constant/db_prefix.ex @@ -9,6 +9,7 @@ defmodule Helper.Constant.DBPrefix do @statistics "statistics" @delivery "delivery" @payment "payment" + @log "log" def default, do: @default def cms, do: @cms @@ -16,4 +17,5 @@ defmodule Helper.Constant.DBPrefix do def statistics, do: @statistics def account, do: @account def payment, do: @payment + def log, do: @log end diff --git a/priv/repo/migrations/20240403124315_add_logs_prefix.exs b/priv/repo/migrations/20240403124315_add_logs_prefix.exs new file mode 100644 index 0000000..cb185c7 --- /dev/null +++ b/priv/repo/migrations/20240403124315_add_logs_prefix.exs @@ -0,0 +1,20 @@ +defmodule GroupherServer.Repo.Migrations.AddLogsPrefix do + use Ecto.Migration + + # user_activities + + def up do + execute "CREATE SCHEMA IF NOT EXISTS log" + + execute "ALTER TABLE user_activity_logs RENAME TO user_activities" + execute "ALTER TABLE public.user_activities SET SCHEMA log" + end + + def down do + execute "ALTER TABLE log.user_activities SET SCHEMA public" + execute "ALTER TABLE user_activities RENAME TO user_activity_logs" + execute "ALTER TABLE user_activity_logs SET SCHEMA public" + + execute "DROP SCHEMA IF EXISTS log" + end +end diff --git a/test/groupher_server/logs/logs_test.exs b/test/groupher_server/log/logs_test.exs similarity index 100% rename from test/groupher_server/logs/logs_test.exs rename to test/groupher_server/log/logs_test.exs From 497f5e5b23cb9d94040f1b2da6016f5a2a9193ba Mon Sep 17 00:00:00 2001 From: mydearxym Date: Fri, 5 Apr 2024 23:11:53 +0800 Subject: [PATCH 16/16] chore: rename to join --- lib/groupher_server/cms/models/community_category.ex | 2 +- .../20240405150535_rename_community_catetory.exs | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 priv/repo/migrations/20240405150535_rename_community_catetory.exs diff --git a/lib/groupher_server/cms/models/community_category.ex b/lib/groupher_server/cms/models/community_category.ex index bae6a81..c9a50c7 100644 --- a/lib/groupher_server/cms/models/community_category.ex +++ b/lib/groupher_server/cms/models/community_category.ex @@ -13,7 +13,7 @@ defmodule GroupherServer.CMS.Model.CommunityCategory do @schema_prefix DBPrefix.cms() @type t :: %CommunityCategory{} - schema "communities_categories" do + schema "communities_join_categories" do belongs_to(:community, Community, foreign_key: :community_id) belongs_to(:category, Category, foreign_key: :category_id) diff --git a/priv/repo/migrations/20240405150535_rename_community_catetory.exs b/priv/repo/migrations/20240405150535_rename_community_catetory.exs new file mode 100644 index 0000000..bcfdd30 --- /dev/null +++ b/priv/repo/migrations/20240405150535_rename_community_catetory.exs @@ -0,0 +1,7 @@ +defmodule GroupherServer.Repo.Migrations.RenameCommunityCatetory do + use Ecto.Migration + + def change do + rename(table("communities_categories", prefix: "cms"), to: table("communities_join_categories", prefix: "cms")) + end +end