diff --git a/lib/data_layer.ex b/lib/data_layer.ex index 61141e3..9525c78 100644 --- a/lib/data_layer.ex +++ b/lib/data_layer.ex @@ -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." ] ] } @@ -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 diff --git a/lib/data_layer/info.ex b/lib/data_layer/info.ex index 98ea859..e77781c 100644 --- a/lib/data_layer/info.ex +++ b/lib/data_layer/info.ex @@ -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) @@ -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" diff --git a/lib/functions/ilike.ex b/lib/functions/ilike.ex index c87cc54..c0554b2 100644 --- a/lib/functions/ilike.ex +++ b/lib/functions/ilike.ex @@ -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 diff --git a/lib/functions/like.ex b/lib/functions/like.ex index 978c8a5..d728a80 100644 --- a/lib/functions/like.ex +++ b/lib/functions/like.ex @@ -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 diff --git a/lib/migration_generator/migration_generator.ex b/lib/migration_generator/migration_generator.ex index 067a445..0c5b3dc 100644 --- a/lib/migration_generator/migration_generator.ex +++ b/lib/migration_generator/migration_generator.ex @@ -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)) @@ -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 @@ -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, @@ -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), @@ -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 @@ -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 diff --git a/lib/mix/helpers.ex b/lib/mix/helpers.ex index 78b16c3..e5a929d 100644 --- a/lib/mix/helpers.ex +++ b/lib/mix/helpers.ex @@ -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 [] -> diff --git a/test/migration_generator_test.exs b/test/migration_generator_test.exs index 7c1d781..4301661 100644 --- a/test/migration_generator_test.exs +++ b/test/migration_generator_test.exs @@ -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", %{