Skip to content

Instantly share code, notes, and snippets.

@isaacs
isaacs / node-and-npm-in-30-seconds.sh
Last active May 16, 2024 16:51
Use one of these techniques to install node and npm without having to sudo. Discussed in more detail at http://joyeur.com/2010/12/10/installing-node-and-npm/ Note: npm >=0.3 is *safer* when using sudo.
echo 'export PATH=$HOME/local/bin:$PATH' >> ~/.bashrc
. ~/.bashrc
mkdir ~/local
mkdir ~/node-latest-install
cd ~/node-latest-install
curl http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1
./configure --prefix=~/local
make install # ok, fine, this step probably takes more than 30 seconds...
curl https://www.npmjs.org/install.sh | sh
@killercup
killercup / pandoc.css
Created July 3, 2013 11:31
Add this to your Pandoc HTML documents using `--css pandoc.css` to make them look more awesome. (Tested with Markdown and LaTeX.)
/*
* I add this to html files generated with pandoc.
*/
html {
font-size: 100%;
overflow-y: scroll;
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
}
@mateuszwenus
mateuszwenus / save_restore_dependencies.sql
Last active April 11, 2024 05:49
PostgreSQL: How to handle table and view dependencies
create table deps_saved_ddl
(
deps_id serial primary key,
deps_view_schema varchar(255),
deps_view_name varchar(255),
deps_ddl_to_run text
);
create or replace function deps_save_and_drop_dependencies(p_view_schema varchar, p_view_name varchar) returns void as
$$
@steinwaywhw
steinwaywhw / One Liner to Download the Latest Release from Github Repo.md
Last active June 7, 2024 09:39
One Liner to Download the Latest Release from Github Repo
  • Use curl to get the JSON response for the latest release
  • Use grep to find the line containing file URL
  • Use cut and tr to extract the URL
  • Use wget to download it
curl -s https://api.github.com/repos/jgm/pandoc/releases/latest \
| grep "browser_download_url.*deb" \
| cut -d : -f 2,3 \
| tr -d \" \
@adewes
adewes / README.md
Last active June 10, 2024 16:33
Borg Backup - Shell script to create regular backups (can e.g. be called hourly by cron).

Borg Backup Script

This is a little helper script to regulary back up your data using the Borg backup tool. I wrote this as I was frustrated with Deja-Dup, which does not allow me to exclude directories by pattern, hence I ended up with backups that were very large and took a really long time to create because they contained many directories with non-essential files (e.g. node modules or Python virtual environments). Borg backup is a simple tool that offers everything that Deja-Dup does and is easier to customize.

  • Put the script in your home folder and make it executable.
  • Call it regularly using e.g. cron (put e.g. "0 * * * * [your username] /home/[your username]/make_backup.sh" in your /etc/crontab - this will call the script every hour). Please note that the script will perform its own checking to see
@Checksum
Checksum / postgresql-event-trigger.sql
Created August 1, 2018 07:20
Automatically create column on new tables using PostgreSQL event triggers
-- Function to automatically create a user_id column for new tables with name starting with user_
create or replace function create_user_id_column()
returns event_trigger
language plpgsql volatile as
$$
declare
obj record;
identity text[];
begin
for obj in select * from pg_event_trigger_ddl_commands()
@lukeramsden
lukeramsden / midString.sql
Created June 24, 2020 08:01
PL/pgSQL implementation of a LexoRank-style lexicographical rank generator function
-- https://stackoverflow.com/questions/38923376/return-a-new-string-that-sorts-between-two-given-strings
create or replace function app_public.mid_string(prev text, next text) returns text as $$
declare
v_p int;
v_n int;
v_pos int := 0;
v_str text;
begin
LOOP -- find leftmost non-matching character
v_p := CASE WHEN v_pos < char_length(prev) THEN ascii(substring(prev from v_pos + 1)) ELSE 96 END;
@Aitem
Aitem / jsonb_helper.sql
Last active December 16, 2022 18:46
PostreSQL JSONB helpers functions
-- jsonb_select_keys take jsonb and keys and return jsonb
-- contains only given keys
create or REPLACE function jsonb_select_keys (resource jsonb, keys text[] ) returns jsonb
as $$
select jsonb_object_agg(k, v)
from
(select unnest(keys) k ) k ,
lateral (select resource->k.k as v) t;
$$ LANGUAGE SQL
IMMUTABLE;
@ziazon
ziazon / add_trigger_to_table.sql
Created August 26, 2020 18:11
an audit log for data changes - postgresql
# 'schema' aka 'public' and 'table_name' aka the table name you wish to add audit logging to
DROP TRIGGER schema_table_name_audit_logging ON schema.table_name;
CREATE TRIGGER table_name_audit_logging
AFTER INSERT OR UPDATE OR DELETE
ON schema.table_name
FOR EACH ROW
EXECUTE PROCEDURE logging.log_data_changes();
@joelonsql
joelonsql / guess_language.sql
Created May 22, 2021 15:08
PostgreSQL guess_language(text) one-liner SQL-function
CREATE OR REPLACE FUNCTION guess_language(string text)
RETURNS text
LANGUAGE sql
AS
$$
SELECT cfgname::text FROM pg_ts_config WHERE cfgname <> 'simple' ORDER BY length(to_tsvector(cfgname::regconfig,string)) LIMIT 1
$$;
--
-- Demo testing some random sentences from Wikipedia: