Skip to content

Instantly share code, notes, and snippets.

View leemour's full-sized avatar

Viacheslav Ptsarev leemour

View GitHub Profile
@leemour
leemour / basic_query_stats.sql
Last active May 1, 2024 14:04
PostgreSQL pg_stat_statements detailed output for query performance analysis and optimization
SELECT
t.tablename,
foo.indexname,
c.reltuples AS num_rows,
pg_size_pretty(pg_relation_size(quote_ident(t.tablename)::text)) AS table_size,
pg_size_pretty(pg_relation_size(quote_ident(indexrelname)::text)) AS index_size,
pg_relation_size(quote_ident(indexrelname)) as index_size_bytes,
CASE WHEN indisunique THEN 'Y'
ELSE 'N'
END AS UNIQUE,
@leemour
leemour / clean_code.md
Created March 29, 2021 18:19 — forked from wojteklu/clean_code.md
Summary of 'Clean code' by Robert C. Martin

Code is clean if it can be understood easily – by everyone on the team. Clean code can be read and enhanced by a developer other than its original author. With understandability comes readability, changeability, extensibility and maintainability.


General rules

  1. Follow standard conventions.
  2. Keep it simple stupid. Simpler is always better. Reduce complexity as much as possible.
  3. Boy scout rule. Leave the campground cleaner than you found it.
  4. Always find root cause. Always look for the root cause of a problem.

Design rules

@leemour
leemour / workspace_ubuntu_install.md
Last active November 20, 2023 16:55
Install and Uninstall (remove) Amazon Workspaces on Linux (Ubuntu 18.04 LTS 64-bit) Wine (Wine64)
sudo apt-get --purge remove wine
sudo apt-get purge wine* ; sudo dpkg --purge wine*
sudo apt-get purge wine64 ; sudo dpkg --purge wine64
sudo apt-get autoclean
sudo apt-get clean
sudo apt-get autoremove
cd $HOME
rm -r .wine
@leemour
leemour / where_is.rb
Created October 24, 2018 18:07 — forked from wtaysom/where_is.rb
A little Ruby module for finding the source location where class and methods are defined.
module Where
class <<self
attr_accessor :editor
def is_proc(proc)
source_location(proc)
end
def is_method(klass, method_name)
source_location(klass.method(method_name))
@leemour
leemour / carrierwave_selectel.rb
Last active July 1, 2021 10:37
Carrierwave integration with Selectel using fog-openstack
CarrierWave.configure do |config|
if Rails.env.test? || Rails.env.cucumber?
config.storage = :file
config.enable_processing = false
else
config.asset_host = Rails.application.secrets.asset_host
config.fog_provider = 'fog/openstack'
config.fog_credentials = {
provider: 'OpenStack',
openstack_auth_url: 'https://api.selcdn.ru/v3',
@leemour
leemour / mass_delete_remote_branches.sh
Last active July 12, 2018 11:04
Mass delete multiple remote branches in git repository (and locally)
# Remote
git br -r | grep -v 'master\|dev\|new_front' | awk -Forigin/ '{print $2 $3}' | xargs -I {} git push origin :{}
# Local
git branch | grep -v 'master\|dev\|new_front' | xargs -I {} git branch -D {}
@leemour
leemour / reflections.rb
Last active June 28, 2018 02:58
Ruby reflections - find source location and source code
# Show method location
method(:meth).source_location
# Nice gem 'method_source' makes life easier. Use it inside console:
require 'method_source'
# Show method source
method(:meth).source.display
# Show class or method definition
show-source Job
show-source Job.work
@leemour
leemour / fibonacci.rb
Last active June 7, 2018 07:31
Even Fibonacci Numbers
module Fibonacci
class << self
def sum_of_even_upto(max)
upto(max).inject(0) do |sum, item|
item.even? ? sum + item : sum
end
end
def upto(max)
@leemour
leemour / curry.rb
Created May 16, 2018 03:51 — forked from KamilLelonek/curry.rb
Currying functions in Ruby
[1] (pry) main: 0> add = -> (a, b) { a + b }
=> #<Proc:0x007ffde11d72c0@(pry):1 (lambda)>
# Call proc with two arguments
[2] (pry) main: 0> add.(1, 2)
=> 3
# Call proc with one argument
[3] (pry) main: 0> add.(1)
ArgumentError: wrong number of arguments (1 for 2)
@leemour
leemour / db.rake
Created May 4, 2018 19:43
Backup all db records older than x
namespace :db do
desc "Backup DB as separate schema"
task :backup_to_schema, [:created_at] => :environment do |t, args|
schema = 'copy'
created_at = args.created_at || 2.years.ago
ApplicationRecord.connection.execute(
"DROP SCHEMA #{schema} CASCADE;
CREATE SCHEMA copy"
)
ActiveRecord::Base.connection.execute "SET search_path TO public, #{schema}"