Skip to content

Instantly share code, notes, and snippets.

@bf4
bf4 / db-switch.rb
Created Aug 30, 2021 — forked from matthewd/db-switch.rb
bin/db-switch
View db-switch.rb
#!/usr/bin/env ruby
require(Dir.pwd + "/config/environment")
ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
ActiveRecord::Base.establish_connection(db_config.config)
context = ActiveRecord::Base.connection.migration_context
missing_migrations = []
@bf4
bf4 / ruby_learning.md
Last active Jul 17, 2021
Some Ruby Learning Resources
View ruby_learning.md
@bf4
bf4 / bin-squash_migrations
Created Nov 12, 2015
Squash Rails migrations in this one easy step
View bin-squash_migrations
#!/usr/bin/env bash
git ls-files db/migrate/*.rb | sort | tail -1 | \
ruby -e "schema_version=STDIN.read[/\d+/]; init_schema=%(db/migrate/#{schema_version}_init_schema.rb);
%x(git rm -f db/migrate/*.rb;
mkdir db/migrate;
git mv db/schema.rb #{init_schema};
rake db:migrate;
git add db/schema.rb; git commit -m 'Squashed migrations')"
@bf4
bf4 / db.rake
Created Nov 2, 2020
Rails 6.0 multi-db extensions for migration tasks
View db.rake
require "active_record"
databases = ActiveRecord::Tasks::DatabaseTasks.setup_initial_database_yaml
db_namespace = namespace :db do
if Rails.version.start_with?("6.0")
# https://github.com/rails/rails/pull/38770/files
namespace :rollback do
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
@bf4
bf4 / polymorphic_many_to_many_in_rails.md
Last active Oct 22, 2020
a polymorphic many-to-many association in Rails
View polymorphic_many_to_many_in_rails.md

This was my solution for a polymorphic many-to-many association

class ItemCountry < ActiveRecord::Base
  belongs_to :locatable, :polymorphic => true
  belongs_to :country
  # fields are :locatable_id, :locatable_type, :country_id
end

class Title < ActiveRecord::Base
  has_many :countries, :through => :item_countries, :as => :locatable
View logfmt.rb
# Based on logfmt:
# https://www.brandur.org/logfmt
# For more complete implementation see:
# see https://github.com/arachnid-cb/logfmtr/blob/master/lib/logfmtr/base.rb
# For usage see:
# https://blog.codeship.com/logfmt-a-log-format-thats-easy-to-read-and-write/
# https://engineering.heroku.com/blogs/2014-09-05-hutils-explore-your-structured-data-logs/
# For Ruby parser see:
# https://github.com/cyberdelia/logfmt-ruby
@bf4
bf4 / Gemfile
Created Jul 2, 2013
Rails lograge and logstash request logging
View Gemfile
gem 'lograge' # more readable logs
gem 'logstash-event' # for logstash json format
gem 'mono_logger' # threadsafe logging
@bf4
bf4 / dump_memcache.rb
Created Aug 2, 2019 — forked from BalmanRawat/dump_memcache.rb
Dumping and restoring the memcache key values using telnet & dalli gem
View dump_memcache.rb
#!/usr/bin/env ruby
# List all keys stored in memcache.
# Credit to Graham King at http://www.darkcoding.net/software/memcached-list-all-keys/ for the original article on how to get the data from memcache in the first place.
require 'net/telnet'
require 'csv'
headings = %w(id expires bytes key)
rows = []
@bf4
bf4 / live_database_dump.rb
Last active Jun 19, 2019 — forked from njakobsen/live_database_dump.rb
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.read(1024)) # per https://gist.github.com/njakobsen/6257887#gistcomment-1238467
stream.write line
Thread.pass # per https://gist.github.com/njakobsen/6257887#gistcomment-1235792