mirror of
https://github.com/ash-project/ash_postgres.git
synced 2024-09-20 05:23:18 +12:00
fix: make migration generator work better for umbrellas
This commit is contained in:
parent
24ba1dbbe4
commit
9d6996be25
4 changed files with 103 additions and 11 deletions
|
@ -1,6 +1,5 @@
|
|||
defmodule AshPostgres.MigrationGenerator do
|
||||
@moduledoc false
|
||||
@default_snapshot_path "priv/resource_snapshots"
|
||||
|
||||
require Logger
|
||||
|
||||
|
@ -8,7 +7,7 @@ defmodule AshPostgres.MigrationGenerator do
|
|||
|
||||
alias AshPostgres.MigrationGenerator.{Operation, Phase}
|
||||
|
||||
defstruct snapshot_path: @default_snapshot_path,
|
||||
defstruct snapshot_path: nil,
|
||||
migration_path: nil,
|
||||
name: nil,
|
||||
tenant_migration_path: nil,
|
||||
|
@ -101,6 +100,17 @@ defmodule AshPostgres.MigrationGenerator do
|
|||
end
|
||||
end
|
||||
|
||||
defp snapshot_path(%{snapshot_path: snapshot_path}, _) when not is_nil(snapshot_path),
|
||||
do: snapshot_path
|
||||
|
||||
defp snapshot_path(config, repo) do
|
||||
# Copied from ecto's mix task, thanks Ecto ❤️
|
||||
config = repo.config()
|
||||
|
||||
app = Keyword.fetch!(config, :otp_app)
|
||||
Path.join([Mix.Project.deps_paths()[app] || File.cwd!(), "priv", "resource_snapshots"])
|
||||
end
|
||||
|
||||
@latest_ash_functions_version 0
|
||||
|
||||
@add_ash_functions """
|
||||
|
@ -143,7 +153,8 @@ defmodule AshPostgres.MigrationGenerator do
|
|||
|
||||
defp create_extension_migrations(repos, opts) do
|
||||
for repo <- repos do
|
||||
snapshot_file = Path.join(opts.snapshot_path, "extensions.json")
|
||||
snapshot_path = snapshot_path(opts, repo)
|
||||
snapshot_file = Path.join(snapshot_path, "extensions.json")
|
||||
|
||||
installed_extensions =
|
||||
if File.exists?(snapshot_file) do
|
||||
|
@ -802,11 +813,13 @@ defmodule AshPostgres.MigrationGenerator do
|
|||
|
||||
snapshot_folder =
|
||||
if tenant? do
|
||||
opts.snapshot_path
|
||||
opts
|
||||
|> snapshot_path(snapshot.repo)
|
||||
|> Path.join(repo_name)
|
||||
|> Path.join("tenants")
|
||||
else
|
||||
opts.snapshot_path
|
||||
opts
|
||||
|> snapshot_path(snapshot.repo)
|
||||
|> Path.join(repo_name)
|
||||
end
|
||||
|
||||
|
@ -1759,11 +1772,13 @@ defmodule AshPostgres.MigrationGenerator do
|
|||
|
||||
folder =
|
||||
if snapshot.multitenancy.strategy == :context do
|
||||
opts.snapshot_path
|
||||
opts
|
||||
|> snapshot_path(snapshot.repo)
|
||||
|> Path.join(repo_name)
|
||||
|> Path.join("tenants")
|
||||
else
|
||||
opts.snapshot_path
|
||||
opts
|
||||
|> snapshot_path(snapshot.repo)
|
||||
|> Path.join(repo_name)
|
||||
end
|
||||
|
||||
|
@ -1800,8 +1815,6 @@ defmodule AshPostgres.MigrationGenerator do
|
|||
end
|
||||
end
|
||||
|
||||
T
|
||||
|
||||
defp get_old_snapshot(folder, snapshot) do
|
||||
old_snapshot_file = Path.join(folder, "#{snapshot.table}.json")
|
||||
# This is adapter code for the old version, where migrations were stored in a flat directory
|
||||
|
@ -2354,6 +2367,7 @@ defmodule AshPostgres.MigrationGenerator do
|
|||
custom_index
|
||||
|> Map.put_new(:fields, [])
|
||||
|> Map.put_new(:include, [])
|
||||
|> Map.put_new(:message, nil)
|
||||
end)
|
||||
end
|
||||
|
||||
|
|
|
@ -876,7 +876,11 @@ defmodule AshPostgres.MigrationGenerator.Operation do
|
|||
option(:prefix, schema)
|
||||
])
|
||||
|
||||
"create index(:#{table}, [#{Enum.map_join(keys, ", ", &inspect/1)}], #{opts})"
|
||||
if opts == "" do
|
||||
"create index(:#{table}, [#{Enum.map_join(keys, ", ", &inspect/1)}])"
|
||||
else
|
||||
"create index(:#{table}, [#{Enum.map_join(keys, ", ", &inspect/1)}], #{opts})"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -600,6 +600,23 @@ defmodule AshPostgres.FilterTest do
|
|||
|> Api.read!()
|
||||
end
|
||||
|
||||
test "it works with join association relationships" do
|
||||
post =
|
||||
Post
|
||||
|> Ash.Changeset.new(%{title: "a"})
|
||||
|> Api.create!()
|
||||
|
||||
Post
|
||||
|> Ash.Changeset.new(%{title: "b"})
|
||||
|> Ash.Changeset.manage_relationship(:linked_posts, [post], type: :append_and_remove)
|
||||
|> Api.create!()
|
||||
|
||||
assert [%{title: "b"}] =
|
||||
Post
|
||||
|> Ash.Query.filter(exists(linked_posts, title == ^"a"))
|
||||
|> Api.read!()
|
||||
end
|
||||
|
||||
test "it works with nested relationships as the path" do
|
||||
post =
|
||||
Post
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
defmodule AshPostgres.SortTest do
|
||||
@moduledoc false
|
||||
use AshPostgres.RepoCase, async: false
|
||||
alias AshPostgres.Test.{Api, Comment, Post}
|
||||
alias AshPostgres.Test.{Api, Comment, Post, PostLink}
|
||||
|
||||
require Ash.Query
|
||||
|
||||
|
@ -104,6 +104,63 @@ defmodule AshPostgres.SortTest do
|
|||
)
|
||||
end
|
||||
|
||||
test "sorting when joining to a many to many relationship sorts properly" do
|
||||
post1 =
|
||||
Post
|
||||
|> Ash.Changeset.new(%{title: "aaa", score: 0})
|
||||
|> Api.create!()
|
||||
|
||||
post2 =
|
||||
Post
|
||||
|> Ash.Changeset.new(%{title: "bbb", score: 1})
|
||||
|> Api.create!()
|
||||
|
||||
post3 =
|
||||
Post
|
||||
|> Ash.Changeset.new(%{title: "ccc", score: 0})
|
||||
|> Api.create!()
|
||||
|
||||
PostLink
|
||||
|> Ash.Changeset.new()
|
||||
|> Ash.Changeset.manage_relationship(:source_post, post1, type: :append)
|
||||
|> Ash.Changeset.manage_relationship(:destination_post, post3, type: :append)
|
||||
|> Api.create!()
|
||||
|
||||
PostLink
|
||||
|> Ash.Changeset.new()
|
||||
|> Ash.Changeset.manage_relationship(:source_post, post2, type: :append)
|
||||
|> Ash.Changeset.manage_relationship(:destination_post, post2, type: :append)
|
||||
|> Api.create!()
|
||||
|
||||
PostLink
|
||||
|> Ash.Changeset.new()
|
||||
|> Ash.Changeset.manage_relationship(:source_post, post3, type: :append)
|
||||
|> Ash.Changeset.manage_relationship(:destination_post, post1, type: :append)
|
||||
|> Api.create!()
|
||||
|
||||
assert [
|
||||
%{title: "aaa"},
|
||||
%{title: "bbb"},
|
||||
%{title: "ccc"}
|
||||
] =
|
||||
Api.read!(
|
||||
Post
|
||||
|> Ash.Query.sort(title: :asc)
|
||||
|> Ash.Query.filter(linked_posts.title in ["aaa", "bbb", "ccc"])
|
||||
)
|
||||
|
||||
assert [
|
||||
%{title: "ccc"},
|
||||
%{title: "bbb"},
|
||||
%{title: "aaa"}
|
||||
] =
|
||||
Api.read!(
|
||||
Post
|
||||
|> Ash.Query.sort(title: :desc)
|
||||
|> Ash.Query.filter(linked_posts.title in ["aaa", "bbb", "ccc"])
|
||||
)
|
||||
end
|
||||
|
||||
test "calculations can be sorted on w/o loading aggregates they reference" do
|
||||
Post
|
||||
|> Ash.Query.load(:count_of_comments)
|
||||
|
|
Loading…
Reference in a new issue