Skip to content

Commit

Permalink
Fix/create table options list guard (#69)
Browse files Browse the repository at this point in the history
* fix: Fix bad error when creating a table with  option
* test: Add test for succesful options usage
  • Loading branch information
Étienne Lévesque authored May 15, 2022
1 parent 69984fa commit 95d4d8c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/ecto/adapters/sqlite3/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ defmodule Ecto.Adapters.SQLite3.Connection do
##

@impl true
def execute_ddl({_command, %Table{options: keyword}, _}) when keyword != nil do
def execute_ddl({_command, %Table{options: options}, _}) when is_list(options) do
raise ArgumentError, "SQLite3 adapter does not support keyword lists in :options"
end

Expand Down Expand Up @@ -1624,7 +1624,7 @@ defmodule Ecto.Adapters.SQLite3.Connection do

defp options_expr(nil), do: []

defp options_expr(keyword) when is_list(keyword) do
defp options_expr(options) when is_list(options) do
raise ArgumentError, "SQLite3 adapter does not support keyword lists in :options"
end

Expand Down
19 changes: 18 additions & 1 deletion test/ecto/adapters/sqlite3/connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2233,11 +2233,28 @@ defmodule Ecto.Adapters.SQLite3.ConnectionTest do
end

test "create table with options" do
create =
{:create, table(:posts, options: "WITH FOO=BAR"),
[
{:add, :id, :serial, [primary_key: true]}
]}

assert execute_ddl(create) == [
"""
CREATE TABLE "posts" (\
"id" INTEGER PRIMARY KEY AUTOINCREMENT\
) \
WITH FOO=BAR\
"""
]
end

test "create table with list as options" do
assert_raise(
ArgumentError,
"SQLite3 adapter does not support keyword lists in :options",
fn ->
{:create, table(:posts, options: "WITH FOO=BAR"),
{:create, table(:posts, options: ["WITH FOO=BAR"]),
[{:add, :id, :serial, [primary_key: true]}, {:add, :created_at, :datetime, []}]}
|> execute_ddl()
end
Expand Down

0 comments on commit 95d4d8c

Please sign in to comment.