Skip to content

Instantly share code, notes, and snippets.

View travispett's full-sized avatar

Travis Pett travispett

View GitHub Profile
@travispett
travispett / time_hack_benchmark.rb
Created May 28, 2019 14:02
Ruby 2.3.1 - Ruby 1.9+ raises an exception when marshaling time pre-1900. This benchmark tests different monkey-patch solutions.
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'benchmark-ips', '2.7.2'
end
require 'benchmark/ips'
require 'time'
JS_PATH = "app/assets/javascripts/**/*.js";
Dir[JS_PATH].each do |file_name|
puts "\n#{file_name}"
puts Uglifier.compile(File.read(file_name))
end
@travispett
travispett / decrypting_encrypted_field.rb
Last active August 21, 2017 19:33
We needed to decrypt an attribute that was encrypted with attr_encrypted 1.x and then switched to insecure mode when upgrading to 3.x. Here is how to do that cleanly with minimal downtime on Rails 4.2.x
# When you have a field encrypted using attr_encrypted on a model,
# attr_encrypted hijacks the readers and writers to delegate them to
# its encrypt/decrypt methods.
# It makes it slightly more difficult to unencrypt the values
# while leaving the column name the same.
# This example uses a Monkey class with an encrypted attribute named 'banana',
# but the code is almost identical to the Account class this was pulled from.
# The end goal is an unencrypted banana.
@travispett
travispett / attr_encrypted_update.rb
Last active August 21, 2017 18:09
Example of how to upgrade attr_encrypted 1.x to 3.x and update the key from a weaker key to stronger key in a Rails app (4.2.7.1) - In theory this way minimizes downtime as much as possible
# When you update attr_encrypted to 3.x, step 1 is CRITICAL
# and must be deployed at the same time of the gem update.
# The first two steps are not order-dependent but the first step
# depends on the gem version bump and the gem version bump depends on it.
# This example uses a Monkey class,
# but the code is almost identical to the Account class this was pulled from.
# *** 1 ***
# Update the column in use to insecure mode - backwards compatible
@travispett
travispett / empty_columns.sql
Created August 8, 2017 13:49
Find empty columns in postgres
SELECT schemaname, tablename, attname
FROM pg_stats
WHERE most_common_vals IS NULL
AND most_common_freqs IS NULL
AND histogram_bounds IS NULL
AND correlation IS NULL
AND null_frac = 1;
system("gem install 'benchmark-ips'")
system("gem install 'benchmark-memory'")
require "benchmark/ips"
require "benchmark/memory"
class Foo
def initialize(stuff)
self.stuff = stuff
end
@travispett
travispett / nightmare-on-amazon-linux.MD
Last active April 12, 2017 12:31 — forked from dimkir/nightmare-on-amazon-linux.MD
How to run nightmare on Amazon Linux

Running nightmare on Amazon Linux

You may have thought of running nightmare on AWS Lambda. But before we can run it on Lambda, we need first to make it run on Amazon Linux.

Provision instance which replicates Lambda environment

According to AWS Documentation on Lambda Execution Environment and available Libraries we would need this AMI image with this alias amzn-ami-hvm-2016.03.3.x86_64-gp2. Keep in mind that AMI-image-id for this instance would be different in different regions (eg):

  • In eu-west-1 - ami-f9dd458a
  • In us-east-1 - ami-6869aa05