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 @@ -462,7 +462,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
41 changes: 22 additions & 19 deletions lib/migration_generator/migration_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,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 @@ -2157,7 +2157,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 @@ -2198,7 +2198,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 @@ -2230,7 +2230,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 @@ -2280,7 +2280,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 @@ -2553,8 +2553,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 Expand Up @@ -2723,7 +2723,7 @@ defmodule AshSqlite.MigrationGenerator do
|> Map.update!(:custom_indexes, &load_custom_indexes/1)
|> Map.put_new(:custom_statements, [])
|> Map.update!(:custom_statements, &load_custom_statements/1)
|> Map.update!(:repo, &String.to_atom/1)
|> Map.update!(:repo, &maybe_to_atom/1)
|> Map.put_new(:multitenancy, %{
attribute: nil,
strategy: nil,
Expand All @@ -2744,14 +2744,14 @@ defmodule AshSqlite.MigrationGenerator do

defp load_custom_statements(statements) do
Enum.map(statements || [], fn statement ->
Map.update!(statement, :name, &String.to_atom/1)
Map.update!(statement, :name, &maybe_to_atom/1)
end)
end

defp load_multitenancy(multitenancy) do
multitenancy
|> Map.update!(:strategy, fn strategy -> strategy && String.to_atom(strategy) end)
|> Map.update!(:attribute, fn attribute -> attribute && String.to_atom(attribute) end)
|> Map.update!(:strategy, fn strategy -> strategy && maybe_to_atom(strategy) end)
|> Map.update!(:attribute, fn attribute -> attribute && maybe_to_atom(attribute) end)
end

defp load_attribute(attribute, table) do
Expand All @@ -2774,9 +2774,9 @@ defmodule AshSqlite.MigrationGenerator do

attribute =
if Map.has_key?(attribute, :name) do
Map.put(attribute, :source, String.to_atom(attribute.name))
Map.put(attribute, :source, maybe_to_atom(attribute.name))
else
Map.update!(attribute, :source, &String.to_atom/1)
Map.update!(attribute, :source, &maybe_to_atom/1)
end

attribute
Expand All @@ -2803,7 +2803,7 @@ defmodule AshSqlite.MigrationGenerator do
)
|> Map.delete(:ignore)
|> rewrite(:ignore?, :ignore)
|> Map.update!(:destination_attribute, &String.to_atom/1)
|> Map.update!(:destination_attribute, &maybe_to_atom/1)
|> Map.put_new(:deferrable, false)
|> Map.update!(:deferrable, fn
"initially" -> :initially
Expand All @@ -2814,8 +2814,8 @@ defmodule AshSqlite.MigrationGenerator do
|> Map.put_new(:match_tenant?, false)
|> Map.put_new(:on_delete, nil)
|> Map.put_new(:on_update, nil)
|> Map.update!(:on_delete, &(&1 && String.to_atom(&1)))
|> Map.update!(:on_update, &(&1 && String.to_atom(&1)))
|> Map.update!(:on_delete, &(&1 && maybe_to_atom(&1)))
|> Map.update!(:on_update, &(&1 && maybe_to_atom(&1)))
|> Map.put(
:name,
Map.get(references, :name) || "#{table}_#{attribute.source}_fkey"
Expand Down Expand Up @@ -2871,15 +2871,15 @@ defmodule AshSqlite.MigrationGenerator do
end

defp load_type(type) do
String.to_atom(type)
maybe_to_atom(type)
end

defp load_identity(identity, table) do
identity
|> Map.update!(:name, &String.to_atom/1)
|> Map.update!(:name, &maybe_to_atom/1)
|> Map.update!(:keys, fn keys ->
keys
|> Enum.map(&String.to_atom/1)
|> Enum.map(&maybe_to_atom/1)
|> Enum.sort()
end)
|> add_index_name(table)
Expand All @@ -2889,4 +2889,7 @@ defmodule AshSqlite.MigrationGenerator do
defp add_index_name(%{name: name} = index, table) do
Map.put_new(index, :index_name, "#{table}_#{name}_unique_index")
end

defp maybe_to_atom(value) when is_atom(value), do: value
defp maybe_to_atom(value), do: String.to_atom(value)
end
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
12 changes: 5 additions & 7 deletions test/migration_generator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -424,13 +424,11 @@ defmodule AshSqlite.MigrationGeneratorTest do
assert [_file1, file2] =
Enum.sort(Path.wildcard("#{migration_path}/**/*_migrate_resources*.exs"))

contents = File.read!(file2)

assert contents =~
~S|drop_if_exists unique_index(:posts, [:title], name: "posts_title_index")|

assert contents =~
~S|create unique_index(:posts, [:title], name: "titles_r_unique_dawg")|
assert File.read!(file2) =~
"""
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