Skip to content

Instantly share code, notes, and snippets.

Nicholas Jakobsen njakobsen

Block or report user

Report or block njakobsen

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@njakobsen
njakobsen / sphinx_indexing.rb
Last active May 19, 2019
Automatic removal of stale Sphinx documents during Realtime Indexing
View sphinx_indexing.rb
# 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 Sep 29, 2016
Poor man's outer join for rails. Just extend ActiveRecord::Base with this beauty, and voila! MyModel.outer_joins(:some_association).ftw!
View gist:6395146
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 Dec 22, 2015
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.
View gist:6393783
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 Oct 12, 2019
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…
View live_database_dump.rb
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
model.association.delete_all performance issue
View association_delete_all_test.rb
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 Nov 18, 2011
Rails 3.1 poor handling of :dependent => :delete_all
View dependent_delete_all_test.rb
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
View gist:162963
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
You can’t perform that action at this time.