Skip to content

Instantly share code, notes, and snippets.

View apolishch's full-sized avatar

Abraham apolishch

  • Babylon Health
  • London
View GitHub Profile
@apolishch
apolishch / package.json
Last active August 13, 2019 15:50
Upstream package.json
"scripts": {
"migrate": "flock-migrate",
"migrate-up": "npm run migrate up",
"migrate-down": "npm run migrate down"
}
@apolishch
apolishch / flock_migrate
Created August 13, 2019 15:46
Migration script
set -eo pipefail
command="up"
input=$(echo "$2" | awk '{print tolower($0)}')
if [ "$input" == "down" ] ; then
command="down"
elif [ "$input" == "create" ] ; then
echo "Please do not attempt to create migrations directly from the server. Check out flock-core-migrations, and do this there!"
@apolishch
apolishch / trigger_and_constraint_tests.sql
Created August 13, 2019 15:25
Advanced Schema Tests
BEGIN;
SELECT plan(2);
INSERT INTO payments(1, 99, 'failed');
PREPARE insert_failure AS
INSERT INTO payments VALUES(10, -7, 'failed');
PREPARE second_failure AS
INSERT INTO policies VALUES(11, 1, 'annual');
@apolishch
apolishch / schema.sql
Last active August 13, 2019 15:07
Schema with triggers
CREATE SEQUENCE IF NOT EXISTS ids
START 1;
CREATE TABLE IF NOT EXISTS payments (
id BIGINT NOT NULL PRIMARY KEY DEFAULT nextval('ids'),
amount BIGINT NOT NULL CHECK((amount > 0) AND (amount <= 100)),
status TEXT DEFAULT 'failed' CONSTRAINT status_validity_check CHECK (type IN ('failed', 'succeeded', 'pending'))
);
@apolishch
apolishch / pgtapschematest.sql
Created August 13, 2019 01:32
PgTap Schema test
BEGIN;
SELECT plan(15);
SELECT columns_are( 'flock_payments', 'payments', ARRAY[
'id',
'policy_id'
]);
SELECT triggers_are( 'flock_payments', 'payments', ARRAY[]::text[]);
SELECT indexes_are( 'flock_payments', 'payments', ARRAY['payments_pkey']);
@apolishch
apolishch / package.json
Created August 13, 2019 01:26
package.json test
"scripts": {
"test": "pg_prove -U $FLOCK_PG_USERNAME -d $FLOCK_PG_DATABASE tests/*.sql tests/**/*.sql local-tests/*.sql"
}
@apolishch
apolishch / package.json
Created August 13, 2019 01:19
package.json bin
"bin": {
"installpgtapci": "bin/installpgtapci"
}
@apolishch
apolishch / installpgtap
Created August 13, 2019 00:55
installpgtap
#!/bin/bash
yes | sudo cpan TAP::Parser::SourceHandler::pgTAP
code=$?
if [[ $code -eq 0 ]] ; then
echo "0"
elif [[ $code -eq 141 ]] ; then
echo "141"
else
@apolishch
apolishch / policy_migration2.js
Last active August 12, 2019 23:22
policy_migration2.js
export const up = (db, callback) => db.runSql(`
ALTER TABLE flock_payments.payments
DROP CONSTRAINT payments_fkey;
ALTER TABLE flock_policies.policies
ALTER COLUMN id TYPE INT;
ALTER TABLE flock_payments.payments
ALTER COLUMN policy_id TYPE INT;
export const up = (db, callback) => db.runSql(`
CREATE TABLE IF NOT EXISTS flock_payments.payments(
id bigint NOT NULL PRIMARY KEY,
policy_id bigint NOT NULL REFERENCES flock_policies.policies (id)
);
`, callback)