Skip to content

Instantly share code, notes, and snippets.

@ibarchenkov
ibarchenkov / migration.ex
Last active August 22, 2022 09:09
PostgreSQL soft delete CASCADE
def create_soft_delete_cascade_function() do
execute(
"""
CREATE OR REPLACE FUNCTION soft_delete_cascade() RETURNS trigger AS $$
DECLARE
ref RECORD;
BEGIN
IF EXISTS (SELECT true FROM new_table WHERE deleted_at IS NOT NULL) THEN
FOR ref IN
SELECT
@ibarchenkov
ibarchenkov / telegram_web_app_init_data_validation.ex
Last active July 8, 2022 19:41
Telegram Bot API Web App initData validation in Elixir
def validate_web_app_init_data(tg_init_data, bot_api_token) do
{received_hash, decoded_map} =
tg_init_data
|> URI.decode_query()
|> Map.pop!("hash")
data_check_string =
decoded_map
|> Enum.sort(fn {k1, _v1}, {k2, _v2} -> k1 <= k2 end)
|> Enum.map_join("\n", fn {k, v} -> "#{k}=#{v}" end)
@ibarchenkov
ibarchenkov / ecto_postgresql_migration.ex
Last active April 27, 2023 06:41
Ecto migration helpers for PostgreSQL.
defmodule MyApp.Migration do
@moduledoc """
Additional helpers for PostgreSQL.
"""
import Ecto.Migration, only: [execute: 2]
defmacro __using__(_) do
quote do
use Ecto.Migration
@ibarchenkov
ibarchenkov / stream_data_ecto_factory.ex
Created January 31, 2019 21:09
Elixir Ecto testing factories combined with StreamData generators.
defmodule MyApp.Factory do
use ExUnitProperties
alias MyApp.{Repo, User, Comment}
### Generators
def generator(:user) do
gen all name <- string(:alphanumeric, min_length: 2),
email <- generator(:email),
age <- integer(10..130) do