Skip to content

Instantly share code, notes, and snippets.

View aks's full-sized avatar
💭
Working

Alan Stebbens aks

💭
Working
View GitHub Profile
@aks
aks / trigger-ci
Last active May 11, 2020 16:04
Script to trigger CircleCI builds
#!/usr/bin/env bash
# Adapted from https://discuss.circleci.com/t/branch-not-building/30499/10
# Usage:
#
# 1. set \$CIRCLE_CLI_TOKEN or set token in ~/.circle/cli.yml
# 2. run `./trigger-ci [[<account>] [[<oroject>] [<branch>]]]`
#
# Examples to trigger builds in different ways:
#
# trigger-ci # on the current branch
@aks
aks / tst.sh
Created November 22, 2018 18:34
Exploration of bash interactivity, SHLVL and `-t 1` values in various forms of invocation
#!/usr/bin/env bash
case "$-" in
*i*) echo 'interactive' ;;
*) echo 'non-interactive' ;;
esac
echo "SHLVL = $SHLVL"
[[ -t 1 ]] && echo "tty STDOUT? : true" || echo "tty STDOUT? : false"
@aks
aks / setup-pgpass.sh
Last active October 25, 2018 22:49
Script to setup `~/.pgpass` from `~/.pgpass.copy` or `~postgres/.pgpass`
#!/bin/bash
# setup-pgpass
# execute (or source) this script to ensure that ~/.pgpass has the most recent credentials
#
# if ~/.pgpass.copy exists, it is used as a source; otherwise, compare against
# ~postgres/.pgpass, and create ~/.pgpass and ~/.pgpass.copy from
# ~postgres/.pgpass
MY_PGPASS=~/.pgpass
PG_PGPASS=~postgres/.pgpass
@aks
aks / clear-sidekiq-jobs.sh
Created August 9, 2018 19:39 — forked from wbotelhos/clear-sidekiq-jobs.sh
Clear Sidekiq Jobs
# 1. Clear retry set
Sidekiq::RetrySet.new.clear
# 2. Clear scheduled jobs
Sidekiq::ScheduledSet.new.clear
# 3. Clear 'Processed' and 'Failed' jobs
@aks
aks / migrations
Created May 25, 2018 04:20
Self-contained ruby script to manage rails migration versions directly: insert, remove, check, match, or names by versions or patterns
#!/usr/bin/env ruby
# migrations insert VERSION
# migrations remove VERSION
# migrations check VERSION [VERSION2]
# migrations match VER_PATTERN
# migrations names NAME_PATTERN
# migrations status [ up | down ]
#
# NOTE: This script relies on 'psql` to access and query the database, which means that DB access can be
# configured entirely with PGUSER, PGHOST, PGDATABASE, and PGPORT.
@aks
aks / create-read-only-user.sql
Created April 24, 2018 21:57
Create read-only SQL user
-- create a read-only user
create role procore_ro;
-- allow the RO user to use the public schema
grant usage on schema public to procore_ro;
-- grant RO access on existing tables
grant select on all tables in schema public to procore_ro;
-- grant RO access on future tables
@aks
aks / create-lock-monitor-view.sql
Created April 12, 2018 22:44
Create a view of locks in a postgresql database
CREATE VIEW lock_monitor AS(
SELECT COALESCE(blockingl.relation::regclass::text,blockingl.locktype) AS locked_item
, now() - blockeda.query_start AS waiting_duration
, blockeda.pid AS blocked_pid
, blockeda.query AS blocked_query
, blockedl.mode AS blocked_mode
, blockinga.pid AS blocking_pid
, blockinga.query AS blocking_query
, blockingl.mode AS blocking_mode
FROM pg_catalog.pg_locks blockedl
-- quickly select the estimated row counts, using reltuples, across all tables
-- sort the results by descending size, and table names alphabetically
--
-- This relies on the table having been recently analyzed.
SELECT relname as "Table"
, to_char(reltuples::bigint, '9,999,999,999,999') AS "~Rows"
FROM pg_class
WHERE relname NOT LIKE 'pg%'
AND relkind = 'r'
ORDER BY 2 DESC, 1;
@aks
aks / dtree
Created March 16, 2018 17:11
Little bash script around a one-liner that produces a tree listing of a directory
#!/bin/sh
# dtree DIR
#
ls -R $* | grep ':$' | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'
exit
@aks
aks / null-logs
Created March 16, 2018 15:02
Bash script to zero out all the log files in `~/log`. Useful for Rails apps.
#!/usr/bin/env bash
if [[ -d log ]] ; then
set -- log
while (( $# > 0 )) ; do
file="$1"
shift
if [[ -f $file && -s $file ]] ; then
case "$file" in
*.log|*.txt|*.lst)
echo " $file"