Skip to content

Instantly share code, notes, and snippets.

@monsha
monsha / copy_db.sh
Last active September 12, 2018 20:45
Database per branch - Rails
parent=$1
if [ -z $1 ]; then
echo "Parent db is empty"
echo "Using master"
parent="development_master"
fi
repo=`git rev-parse --abbrev-ref HEAD`
db=$(echo "development_$repo" | tr "-" "_")
echo 'Creating dev db'
@monsha
monsha / gist:a703b010aa63e2046645becbed1991db
Created April 6, 2018 14:51
Terminate a sql query from psql
# run in your psql console
#
# test=# SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE query='SELECT "posts".* FROM "posts"';

Deploy Rails 4 app with Dokku on DigitalOcean

Install dokku

First create a Ubuntu 13.04 x64 droplet on DigitalOcean Control Panel

Then ssh with root account, run this in termianl:

$ wget -qO- https://raw.github.com/progrium/dokku/master/bootstrap.sh | sudo bash
@monsha
monsha / Pghero on Rails with Dokku.txt
Last active April 29, 2024 00:30
Pghero on Rails with Dokku
#postgresql setup
ssh -i ~/.ssh/id_rsa root@<domain>
dokku postgres:connect <db>
psql=# SHOW config_file;
-> /var/lib/postgresql/data/postgresql.conf
psql=# \q
docker ps
@monsha
monsha / db.rake
Created June 22, 2018 20:23 — forked from hopsoft/db.rake
Rails rake tasks for dump & restore of PostgreSQL databases
# lib/tasks/db.rake
namespace :db do
desc "Dumps the database to db/APP_NAME.dump"
task :dump => :environment do
cmd = nil
with_config do |app, host, db, user|
cmd = "pg_dump --host #{host} --username #{user} --verbose --clean --no-owner --no-acl --format=c #{db} > #{Rails.root}/db/#{app}.dump"
end
puts cmd
@monsha
monsha / controllers.application.js
Last active July 23, 2019 15:05
compute_helper
import Ember from 'ember';
import { A } from '@ember/array';
import EmberObject, { computed } from '@ember/object';
const { alias } = computed;
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
title: '',
todos: null,
currentItem: null,
@monsha
monsha / json.sql
Last active May 27, 2020 22:36
PostgreSQL JSON cheatsheet
-- return details.approved content as text
select details->>'approved' from events
-- value (json)
select details -> 'title' -> 'value' as value as from events
-- with a specific value
select * from events where details->>'title' = 'some'
select * from events where where details -> 'title' ->> 'value' = 'some'
select * from events where (details-> 'approved' ->> 'value')::boolean is true
@monsha
monsha / pg_corrupted_data.sql
Last active March 13, 2021 00:44
How to find corrupted records in postgresql
#Find the corrupted rows
DO $$
declare
curid INT := 0;
vcontent TEXT;
badid INT;
begin
FOR badid IN SELECT id FROM events LOOP
curid = curid + 1;
@monsha
monsha / benchmark_hashing_algorithms.rb
Last active August 5, 2021 13:33
Benchmarking hashing algorithms with json
require "benchmark"
require "benchmark/memory"
payload = {
"was_changed"=>0,
"store"=>"gp",
"os"=>nil,
"thumbs_down_cnt"=>0,
"manufactorer"=>nil,
"content"=>"The voices are so soothing. I am only six days in, but I feel good about what little I have done and what I am doing. If I remember to do so I will give an update at the end of the month.",