Skip to content
Draft
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
7 changes: 4 additions & 3 deletions lib/data_layer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ defmodule AshSqlite.DataLayer do
"For polymorphic resources, configures the on_update behavior of the automatically generated foreign keys to source tables."
],
polymorphic_name: [
type: {:one_of, [:update, :nilify, :nothing, :restrict]},
type: :string,
doc:
"For polymorphic resources, configures the on_update behavior of the automatically generated foreign keys to source tables."
"For polymorphic resources, then index name to use for the foreign key to the source table."
]
]
}
Expand Down Expand Up @@ -460,7 +460,8 @@ defmodule AshSqlite.DataLayer do
other_data_layer = Ash.DataLayer.data_layer(other_resource)

data_layer == other_data_layer and
AshSqlite.DataLayer.Info.repo(resource) == AshSqlite.DataLayer.Info.repo(other_resource)
AshSqlite.DataLayer.Info.repo(resource, :read) ==
AshSqlite.DataLayer.Info.repo(other_resource, :read)
end

def can?(_resource, {:lateral_join, _}) do
Expand Down
24 changes: 23 additions & 1 deletion lib/data_layer/info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,28 @@ defmodule AshSqlite.DataLayer.Info do
defp resource_module(dsl_state),
do: Spark.Dsl.Transformer.get_persisted(dsl_state, :module)

@doc "Checks a version requirement against the resource's repo's sqlite version"
def sqlite_version_matches?(resource, requirement) do
resource
|> min_sqlite_version()
|> Version.match?(requirement)
end

@doc "Gets the resource's repo's sqlite version"
def min_sqlite_version(resource) do
case repo(resource, :read).min_sqlite_version() do
%Version{} = version ->
version

string when is_binary(string) ->
IO.warn(
"Got a `string` for min_sqlite_version, expected a `Version` struct. Got: #{inspect(string)}. Please call `Version.parse!` before returning the value."
)

Version.parse!(string)
end
end

@doc "The configured table for a resource"
def table(resource) do
Extension.get_opt(resource, [:sqlite], :table, nil, true)
Expand Down Expand Up @@ -120,7 +142,7 @@ defmodule AshSqlite.DataLayer.Info do

@doc "The configured polymorphic_reference_name for a resource"
def polymorphic_name(resource) do
Extension.get_opt(resource, [:sqlite, :references], :polymorphic_on_delete, nil, true)
Extension.get_opt(resource, [:sqlite, :references], :polymorphic_name, nil, true)
end

@doc "The configured polymorphic? for a resource"
Expand Down
10 changes: 8 additions & 2 deletions lib/functions/ilike.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ defmodule AshSqlite.Functions.ILike do
Maps to the builtin sqlite function `ilike`.
"""

use Ash.Query.Function, name: :ilike
use Ash.Query.Function, name: :ilike, predicate?: true

def args, do: [[:string, :string]]
def args,
do: [
[:string, :string],
[:string, :ci_string],
[:ci_string, :string],
[:ci_string, :ci_string]
]
end
10 changes: 8 additions & 2 deletions lib/functions/like.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ defmodule AshSqlite.Functions.Like do
Maps to the builtin sqlite function `like`.
"""

use Ash.Query.Function, name: :like
use Ash.Query.Function, name: :like, predicate?: true

def args, do: [[:string, :string]]
def args,
do: [
[:string, :string],
[:string, :ci_string],
[:ci_string, :string],
[:ci_string, :ci_string]
]
end
14 changes: 7 additions & 7 deletions lib/migration_generator/migration_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ defmodule AshSqlite.MigrationGenerator do
all_resources
|> Enum.filter(fn resource ->
Ash.DataLayer.data_layer(resource) == AshSqlite.DataLayer &&
AshSqlite.DataLayer.Info.repo(resource) == repo &&
AshSqlite.DataLayer.Info.repo(resource, :mutate) == repo &&
(is_nil(only_resources) || resource in only_resources)
end)
|> Enum.flat_map(&get_snapshots(&1, all_resources))
Expand Down Expand Up @@ -2159,7 +2159,7 @@ defmodule AshSqlite.MigrationGenerator do
defp pad(i), do: to_string(i)

def get_snapshots(resource, all_resources) do
Code.ensure_compiled!(AshSqlite.DataLayer.Info.repo(resource))
Code.ensure_compiled!(AshSqlite.DataLayer.Info.repo(resource, :mutate))

if AshSqlite.DataLayer.Info.polymorphic?(resource) do
all_resources
Expand Down Expand Up @@ -2200,7 +2200,7 @@ defmodule AshSqlite.MigrationGenerator do
default(
source_attribute,
relationship.destination,
AshSqlite.DataLayer.Info.repo(relationship.destination)
AshSqlite.DataLayer.Info.repo(relationship.destination, :mutate)
),
deferrable: false,
match_tenant?: false,
Expand Down Expand Up @@ -2232,7 +2232,7 @@ defmodule AshSqlite.MigrationGenerator do
table: table || AshSqlite.DataLayer.Info.table(resource),
custom_indexes: custom_indexes(resource),
custom_statements: custom_statements(resource),
repo: AshSqlite.DataLayer.Info.repo(resource),
repo: AshSqlite.DataLayer.Info.repo(resource, :mutate),
multitenancy: multitenancy(resource),
base_filter: AshSqlite.DataLayer.Info.base_filter_sql(resource),
has_create_action: has_create_action?(resource),
Expand Down Expand Up @@ -2282,7 +2282,7 @@ defmodule AshSqlite.MigrationGenerator do
end

defp attributes(resource, table) do
repo = AshSqlite.DataLayer.Info.repo(resource)
repo = AshSqlite.DataLayer.Info.repo(resource, :mutate)
ignored = AshSqlite.DataLayer.Info.migration_ignore_attributes(resource) || []

resource
Expand Down Expand Up @@ -2555,8 +2555,8 @@ defmodule AshSqlite.MigrationGenerator do

defp foreign_key?(relationship) do
Ash.DataLayer.data_layer(relationship.source) == AshSqlite.DataLayer &&
AshSqlite.DataLayer.Info.repo(relationship.source) ==
AshSqlite.DataLayer.Info.repo(relationship.destination)
AshSqlite.DataLayer.Info.repo(relationship.source, :mutate) ==
AshSqlite.DataLayer.Info.repo(relationship.destination, :mutate)
end

defp identities(resource) do
Expand Down
4 changes: 3 additions & 1 deletion lib/mix/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ defmodule AshSqlite.Mix.Helpers do
end

resources
|> Enum.map(&AshSqlite.DataLayer.Info.repo/1)
|> Enum.flat_map(
&[AshSqlite.DataLayer.Info.repo(&1, :read), AshSqlite.DataLayer.Info.repo(&1, :mutate)]
)
|> Enum.uniq()
|> case do
[] ->
Expand Down
5 changes: 4 additions & 1 deletion test/migration_generator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,10 @@ defmodule AshSqlite.MigrationGeneratorTest do
Enum.sort(Path.wildcard("#{migration_path}/**/*_migrate_resources*.exs"))

assert File.read!(file2) =~
~S[ALTER INDEX posts_title_index RENAME TO titles_r_unique_dawg]
"""
drop_if_exists unique_index(:posts, [:title], name: "posts_title_index")
create unique_index(:posts, [:title], name: "titles_r_unique_dawg")
"""
end

test "when adding a field, it adds the field", %{
Expand Down
Loading