marcojulian

marcojulian

Looking for a Change Data Capture (CDC) lib for MySQL

My Elixir application needs to connect with a MySQL database and listen for insert and updates in certain tables, does anyone know about a library that makes this easier? I would use WalEx or Cainophile but they are intended for PostgreSQL DBs.

Marked As Solved

joerichsen

joerichsen

Hi

You could use Port — Elixir v1.20.2 with the stdout option of https://maxwells-daemon.io/

I have this proof of concept code that might get you started

defmodule MaxwellListener do
  use GenServer

  # Adjust the command to include the full executable path if necessary
  @command "maxwell"
  @args [
    "--user='maxwell'",
    "--password='XXXXXX'",
    "--host='127.0.0.1'",
    "--filter='exclude: *.*, include: holdsport.users, include: holdsport.teams_users'",
    "--producer=stdout"
  ]

  def start_link(opts \\ []) do
    GenServer.start_link(__MODULE__, :ok, opts)
  end

  def init(:ok) do
    command_with_args = Enum.join([@command | @args], " ")
    options = [:binary, :exit_status]
    port = Port.open({:spawn, command_with_args}, options)
    {:ok, %{port: port}}
  end

  def handle_info({port, {:data, data}}, state) do
    process_output(data)
    {:noreply, state}
  end

  def handle_info({:EXIT, _port, _reason}, state) do
    # Handle port exit
    {:stop, :normal, state}
  end

  defp process_output(data) do
    data
    |> String.trim_trailing()
    |> Jason.decode!(keys: :atoms)
    |> handle()
  end

  defp handle(%{table: "teams_users", data: %{users_id: user_id}}) do
    IO.inspect(user_id, label: "User ID")
  end

  defp handle(d) do
    IO.inspect(d, limit: :infinity)
  end
end

Last Post!

marcojulian

marcojulian

Thank you! I will give it a try

Where Next?

Popular in Questions Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement