Skip to content

Instantly share code, notes, and snippets.

View Samuyi's full-sized avatar
🎯
Focusing

Samuyi Obasuyi Samuyi

🎯
Focusing
  • Far Away
View GitHub Profile
@Samuyi
Samuyi / nginx.conf
Created January 20, 2019 13:39 — forked from plentz/nginx.conf
Best nginx configuration for improved security(and performance). Complete blog post here http://tautt.com/best-nginx-configuration-for-security/
# to generate your dhparam.pem file, run in the terminal
openssl dhparam -out /etc/nginx/ssl/dhparam.pem 2048
@Samuyi
Samuyi / main_trigger.sql
Created January 8, 2019 16:48
another trigger
CREATE TRIGGER account_audit_trigger
AFTER INSERT OR UPDATE OR DELETE ON account
FOR EACH ROW EXECUTE PROCEDURE account_audit_func();
@Samuyi
Samuyi / audit_trigger.sql
Created January 8, 2019 16:43
a stored function for a trigger
CREATE FUNCTION account_audit_func()
RETURNS TRIGGER
AS $$
BEGIN
IF TG_OP = 'INSERT' THEN
INSERT INTO account_audit (operation, account_id, account_name, debt, balance) VALUES
(TG_OP, NEW.*);
RETURN NEW;
ELSIF TG_OP = 'UPDATE' THEN
INSERT INTO account_audit (operation, account_id, account_name, debt, balance) VALUES
@Samuyi
Samuyi / account_table.sql
Created January 8, 2019 16:39
more tables
CREATE TABLE account (
id serial primary key,
name text,
debt int,
balance int
);
CREATE TABLE account_audit(
id serial primary key,
db_user text NOT NULL default session_user,
@Samuyi
Samuyi / passwd_trigger.sql
Created January 8, 2019 16:35
postgresql trigger
CREATE TRIGGER passwd_trigger BEFORE INSERT OR UPDATE
ON passwd
FOR EACH ROW EXECUTE PROCEDURE passwd_func();
@Samuyi
Samuyi / passwd_func.sql
Created January 8, 2019 16:22
passwd function for my article
CREATE FUNCTION passwd_func()
RETURNS TRIGGER
AS $$
BEGIN
IF length(NEW.password) < 10 OR NEW.password IS NULL THEN
RAISE EXCEPTION 'password cannot be less than 10 characters';
END IF;
IF NEW.NAME IS NULL THEN
RAISE EXCEPTION 'Name cannot be NULL';
END IF;
@Samuyi
Samuyi / passwd_table.sql
Last active January 9, 2019 05:14
passwd sample table for my article
CREATE TABLE passwd (
id serial primary key,
name text,
password text,
position text
);
@Samuyi
Samuyi / car_bonus.sql
Created January 7, 2019 12:16
a sample postgresql function
CREATE FUNCTION car_bonus_func(vehicle_id int, bonus int)
RETURNS cars
AS $$
DECLARE
car cars;
BEGIN
PERFORM model FROM cars WHERE car_id = vehicle_id;
IF NOT FOUND THEN
RAISE EXCEPTION 'car with id of % not found', vehicle_id;
END IF;
@Samuyi
Samuyi / quarterly_summary_func.sql
Last active January 7, 2019 12:14
a postgreql example function for summarizing data
CREATE FUNCTION quarterly_summary_func(start_date date DEFAULT CURRENT_TIMESTAMP)
RETURNS TABLE (staff_name text, staff_bonus int, quarter tsrange)
As $$
DECLARE
employee RECORD;
total_bonus int;
sales_total int;
end_date date := start_date + interval '3 months';
BEGIN
FOR employee IN SELECT staff_id FROM staff LOOP
@Samuyi
Samuyi / tables.sql
Last active January 7, 2019 12:20
tables for a tutorial
CREATE TABLE staff (
staff_id serial primary key,
name text,
salary int,
created_at timestamp with time zone default now()
);
CREATE TABLE cars(
car_id serial primary key,
brand text,