Skip to content

Instantly share code, notes, and snippets.

@ruckus
ruckus / gist:7295501
Created November 3, 2013 22:15
Monitor PG slave lag
-- SELECT * FROM streaming_slave_check()
CREATE OR REPLACE FUNCTION streaming_slave_check() RETURNS TABLE (client_hostname text, client_addr inet, byte_lag float)
LANGUAGE SQL SECURITY DEFINER
AS $$
SELECT
client_hostname,
client_addr,
sent_offset - (replay_offset - (sent_xlog - replay_xlog) * 255 * 16 ^ 6 ) AS byte_lag
FROM (
@ruckus
ruckus / gist:6464900
Created September 6, 2013 14:50
mod_rewrite rule to block a bare POST /
# Block POSTS to /
RewriteBase /
RewriteCond %{REQUEST_METHOD} POST [NC]
RewriteRule ^$ - [F]
@ruckus
ruckus / gist:5819398
Created June 20, 2013 00:36
nginx: proxy files from S3. Let's masking the URL by pass Expires timestamp as e and Signature as st. For instance the basis URL is https://yourbucket.s3.amazonaws.com/readme.txt?AWSAccessKeyId=YOURONLYACCESSKEY&Signature=sagw4gsafdhsd&Expires=3453445231 Source: https://coderwall.com/p/rlguog
location ~* ^/proxy_private_file/(.*) {
set $s3_bucket 'your_bucket.s3.amazonaws.com';
set $aws_access_key 'AWSAccessKeyId=YOUR_ONLY_ACCESS_KEY';
set $url_expires 'Expires=$arg_e';
set $url_signature 'Signature=$arg_st';
set $url_full '$1?$aws_access_key&$url_expires&$url_signature';
proxy_http_version 1.1;
proxy_set_header Host $s3_bucket;
proxy_set_header Authorization '';
module HstoreAccessor
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def hstore_accessor(hstore_attribute, *keys)
Array(keys).flatten.each do |key|
define_method("#{key}=") do |value|
send("#{hstore_attribute}=", (send(hstore_attribute) || {}).merge(key.to_s => value))
@ruckus
ruckus / RecommendationsController.php
Last active December 14, 2015 19:19
RecommendationsController example beforeFilter usage
<?php
class RecommendationsController extends AppController {
private $recommendation;
private $actions_that_require_a_recommendation = array('show', 'edit');
private $actions_that_require_ownership = array('show', 'edit');
function beforeFilter() {
if (in_array($this->action, $actions_that_require_a_recommendation)) {
@ruckus
ruckus / gist:4055780
Created November 11, 2012 18:23
Redis config
# Redis configuration file example
# Note on units: when memory size is needed, it is possible to specify
# it in the usual form of 1k 5GB 4M and so forth:
#
# 1k => 1000 bytes
# 1kb => 1024 bytes
# 1m => 1000000 bytes
# 1mb => 1024*1024 bytes
# 1g => 1000000000 bytes
@ruckus
ruckus / 20120925150949_create_foo_table
Created September 25, 2012 22:38
Sample SQL migration file
// Generate the timestamp for the filename from the command line like:
// php -r "echo date('YmdHis', time());"
UP
--
CREATE TABLE foo (
id int(11) PRIMARY KEY AUTO_INCREMENT,
first_name varchar(64)
);
#import "Account.h"
#import "JSONKit.h"
@implementation Account
@synthesize name = _name;
@synthesize accountId = _accountId;
@synthesize initial = _initial;
@synthesize code = _code;
CREATE EXTENSION unaccent;
CREATE TEXT SEARCH CONFIGURATION vinosmith ( COPY = english );
ALTER TEXT SEARCH CONFIGURATION vinosmith ALTER MAPPING FOR hword, hword_part, word WITH unaccent, english_stem;
CREATE TRIGGER contacts_search_content_trigger BEFORE INSERT OR UPDATE
ON contacts FOR EACH ROW EXECUTE PROCEDURE
tsvector_update_trigger(search_content, 'public.vinosmith', title, name, phone, fax, email, mobile_phone);
SELECT varietals.*, ts_rank_cd(varietals.search_content, plainto_tsquery('vinosmith', 'gruner')) as rank
FROM varietals WHERE varietals.search_content @@ plainto_tsquery('vinosmith', 'gruner')
ORDER BY rank DESC limit 25
=> [#<Varietal id: 657, name: "Grüner Veltliner" .. ]