Skip to content

Instantly share code, notes, and snippets.

View njakobsen's full-sized avatar

Nicholas Jakobsen njakobsen

View GitHub Profile
@njakobsen
njakobsen / sphinx_indexing.rb
Last active May 19, 2019 16:50
Automatic removal of stale Sphinx documents during Realtime Indexing
# Improve indexing to remove records whose sphinx document id is stale due to changes to the number/order of indices
module SphinxIndexing
# Delete existing record before inserting new ones. This avoids the need to rebuild the index after adding or removing
# an index. Without this, the old indexed record would not be deleted since Sphinx's calculation of that record's id
# would not match now that the number/order of indices has changed.
module Transcriber
def copy(*instances)
return unless instances.present?
ids = instances.map(&:id).compact
@njakobsen
njakobsen / gist:6395146
Last active September 29, 2016 20:00
Poor man's outer join for rails. Just extend ActiveRecord::Base with this beauty, and voila! MyModel.outer_joins(:some_association).ftw!
module OuterJoins
def outer_joins(*associations)
pattern = / (?:INNER )?JOIN /i
scope = all
associations.each do |association|
inner_join_sql = scope.unscoped.joins(association).to_sql
outer_join_sql = inner_join_sql.gsub(pattern, ' LEFT OUTER JOIN ')
scope = scope.joins(outer_join_sql[/LEFT OUTER JOIN .+/])
end
@njakobsen
njakobsen / gist:6393783
Last active December 22, 2015 01:08
Test performance of ActiveRecord Includes. This is a test script designed to test for a performance regression when using the .includes() method on a scope.
begin
class CreateModels < ActiveRecord::Migration
def change
create_table :items do |t|
end
create_table :events do |t|
t.belongs_to :item
end
@njakobsen
njakobsen / live_database_dump.rb
Last active November 5, 2021 02:28
Live stream a database dump (or any other STDOUT) using Rails 4. Why would you want this? If you have a large database dump and want to avoid storing it in memory as Rails streams it. This allows pipe the dump directly into the http response instead of storing it as a file, sending it, and then deleting it. Let me know what you think! I've teste…
class DatabaseController < ApplicationController
def database_dump
database = Rails.configuration.database_configuration[Rails.env]["database"]
send_file_headers!(:type => 'application/octet-stream', :filename => "#{database}_#{Time.now.to_s(:human)}.backup")
pipe = IO.popen("pg_dump '#{database}' -F c")
stream = response.stream
while (line = pipe.gets)
stream.write line
sleep 0.0001 # HACK: Prevent server instance from sleeping forever if client disconnects during download
@njakobsen
njakobsen / association_delete_all_test.rb
Created May 12, 2012 19:20
model.association.delete_all performance issue
require 'active_record'
require 'logger'
# Print out what version we're running
puts "Active Record #{ActiveRecord::VERSION::STRING}"
# Connect to an in-memory sqlite3 database (more on this in a moment)
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
# Create the minimal database schema necessary to reproduce the bug
@njakobsen
njakobsen / dependent_delete_all_test.rb
Created November 18, 2011 01:46
Rails 3.1 poor handling of :dependent => :delete_all
require 'active_record'
require 'logger'
# Print out what version we're running
puts "Active Record #{ActiveRecord::VERSION::STRING}"
# Connect to an in-memory sqlite3 database (more on this in a moment)
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
# Create the minimal database schema necessary to reproduce the bug
class EntityViewSweeperWorker < Workling::Base
# Expires the view fragments for a particular entity based on options[:type] and options[:id]
def expire_fragment_for_all(options)
expire_fragment(/#{options[:type].tableize}\/#{options[:id]}\/views\//)
end
end