Skip to content

Instantly share code, notes, and snippets.

# Add it to Gemfile
gem 'httplog'
Typhoeus.get("http://google.com", verbose: true)
@jimytc
jimytc / introrx.md
Created June 1, 2016 15:56 — forked from staltz/introrx.md
The introduction to Reactive Programming you've been missing
@jimytc
jimytc / upsert_with_insert_left_join.sql
Created March 24, 2016 16:35
Use INSERT with LEFT JOIN to achieve UPSERT
INSERT INTO creation_metadata (creation_id, category)
SELECT creations.id, creations.type
FROM creations
LEFT JOIN creation_metadata
ON creations.id = creation_metadata.creation_id
WHERE creations.type IS NOT NULL
AND creation_metadata.category IS NULL
@jimytc
jimytc / upsert_using_plpgsql.sql
Created March 24, 2016 16:33
Use PL/PGSQL to do UPSERT
CREATE OR REPLACE FUNCTION upsert(target_id INT, type_value TEXT) RETURNS VOID AS $$
BEGIN
-- Try update first
UPDATE creation_metadata SET category = type_value
WHERE creation_id = target_id;
-- Return if UPDATE command runs successfully
IF FOUND THEN
RETURN;
END IF;
-- Since there's no record in creation_metada
@jimytc
jimytc / upsert_using_with_queries.sql
Created March 24, 2016 16:27
Use WITH queries to do UPSERT
WITH creations_need_move AS (
SELECT id, type
FROM creations
WHERE type IS NOT NULL
),
update_existing_metadata AS (
UPDATE creation_metadata (creation_id, category)
SET category = source.type
FROM creations_need_move source
WHERE creation_id = source.id
@jimytc
jimytc / upsert_sample.sql
Created March 24, 2016 14:41
PostgreSQL 9.5+ only, UPSERT command for moving data from one table to another
INSERT INTO creation_metadata (creation_id, category)
SELECT id, type
FROM creations
ON CONFLICT DO UPDATE SET category = EXCLUDED.type;