Skip to content

Instantly share code, notes, and snippets.

View mattlong's full-sized avatar
🌊

Matt Long mattlong

🌊
  • Medplum
  • San Francisco, CA
View GitHub Profile
@mattlong
mattlong / rails_5_dirty.md
Created November 18, 2019 19:36 — forked from crivotz/rails_dirty.md
Rails 5.2 dirty

Rails dirty

After modifying an object and after saving to the database, or within after_save:

Rails <= 5 Rails >= 5.1
attribute_changed? saved_change_to_attribute?
changed? saved_changes?
changes saved_changes
attribute_was attribute_before_last_save
@mattlong
mattlong / kill-defunct.sh
Created April 23, 2019 18:41
Kill defunct proccesses
#!/bin/bash
parents_of_dead_kids=$(ps -ef | grep [d]efunct | awk '{print $3}' | sort | uniq | egrep -v '^1$'); echo "$parents_of_dead_kids" | xargs kill
@mattlong
mattlong / organization.rb
Last active November 15, 2018 16:38
Rails / ActiveRecord has_one and has_many to same model
# From https://github.com/rails/rails/issues/20606#issuecomment-113323102
# Almost good, but I don't think it handles the case of promoting an
# existing location to being the primary location correctly in some cases
class Organization < ActiveRecord::Base
has_many :locations, dependent: :destroy, autosave: true # autosave necessary for the importer
has_one :primary_location, -> { where(locations: { primary: true }) }, class_name: "Location", autosave: true
# Override getter to fix issue with Rails not reloading the primary_location after resetting it to nil
def primary_location
@mattlong
mattlong / rails-jsonb-queries
Created September 24, 2018 05:28 — forked from mankind/rails-jsonb-queries
Rails-5 postgresql-9.6 jsonb queries
http://stackoverflow.com/questions/22667401/postgres-json-data-type-rails-query
http://stackoverflow.com/questions/40702813/query-on-postgres-json-array-field-in-rails
#payload: [{"kind"=>"person"}]
Segment.where("payload @> ?", [{kind: "person"}].to_json)
#data: {"interest"=>["music", "movies", "programming"]}
Segment.where("data @> ?", {"interest": ["music", "movies", "programming"]}.to_json)
Segment.where("data #>> '{interest, 1}' = 'movies' ")
Segment.where("jsonb_array_length(data->'interest') > 1")
@mattlong
mattlong / where_is.rb
Created August 27, 2018 23:37 — forked from wtaysom/where_is.rb
A little Ruby module for finding the source location where class and methods are defined.
module Where
class <<self
attr_accessor :editor
def is_proc(proc)
source_location(proc)
end
def is_method(klass, method_name)
source_location(klass.method(method_name))
/**
* Retrieves all the rows in the active spreadsheet that contain data and logs the
* values for each row.
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/
function readRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();

Effective Engineer - Notes

What's an Effective Engineer?

  • They are the people who get things done. Effective Engineers produce results.

Adopt the Right Mindsets

@mattlong
mattlong / pg.sql
Last active June 29, 2021 01:51
Useful Postgres Queries
-- active connections
SELECT application_name, state, age(now(), xact_start) age, query FROM pg_stat_activity WHERE state <> 'idle';
SELECT application_name, pid, state, age(now(), xact_start) age,
left(regexp_replace(query,E'[\\n\\r]+',' ','g'), 100)
FROM pg_stat_activity WHERE state <> 'idle' ORDER BY age;
-- bloated tables
select relname, n_tup_ins, n_tup_upd, n_tup_del, n_tup_hot_upd, n_live_tup,
n_dead_tup, round(n_dead_tup::numeric/n_live_tup, 2) as bloat,
@mattlong
mattlong / models.py
Last active June 27, 2017 23:27
Altering UUID column types
from __future__ import print_function
from sqlalchemy import (
Column,
create_engine,
MetaData,
)
from sqlalchemy.types import String, Integer
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
@mattlong
mattlong / gist:d5c97c3829a24a19dcef1e65e00c37c1
Created September 28, 2016 00:20
Purge git history of old files
# From http://stackoverflow.com/questions/17901588/new-repo-with-copied-history-of-only-currently-tracked-files
Delete everything and restore what you want
Rather than delete this-list-of-files one at a time, do the almost-opposite, delete everything and just restore the files you want to keep:
$ git checkout master
$ git ls-files > keep-these.txt
$ git filter-branch --force --index-filter \
"git rm --ignore-unmatch --cached -qr . ; \