Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdb8255e

Browse files
committed
improvement: support a 2 argument function for the repo option
1 parentcae89d8 commitdb8255e

File tree

10 files changed

+122
-107
lines changed

10 files changed

+122
-107
lines changed

‎documentation/dsls/DSL:-AshPostgres.DataLayer.cheatmd‎renamed to ‎documentation/dsls/DSL:-AshPostgres.DataLayer.md‎

Lines changed: 64 additions & 70 deletions
Large diffs are not rendered by default.

‎lib/data_layer.ex‎

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,10 @@ defmodule AshPostgres.DataLayer do
269269
],
270270
schema:[
271271
repo:[
272-
type::atom,
272+
type:{:or,[{:behaviour,Ecto.Repo},{:fun,2}]},
273273
required:true,
274274
doc:
275-
"The repo that will be used to fetch your data. See the `AshPostgres.Repo` documentation for more"
275+
"The repo that will be used to fetch your data. See the `AshPostgres.Repo` documentation for more. Can also be a function that takes a resource and a type `:read | :mutate` and returns the repo"
276276
],
277277
migrate?:[
278278
type::boolean,
@@ -447,17 +447,18 @@ defmodule AshPostgres.DataLayer do
447447
other_data_layer=Ash.DataLayer.data_layer(other_resource)
448448

449449
data_layer==other_data_layerand
450-
AshPostgres.DataLayer.Info.repo(resource)==AshPostgres.DataLayer.Info.repo(other_resource)
450+
AshPostgres.DataLayer.Info.repo(resource,:read)==
451+
AshPostgres.DataLayer.Info.repo(other_resource,:read)
451452
end
452453

453454
defcan?(resource,{:lateral_join,resources})do
454-
repo=AshPostgres.DataLayer.Info.repo(resource)
455+
repo=AshPostgres.DataLayer.Info.repo(resource,:read)
455456
data_layer=Ash.DataLayer.data_layer(resource)
456457

457458
data_layer==__MODULE__&&
458459
Enum.all?(resources,fnresource->
459460
Ash.DataLayer.data_layer(resource)==data_layer&&
460-
AshPostgres.DataLayer.Info.repo(resource)==repo
461+
AshPostgres.DataLayer.Info.repo(resource,:read)==repo
461462
end)
462463
end
463464

@@ -510,7 +511,7 @@ defmodule AshPostgres.DataLayer do
510511

511512
@impltrue
512513
defin_transaction?(resource)do
513-
AshPostgres.DataLayer.Info.repo(resource).in_transaction?()
514+
AshPostgres.DataLayer.Info.repo(resource,:mutate).in_transaction?()
514515
end
515516

516517
@impltrue
@@ -683,7 +684,7 @@ defmodule AshPostgres.DataLayer do
683684

684685
@impltrue
685686
deffunctions(resource)do
686-
config=AshPostgres.DataLayer.Info.repo(resource).config()
687+
config=AshPostgres.DataLayer.Info.repo(resource,:mutate).config()
687688

688689
functions=[
689690
AshPostgres.Functions.Fragment,
@@ -1131,7 +1132,7 @@ defmodule AshPostgres.DataLayer do
11311132

11321133
@docfalse
11331134
defset_subquery_prefix(data_layer_query,source_query,resource)do
1134-
config=AshPostgres.DataLayer.Info.repo(resource).config()
1135+
config=AshPostgres.DataLayer.Info.repo(resource,:mutate).config()
11351136

11361137
ifAsh.Resource.Info.multitenancy_strategy(resource)==:contextdo
11371138
%{
@@ -1357,7 +1358,7 @@ defmodule AshPostgres.DataLayer do
13571358

13581359
AshPostgres.MultiTenancy.create_tenant!(
13591360
tenant_name,
1360-
AshPostgres.DataLayer.Info.repo(resource)
1361+
AshPostgres.DataLayer.Info.repo(resource,:read)
13611362
)
13621363
else
13631364
:ok
@@ -1378,7 +1379,7 @@ defmodule AshPostgres.DataLayer do
13781379
new_tenant_name=tenant_name(resource,result)
13791380

13801381
AshPostgres.MultiTenancy.rename_tenant(
1381-
AshPostgres.DataLayer.Info.repo(resource),
1382+
AshPostgres.DataLayer.Info.repo(resource,:read),
13821383
old_tenant_name,
13831384
new_tenant_name
13841385
)
@@ -2621,7 +2622,7 @@ defmodule AshPostgres.DataLayer do
26212622
repo
26222623

26232624
_->
2624-
AshPostgres.DataLayer.Info.repo(resource)
2625+
AshPostgres.DataLayer.Info.repo(resource,:read)
26252626
end
26262627

26272628
func=fn->
@@ -2638,7 +2639,7 @@ defmodule AshPostgres.DataLayer do
26382639

26392640
@impltrue
26402641
defrollback(resource,term)do
2641-
AshPostgres.DataLayer.Info.repo(resource).rollback(term)
2642+
AshPostgres.DataLayer.Info.repo(resource,:mutate).rollback(term)
26422643
end
26432644

26442645
defptable(resource,changeset)do
@@ -2661,14 +2662,25 @@ defmodule AshPostgres.DataLayer do
26612662
end
26622663

26632664
defpdynamic_repo(resource,%{__ash_bindings__:%{context:%{data_layer:%{repo:repo}}}})do
2664-
repo||AshPostgres.DataLayer.Info.repo(resource)
2665+
repo||AshPostgres.DataLayer.Info.repo(resource,:read)
26652666
end
26662667

2667-
defpdynamic_repo(resource,%{context:%{data_layer:%{repo:repo}}})do
2668-
repo||AshPostgres.DataLayer.Info.repo(resource)
2668+
defpdynamic_repo(resource,%struct{context:%{data_layer:%{repo:repo}}})do
2669+
type=struct_to_repo_type(struct)
2670+
2671+
repo||AshPostgres.DataLayer.Info.repo(resource,type)
2672+
end
2673+
2674+
defpdynamic_repo(resource,%struct{})do
2675+
AshPostgres.DataLayer.Info.repo(resource,struct_to_repo_type(struct))
26692676
end
26702677

2671-
defpdynamic_repo(resource,_)do
2672-
AshPostgres.DataLayer.Info.repo(resource)
2678+
defpstruct_to_repo_type(struct)do
2679+
casestructdo
2680+
Ash.Changeset->:mutate
2681+
Ash.Query->:read
2682+
Ecto.Query->:read
2683+
Ecto.Changeset->:mutate
2684+
end
26732685
end
26742686
end

‎lib/data_layer/info.ex‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@ defmodule AshPostgres.DataLayer.Info do
44
aliasSpark.Dsl.Extension
55

66
@doc"The configured repo for a resource"
7-
defrepo(resource)do
8-
Extension.get_opt(resource,[:postgres],:repo,nil,true)
7+
defrepo(resource,type\\:mutate)do
8+
caseExtension.get_opt(resource,[:postgres],:repo,nil,true)do
9+
funwhenis_function(fun,2)->
10+
fun.(resource,type)
11+
12+
repo->
13+
repo
14+
end
915
end
1016

1117
@doc"The configured table for a resource"

‎lib/expr.ex‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ defmodule AshPostgres.Expr do
251251
embedded?,
252252
type
253253
)do
254-
if"citext"inAshPostgres.DataLayer.Info.repo(query.__ash_bindings__.resource).installed_extensions()do
254+
if"citext"inAshPostgres.DataLayer.Info.repo(query.__ash_bindings__.resource,:mutate).installed_extensions()do
255255
do_dynamic_expr(
256256
query,
257257
%Fragment{
@@ -1528,7 +1528,7 @@ defmodule AshPostgres.Expr do
15281528

15291529
defprequire_ash_functions!(query,operator)do
15301530
installed_extensions=
1531-
AshPostgres.DataLayer.Info.repo(query.__ash_bindings__.resource).installed_extensions()
1531+
AshPostgres.DataLayer.Info.repo(query.__ash_bindings__.resource,:mutate).installed_extensions()
15321532

15331533
unless"ash-functions"ininstalled_extensionsdo
15341534
raise"""
@@ -1540,7 +1540,7 @@ defmodule AshPostgres.Expr do
15401540
end
15411541

15421542
defprequire_extension!(query,extension,context)do
1543-
repo=AshPostgres.DataLayer.Info.repo(query.__ash_bindings__.resource)
1543+
repo=AshPostgres.DataLayer.Info.repo(query.__ash_bindings__.resource,:mutate)
15441544

15451545
unlessextensioninrepo.installed_extensions()do
15461546
raiseAsh.Error.Query.InvalidExpression,

‎lib/join.ex‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ defmodule AshPostgres.Join do
386386
join_query
387387
|prefix:
388388
AshPostgres.DataLayer.Info.schema(resource)||
389-
AshPostgres.DataLayer.Info.repo(resource).config()[:default_prefix]||
389+
AshPostgres.DataLayer.Info.repo(resource,:mutate).config()[:default_prefix]||
390390
"public"
391391
}
392392
end

‎lib/migration_generator/migration_generator.ex‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ defmodule AshPostgres.MigrationGenerator do
6666
all_resources
6767
|>Enum.filter(fnresource->
6868
Ash.DataLayer.data_layer(resource)==AshPostgres.DataLayer&&
69-
AshPostgres.DataLayer.Info.repo(resource)==repo&&
69+
AshPostgres.DataLayer.Info.repo(resource,:mutate)==repo&&
7070
(is_nil(only_resources)||resourceinonly_resources)
7171
end)
7272
|>Enum.flat_map(&get_snapshots(&1,all_resources))
@@ -2415,7 +2415,7 @@ defmodule AshPostgres.MigrationGenerator do
24152415
defppad(i),do:to_string(i)
24162416

24172417
defget_snapshots(resource,all_resources)do
2418-
Code.ensure_compiled!(AshPostgres.DataLayer.Info.repo(resource))
2418+
Code.ensure_compiled!(AshPostgres.DataLayer.Info.repo(resource,:mutate))
24192419

24202420
ifAshPostgres.DataLayer.Info.polymorphic?(resource)do
24212421
all_resources
@@ -2459,7 +2459,7 @@ defmodule AshPostgres.MigrationGenerator do
24592459
default(
24602460
source_attribute,
24612461
relationship.destination,
2462-
AshPostgres.DataLayer.Info.repo(relationship.destination)
2462+
AshPostgres.DataLayer.Info.repo(relationship.destination,:mutate)
24632463
),
24642464
deferrable:false,
24652465
destination_attribute_generated:source_attribute.generated?,
@@ -2493,7 +2493,7 @@ defmodule AshPostgres.MigrationGenerator do
24932493
check_constraints:check_constraints(resource),
24942494
custom_indexes:custom_indexes(resource),
24952495
custom_statements:custom_statements(resource),
2496-
repo:AshPostgres.DataLayer.Info.repo(resource),
2496+
repo:AshPostgres.DataLayer.Info.repo(resource,:mutate),
24972497
multitenancy:multitenancy(resource),
24982498
base_filter:AshPostgres.DataLayer.Info.base_filter_sql(resource),
24992499
has_create_action:has_create_action?(resource)
@@ -2584,7 +2584,7 @@ defmodule AshPostgres.MigrationGenerator do
25842584
end
25852585

25862586
defpattributes(resource,table)do
2587-
repo=AshPostgres.DataLayer.Info.repo(resource)
2587+
repo=AshPostgres.DataLayer.Info.repo(resource,:mutate)
25882588
ignored=AshPostgres.DataLayer.Info.migration_ignore_attributes(resource)||[]
25892589

25902590
resource
@@ -2680,7 +2680,7 @@ defmodule AshPostgres.MigrationGenerator do
26802680
schema:
26812681
relationship.context[:data_layer][:schema]||
26822682
AshPostgres.DataLayer.Info.schema(relationship.destination)||
2683-
AshPostgres.DataLayer.Info.repo(relationship.destination).config()[
2683+
AshPostgres.DataLayer.Info.repo(relationship.destination,:mutate).config()[
26842684
:default_prefix
26852685
],
26862686
table:
@@ -2704,7 +2704,9 @@ defmodule AshPostgres.MigrationGenerator do
27042704
schema:
27052705
relationship.context[:data_layer][:schema]||
27062706
AshPostgres.DataLayer.Info.schema(relationship.destination)||
2707-
AshPostgres.DataLayer.Info.repo(relationship.destination).config()[:default_prefix],
2707+
AshPostgres.DataLayer.Info.repo(relationship.destination,:mutate).config()[
2708+
:default_prefix
2709+
],
27082710
name:nil,
27092711
ignore?:false
27102712
})
@@ -2741,8 +2743,8 @@ defmodule AshPostgres.MigrationGenerator do
27412743

27422744
defpforeign_key?(relationship)do
27432745
Ash.DataLayer.data_layer(relationship.source)==AshPostgres.DataLayer&&
2744-
AshPostgres.DataLayer.Info.repo(relationship.source)==
2745-
AshPostgres.DataLayer.Info.repo(relationship.destination)
2746+
AshPostgres.DataLayer.Info.repo(relationship.source,:mutate)==
2747+
AshPostgres.DataLayer.Info.repo(relationship.destination,:mutate)
27462748
end
27472749

27482750
defpidentities(resource)do

‎lib/mix/helpers.ex‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ defmodule AshPostgres.MixHelpers do
5757
end
5858

5959
resources
60-
|>Enum.map(&AshPostgres.DataLayer.Info.repo(&1))
60+
|>Enum.flat_map(
61+
&[AshPostgres.DataLayer.Info.repo(&1,:read),AshPostgres.DataLayer.Info.repo(&1,:mutate)]
62+
)
6163
|>Enum.uniq()
6264
|>casedo
6365
[]->

‎mix.exs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ defmodule AshPostgres.MixProject do
241241
"sobelow --skip -i Config.Secrets --ignore-files lib/migration_generator/migration_generator.ex",
242242
credo:"credo --strict",
243243
docs:[
244-
"spark.cheat_sheets",
244+
#"spark.cheat_sheets",
245245
"docs",
246246
"spark.replace_doc_links",
247247
"spark.cheat_sheets_in_search"

‎mix.lock‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
%{
2-
"ash":{:hex,:ash,"2.15.20","0702181ca817ab1cad5e3ccb53851c56a9c8ec0a010780ad7eb881a997151e38",[:mix],[{:comparable,"~> 1.0",[hex::comparable,repo:"hexpm",optional:false]},{:decimal,"~> 2.0",[hex::decimal,repo:"hexpm",optional:false]},{:earmark,"~> 1.4",[hex::earmark,repo:"hexpm",optional:false]},{:ecto,"~> 3.7",[hex::ecto,repo:"hexpm",optional:false]},{:ets,"~> 0.8",[hex::ets,repo:"hexpm",optional:false]},{:jason,">= 1.0.0",[hex::jason,repo:"hexpm",optional:false]},{:picosat_elixir,"~> 0.2",[hex::picosat_elixir,repo:"hexpm",optional:false]},{:plug,">= 0.0.0",[hex::plug,repo:"hexpm",optional:true]},{:spark,">= 1.1.47 and < 2.0.0-0",[hex::spark,repo:"hexpm",optional:false]},{:stream_data,"~> 0.6",[hex::stream_data,repo:"hexpm",optional:false]},{:telemetry,"~> 1.1",[hex::telemetry,repo:"hexpm",optional:false]}],"hexpm","efa3afe081ed22f1c7d6dc45de13b8b80f8ab831ebaa255108aad11cacd23b13"},
2+
"ash":{:hex,:ash,"2.17.1","728a917baab9599b4bed4156009b3465fa5a24a9511dab212894b044d13d1a36",[:mix],[{:comparable,"~> 1.0",[hex::comparable,repo:"hexpm",optional:false]},{:decimal,"~> 2.0",[hex::decimal,repo:"hexpm",optional:false]},{:earmark,"~> 1.4",[hex::earmark,repo:"hexpm",optional:false]},{:ecto,"~> 3.7",[hex::ecto,repo:"hexpm",optional:false]},{:ets,"~> 0.8",[hex::ets,repo:"hexpm",optional:false]},{:jason,">= 1.0.0",[hex::jason,repo:"hexpm",optional:false]},{:picosat_elixir,"~> 0.2",[hex::picosat_elixir,repo:"hexpm",optional:false]},{:plug,">= 0.0.0",[hex::plug,repo:"hexpm",optional:true]},{:spark,">= 1.1.50 and < 2.0.0-0",[hex::spark,repo:"hexpm",optional:false]},{:stream_data,"~> 0.6",[hex::stream_data,repo:"hexpm",optional:false]},{:telemetry,"~> 1.1",[hex::telemetry,repo:"hexpm",optional:false]}],"hexpm","d45d8c6a9c99a0088ec5451692e46e03556d00575bfe00497a2e32f90163763f"},
33
"benchee":{:hex,:benchee,"1.1.0","f3a43817209a92a1fade36ef36b86e1052627fd8934a8b937ac9ab3a76c43062",[:mix],[{:deep_merge,"~> 1.0",[hex::deep_merge,repo:"hexpm",optional:false]},{:statistex,"~> 1.0",[hex::statistex,repo:"hexpm",optional:false]}],"hexpm","7da57d545003165a012b587077f6ba90b89210fd88074ce3c60ce239eb5e6d93"},
44
"bunt":{:hex,:bunt,"0.2.0","951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38",[:mix],[],"hexpm","7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"},
55
"certifi":{:hex,:certifi,"2.9.0","6f2a475689dd47f19fb74334859d460a2dc4e3252a3324bd2111b8f0429e7e21",[:rebar3],[],"hexpm","266da46bdb06d6c6d35fde799bcb28d36d985d424ad7c08b5bb48f5b5cdd4641"},
@@ -37,7 +37,7 @@
3737
"postgrex":{:hex,:postgrex,"0.17.3","c92cda8de2033a7585dae8c61b1d420a1a1322421df84da9a82a6764580c503d",[:mix],[{:db_connection,"~> 2.1",[hex::db_connection,repo:"hexpm",optional:false]},{:decimal,"~> 1.5 or ~> 2.0",[hex::decimal,repo:"hexpm",optional:false]},{:jason,"~> 1.0",[hex::jason,repo:"hexpm",optional:true]},{:table,"~> 0.1.0",[hex::table,repo:"hexpm",optional:true]}],"hexpm","946cf46935a4fdca7a81448be76ba3503cff082df42c6ec1ff16a4bdfbfb098d"},
3838
"sobelow":{:hex,:sobelow,"0.11.1","23438964486f8112b41e743bbfd402da3e5b296fdc9eacab29914b79c48916dd",[:mix],[{:jason,"~> 1.0",[hex::jason,repo:"hexpm",optional:false]}],"hexpm","9897363a7eff96f4809304a90aad819e2ad5e5d24db547af502885146746a53c"},
3939
"sourceror":{:hex,:sourceror,"0.14.1","c6fb848d55bd34362880da671debc56e77fd722fa13b4dcbeac89a8998fc8b09",[:mix],[],"hexpm","8b488a219e4c4d7d9ff29d16346fd4a5858085ccdd010e509101e226bbfd8efc"},
40-
"spark":{:hex,:spark,"1.1.50","809da1214151ad7c592389b3ea85eb4424f727b681b90439d8ffbe5305400ce9",[:mix],[{:jason,"~> 1.4",[hex::jason,repo:"hexpm",optional:false]},{:nimble_options,"~> 0.5 or ~> 1.0",[hex::nimble_options,repo:"hexpm",optional:false]},{:sourceror,"~> 0.1",[hex::sourceror,repo:"hexpm",optional:false]}],"hexpm","ed9b1b817b52c3aaeee283032640857ee9d8398b8c4e9e7d78d77929d387b9a1"},
40+
"spark":{:hex,:spark,"1.1.51","8458de5abbb89d18dd5c9235dd39e3757076eba84a5078d1cdc2c1e23c39aa95",[:mix],[{:jason,"~> 1.4",[hex::jason,repo:"hexpm",optional:false]},{:nimble_options,"~> 0.5 or ~> 1.0",[hex::nimble_options,repo:"hexpm",optional:false]},{:sourceror,"~> 0.1",[hex::sourceror,repo:"hexpm",optional:false]}],"hexpm","ed8410aa8db08867b8fff3d65e54deeb7f6f6cf2b8698fc405a386c1c7a9e4f0"},
4141
"ssl_verify_fun":{:hex,:ssl_verify_fun,"1.1.7","354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df",[:make,:mix,:rebar3],[],"hexpm","fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"},
4242
"statistex":{:hex,:statistex,"1.0.0","f3dc93f3c0c6c92e5f291704cf62b99b553253d7969e9a5fa713e5481cd858a5",[:mix],[],"hexpm","ff9d8bee7035028ab4742ff52fc80a2aa35cece833cf5319009b52f1b5a86c27"},
4343
"stream_data":{:hex,:stream_data,"0.6.0","e87a9a79d7ec23d10ff83eb025141ef4915eeb09d4491f79e52f2562b73e5f47",[:mix],[],"hexpm","b92b5031b650ca480ced047578f1d57ea6dd563f5b57464ad274718c9c29501c"},

‎test/sort_test.exs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ defmodule AshPostgres.SortTest do
55

66
requireAsh.Query
77
requireAsh.Sort
8-
importAsh.Expr
98

109
test"multi-column sorts work"do
1110
Post

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp