Skip to content

Instantly share code, notes, and snippets.

@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;
@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_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_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 / introrx.md
Created June 1, 2016 15:56 — forked from staltz/introrx.md
The introduction to Reactive Programming you've been missing
Typhoeus.get("http://google.com", verbose: true)
# Add it to Gemfile
gem 'httplog'
# Install the gem
$ bundle install
# Configure HttpLog to capture the detail we want to know.
HttpLog.configure do |config|
# Enable or disable all logging
config.enabled = true
# You can assign a different logger
config.logger = Logger.new($stdout)
irb(main):023:0> uri = URI('http://www.google.com')
=> #<URI::HTTP http://www.google.com>
irb(main):059:0> Net::HTTP.get(uri)
D, [2018-08-13T22:33:30.030400 #19738] DEBUG -- : [httplog] Connecting: www.google.com:80
D, [2018-08-13T22:33:30.055285 #19738] DEBUG -- : [httplog] Sending: GET http://www.google.com:80/
D, [2018-08-13T22:33:30.055377 #19738] DEBUG -- : [httplog] Header: accept-encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3
D, [2018-08-13T22:33:30.055412 #19738] DEBUG -- : [httplog] Header: accept: */*
D, [2018-08-13T22:33:30.055579 #19738] DEBUG -- : [httplog] Header: user-agent: Ruby
D, [2018-08-13T22:33:30.055605 #19738] DEBUG -- : [httplog] Header: host: www.google.com
D, [2018-08-13T22:33:30.055646 #19738] DEBUG -- : [httplog] Data: