From 821bd770a571b5cf9199cc77d980bef08d0a133f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filipe=20Caba=C3=A7o?= Date: Fri, 20 Dec 2024 04:39:33 +0000 Subject: [PATCH] fix: Improve partition creation on realtime.send (#1251) --- config/runtime.exs | 4 +- lib/realtime/tenants/migrations.ex | 6 ++- ...12_fix_send_function_partition_creation.ex | 38 +++++++++++++++++++ mix.exs | 2 +- 4 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 lib/realtime/tenants/repo/migrations/20241220035512_fix_send_function_partition_creation.ex diff --git a/config/runtime.exs b/config/runtime.exs index 6959e11db..cf6c9cd74 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -92,6 +92,8 @@ if config_env() == :prod do end if config_env() != :test do + config :logger, level: System.get_env("LOG_LEVEL", "info") |> String.to_existing_atom() + platform = if System.get_env("AWS_EXECUTION_ENV") == "AWS_ECS_FARGATE", do: :aws, else: :fly config :realtime, @@ -245,5 +247,3 @@ if System.get_env("LOGS_ENGINE") == "logflare" do discard_threshold: 1_000, backends: [LogflareLogger.HttpBackend] end - -config :logger, level: System.get_env("LOG_LEVEL", "info") |> String.to_existing_atom() diff --git a/lib/realtime/tenants/migrations.ex b/lib/realtime/tenants/migrations.ex index 822c5e1ff..c2c54746a 100644 --- a/lib/realtime/tenants/migrations.ex +++ b/lib/realtime/tenants/migrations.ex @@ -67,7 +67,8 @@ defmodule Realtime.Tenants.Migrations do MessagesPartitioning, MessagesUsingUuid, FixSendFunction, - RecreateEntityIndexUsingBtree + RecreateEntityIndexUsingBtree, + FixSendFunctionPartitionCreation } @migrations [ @@ -124,7 +125,8 @@ defmodule Realtime.Tenants.Migrations do {20_241_030_150_047, MessagesPartitioning}, {20_241_108_114_728, MessagesUsingUuid}, {20_241_121_104_152, FixSendFunction}, - {20_241_130_184_212, RecreateEntityIndexUsingBtree} + {20_241_130_184_212, RecreateEntityIndexUsingBtree}, + {20_241_220_035_512, FixSendFunctionPartitionCreation} ] defstruct [:tenant_external_id, :settings] diff --git a/lib/realtime/tenants/repo/migrations/20241220035512_fix_send_function_partition_creation.ex b/lib/realtime/tenants/repo/migrations/20241220035512_fix_send_function_partition_creation.ex new file mode 100644 index 000000000..0d6c79eed --- /dev/null +++ b/lib/realtime/tenants/repo/migrations/20241220035512_fix_send_function_partition_creation.ex @@ -0,0 +1,38 @@ +defmodule Realtime.Tenants.Migrations.FixSendFunctionPartitionCreation do + @moduledoc false + use Ecto.Migration + + # We missed the schema prefix of `realtime.` in the create table partition statement + def change do + execute(""" + CREATE OR REPLACE FUNCTION realtime.send(payload jsonb, event text, topic text, private boolean DEFAULT true) + RETURNS void + AS $$ + DECLARE + partition_name text; + partition_start timestamp; + partition_end timestamp; + BEGIN + partition_start := date_trunc('day', NOW()); + partition_end := partition_start + interval '1 day'; + partition_name := 'messages_' || to_char(partition_start, 'YYYY_MM_DD'); + + BEGIN + EXECUTE format( + 'CREATE TABLE IF NOT EXISTS realtime.%I PARTITION OF realtime.messages FOR VALUES FROM (%L) TO (%L)', + partition_name, + partition_start, + partition_end + ); + EXCEPTION WHEN duplicate_table THEN + -- Ignore; table already exists + END; + + INSERT INTO realtime.messages (payload, event, topic, private, extension) + VALUES (payload, event, topic, private, 'broadcast'); + END; + $$ + LANGUAGE plpgsql; + """) + end +end diff --git a/mix.exs b/mix.exs index a2a64fcf5..80fba63da 100644 --- a/mix.exs +++ b/mix.exs @@ -4,7 +4,7 @@ defmodule Realtime.MixProject do def project do [ app: :realtime, - version: "2.33.70", + version: "2.33.71", elixir: "~> 1.17.3", elixirc_paths: elixirc_paths(Mix.env()), start_permanent: Mix.env() == :prod,