Skip to content

Instantly share code, notes, and snippets.

Avatar

Carter carterbryden

View GitHub Profile
@carterbryden
carterbryden / AddPostgresTriggerAndFunctionForAllTables.exs
Last active November 22, 2022 02:06
Elixir Phoenix Postgresql migration to add triggers for pubsub to run on every CRUD operation on every table. If a new table is added, it'll automatically add a trigger to that table too.
View AddPostgresTriggerAndFunctionForAllTables.exs
defmodule MyApp.Repo.Migrations.AddPostgresTriggerAndFunctionForAllTables do
use Ecto.Migration
def up do
# Create a function that broadcasts row changes
execute "
CREATE OR REPLACE FUNCTION broadcast_changes()
RETURNS trigger AS $$
DECLARE
current_row RECORD;