From 9e6c267703bf4c211f736ba90548d1b3d3991fdf Mon Sep 17 00:00:00 2001 From: Alykhan Jetha Date: Wed, 9 Oct 2024 19:33:36 -0400 Subject: [PATCH] Add multi-tenant attributes & indexes --- CHANGELOG.md | 7 ++++++- README.md | 4 +++- lib/logger_psql/log.ex | 16 ++++++++++++++++ priv/templates/migration.exs.eex | 21 ++++++++++++++++++--- 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1340972..6241b04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## [0.1.3] - 2024-10-09 + +### Added + - Added multi-tenancy attributes & indexes. + ## [0.1.2] - 2023-07-25 Fixing message field. @@ -44,4 +49,4 @@ Initial Release of the package. - Added License ### Changed -### Fixed \ No newline at end of file +### Fixed diff --git a/README.md b/README.md index 9e77ed9..806a6ea 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ LoggerPSQL is a Logger backend that emits the logs to a PostgreSQL Repo. +** Added some customizations that's pretty unique to us. It would be nice if we could generalize and publish... ** + ## Installation Add `logger_psql` to your list of dependencies in `mix.exs`: @@ -11,7 +13,7 @@ Add `logger_psql` to your list of dependencies in `mix.exs`: ```elixir def deps do [ - {:logger_psql, "~> 0.1.2"} + {:logger_psql, "~> 0.1.3"} ] end ``` diff --git a/lib/logger_psql/log.ex b/lib/logger_psql/log.ex index e91d663..5da132d 100644 --- a/lib/logger_psql/log.ex +++ b/lib/logger_psql/log.ex @@ -7,6 +7,7 @@ defmodule LoggerPSQL.Log do @schema_prefix Application.compile_env(:logger_psql, [:backend, :prefix], "public") schema "logs" do + field(:customer_id, :integer) field(:level, :string) field(:application, :string) field(:domain, :string) @@ -16,6 +17,13 @@ defmodule LoggerPSQL.Log do field(:module, :string) field(:pid, :string) field(:time, :utc_datetime) + field(:user_email, :string) + field(:user_id, :integer) + field(:device_id, :string) + field(:device_name, :string) + field(:marketing_version, :string) + field(:build_number, :string) + field(:system_version, :string) field(:message, :string) field(:metadata, :map) @@ -27,6 +35,7 @@ defmodule LoggerPSQL.Log do def changeset(log \\ %__MODULE__{}, params) do log |> cast(params, [ + :customer_id, :level, :application, :domain, @@ -36,6 +45,13 @@ defmodule LoggerPSQL.Log do :module, :pid, :time, + :user_email, + :user_id, + :device_id, + :device_name, + :marketing_version, + :build_number, + :system_version, :metadata, :message, :request_id diff --git a/priv/templates/migration.exs.eex b/priv/templates/migration.exs.eex index e3e6ca7..60f8991 100644 --- a/priv/templates/migration.exs.eex +++ b/priv/templates/migration.exs.eex @@ -3,11 +3,11 @@ defmodule <%= module_prefix %>.Repo.Migrations.CreateLogTable do def change do <%= unless is_nil(db_prefix) or db_prefix == "" do %> - execute("CREATE SCHEMA <%= db_prefix %>", "DROP SCHEMA <%= db_prefix %> CASCADE") + execute("CREATE SCHEMA IF NOT EXISTS <%= db_prefix %>") <% end %> - create table(<%= inspect(schema_name) %>, primary_key: false<%= unless is_nil(db_prefix) or db_prefix == "", do: ", prefix: \"#{db_prefix}\"" %>) do add :id, :binary_id, primary_key: true + add :customer_id, :integer add :level, :string add :application, :string add :domain, :string @@ -17,11 +17,26 @@ defmodule <%= module_prefix %>.Repo.Migrations.CreateLogTable do add :module, :string add :pid, :string add :time, :utc_datetime + add :user_email, :string + add :user_id, :integer + add :device_id, :string + add :device_name, :string + add :marketing_version, :string + add :build_number, :string + add :system_version, :string + add :message, :text add :metadata, :map add :request_id, :string timestamps() end + + create index(<%= inspect(schema_name) %>, [:customer_id]<%= unless is_nil(db_prefix) or db_prefix == "", do: ", prefix: \"#{db_prefix}\"" %>) + create index(<%= inspect(schema_name) %>, [:user_email]<%= unless is_nil(db_prefix) or db_prefix == "", do: ", prefix: \"#{db_prefix}\"" %>) + create index(<%= inspect(schema_name) %>, [:device_id]<%= unless is_nil(db_prefix) or db_prefix == "", do: ", prefix: \"#{db_prefix}\"" %>) + create index(<%= inspect(schema_name) %>, [:level]<%= unless is_nil(db_prefix) or db_prefix == "", do: ", prefix: \"#{db_prefix}\"" %>) + create index(<%= inspect(schema_name) %>, [:inserted_at]<%= unless is_nil(db_prefix) or db_prefix == "", do: ", prefix: \"#{db_prefix}\"" %>) + end -end \ No newline at end of file +end