Skip to content

Instantly share code, notes, and snippets.

View ichux's full-sized avatar
🏠
Working from home

Chukwudi Nwachukwu ichux

🏠
Working from home
View GitHub Profile
@ichux
ichux / celery.sh
Created October 7, 2017 08:44 — forked from amatellanes/celery.sh
Celery handy commands
/* Useful celery config.
app = Celery('tasks',
broker='redis://localhost:6379',
backend='redis://localhost:6379')
app.conf.update(
CELERY_TASK_RESULT_EXPIRES=3600,
CELERY_QUEUES=(
Queue('default', routing_key='tasks.#'),
@ichux
ichux / list_public_tables.sql
Last active September 15, 2018 07:04
List all the tables in Postgres public schema
CREATE OR REPLACE FUNCTION public.list_public_tables()
RETURNS TABLE(name character varying)
LANGUAGE plpgsql
AS $function$
BEGIN
FOR name IN SELECT quote_ident(table_name) FROM information_schema.tables WHERE table_schema = 'public' LOOP
RETURN NEXT;
END LOOP;
END;
@ichux
ichux / postgres-after-insert.sql
Last active September 15, 2018 07:10
postgres after insert
BEGIN;
DROP TRIGGER IF EXISTS accumulate_ait
on accumulate;
DROP FUNCTION IF EXISTS adapt_to_service();
DROP TABLE IF EXISTS accumulate CASCADE;
CREATE TABLE IF NOT EXISTS accumulate (
id bigint NOT NULL,
monies bigint,
@ichux
ichux / slugify.sql
Last active September 16, 2018 07:17
generating slugs in postgres
BEGIN;
DROP FUNCTION IF EXISTS slugify CASCADE;
DROP TRIGGER IF EXISTS slugify
ON talks;
DROP TABLE IF EXISTS talks CASCADE;
CREATE
OR REPLACE FUNCTION slugify(intake TEXT)
RETURNS TEXT AS
@ichux
ichux / postgres-array.sql
Created September 17, 2018 05:35
demo arrays in postgres
SELECT
( n % 2 ) :: TEXT,
ARRAY_AGG ( n )
FROM
generate_series ( 1, 10 ) n
GROUP BY
( n % 2 ) :: TEXT;
@ichux
ichux / dropdb.sql
Created September 18, 2018 09:37
Drop DB in Postgres
-- Terminate any existing connections to `thedb`
SELECT
pg_terminate_backend(pg_stat_activity.pid)
FROM
pg_stat_activity
WHERE
pg_stat_activity.datname = 'thedb'
AND pid <> pg_backend_pid();
-- Now go ahead and drop the database, `thedb`
@ichux
ichux / rowtojson.sql
Created September 18, 2018 09:41
Converts Posgres rows to json
SELECT
row_to_json(details)
FROM
(
SELECT
ARRAY[ 1,
2,
3 ] as arrays,
'Some information' AS text
) details
@ichux
ichux / date-calculations.sql
Created September 18, 2018 10:40
add and subtract Postgres dates
select date '2018-09-18' - date '1990-11-22' AS difference;
select date '1990-11-22' + 10162 AS difference;
@ichux
ichux / play-with-char.sql
Created September 18, 2018 10:57
playing with Postgre characters
SELECT ASCII('<'), CHR(60), regexp_split_to_array(md5(random() :: TEXT), '') AS value
FROM generate_series(1, 3)
ORDER BY random()
@ichux
ichux / function.sql
Last active September 18, 2018 19:43
sharding id at instagram
-- create the needed sequence
CREATE SEQUENCE table_id_seq;
-- change the public to schema of choice
CREATE OR REPLACE FUNCTION public.next_id(OUT result bigint) AS $$
DECLARE
our_epoch bigint := 1314220021721;
seq_id bigint;
now_millis bigint;
shard_id int := 5;