Skip to content

Instantly share code, notes, and snippets.

@pond
pond / painter.html
Created February 8, 2023 02:58
No-framework ES6 "pixel painter" example
<!DOCTYPE html>
<html>
<head>
<title>Painter</title>
<style>
body {
margin: 0;
padding: 0;
background: #eee;
@pond
pond / gist:5eaec0c30c0b4477f234
Last active August 29, 2015 14:05
PostgreSQL "active?" Rails adapter check async patch
# This monkey patches the PostgreSQL adapter to use asynchronous
# communication when doing its "SELECT 1" check for is-connection-active.
# One symptom of this problem is stalled processing queues in Sidekiq.
# See also:
#
# https://github.com/rails/rails/issues/12867
#
# At the time of writing we have seen indefinite blocking down in the C
# library's "poll()" method due to the out-of-box Rails code using blocking
# I/O, in multithreaded environments - one thread gets stuck in the I/O and
@pond
pond / gist:2942792d222e207d82a7
Created May 7, 2014 04:30
AR patch to poll SELECT 1 is-alive query on PostgreSQL in a manner which can be timed out (in theory)
# Is this connection alive and ready for queries?
def active?
begin
conn = @connection
timeout = 5 # seconds
# Now grab a reference to the underlying socket so we know when the
# connection is established
socket = conn.socket_io # AKA IO.for_fd(conn.socket) - same thing
#$ rails new foo
#$ cd foo
# <set up config/database.yml to use Postgres with "pool: 1">
# development:
# adapter: postgresql
# encoding: utf8
# database: foo_development
# pool: 1
# <add "gem 'pg'" to Gemfile>
#$ bundle install
def visit_Arel_Nodes_Between o
if (o.right.children[0].class == Date && o.right.children[1].class == Date)
min = o.right.children[ 0 ]
max = o.right.children[ 1 ] + 1
"(#{visit o.left} >= #{visit min} AND #{visit o.left} < #{visit max})"
else
"#{visit o.left} BETWEEN #{visit o.right}"
end
end
Rails database adapter issue: Postgres vs SQLite and BETWEEN
"Foo.where(:date=>(d..d))" in Postgres => finds records spanning the date
"Foo.where(:date=>(d..d))" in SQLite => finds nothing
This is an inclusive/exclusive ambiguity in BETWEEN which Rails doesn't resolve
1.9.3p392 :010 > reference = YAML::load( reference )
NoMethodError: undefined method `from' for nil:NilClass
from /Users/adh1003/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.14/lib/active_record/relation/query_methods.rb:264:in `build_arel'
from /Users/adh1003/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.14/lib/active_record/relation/query_methods.rb:260:in `arel'
from /Users/adh1003/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.14/lib/active_record/relation/delegation.rb:29:in `respond_to?'
from /Users/adh1003/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/psych/visitors/to_ruby.rb:291:in `init_with'
from /Users/adh1003/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/psych/visitors/to_ruby.rb:284:in `revive'
from /Users/adh1003/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/psych/visitors/to_ruby.rb:219:in `visit_Psych_Nodes_Mapping'
from /Users/adh1003/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/psych/visitors/visitor.rb:15:in `visit'
from /Users/adh1003/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1
pondmac:log adh1003$ irb
1.9.3p392 :004 > {:thing => ("foo" if true)}
=> {:thing=>"foo"}
1.9.3p392 :005 > {:thing => ("foo" if false)}
=> {:thing=>nil}
1.9.3p392 :006 >
pondmac:log adh1003$ rake db:migrate
(in /Users/adh1003/Documents/Work/Hipposoft/TrackRecord/Development/trackrecord)
== CreateHomes: migrating ====================================================
-- create_table(:homes)
NOTICE: CREATE TABLE will create implicit sequence "homes_id_seq" for serial column "homes.id"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "homes_pkey" for table "homes"
-> 0.0236s
== CreateHomes: migrated (0.0237s) ===========================================
pondmac:log adh1003$ rails c
require File.dirname(__FILE__) + '/../test_helper'
class SavedReportTest < ActiveSupport::TestCase
def not_nil( a, msg )
@errors << [ msg, a ] if a.nil?
end
def check( a, b, msg )
@errors << [ msg, a, b ] unless a == b