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 / asimple.txt
Last active March 11, 2021 19:22
Simple git information
git rm --cache /path/to/file
git commit -am "Remove file"
git push
####
# remove a mistakenly committed file named 'queries.sql'
git reset HEAD queries.sql
git rm --cached queries.sql
# make a new branch and commit data to it
git status && git checkout -b branch-name
@ichux
ichux / accumulates.sql
Last active May 11, 2021 05:42
summary for a transactions table
CREATE TABLE IF NOT EXISTS accumulates (
added_on TIMESTAMP WITHOUT TIME ZONE NOT NULL,
added_by BIGINT NOT NULL,
id BIGSERIAL NOT NULL,
enabled BOOLEAN NOT NULL,
user_id BIGINT NOT NULL,
transaction BIGINT NOT NULL,
tablemeta JSONB NOT NULL,
CONSTRAINT pk_accumulates PRIMARY KEY (id)
--, CONSTRAINT fk_accumulates_user_id_users FOREIGN KEY(user_id) REFERENCES users (id) ON UPDATE CASCADE
@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;
@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 / 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 / generate_series.sql
Last active May 3, 2022 04:56
Postgres generate_series usage
SELECT step.n
FROM generate_series(1, 5) AS step (n),
generate_series(1, 3)
@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 / 1-get-functions.sql
Last active September 19, 2018 09:28
get all the triggers declared on a postgres table
SELECT routine_name FROM information_schema.routines
WHERE routine_type='FUNCTION' AND specific_schema='public' ORDER BY routine_name
@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;