Skip to content

Instantly share code, notes, and snippets.

View toddsiegel's full-sized avatar

Todd Siegel toddsiegel

  • Solera Software, Inc.
  • Denver, CO
View GitHub Profile
@toddsiegel
toddsiegel / histogram.sql
Last active February 5, 2024 19:00
PostgreSQL Histogram
-- this groups by 5 to keep the length down
select length(username) as length,
count(*) as count,
repeat('*'::text, (COUNT(length(username))::int / 5) + 1) AS histogram
from users
where username is not null
group by length(username)
order by length;
@toddsiegel
toddsiegel / capybara.md
Last active January 11, 2022 16:08
Capybara Cheatsheet

Forms

Fill in inputs by name

# e.g.
# <table>
#   <tbody>
#     <tr>
#      <td><input name="foo" type="text" /></td>
#      <td><input name="bar" type="text" /></td>
# 
@toddsiegel
toddsiegel / cookbook.md
Created January 4, 2022 23:18
Ruby Cookbook

CSVs

Generate a CSV

csv = CSV.generate(headers: true, force_quotes: true) do |row|
  row << ['ID', 'Name',  'Email']
  people.each do |person|
    row << [person.id, person.name, person.email]
  end
end
csv = "#{csv}" # Add BOM if people are going open this in Excel
@toddsiegel
toddsiegel / Elasticsearch Cheatsheet.md
Last active February 9, 2024 16:53
Elasticsearch Cheatsheet

Introspection

List all indicies

The v query params shows the heading in the response

GET /_cat/indices?v

List indicies with wildcard

GET /_cat/indices/profiles*?v
@toddsiegel
toddsiegel / generator.rb
Created September 30, 2021 22:42
Rails Generators: Turn off I don't use
# config/initialers/generators.rb
# This config will not generate the following:
# - per model view helpers, or stylesheets
# - scaffold.scss
# - jbuilder templates, which I only occasionally use
# - controller tests, which I don't really use. I do system tests instead.
Rails.application.config.generators do |g|
@toddsiegel
toddsiegel / aws.md
Last active August 18, 2021 20:30
Cheatsheets

Get object count

$ aws s3 ls s3://<bucketname> --recursive | wc -l

@toddsiegel
toddsiegel / uses_tempfile.rb
Created July 23, 2021 19:28
Ruby and Rails Utiilities
# frozen_string_literal: true
module UsesTempfile
def with_tempfile(name: 'temp-zip-file', temp_dir: Rails.root.join('tmp').to_s, binmode: false)
tempfile = Tempfile.new('temp-zip-file', temp_dir)
tempfile.binmode if binmode
yield tempfile
ensure
tempfile.close
@toddsiegel
toddsiegel / README.md
Created October 28, 2020 20:09 — forked from tristanm/README.md
Migrating a Rails project from MySQL to PostgreSQL

Migrating a Rails project from MySQL to PostgreSQL

This brief guide is written from my own experience with migrating a large (~5GB) MySQL database to PostgreSQL for a Rails project.

No warranties, guarantees, support etc. Use at your own risk and, as always, ENSURE YOU MAKE BACKUPS FIRST!

I chose [pgloader][1] because it's extremely fast. YMMV.

  1. Replace mysql2 gem with pg in Gemfile.
  2. Update config/database.yml for PostgreSQL. I used [Rails' template][2] as a starting point.
@toddsiegel
toddsiegel / download_csv.js
Last active June 24, 2020 21:35
Client-side CSV Download
const exportRows = [
["Name", "Email"],
["Foo Bar", "foo@bar.com"]
];
const csvContent = "data:text/csv;charset=utf-8," + exportRows.map((e) => e.join(",")).join("\n");
const encodedUri = encodeURI(csvContent);
const link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "export.csv"); // https://caniuse.com/#feat=download
@toddsiegel
toddsiegel / LogEntry.java
Last active August 21, 2019 21:29
Ingesting a CSV using OpenCSV
package com.solera.domain;
import com.opencsv.bean.CsvBindByName;
import java.util.Objects;
public class LogEntry {
@CsvBindByName(column = "insertId")
private String insertId;