Skip to content

Instantly share code, notes, and snippets.

View ventsislaf's full-sized avatar

Ventsislav Nikolov ventsislaf

View GitHub Profile
list = Enum.to_list(1..100_000)
Benchee.init(%{time: 3})
|> Benchee.benchmark("Enum.at", fn -> Enum.at(list, length(list) - 2) end)
|> Benchee.benchmark(
"revers + pattern matching",
fn ->
[_, next_to_last | _] = Enum.reverse(list)
next_to_last
end
@ventsislaf
ventsislaf / turn_off_logger.exs
Created October 21, 2015 17:50
Turn off logging in Phoenix console
Logger.remove_backend(:console)
@ventsislaf
ventsislaf / phoenix_heroku_reset_db.md
Last active May 18, 2023 14:14
Reset database on Heroku running Phoenix app

Note: Don't do this on a production evniroment!

Get the heroku database name:

heroku pg:info

Name can be found in the reponse from the command above. For example: Add-on: soaring-newly-1337.

@ventsislaf
ventsislaf / ecto_normalize_data.md
Created October 8, 2015 10:34
Normalize data in Ecto before insert to DB

Example code:

def changeset(model, params \\ :empty) do
  model
  |> cast(params, @required_fieds, @optional_fields)
  |> validate_length(:code, is: 2)
  |> normalize_code
end
@ventsislaf
ventsislaf / ecto_count_query.md
Created August 12, 2015 10:08
Count query using Ecto
from(u in User, select: count(u.id)) |> Repo.one!
@ventsislaf
ventsislaf / rails_sql_in_console.md
Created July 12, 2015 12:58
Show SQL queries in Rails console

Execute this in the Rails console and then all SQL queries from ActiveRecord will be shown:

ActiveRecord::Base.logger = Logger.new(STDOUT)
@ventsislaf
ventsislaf / postgres_error.md
Last active November 25, 2019 10:06
Postgres error after update

After updating postgres brew formula or OS X, afterwards there could be an error similar to:

psql: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

One possible fix is:

@ventsislaf
ventsislaf / create_phoenix_app_on_heroku.md
Last active August 29, 2015 14:23
Create Phoenix app on Heroku

Initialize your git repository git init. This would save you later a step to add heroku as remote.

Move configuration from config/prod.secret.exs to config/prod.exs and replace the following parts:

config :app_name, AppName.Endpoint,
  secret_key_base: System.get_env("SECRET_KEY_BASE")

# Configure your database
config :app_name, AppName.Repo,
@ventsislaf
ventsislaf / phoenix_secret_key_base.sh
Last active August 1, 2020 06:20
Generate secret key base for Phoenix app
mix phoenix.gen.secret