Skip to content

Instantly share code, notes, and snippets.

View dmkit's full-sized avatar

Dan Bangayan dmkit

View GitHub Profile
@dmkit
dmkit / importer.ex
Last active July 14, 2023 15:57
[Elixir] [csv] [stream] A sample of how I would parse a big csv file and insert the data to a database. How would you do it?
defmodule Importer do
@moduledoc """
A sample module
"""
alias Sample.Repo
alias Sample.User
@doc """
Parses and inserts data to database
@dmkit
dmkit / sort-enum.md
Created December 15, 2022 02:01
[SQL][MySQL] sorting enum columns

ENUM values are sorted based on their index numbers, which depend on the order in which the enumeration members were listed in the column specification. For example, 'b' sorts before 'a' for ENUM('b', 'a')

So the following SELECT result is expected:

CREATE TABLE `colors` (
  `color` ENUM('white', 'blue', 'yellow', 'orange')
);
@dmkit
dmkit / user.ex
Created December 15, 2022 01:19
[Elixir][Ecto] trim input fields / remove trailing and leading whitespaces with Ecto changeset (User.trim/2)
defmodule User do
use Ecto.Schema
import Ecto.Changeset
alias Ecto.Changeset
schema "users" do
field :name
field :email
field :age, :integer
field :about
@dmkit
dmkit / repo.ex
Created December 15, 2022 01:18
[Elixir][Ecto] Copy an existing Ecto struct (Repo.build_from/1)
defmodule Repo do
use Ecto.Repo,
otp_app: :my_app,
adapter: Ecto.Adapters.Postgres
def build_from(struct) do
struct = Ecto.put_meta(struct, state: :built)
:primary_key
|> struct.__struct__.__schema__()