Skip to content

Instantly share code, notes, and snippets.

@Himstar8
Himstar8 / disable_signals.py
Created August 8, 2022 08:12 — forked from bruce-shi/disable_signals.py
Temporarily disable all signals in django.
from collections import defaultdict
from django.db.models.signals import *
class DisableSignals(object):
def __init__(self, disabled_signals=None):
self.stashed_signals = defaultdict(list)
self.disabled_signals = disabled_signals or [
pre_init, post_init,
pre_save, post_save,
@Himstar8
Himstar8 / psql-with-gzip-cheatsheet.sh
Created March 16, 2022 15:19 — forked from brock/psql-with-gzip-cheatsheet.sh
Exporting and Importing Postgres Databases using gzip
# This is just a cheat sheet:
# On production
sudo -u postgres pg_dump database | gzip -9 > database.sql.gz
# On local
scp -C production:~/database.sql.gz
dropdb database && createdb database
gunzip < database.sql.gz | psql database
@Himstar8
Himstar8 / pg_extract.sh
Created January 13, 2022 14:33 — forked from brock/pg_extract.sh
Extract all databases (or one by name) from a sql file created by pg_dumpall
#!/bin/bash
# extract all postgres databases from a sql file created by pg_dumpall
# this script outputs one .sql file for each database in the original .sql file
# unless you pass the name of a database in the dump
if [ $# -lt 1 ]
then
echo "Usage: $0 <postgresql sql dump> [dbname]" >&2
exit 1
fi
# creating and testing permissions and test groups in django tests.
from django.contrib.auth.models import User, Permission, Group
from django.test import TestCase
from django.test import Client
class ExampleGroupPermissionsTests(TestCase):
def setUp(self):
#create permissions group
@Himstar8
Himstar8 / reload_varnish.sh
Created August 10, 2020 08:29
Use varnishadm to reload varnish config
#!/bin/bash
TIME=$(date +%s)
varnishadm vcl.load r_$TIME /etc/varnish/default.vcl
varnishadm vcl.use r_$TIME
CREATE OR REPLACE FUNCTION array_sort (ANYARRAY)
RETURNS ANYARRAY LANGUAGE SQL
AS $$
SELECT ARRAY(SELECT unnest($1) ORDER BY 1)
$$;
DROP FUNCTION IF EXISTS anyarray_uniq(anyarray);
CREATE OR REPLACE FUNCTION anyarray_uniq(with_array anyarray)
RETURNS anyarray AS
$BODY$
DECLARE
-- The variable used to track iteration over "with_array".
loop_offset integer;
-- The array to be returned by this function.
return_array with_array%TYPE := '{}';
def main():
key_path = os.path.join(settings.INTERNAL_FILES, 'crm2', 'service_account_secrets.json')
credentials = get_api_credentials(key_path=key_path)
if not credentials:
logger.info("No credential found, so stopping")
return
active_users = User.objects.filter(is_active=True)
@Himstar8
Himstar8 / slugify.sql
Created January 7, 2020 10:41 — forked from kez/slugify.sql
Generating Slugs in Postgres
CREATE EXTENSION IF NOT EXISTS "unaccent"
CREATE OR REPLACE FUNCTION slugify("value" TEXT)
RETURNS TEXT AS $$
-- removes accents (diacritic signs) from a given string --
WITH "unaccented" AS (
SELECT unaccent("value") AS "value"
),
-- lowercases the string
"lowercase" AS (