Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(migration_name): allow migrations with custom table name #124

Merged
merged 3 commits into from
Apr 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,34 @@ end

Now run the migration and you'll be good to go.

### Custom schema name

`Guardian.DB` allows custom schema name in migrations, based on following
configuration:

```elixir
config :guardian, Guardian.DB,
schema_name: "my_custom_schema"
```

And when you run the migration, it'll generate the following migration:

```elixir
create table(:my_custom_schema, primary_key: false) do
add(:jti, :string, primary_key: true)
add(:typ, :string)
add(:aud, :string)
add(:iss, :string)
add(:sub, :string)
add(:exp, :bigint)
add(:jwt, :text)
add(:claims, :map)
timestamps()
end
```

Then, run the migration and you'll be good to go.

### Considerations

`Guardian` is already a very robust JWT solution. However, if your
Expand Down
25 changes: 25 additions & 0 deletions lib/guardian/db.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,31 @@ defmodule Guardian.DB do
end
```

`Guardian.DB` allow to use a custom schema name when creating the migration.
You can configure the schema name from config like the following:

```elixir
config :guardian, Guardian.DB,
schema_name: "my_customy_custom_schema
```

And when you run `mix guardian.db.gen.migration` it'll generate the following
migration:

```elixir
create table(:my_custom_schema, primary_key: false) do
add(:jti, :string, primary_key: true)
add(:typ, :string)
add(:aud, :string)
add(:iss, :string)
add(:sub, :string)
add(:exp, :bigint)
add(:jwt, :text)
add(:claims, :map)
timestamps()
end
```

`Guardian.DB` works by hooking into the lifecycle of your token module.

You'll need to add it to
Expand Down
10 changes: 10 additions & 0 deletions lib/mix/tasks/guardian_db.gen.migration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ defmodule Mix.Tasks.Guardian.Db.Gen.Migration do

@moduledoc """
Generates the required GuardianDb's database migration.

It allows custom schema name, using the config
entry `schema_name`.
"""
use Mix.Task

Expand All @@ -25,9 +28,16 @@ defmodule Mix.Tasks.Guardian.Db.Gen.Migration do
|> Application.app_dir()
|> Path.join("priv/templates/migration.exs.eex")

schema_name =
:guardian
|> Application.fetch_env!(Guardian.DB)
|> Keyword.get(:schema_name, "guardian_tokens")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to add some documentation about it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, where's the best location for it? README or moduledoc?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the most part, moduledoc first and then README

|> String.to_atom()

generated_file =
EEx.eval_file(source_path,
module_prefix: app_module(),
schema_name: schema_name,
db_prefix: Token.prefix()
)

Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ defmodule Guardian.DB.Mixfile do
{:dialyxir, ">= 0.0.0", only: [:dev], runtime: false},
{:credo, ">= 0.0.0", only: [:dev, :test], runtime: false},
{:excoveralls, ">= 0.0.0", only: [:test], runtime: false},
{:temporary_env, ">= 0.0.0", only: [:test], runtime: false},
{:ex_doc, ">= 0.0.0", only: [:dev], runtime: false},
{:inch_ex, ">= 0.0.0", only: [:dev], runtime: false}
]
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.4", "f0eafff810d2041e93f915ef59899c923f4568f4585904d010387ed74988e77b", [:make, :mix, :rebar3], [], "hexpm"},
"telemetry": {:hex, :telemetry, "0.4.1", "ae2718484892448a24470e6aa341bc847c3277bfb8d4e9289f7474d752c09c7f", [:rebar3], [], "hexpm"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm"},
"temporary_env": {:hex, :temporary_env, "2.0.1", "d4b5e031837e5619485e1f23af7cba7e897b8fd546eaaa8b10c812d642ec4546", [:mix], [], "hexpm", "f9420044742b5f0479a7f8243e86b048b6a2d4878bce026a3615065b11199c27"},
}
2 changes: 1 addition & 1 deletion priv/templates/migration.exs.eex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule <%= module_prefix %>.Repo.Migrations.CreateGuardianDBTokensTable do
use Ecto.Migration

def change do
create table(:guardian_tokens, primary_key: false<%= if not is_nil(db_prefix), do: ", prefix: \"#{db_prefix}\"" %>) do
create table(<%= inspect(schema_name) %>, primary_key: false<%= if not is_nil(db_prefix), do: ", prefix: \"#{db_prefix}\"" %>) do
add(:jti, :string, primary_key: true)
add(:aud, :string, primary_key: true)
add(:typ, :string)
Expand Down
17 changes: 17 additions & 0 deletions test/mix/tasks/guardian.db.gen.migration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ defmodule Mix.Tasks.Guardian.Db.Gen.MigrationTest do
import Mix.Tasks.Guardian.Db.Gen.Migration, only: [run: 1]
import Guardian.DB.TestSupport.FileHelpers

require TemporaryEnv

@tmp_path Path.join(tmp_path(), inspect(Guardian.Db.Gen.Migration))
@migrations_path Path.join(@tmp_path, "migrations")

Expand All @@ -27,4 +29,19 @@ defmodule Mix.Tasks.Guardian.Db.Gen.MigrationTest do
assert [name] = File.ls!(@migrations_path)
assert String.match?(name, ~r/^\d{14}_guardiandb\.exs$/)
end

test "generates a new migration with custom name" do
custom_schema_name = "my_custom_guardian_tokens"
value = [schema_name: custom_schema_name]

TemporaryEnv.put :guardian, Guardian.DB, value do
run(["-r", to_string(My.Repo)])
assert [name] = File.ls!(@migrations_path)

path = Path.join(@migrations_path, name)

assert String.match?(name, ~r/^\d{14}_guardiandb\.exs$/)
assert File.read!(path) =~ ":#{custom_schema_name}"
end
end
end