Skip to content

Instantly share code, notes, and snippets.

View jnjcub's full-sized avatar

j-n-j-cub jnjcub

  • Netflix
  • Los Gatos, CA
View GitHub Profile
Postgress ---
---------
1. Terminate open connection except mine.
SELECT pg_terminate_backend(pg_stat_activity.pid), pg_stat_activity.pid, pg_stat_activity.client_addr, pg_stat_activity.application_name, pg_stat_activity.usename
FROM pg_stat_activity
WHERE datname = current_database()
AND pid <> pg_backend_pid();
@jnjcub
jnjcub / gist:862c89ef6bf3b5830dab4cfcd60b4b63
Created September 17, 2018 17:00
Empty Array with IN generate SQL with 1=0
Orion::Api::V1::Event.where(id: [])
Orion::Api::V1::Event Load (36.5ms) SELECT "events".* FROM "events" WHERE "events"."deleted_at" IS NULL AND 1=0
=> []
@jnjcub
jnjcub / user.sql
Last active July 18, 2023 04:13
Postgresql table with auto increment id , created_at, updated_at
CREATE OR REPLACE FUNCTION trigger_set_timestamp()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
create table users (
id SERIAL PRIMARY KEY NOT NULL,
@jnjcub
jnjcub / gist:f52755b1bd68cbe47cad62913512d366
Last active April 5, 2018 18:27
How to get Instrumentation logs locally using Activesupport Notifications
jjacob~/Code/netflix/orion_api/src $ cat config/initializers/my_per_logger.rb
class InstrumentationLogger
MAX_DURATION = 3.0
def self.initialize!
ActiveSupport::Notifications.subscribe('sql.active_record') do |name, start, finish, id, payload|
duration =( (finish.to_f - start.to_f) * 1000).round(2)
Rails.logger.info("slow query detected: #{payload[:name]}, duration: #{duration}")
end
# you have ctags but it does not work...
$ ctags -R --exclude=.git --exclude=log *
ctags: illegal option -- R
usage: ctags [-BFadtuwvx] [-f tagsfile] file ...
#you need to get new ctags
$ brew install ctags
#alias ctags if you used homebrew
$ alias ctags="`brew --prefix`/bin/ctags"
@jnjcub
jnjcub / tracker.js
Created February 17, 2017 18:41
tracker cp
trackers: computed('model.isLoaded',{
get(key) {
if (get(this, 'model.isLoaded')) {
get(this, 'store').query('tracker', {'kid_id' : get(this, 'model').get('id')}).then((trackers) =>
return set(this, 'trackers', trackers);
});
}
return null;
},
set(key, value) {
find ./ -type f \( -name "*.erb" -o -name "*.rb" -o -name "*.js" \) -exec grep -H "searchstring" {} \;
@jnjcub
jnjcub / gist:7516f1f01c923f6f00f2
Created November 25, 2014 16:44
How to delete stale session from postgres
select pg_terminate_backend(pid) from pg_stat_activity where datname='dbname'
assuming all the sessions have to be terminated.
@jnjcub
jnjcub / gist:5438930
Created April 22, 2013 21:59
recursive_stringify_keys! on Hash
class Hash
def recursive_stringify_keys!
self.each do |key, value|
string_key = key.to_s
unless key.is_a?(String)
self[string_key] = self[key]
self.delete(key)
if self[string_key].is_a?(Hash)
self[string_key] = self[key.to_s].recursive_stringify_keys!
elsif self[string_key].is_a?(Array) #this can be extended to any collection, reverted back because String had :each !
@jnjcub
jnjcub / gist:5159057
Last active December 14, 2015 22:29
Webrat hack to create a method to update form action(or any form attribute)
module Webrat
class Scope
def update_form_attr(id, action)
ele = FormLocator.new(@session, dom, id).locate
ele.update({'action' => action})
end
end
class Form < Element #:nodoc:
def update(attr_hash = {})