Skip to content

Commit

Permalink
Adds support for generate truncate table queries.
Browse files Browse the repository at this point in the history
  • Loading branch information
mishafw committed Jun 9, 2023
1 parent e29489b commit fbd572d
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/cassandrax/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ defmodule Cassandrax.Connection do
["DELETE FROM ", quote_table(keyspace, table), " WHERE " | filters]
end

def truncate(keyspace, table), do: ["TRUNCATE TABLE ", quote_table(keyspace, table)]

defp assemble_filters(filters) do
intersperse_map(filters, " AND ", fn field ->
field = field |> Atom.to_string() |> quote_name()
Expand Down
3 changes: 3 additions & 0 deletions lib/cassandrax/keyspace.ex
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ defmodule Cassandrax.Keyspace do
def delete!(struct, opts \\ []),
do: Cassandrax.Keyspace.Schema.delete!(__MODULE__, struct, opts)

def truncate(struct),
do: Cassandrax.Keyspace.Schema.truncate(__MODULE__, struct)

## Queryable

def all(queryable, opts \\ []),
Expand Down
21 changes: 21 additions & 0 deletions lib/cassandrax/keyspace/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,27 @@ defmodule Cassandrax.Keyspace.Schema do
end
end

@doc """
Implementation for `Cassandrax.Keyspace.truncate/2`.
"""
def truncate(keyspace, struct) do
conn = keyspace.__conn__
changeset = Ecto.Changeset.change(struct.__struct__)
schema = changeset.data.__struct__
keyspace_name = keyspace.__keyspace__
table = schema.__schema__(:source)

statement = Cassandrax.Connection.truncate(keyspace_name, table)

case Cassandrax.cql(conn, statement) do
{:ok, results} ->
{:ok, results}

{:error, error} ->
{:error, error}
end
end

@doc """
Implementation for `Cassandrax.Keyspace.insert/2`.
"""
Expand Down
15 changes: 15 additions & 0 deletions test/cassandrax/connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ defmodule Cassandrax.ConnectionTest do
end

defp to_string({iodata, _values}), do: IO.iodata_to_binary(iodata)
defp to_string(iodata), do: IO.iodata_to_binary(iodata)
defp all(queryable), do: Cassandrax.Connection.all(TestKeyspace, queryable) |> to_string()

describe "all/2" do
Expand Down Expand Up @@ -152,4 +153,18 @@ defmodule Cassandrax.ConnectionTest do
assert all(queryable) =~ ~r/WHERE \("id" < \?\)/
end
end

defp truncate(queryable) do
changeset = Ecto.Changeset.change(queryable.__struct__)
schema = changeset.data.__struct__
keyspace_name = TestKeyspace.__keyspace__()
table = schema.__schema__(:source)
Cassandrax.Connection.truncate(keyspace_name, table) |> to_string()
end

describe "truncate" do
test "successfully generate a truncate clause when passed a schema" do
assert truncate(TestSchema) =~ ~r/TRUNCATE TABLE "test_keyspace"."my_table"/
end
end
end
21 changes: 21 additions & 0 deletions test/cassandrax/keyspace_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,27 @@ defmodule Cassandrax.KeyspaceTest do
end
end

describe "truncate" do
setup do
[
first: fixture(TestData, id: "0", timestamp: "00:00", data: "0"),
second:
fixture(TestData,
id: "1",
timestamp: "01:00",
data: "1",
svalue: MapSet.new(["one", "another one"])
)
]
end

test "truncates all records on the table" do
assert Enum.count(TestKeyspace.all(TestData)) == 2
assert {:ok, %Xandra.Void{}} = TestKeyspace.truncate(TestData)
assert Enum.count(TestKeyspace.all(TestData)) == 0
end
end

describe "cql" do
setup do
[
Expand Down

0 comments on commit fbd572d

Please sign in to comment.