Skip to content

Commit

Permalink
improvement: honor repo configuration in migration generator
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Nov 25, 2024
1 parent 9eef1f4 commit 17abe38
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
23 changes: 12 additions & 11 deletions lib/migration_generator/migration_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -865,27 +865,28 @@ defmodule AshPostgres.MigrationGenerator do
end

defp migration_path(opts, repo, tenant? \\ false) do
repo_name = repo_name(repo)
# Copied from ecto's mix task, thanks Ecto ❤️
config = repo.config()
app = Keyword.fetch!(config, :otp_app)

if tenant? do
if opts.tenant_migration_path do
opts.tenant_migration_path
if path = opts.tenant_migration_path || config[:tenant_migrations_path] do
path
else
Path.join([Mix.Project.deps_paths()[app] || File.cwd!(), "priv"])
priv =
config[:priv] || "priv/#{repo |> Module.split() |> List.last() |> Macro.underscore()}"

Application.app_dir(app, Path.join(priv, "tenant_migrations"))
end
|> Path.join(repo_name)
|> Path.join("tenant_migrations")
else
if opts.migration_path do
opts.migration_path
if path = opts.migration_path || config[:tenant_migrations_path] do
path
else
Path.join([Mix.Project.deps_paths()[app] || File.cwd!(), "priv"])
priv =
config[:priv] || "priv/#{repo |> Module.split() |> List.last() |> Macro.underscore()}"

Application.app_dir(app, Path.join(priv, "migrations"))
end
|> Path.join(repo_name)
|> Path.join("migrations")
end
end

Expand Down
6 changes: 3 additions & 3 deletions lib/mix/tasks/ash_postgres.generate_migrations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ defmodule Mix.Tasks.AshPostgres.GenerateMigrations do
Options:
* `domains` - a comma separated list of Domain modules, for which migrations will be generated
* `snapshot-path` - a custom path to store the snapshots, defaults to "priv/resource_snapshots"
* `migration-path` - a custom path to store the migrations, defaults to "priv".
* `snapshot-path` - a custom path to store the snapshots, defaults to "priv/repo_name/resource_snapshots"
* `migration-path` - a custom path to store the migrations, defaults to "priv/repo_name/migrations".
Migrations are stored in a folder for each repo, so `priv/repo_name/migrations`
* `tenant-migration-path` - Same as `migration_path`, except for any tenant specific migrations
* `tenant-migration-path` - Same as `migration_path`, except for tenant-specific migrations
* `drop-columns` - whether or not to drop columns as attributes are removed. See below for more
* `name` -
names the generated migrations, prepending with the timestamp. The default is `migrate_resources_<n>`,
Expand Down
2 changes: 1 addition & 1 deletion lib/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ defmodule AshPostgres.Repo do
Because an `AshPostgres.Repo` is also an `Ecto.Repo`, it has all of the same callbacks.
In the `c:Ecto.Repo.config/0` callback, you can configure the following additional items:
In the `c:Ecto.Repo.init/2` callback, you can configure the following additional items:
- `:tenant_migrations_path` - The path where your tenant migrations are stored (only relevant for a multitenant implementation)
- `:snapshots_path` - The path where the resource snapshots for the migration generator are stored.
Expand Down

0 comments on commit 17abe38

Please sign in to comment.