Rename archiver to partitions and drop archive SQL helper
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
defmodule Parrhesia.Storage.ArchiverTest do
|
||||
defmodule Parrhesia.Storage.PartitionsTest do
|
||||
use ExUnit.Case, async: false
|
||||
|
||||
alias Ecto.Adapters.SQL.Sandbox
|
||||
alias Parrhesia.Repo
|
||||
alias Parrhesia.Storage.Archiver
|
||||
alias Parrhesia.Storage.Partitions
|
||||
|
||||
setup do
|
||||
:ok = Sandbox.checkout(Repo)
|
||||
@@ -11,16 +11,16 @@ defmodule Parrhesia.Storage.ArchiverTest do
|
||||
end
|
||||
|
||||
test "list_partitions returns partition tables" do
|
||||
partitions = Archiver.list_partitions()
|
||||
partitions = Partitions.list_partitions()
|
||||
assert is_list(partitions)
|
||||
end
|
||||
|
||||
test "ensure_monthly_partitions creates aligned monthly partitions for events and event_tags" do
|
||||
assert :ok =
|
||||
Archiver.ensure_monthly_partitions(reference_date: ~D[2026-06-14], months_ahead: 1)
|
||||
Partitions.ensure_monthly_partitions(reference_date: ~D[2026-06-14], months_ahead: 1)
|
||||
|
||||
monthly_partition_names =
|
||||
Archiver.list_monthly_partitions()
|
||||
Partitions.list_monthly_partitions()
|
||||
|> Enum.map(& &1.name)
|
||||
|
||||
assert "events_2026_06" in monthly_partition_names
|
||||
@@ -30,43 +30,32 @@ defmodule Parrhesia.Storage.ArchiverTest do
|
||||
assert table_exists?("event_tags_2026_07")
|
||||
end
|
||||
|
||||
test "archive_sql builds insert-select statement" do
|
||||
assert Archiver.archive_sql("events_2026_03", "events_archive") ==
|
||||
~s(INSERT INTO "events_archive" SELECT * FROM "events_2026_03";)
|
||||
end
|
||||
|
||||
test "drop_partition returns an error for protected partitions" do
|
||||
assert {:error, :protected_partition} = Archiver.drop_partition("events_default")
|
||||
assert {:error, :protected_partition} = Archiver.drop_partition("events")
|
||||
assert {:error, :protected_partition} = Archiver.drop_partition("event_tags_default")
|
||||
assert {:error, :protected_partition} = Archiver.drop_partition("event_tags")
|
||||
assert {:error, :protected_partition} = Partitions.drop_partition("events_default")
|
||||
assert {:error, :protected_partition} = Partitions.drop_partition("events")
|
||||
assert {:error, :protected_partition} = Partitions.drop_partition("event_tags_default")
|
||||
assert {:error, :protected_partition} = Partitions.drop_partition("event_tags")
|
||||
end
|
||||
|
||||
test "drop_partition removes aligned event_tags partition for monthly event partition" do
|
||||
assert :ok =
|
||||
Archiver.ensure_monthly_partitions(reference_date: ~D[2026-08-14], months_ahead: 0)
|
||||
Partitions.ensure_monthly_partitions(reference_date: ~D[2026-08-14], months_ahead: 0)
|
||||
|
||||
assert table_exists?("events_2026_08")
|
||||
assert table_exists?("event_tags_2026_08")
|
||||
|
||||
assert :ok = Archiver.drop_partition("events_2026_08")
|
||||
assert :ok = Partitions.drop_partition("events_2026_08")
|
||||
|
||||
refute table_exists?("events_2026_08")
|
||||
refute table_exists?("event_tags_2026_08")
|
||||
end
|
||||
|
||||
test "database_size_bytes returns the current database size" do
|
||||
assert {:ok, size} = Archiver.database_size_bytes()
|
||||
assert {:ok, size} = Partitions.database_size_bytes()
|
||||
assert is_integer(size)
|
||||
assert size >= 0
|
||||
end
|
||||
|
||||
test "archive_sql rejects invalid SQL identifiers" do
|
||||
assert_raise ArgumentError, fn ->
|
||||
Archiver.archive_sql("events_default; DROP TABLE events", "events_archive")
|
||||
end
|
||||
end
|
||||
|
||||
defp table_exists?(table_name) when is_binary(table_name) do
|
||||
case Repo.query("SELECT to_regclass($1)", ["public." <> table_name]) do
|
||||
{:ok, %{rows: [[nil]]}} -> false
|
||||
@@ -2,13 +2,13 @@ defmodule Parrhesia.Tasks.PartitionRetentionWorkerTest do
|
||||
use ExUnit.Case, async: false
|
||||
|
||||
alias Parrhesia.Tasks.PartitionRetentionWorker
|
||||
alias Parrhesia.TestSupport.PartitionRetentionStubArchiver
|
||||
alias Parrhesia.TestSupport.PartitionRetentionStubPartitions
|
||||
|
||||
@bytes_per_gib 1_073_741_824
|
||||
|
||||
test "drops oldest partition when max_months_to_keep is exceeded" do
|
||||
start_supervised!(
|
||||
{PartitionRetentionStubArchiver,
|
||||
{PartitionRetentionStubPartitions,
|
||||
partitions: [
|
||||
partition(2026, 1),
|
||||
partition(2026, 2),
|
||||
@@ -24,7 +24,7 @@ defmodule Parrhesia.Tasks.PartitionRetentionWorkerTest do
|
||||
start_supervised!(
|
||||
{PartitionRetentionWorker,
|
||||
name: nil,
|
||||
archiver: PartitionRetentionStubArchiver,
|
||||
partition_ops: PartitionRetentionStubPartitions,
|
||||
interval_ms: :timer.hours(24),
|
||||
months_ahead: 0,
|
||||
max_db_bytes: :infinity,
|
||||
@@ -42,7 +42,7 @@ defmodule Parrhesia.Tasks.PartitionRetentionWorkerTest do
|
||||
|
||||
test "drops oldest completed partition when size exceeds max_db_bytes" do
|
||||
start_supervised!(
|
||||
{PartitionRetentionStubArchiver,
|
||||
{PartitionRetentionStubPartitions,
|
||||
partitions: [partition(2026, 3), partition(2026, 4), partition(2026, 5)],
|
||||
db_size_bytes: 12 * @bytes_per_gib,
|
||||
test_pid: self()}
|
||||
@@ -52,7 +52,7 @@ defmodule Parrhesia.Tasks.PartitionRetentionWorkerTest do
|
||||
start_supervised!(
|
||||
{PartitionRetentionWorker,
|
||||
name: nil,
|
||||
archiver: PartitionRetentionStubArchiver,
|
||||
partition_ops: PartitionRetentionStubPartitions,
|
||||
interval_ms: :timer.hours(24),
|
||||
months_ahead: 0,
|
||||
max_db_bytes: 10,
|
||||
@@ -69,7 +69,7 @@ defmodule Parrhesia.Tasks.PartitionRetentionWorkerTest do
|
||||
|
||||
test "does not drop partitions when both limits are infinity" do
|
||||
start_supervised!(
|
||||
{PartitionRetentionStubArchiver,
|
||||
{PartitionRetentionStubPartitions,
|
||||
partitions: [partition(2026, 1), partition(2026, 2), partition(2026, 3)],
|
||||
db_size_bytes: 50 * @bytes_per_gib,
|
||||
test_pid: self()}
|
||||
@@ -79,7 +79,7 @@ defmodule Parrhesia.Tasks.PartitionRetentionWorkerTest do
|
||||
start_supervised!(
|
||||
{PartitionRetentionWorker,
|
||||
name: nil,
|
||||
archiver: PartitionRetentionStubArchiver,
|
||||
partition_ops: PartitionRetentionStubPartitions,
|
||||
interval_ms: :timer.hours(24),
|
||||
months_ahead: 0,
|
||||
max_db_bytes: :infinity,
|
||||
|
||||
Reference in New Issue
Block a user