Skip to content

Commit

Permalink
Add multi-tenant attributes & indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
alykhanjetha committed Oct 9, 2024
1 parent ed103a5 commit 9e6c267
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -44,4 +49,4 @@ Initial Release of the package.
- Added License

### Changed
### Fixed
### Fixed
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

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`:

```elixir
def deps do
[
{:logger_psql, "~> 0.1.2"}
{:logger_psql, "~> 0.1.3"}
]
end
```
Expand Down
16 changes: 16 additions & 0 deletions lib/logger_psql/log.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -27,6 +35,7 @@ defmodule LoggerPSQL.Log do
def changeset(log \\ %__MODULE__{}, params) do
log
|> cast(params, [
:customer_id,
:level,
:application,
:domain,
Expand All @@ -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
Expand Down
21 changes: 18 additions & 3 deletions priv/templates/migration.exs.eex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
end

0 comments on commit 9e6c267

Please sign in to comment.