Skip to content
Merged
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
1 change: 1 addition & 0 deletions documentation/dsls/DSL-AshSqlite.DataLayer.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ end
| [`migration_ignore_attributes`](#sqlite-migration_ignore_attributes){: #sqlite-migration_ignore_attributes } | `list(atom)` | `[]` | A list of attributes that will be ignored when generating migrations. |
| [`table`](#sqlite-table){: #sqlite-table } | `String.t` | | The table to store and read the resource from. If this is changed, the migration generator will not remove the old table. |
| [`polymorphic?`](#sqlite-polymorphic?){: #sqlite-polymorphic? } | `boolean` | `false` | Declares this resource as polymorphic. See the [polymorphic resources guide](/documentation/topics/resources/polymorphic-resources.md) for more. |
| [`strict?`](#sqlite-strict?){: #sqlite-strict? } | `boolean` | `false` | Whether the migration generator should create a [strict table](https://www.sqlite.org/stricttables.html), which enforces types more strictly. |


### sqlite.custom_indexes
Expand Down
7 changes: 7 additions & 0 deletions lib/data_layer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,13 @@ defmodule AshSqlite.DataLayer do
doc: """
Declares this resource as polymorphic. See the [polymorphic resources guide](/documentation/topics/resources/polymorphic-resources.md) for more.
"""
],
strict?: [
type: :boolean,
default: false,
doc: """
Whether the migration generator should create a [strict table](https://www.sqlite.org/stricttables.html), which enforces types more strictly.
"""
]
]
}
Expand Down
5 changes: 5 additions & 0 deletions lib/data_layer/info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,9 @@ defmodule AshSqlite.DataLayer.Info do
def skip_unique_indexes(resource) do
Extension.get_opt(resource, [:sqlite], :skip_unique_indexes, [])
end

@doc "Whether the migration generator should create a strict table"
def strict?(resource) do
Extension.get_opt(resource, [:sqlite], :strict?, false)
end
end
10 changes: 7 additions & 3 deletions lib/migration_generator/migration_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,8 @@ defmodule AshSqlite.MigrationGenerator do

defp group_into_phases(
[
%Operation.CreateTable{table: table, multitenancy: multitenancy} | rest
%Operation.CreateTable{table: table, options: options, multitenancy: multitenancy}
| rest
],
nil,
acc
Expand All @@ -1120,6 +1121,7 @@ defmodule AshSqlite.MigrationGenerator do
%Phase.Create{
table: table,
multitenancy: multitenancy,
options: options,
operations: has_to_be_in_this_phase
},
acc
Expand Down Expand Up @@ -1543,7 +1545,8 @@ defmodule AshSqlite.MigrationGenerator do
%Operation.CreateTable{
table: snapshot.table,
multitenancy: snapshot.multitenancy,
old_multitenancy: empty_snapshot.multitenancy
old_multitenancy: empty_snapshot.multitenancy,
options: [strict?: snapshot.strict?]
}
| acc
])
Expand Down Expand Up @@ -2204,7 +2207,8 @@ defmodule AshSqlite.MigrationGenerator do
repo: AshSqlite.DataLayer.Info.repo(resource),
multitenancy: multitenancy(resource),
base_filter: AshSqlite.DataLayer.Info.base_filter_sql(resource),
has_create_action: has_create_action?(resource)
has_create_action: has_create_action?(resource),
strict?: AshSqlite.DataLayer.Info.strict?(resource)
}

hash =
Expand Down
2 changes: 1 addition & 1 deletion lib/migration_generator/operation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ defmodule AshSqlite.MigrationGenerator.Operation do

defmodule CreateTable do
@moduledoc false
defstruct [:table, :multitenancy, :old_multitenancy]
defstruct [:table, :multitenancy, :old_multitenancy, options: []]
end

defmodule AddAttribute do
Expand Down
11 changes: 8 additions & 3 deletions lib/migration_generator/phase.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ defmodule AshSqlite.MigrationGenerator.Phase do

defmodule Create do
@moduledoc false
defstruct [:table, :multitenancy, operations: [], commented?: false]
defstruct [:table, :multitenancy, operations: [], options: [], commented?: false]

import AshSqlite.MigrationGenerator.Operation.Helper, only: [as_atom: 1]

def up(%{table: table, operations: operations}) do
opts = ""
def up(%{table: table, operations: operations, options: options}) do
opts =
if options[:strict?] do
~s', options: "STRICT"'
else
""
end

"create table(:#{as_atom(table)}, primary_key: false#{opts}) do\n" <>
Enum.map_join(operations, "\n", fn operation -> operation.__struct__.up(operation) end) <>
Expand Down
47 changes: 47 additions & 0 deletions test/migration_generator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,53 @@ defmodule AshSqlite.MigrationGeneratorTest do
end
end

describe "strict table" do
setup do
on_exit(fn ->
File.rm_rf!("test_snapshots_path")
File.rm_rf!("test_migration_path")
end)

defposts do
sqlite do
strict?(true)
end

attributes do
uuid_primary_key(:id)
attribute(:title, :string)
end
end

defdomain([Post])

Mix.shell(Mix.Shell.Process)

AshSqlite.MigrationGenerator.generate(Domain,
snapshot_path: "test_snapshots_path",
migration_path: "test_migration_path",
quiet: true,
format: false,
auto_name: true
)

:ok
end

test "creates the table with the strict option" do
# the snapshot exists and contains valid json
assert File.read!(Path.wildcard("test_snapshots_path/test_repo/posts/*.json"))
|> Jason.decode!(keys: :atoms!)

assert [file] = Path.wildcard("test_migration_path/**/*_migrate_resources*.exs")

file_contents = File.read!(file)

# the migration creates the table
assert file_contents =~ ~s'create table(:posts, primary_key: false, options: "STRICT") do'
end
end

describe "creating follow up migrations" do
setup do
on_exit(fn ->
Expand Down
Loading