Skip to content

Instantly share code, notes, and snippets.

View jonpemby's full-sized avatar

Jonathon Pemberton jonpemby

  • Chewy, Inc
  • Salem, MA
View GitHub Profile
@jonpemby
jonpemby / get_required.sql
Last active March 15, 2018 18:22
MySQL find all required columns from table
select * from information_schema.COLUMNS
where TABLE_SCHEMA = 'schema'
and TABLE_NAME = 'table' -- optionally specify the table
and IS_NULLABLE = 'NO' -- optionally set to 'YES' to find nullable columns
and COLUMN_DEFAULT is null; -- optionally specify columns without a default
@jonpemby
jonpemby / scandir.lua
Created March 6, 2018 02:14
Lua scandir implementation
-- Lua implementation of PHP scandir function
-- special thanks to:
-- rhoster (https://stackoverflow.com/users/974053/rhoster)
-- ulmangt (https://stackoverflow.com/users/1212363/ulmangt)
function scandir(directory)
local i, t, popen = 0, {}, io.popen
local pfile = popen('ls -a "'..directory..'"')
for filename in pfile:lines() do
i = i + 1
t[i] = filename
@jonpemby
jonpemby / date_to_input_value.js
Created March 5, 2018 19:00
Format a JavaScript date for HTML5 "date" input value
document.querySelector('.datepicker').value = (new Date()).toISOString().substr(0, 10);
@jonpemby
jonpemby / random_string_from_list.sql
Created March 5, 2018 15:33
PostgreSQL random string from list
select (array['hello', 'world'])[trunc(1 + random() * @len)];
@jonpemby
jonpemby / random_string_from_list.sql
Last active March 7, 2018 16:19
MySQL random string from list
select elt(floor(1 + (rand() * @len)), "hello", "world", "this", "is", "a", "random", "list");
@jonpemby
jonpemby / recursive_comments.sql
Created March 3, 2018 17:35
Recursive comments in PostgreSQL
with recursive co (id, parent_id, body) as
(select c.id, c.parent_id, c.body
from comments c
where c.id = 1
union all
select c.id, c.parent_id, c.body
from co
join comments c
on c.parent_id = co.id)
select * from co;
@jonpemby
jonpemby / substr_last.sql
Created March 1, 2018 17:16
Find last part of a string after delimiter in SQL
SELECT SUBSTRING_INDEX("first_middle_last", '_', -1);
@jonpemby
jonpemby / theme.info.yml
Created February 4, 2018 20:33
Theme info yml file for Drupal 8
name: Fluffiness
type: theme
description: 'A cuddly theme that offers extra fluffiness.'
core: 8.x
libraries:
- fluffiness/global-styling
base theme: classy
regions:
header: Header
content: Content
@jonpemby
jonpemby / selectEnd.es6.js
Last active February 2, 2018 17:15
Select end of text in a contenteditable.
/**
* Selects the end of the text in an element. Useful for contenteditable elements.
* @param {HTMLElement} target
* @return {void}
*
* note: This function does not support browsers < IE9
*/
export default (target) => {
const range = document.createRange();
range.selectNodeContents(target);
@jonpemby
jonpemby / new-drupal-project.sh
Last active January 21, 2018 19:22
Starting a new Drupal project with Docker (SQLite)
#!/bin/bash
# Change directory to where you want to create your project.
# Composer's create-project command creates a project with
# a particular vendor package as the base into a provided directory
# You can change the project variable to the
# preferred folder name for your project
export project="project-name"
composer create-project drupal/drupal $project