Skip to content
Closed
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: 0 additions & 1 deletion integration_test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ excludes = [
:lock_for_migrations,

# Migration we don't support
:prefix,
:add_column_if_not_exists,
:remove_column_if_exists,
:alter_primary_key,
Expand Down
6 changes: 3 additions & 3 deletions lib/ecto/adapters/sqlite3/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1826,7 +1826,7 @@ defmodule Ecto.Adapters.SQLite3.Connection do
" CONSTRAINT ",
reference_name(ref, table, name),
" REFERENCES ",
quote_table(ref.prefix || table.prefix, ref.table),
quote_table(nil, ref.table),
?(,
quote_name(ref.column),
?),
Expand Down Expand Up @@ -1945,8 +1945,8 @@ defmodule Ecto.Adapters.SQLite3.Connection do

defp quote_table(nil, name), do: quote_entity(name)

defp quote_table(prefix, _name) when is_atom(prefix) or is_binary(prefix) do
raise ArgumentError, "SQLite3 does not support table prefixes"
defp quote_table(prefix, name) when is_atom(prefix) or is_binary(prefix) do
[prefix, ?., name]
end

defp quote_table(_, name), do: quote_entity(name)
Expand Down
12 changes: 6 additions & 6 deletions test/ecto/adapters/sqlite3/connection/delete_all_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,20 @@ defmodule Ecto.Adapters.SQLite3.Connection.DeleteAllTest do
end

test "delete all with prefix" do
assert_raise ArgumentError, "SQLite3 does not support table prefixes", fn ->
query =
Schema
|> Ecto.Queryable.to_query()
|> Map.put(:prefix, "prefix")
|> plan()
|> delete_all()
end

assert_raise ArgumentError, "SQLite3 does not support table prefixes", fn ->
assert ~s{DELETE FROM prefix.schema AS s0} == delete_all(query)

query =
Schema
|> from(prefix: "first")
|> Map.put(:prefix, "prefix")
|> plan()
|> delete_all()
end

assert ~s{DELETE FROM first.schema AS s0} == delete_all(query)
end
end
5 changes: 2 additions & 3 deletions test/ecto/adapters/sqlite3/connection/insert_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ defmodule Ecto.Adapters.SQLite3.Connection.InsertTest do
query = insert(nil, "schema", [], [[]], {:raise, [], []}, [])
assert query == ~s{INSERT INTO "schema" DEFAULT VALUES}

assert_raise ArgumentError, "SQLite3 does not support table prefixes", fn ->
insert("prefix", "schema", [], [[]], {:raise, [], []}, [])
end
query = insert("prefix", "schema", [], [[]], {:raise, [], []}, [])
assert query == ~s{INSERT INTO prefix.schema DEFAULT VALUES}

query = insert(nil, "schema", [:x, :y], [[:x, :y]], {:raise, [], []}, [:id])
assert query == ~s{INSERT INTO "schema" ("x","y") VALUES (?1,?2) RETURNING "id"}
Expand Down
16 changes: 9 additions & 7 deletions test/ecto/adapters/sqlite3/connection/join_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -114,25 +114,27 @@ defmodule Ecto.Adapters.SQLite3.Connection.JoinTest do
~s{) AS s1 ON 1} == all(query)
end

test "join with prefix is not supported" do
assert_raise ArgumentError, "SQLite3 does not support table prefixes", fn ->
test "join with prefix" do
query =
Schema
|> join(:inner, [p], q in Schema2, on: p.x == q.z)
|> select([], true)
|> Map.put(:prefix, "prefix")
|> plan()
|> all()
end

assert_raise ArgumentError, "SQLite3 does not support table prefixes", fn ->
assert ~s{SELECT 1 FROM prefix.schema AS s0 INNER JOIN prefix.schema2 AS s1 ON s0."x" = s1."z"} ==
all(query)

query =
Schema
|> from(prefix: "first")
|> join(:inner, [p], q in Schema2, on: p.x == q.z, prefix: "second")
|> select([], true)
|> Map.put(:prefix, "prefix")
|> plan()
|> all()
end

assert ~s{SELECT 1 FROM first.schema AS s0 INNER JOIN second.schema2 AS s1 ON s0."x" = s1."z"} ==
all(query)
end

test "join with values" do
Expand Down
48 changes: 21 additions & 27 deletions test/ecto/adapters/sqlite3/connection/migration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ defmodule Ecto.Adapters.SQLite3.Connection.MigrationTest do
{:create, table(:posts, prefix: :foo),
[{:add, :category_0, %Reference{table: :categories}, []}]}

assert_raise ArgumentError, "SQLite3 does not support table prefixes", fn ->
execute_ddl(create)
end
assert execute_ddl(create) == [
"""
CREATE TABLE foo.posts ("category_0" INTEGER CONSTRAINT "posts_category_0_fkey" REFERENCES foo.categories("id"))
"""
|> remove_newlines
]
end

test "create table with references" do
Expand Down Expand Up @@ -295,10 +298,7 @@ defmodule Ecto.Adapters.SQLite3.Connection.MigrationTest do

test "drop table with prefix" do
drop = {:drop, table(:posts, prefix: :foo)}

assert_raise ArgumentError, "SQLite3 does not support table prefixes", fn ->
execute_ddl(drop)
end
assert execute_ddl(drop) == [~s|DROP TABLE foo.posts|]
end

test "alter table" do
Expand Down Expand Up @@ -328,9 +328,9 @@ defmodule Ecto.Adapters.SQLite3.Connection.MigrationTest do
{:alter, table(:posts, prefix: :foo),
[{:add, :author_id, %Reference{table: :author}, []}]}

assert_raise ArgumentError, "SQLite3 does not support table prefixes", fn ->
execute_ddl(alter)
end
assert execute_ddl(alter) == [
~s|ALTER TABLE foo.posts ADD COLUMN "author_id" INTEGER CONSTRAINT "posts_author_id_fkey" REFERENCES foo.author("id")|
]
end

test "alter table with serial primary key" do
Expand Down Expand Up @@ -374,16 +374,16 @@ defmodule Ecto.Adapters.SQLite3.Connection.MigrationTest do
test "create index with prefix" do
create = {:create, index(:posts, [:category_id, :permalink], prefix: :foo)}

assert_raise ArgumentError, "SQLite3 does not support table prefixes", fn ->
execute_ddl(create)
end
assert execute_ddl(create) == [
~s|CREATE INDEX "posts_category_id_permalink_index" ON foo.posts ("category_id", "permalink")|
]

create =
{:create, index(:posts, ["lower(permalink)"], name: "posts$main", prefix: :foo)}

assert_raise ArgumentError, "SQLite3 does not support table prefixes", fn ->
execute_ddl(create)
end
assert execute_ddl(create) == [
~s|CREATE INDEX "posts$main" ON foo.posts (lower(permalink))|
]
end

test "create index with comment" do
Expand Down Expand Up @@ -470,10 +470,7 @@ defmodule Ecto.Adapters.SQLite3.Connection.MigrationTest do

test "drop index with prefix" do
drop = {:drop, index(:posts, [:id], name: "posts$main", prefix: :foo), :restrict}

assert_raise ArgumentError, "SQLite3 does not support table prefixes", fn ->
execute_ddl(drop)
end
assert execute_ddl(drop) == [~s|DROP INDEX foo.posts$main|]
end

test "drop index concurrently not supported" do
Expand Down Expand Up @@ -516,10 +513,7 @@ defmodule Ecto.Adapters.SQLite3.Connection.MigrationTest do

test "rename table with prefix" do
rename = {:rename, table(:posts, prefix: :foo), table(:new_posts, prefix: :foo)}

assert_raise ArgumentError, "SQLite3 does not support table prefixes", fn ->
execute_ddl(rename)
end
assert execute_ddl(rename) == [~s|ALTER TABLE foo.posts RENAME TO "new_posts"|]
end

test "rename column" do
Expand All @@ -533,9 +527,9 @@ defmodule Ecto.Adapters.SQLite3.Connection.MigrationTest do
test "rename column in prefixed table" do
rename = {:rename, table(:posts, prefix: :foo), :given_name, :first_name}

assert_raise ArgumentError, "SQLite3 does not support table prefixes", fn ->
execute_ddl(rename)
end
assert execute_ddl(rename) == [
~s|ALTER TABLE foo.posts RENAME COLUMN "given_name" TO "first_name"|
]
end

test "autoincrement support" do
Expand Down
6 changes: 3 additions & 3 deletions test/ecto/adapters/sqlite3/connection/update_all_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ defmodule Ecto.Adapters.SQLite3.Connection.UpdateAllTest do
end

test "update all with prefix" do
assert_raise ArgumentError, "SQLite3 does not support table prefixes", fn ->
query =
from(m in Schema, update: [set: [x: 0]])
|> Map.put(:prefix, "prefix")
|> plan(:update_all)
|> update_all()
end

assert ~s{UPDATE prefix.schema AS s0 SET "x" = 0} == update_all(query)
end

test "update all with left join" do
Expand Down