Skip to content

Instantly share code, notes, and snippets.

View nara-l's full-sized avatar

Lawrence Nara nara-l

View GitHub Profile
git reset --hard HEAD~1
This would delete last commit https://stackoverflow.com/a/1338744
@nara-l
nara-l / clean_untracked
Created June 15, 2018 14:48
How to clean untracked git files
# trying to commit without needing to add all those extra files? just clean them
git clean -n -d # to see the files and folders to be deleted first before applying command below
git clean -f -d # -f force clean and -d cleaning directories. git just deletes.
@nara-l
nara-l / empty_postgres_table
Created June 15, 2018 14:57
Postgres empty table using TRUNCATE
# using truncate
TRUNCATE TABLE table_name RESTART IDENTITY; # to reset index counts
TRUNCATE TABlE table_name CASCADE; # to delete foreign key dependencies
@nara-l
nara-l / failed_keys_try_except.py
Last active August 1, 2018 14:20
Get failed keys in Try , except python 2 and 3
# We want to get the keys which failed this could be applied to other situations
# Python 3
dict_of_elements = {1: 'john', 2: 'peter', 3: 'luke', 4: 'mark'} # can equally be a list?
failed_keys = set()
for list in dict_of_elements:
try:
dict_of_elements[9] # trying to access keys that don't exist
except Exception as e:
f_k = e.args[0]
failed_keys.add(f_k)
@nara-l
nara-l / resequencing_id_postgres
Created June 22, 2018 14:40
Resequencing Id's in Postgres
# After an import of data sometimes when you try to create new records, postgres creates same ids and cause clashes from time to time
# To correct the sequence and tell postgres where to start counting next run this
SELECT pg_catalog.setval(pg_get_serial_sequence('table_name', 'id'), MAX(id)) FROM table_name; # replace table_name with required table
@nara-l
nara-l / fixing_merge_before_pull_request
Created July 5, 2018 19:16
Git - Fixing conflict between master and branch before pull request
git fetch origin # gets latest changes made to master
git checkout branch_name # switch to your branch
git merge master # merge with master
# manually resolve any merge conflicts here
git push origin branch_name # push branch and update the pull request
# branch_name is the name of the branch you you have the conflict on
@nara-l
nara-l / highsierra_permissions
Created July 5, 2018 21:08
Homebrew permission issues on MacOS high sierra 10.13.5
Recently upgraded to lattest version of High sierra and had permission issues in trying to upgrade node
sudo chown -R $(whoami) /usr/local # does not work any more
sudo chown -R $(whoami) $(brew --prefix)/* # this works
@nara-l
nara-l / postgres_db_copy
Created July 6, 2018 14:45
How to create copy of local postgres db
CREATE DATABASE newdb WITH TEMPLATE originaldb OWNER dbuser; # newdb and originaldb are names of the new database and old database
# getting an error ?
ERROR: source database "originaldb" is being accessed by other users
# run this query to stop error before running create data base query
SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'originaldb' AND pid <> pg_backend_pid(); # change originaldb with name of old database
@nara-l
nara-l / psycopg_raw_cursor
Last active February 5, 2019 20:22
Database cursor / Postgres cheatsheet
https://stackoverflow.com/questions/32812463/setting-schema-for-all-queries-of-a-connection-in-psycopg2-getting-race-conditi
cheat sheet
http://www.postgresqltutorial.com/postgresql-cheat-sheet/
Postgres setup with django
@nara-l
nara-l / count_distinct
Created July 16, 2018 20:10
A better and faster way to count distinct psycopg2 / python
SELECT COUNT(*) FROM (SELECT DISTINCT column_name FROM table_name) AS temp;