Skip to content

Instantly share code, notes, and snippets.

View raven-rock's full-sized avatar

Levi Gillette raven-rock

View GitHub Profile
@raven-rock
raven-rock / trap_error.bash
Created July 18, 2019 20:46
trap error in bash and always execute some desired code
#!/bin/bash
# Credit: https://nickjanetakis.com/blog/executing-code-after-an-error-occurs-with-bash-when-using-set-e
# Note: does not work for zsh.
set -e
do_something() {
echo "This will always execute"
}
@raven-rock
raven-rock / bitbucket2github.zsh
Last active July 18, 2019 07:39
Move a BitBucket repository to a new GitHub repository
#!/usr/bin/env zsh
# example run: ./bitbucket2github.sh cool-project ~/code johndoe-bitbucketname johndoe-githubname true
#
# To step thru, use `brew install zshdb`, then `zshdb ./bitbucket2github.sh`, then `s`, `s`, etc...
set -ex
PROJECT_NAME=$1
REPOS_DIR=${2:-$(pwd)}
@raven-rock
raven-rock / mysql_change_character_set_collation.sql
Created July 5, 2019 17:26
Change MySQL character set and collation of existing table
-- UTF8mb4, case insensitive
ALTER TABLE my_table CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- Latin1, Swedish case insensitive
ALTER TABLE my_table CONVERT TO CHARACTER SET latin1 COLLATE latin1_swedish_ci;
@raven-rock
raven-rock / README.md
Last active June 21, 2019 19:53
MySQL template for CREATE TABLE

Considerations

  • Create DB auditing columns that take care of setting themselves.
  • Add comments to columns and tables. These can be seen with the SHOW FULL COLUMNS foos command as well as in the table schema view of many SQL GUIs. Note that they do not appear in a DESC foos, which a provides subset of the information in the former command.
  • For modern, full UTF-8 support, use character set utf8mb4 and collation utf8mb4_unicode_ci. The default settings character settings are established by variables at the database and/or connection level, so if these are dumb you need to set up utf8mb4 for every table you create. Also note that different CLI and and GUI tools may use different character settings.
  • Especially for small tables, create covering indexes for the common lookups so that queries can read the needed data straight out of index, saving the trip to fetch the data from the table.
@raven-rock
raven-rock / myuuid.rb
Created June 21, 2019 13:49
Experimenting with making more compact, chronologically-sortable, database-friendly unique identifiers similar to UUID v1
#!/usr/bin/env ruby
require 'radix'
require 'securerandom'
require 'macaddr'
class MyUUID
BASE = [*'0'..'9'] + [*'A'..'Z'] + [*'a'..'z']
MAC_ADDRESS = Mac.addr.gsub(/:/, '')
@raven-rock
raven-rock / clojure-shell-analogs.md
Last active June 20, 2019 03:50
Clojure-Shell Analogs
Clojure Shell Notes
butlast sed '$d'
clojure.string/lower-case tr A-Z a-z
clojure.string/upper-case tr a-z A-Z
count wc -l
distinct perl -ne 'print unless $seen{$_}++' Note that Clojure's distinct behavior
@raven-rock
raven-rock / etl.rb
Created June 19, 2019 22:36
Example of ETL with Ruby
#!/usr/bin/env ruby
require "table_print"
STRINGY_NULL_REPRESENTATIONS = [
/\ANULL\z/i,
/\AN\/A\z/i, # User N/A
/\A\z/, # empty string
/\A#N\/A\z/ # Excel #N/A ("not applicable")
]
@raven-rock
raven-rock / git-sync
Created May 24, 2019 18:41
Foundation of a simple, git-based, Dropbox-like tool
#!/usr/bin/env bash
changes=`git status --porcelain`;
git pull
if [[ -n $changes ]]; then
echo "$changes"
git add . &&
git commit -m "Automatic commit at `date`" -m "$changes" &&
@raven-rock
raven-rock / tmux.conf
Created March 4, 2019 17:11
Levi's tmux.conf
# spawn new windows/panes w/ reattach-to-user-namespace so all commands can interact with macOS GUI
# set-option -g default-command "reattach-to-user-namespace zsh"
# set-option -g default-command "/home/levi/bin/run-tmux-with-zsh.sh"
set-option -g default-command "/bin/zsh -l"
# window switching with page up/down (doesn't work):
# bind-key -n C-PPage previous-window
# bind-key -n C-NPage next-window
# didnt work:
# bind C-PPage previous-window
@raven-rock
raven-rock / normalize-matrix-dimensions-example.rb
Created February 16, 2019 18:45
Normalize a matrix (aka pivot table) of data to prepare to insert it into a normalized table
require 'date'
# Simulate delimited data parsed from a file. In this example
# dimension #1 (d1) is the OpEx (Operating Expenditure), and dimension
# #2 (d2) is the balance_date of the period, as if this came from from
# a financial planning speadsheet that was exported to CSV.
matrix_raw_data = [
%w[ OpEx 1/1/2019 2/1/2019 3/1/2019 ], # header row
%w[ Wages 1 2 3 ],
%w[ Parties 4 5 6 ],