|
| 1 | +defmodule Realtime.Integration.RegionAwareMigrationsTest do |
| 2 | + use Realtime.DataCase, async: false |
| 3 | + use Mimic |
| 4 | + |
| 5 | + alias Containers |
| 6 | + alias Realtime.Tenants |
| 7 | + alias Realtime.Tenants.Migrations |
| 8 | + |
| 9 | + setup do |
| 10 | + {:ok, port} = Containers.checkout() |
| 11 | + |
| 12 | + settings = [ |
| 13 | + %{ |
| 14 | + "type" => "postgres_cdc_rls", |
| 15 | + "settings" => %{ |
| 16 | + "db_host" => "127.0.0.1", |
| 17 | + "db_name" => "postgres", |
| 18 | + "db_user" => "supabase_admin", |
| 19 | + "db_password" => "postgres", |
| 20 | + "db_port" => "#{port}", |
| 21 | + "poll_interval" => 100, |
| 22 | + "poll_max_changes" => 100, |
| 23 | + "poll_max_record_bytes" => 1_048_576, |
| 24 | + "region" => "ap-southeast-2", |
| 25 | + "publication" => "supabase_realtime_test", |
| 26 | + "ssl_enforced" => false |
| 27 | + } |
| 28 | + } |
| 29 | + ] |
| 30 | + |
| 31 | + tenant = tenant_fixture(%{extensions: settings}) |
| 32 | + region = Application.get_env(:realtime, :region) |
| 33 | + |
| 34 | + {:ok, node} = |
| 35 | + Clustered.start(nil, |
| 36 | + extra_config: [ |
| 37 | + {:realtime, :region, Tenants.region(tenant)}, |
| 38 | + {:realtime, :master_region, region} |
| 39 | + ] |
| 40 | + ) |
| 41 | + |
| 42 | + on_exit(fn -> Clustered.stop() end) |
| 43 | + |
| 44 | + %{tenant: tenant, node: node} |
| 45 | + end |
| 46 | + |
| 47 | + test "run_migrations routes to node in tenant's region with expected arguments", %{tenant: tenant, node: node} do |
| 48 | + assert tenant.migrations_ran == 0 |
| 49 | + |
| 50 | + Realtime.GenRpc |
| 51 | + |> Mimic.expect(:call, fn called_node, mod, func, args, opts -> |
| 52 | + assert called_node == node |
| 53 | + assert mod == Migrations |
| 54 | + assert func == :start_migration |
| 55 | + assert opts[:tenant_id] == tenant.external_id |
| 56 | + |
| 57 | + arg = hd(args) |
| 58 | + assert arg.tenant_external_id == tenant.external_id |
| 59 | + assert arg.migrations_ran == tenant.migrations_ran |
| 60 | + assert arg.settings == hd(tenant.extensions).settings |
| 61 | + |
| 62 | + call_original(Realtime.GenRpc, :call, [node, mod, func, args, opts]) |
| 63 | + end) |
| 64 | + |
| 65 | + assert :ok = Migrations.run_migrations(tenant) |
| 66 | + Process.sleep(1000) |
| 67 | + tenant = Realtime.Repo.reload!(tenant) |
| 68 | + refute tenant.migrations_ran == 0 |
| 69 | + end |
| 70 | +end |
0 commit comments