Skip to content

Instantly share code, notes, and snippets.

View jedprentice's full-sized avatar

Jed Prentice jedprentice

View GitHub Profile
@jedprentice
jedprentice / jed.cnf
Last active October 25, 2019 15:31
MySQL 8 configuration; on Ubuntu this is included by my.cnf, so some of the most basic settings are not present. Values in this file override those in my.cnf.
[mysqld]
# Slow Log
#slow_query_log = 1
#log_queries_not_using_indexes = 1
#long_query_time = 1
# Allow GROUP BY to work like it did before MySQL 5.7.5, for Rails 3.2
# compatibility, i.e., remove ONLY_FULL_GROUP_BY from the default
sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
@jedprentice
jedprentice / gist:4e3dfba017b0b4d29ef3102bee1f1069
Created October 18, 2018 21:22
How to find out which query is holding a lock in MySQL
mysql> SELECT r.trx_id waiting_trx_id, r.trx_mysql_thread_id waiting_thread, r.trx_query waiting_query,
b.trx_id blocking_trx_id, b.trx_mysql_thread_id blocking_thread, b.trx_query blocking_query
FROM information_schema.innodb_lock_waits w
INNER JOIN information_schema.innodb_trx b ON b.trx_id = w.blocking_trx_id
INNER JOIN information_schema.innodb_trx r ON r.trx_id = w.requesting_trx_id\G
#!/usr/bin/env ruby
# Don't remove devops containers that might be databases
def database_container?(line)
line.match?(/postgres/) || line.match?(/redis/)
end
# docker ps -f "before=containerId" | grep expression > /var/tmp/containers.txt
File.open('/var/tmp/containers.txt').each do |line|
next if database_container?(line)
@jedprentice
jedprentice / export_objects_to_csv.rb
Created April 13, 2022 15:12
Exports Active Record objects to CSV
# Copy to Rails console and call with a class parameter. Assumes
# /var/tmp exists and is writable
require 'csv'
def export_objects(klass)
objects = klass.last(10000)
CSV.open("/var/tmp/#{klass}.csv", 'w') do |row|
row << klass.attribute_names
objects.each do |o|
row << o.attributes.values