Skip to content

Commit

Permalink
fix: alter id to uuid type (#1199)
Browse files Browse the repository at this point in the history
Changes the existing ID to UUID type.
  • Loading branch information
filipecabaco authored Nov 8, 2024
1 parent f2e68dc commit b3c59dc
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 15 deletions.
15 changes: 7 additions & 8 deletions lib/realtime/api/message.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ defmodule Realtime.Api.Message do
use Ecto.Schema
import Ecto.Changeset

@primary_key {:id, Ecto.UUID, autogenerate: true}
@schema_prefix "realtime"

schema "messages" do
field :uuid, :string
field :topic, :string
field :extension, Ecto.Enum, values: [:broadcast, :presence]
field :payload, :map
field :event, :string
field :private, :boolean
field(:topic, :string)
field(:extension, Ecto.Enum, values: [:broadcast, :presence])
field(:payload, :map)
field(:event, :string)
field(:private, :boolean)

timestamps()
end
Expand All @@ -27,8 +27,7 @@ defmodule Realtime.Api.Message do
:event,
:private,
:inserted_at,
:updated_at,
:uuid
:updated_at
])
|> validate_required([:topic, :extension])
|> put_timestamp(:updated_at)
Expand Down
2 changes: 1 addition & 1 deletion lib/realtime/broadcast_changes/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ defmodule Realtime.BroadcastChanges.Handler do
{:noreply, state}

payload ->
id = Map.fetch!(to_broadcast, "uuid")
id = Map.fetch!(to_broadcast, "id")

to_broadcast =
%{
Expand Down
2 changes: 1 addition & 1 deletion lib/realtime/tenants.ex
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ defmodule Realtime.Tenants do
|> Cache.get_tenant_by_external_id()
|> Tenant.management_changeset(attrs)
|> Repo.update!()
|> tap(fn _ -> Cache.distributed_invalidate_tenant_cache(tenant_id) end)
|> tap(fn _ -> Cache.invalidate_tenant_cache(tenant_id) end)
end

defp broadcast_operation_event(action, external_id) do
Expand Down
6 changes: 4 additions & 2 deletions lib/realtime/tenants/migrations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ defmodule Realtime.Tenants.Migrations do
AddPayloadToMessages,
ChangeMessagesIdType,
UuidAutoGeneration,
MessagesPartitioning
MessagesPartitioning,
MessagesUsingUuid
}

@migrations [
Expand Down Expand Up @@ -117,7 +118,8 @@ defmodule Realtime.Tenants.Migrations do
{20_240_919_163_303, AddPayloadToMessages},
{20_240_919_163_305, ChangeMessagesIdType},
{20_241_019_105_805, UuidAutoGeneration},
{20_241_030_150_047, MessagesPartitioning}
{20_241_030_150_047, MessagesPartitioning},
{20_241_108_114_728, MessagesUsingUuid}
]
defstruct [:tenant_external_id, :settings]
@spec run_migrations(map()) :: :ok | {:error, any()}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule Realtime.Tenants.Migrations.MessagesUsingUuid do
@moduledoc false
use Ecto.Migration

def change do
alter table(:messages) do
remove(:id)
remove(:uuid)
add(:id, :uuid, null: false, default: fragment("gen_random_uuid()"))
end

execute("ALTER TABLE realtime.messages ADD PRIMARY KEY (id, inserted_at)")
execute("DROP SEQUENCE realtime.messages_id_seq")
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Realtime.MixProject do
def project do
[
app: :realtime,
version: "2.33.26",
version: "2.33.27",
elixir: "~> 1.16.0",
elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod,
Expand Down
4 changes: 2 additions & 2 deletions test/realtime/tenants_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ defmodule Realtime.TenantsTest do
test "sets private_only flag to true and invalidates cache" do
%{external_id: external_id} = tenant_fixture(%{private_only: false})
tenant = Tenants.update_management(external_id, %{private_only: true})
assert tenant.private_only == true
assert_receive {:invalidate_cache, ^external_id}, 1000
assert tenant.private_only
assert Tenants.Cache.get_tenant_by_external_id(external_id).private_only
end
end
end

0 comments on commit b3c59dc

Please sign in to comment.