Skip to content

Commit

Permalink
change id to be of uuid type
Browse files Browse the repository at this point in the history
  • Loading branch information
filipecabaco committed Sep 19, 2024
1 parent dabb8b2 commit 25a965b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
2 changes: 2 additions & 0 deletions lib/realtime/api/message.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ 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 :topic, :string
field :extension, Ecto.Enum, values: [:broadcast, :presence]
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 @@ -60,7 +60,8 @@ defmodule Realtime.Tenants.Migrations do
UnloggedMessagesTable,
LoggedMessagesTable,
FilterDeletePostgresChanges,
AddPayloadToMessages
AddPayloadToMessages,
ChangeMessagesIdType
}

@migrations [
Expand Down Expand Up @@ -111,7 +112,8 @@ defmodule Realtime.Tenants.Migrations do
{20_240_801_235_015, UnloggedMessagesTable},
{20_240_805_133_720, LoggedMessagesTable},
{20_240_827_160_934, FilterDeletePostgresChanges},
{20_240_917_170_412, AddPayloadToMessages}
{20_240_919_163_303, AddPayloadToMessages},
{20_240_919_163_305, ChangeMessagesIdType}
]
defstruct [:tenant_external_id, :settings]
@spec run_migrations(map()) :: :ok | {:error, any()}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule Realtime.Tenants.Migrations.AddPayloadToMessages do
add_if_not_exists :event, :text
add_if_not_exists :topic, :text
add_if_not_exists :private, :boolean, default: true

modify :inserted_at, :utc_datetime, default: fragment("now()")
modify :updated_at, :utc_datetime, default: fragment("now()")
end
Expand All @@ -25,7 +26,7 @@ defmodule Realtime.Tenants.Migrations.AddPayloadToMessages do
"""

execute """
CREATE OR REPLACE FUNCTION realtime.broadcast_changes (topic_name text, event_name text, operation text, table_name text, table_schema text, NEW record, OLD record, level string DEFAULT 'ROW')
CREATE OR REPLACE FUNCTION realtime.broadcast_changes (topic_name text, event_name text, operation text, table_name text, table_schema text, NEW record, OLD record, level text DEFAULT 'ROW')
RETURNS void
AS $$
DECLARE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule Realtime.Tenants.Migrations.ChangeMessagesIdType do
@moduledoc false
use Ecto.Migration

def change do
alter table(:messages) do
add_if_not_exists :uuid, :binary_id,
primary_key: true,
default: fragment("gen_random_uuid()")

remove_if_exists :id, :id
end

rename table(:messages), :uuid, to: :id
end
end
6 changes: 0 additions & 6 deletions test/support/generators.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,6 @@ defmodule Generators do
end)

Postgrex.query!(transaction_conn, "TRUNCATE TABLE #{schema}.#{table} CASCADE", [])

Postgrex.query!(
transaction_conn,
"ALTER SEQUENCE #{schema}.#{table}_id_seq RESTART WITH 1",
[]
)
end)
end

Expand Down

0 comments on commit 25a965b

Please sign in to comment.