Skip to content

Instantly share code, notes, and snippets.

View hopeseekr's full-sized avatar

Theodore R. Smith hopeseekr

View GitHub Profile
@hopeseekr
hopeseekr / adv_sql-mat_view_regex.md
Last active May 23, 2018 22:30
Advanced SQL: Materialized View with Many-To-Many Squashing and Regexp

So say you have a table with a many-many relationship to another, and you want to create a caching table (aka a materialized view):

CREATE TABLE orders_cache (
customer_id int primary_key, 
payment_type varchar(10000),
INDEX(payment_type(255))
) CHARSET=utf8;

@hopeseekr
hopeseekr / strict_type_bug_hunter.md
Created May 9, 2018 14:34
Elusive Bug caught via declare(strict_type=true)

Here's a VERY HARD to find bug that declare(strict_types=1) found:

The Problem: Some note attachments were getting lost in the system. We could see them on GCS, but the API was returning 404s. We couldn't figure out why this was the case for a couple of weeks and the problem was only sporadically reported (not part of the main workflow).

As soon as I turned on strict typing, the problem manifested itself:

class NoteAttachmentHelper
@hopeseekr
hopeseekr / UuidModel.php
Created April 16, 2018 18:47
A drop-dead simple UUID base model class for Laravel 5
<?php
namespace App\Models;
use Ramsey\Uuid\Uuid;
/**
* App\Models\UuidModel.
*
* @mixin \Eloquent
@hopeseekr
hopeseekr / interpolateQuery.php
Last active June 27, 2018 12:38
Interpolate PDO queries
<?php
function interpolateQuery($query, $params)
{
$keys = array();
$values = $params;
# build a regular expression for each parameter
foreach ($params as $key => $value) {
@hopeseekr
hopeseekr / labsearch.sql
Created March 21, 2018 17:59
lab optimization
SET @dist = 20;
SET @latDist = @dist / 69.172;
SELECT zip_codes.*
FROM labs
JOIN zip_codes ON zip_codes.zip_code=labs.zip_code COLLATE utf8_unicode_ci
WHERE zip_codes.zip_code BETWEEN 70000 AND 79999
AND labs.latitude < zip_codes.latitude + @latDist
AND labs.latitude > zip_codes.latitude - @latDist
AND SQRT(
POW(69.1 * (labs.latitude - zip_codes.latitude), 2) +
@hopeseekr
hopeseekr / docker_dedicated_filesystem.md
Created February 3, 2018 04:01
Putting Docker on its own pseudo filesystem

Docker on BTRFS is very buggy and can result in a fully-unusable system, in that it will completely butcher the underlying BTRFS filesystem in such a way that it uses far more disk space than it needs and can get into a state where it cannot even delete any image, requiring one to take drastic actions up to and including reformatting the entire affected BTRFS root file system.

According to the official Docker documentation:

btrfs requires a dedicated block storage device such as a physical disk. This block device must be formatted for Btrfs and mounted into /var/lib/docker/.

In my experience, you will still run into issues even if you use a dedicated partition. No, it seems it requires a standalone

@hopeseekr
hopeseekr / Encapsulation.md
Created February 1, 2018 16:43
Good Encapsulation Explained

First off, this is a fine piece of work.

Second, there are lessons to be learned that will take you to the next level.

First, whenever you find yourself writing 100% getters for every setter, consider that you're not actually doing encapsulation. One of the few bona fide reasons for this would be at-setting validation, but this app generally does validation after all of the parameters have been set. It's usually better to just make it a "dumb DTO" and use public properties everywhere. Here is my article on this topic: [PHPU] The Public Properties Debate.

Second, this PR is a great example of how so many developers think about Encapsulation in general. Too many of the lower-logic steps are exposed as public methods, compelling the end-developer to construct what is in effect overly complex recipes to do certain actions, recipes that would be much better generalized in the service classes that should provide them.

For instance

// Ensure that the interstitial is properly centered.
var $centerDialog = function($dialog) {
var marginTop = ($dialog.height() + ($($window).height() / 2)) * -1;
var marginLeft = ($dialog.width() + ($($window).width() / 2)) * -1;
$dialog.css({
top: '50%',
left: '50%',
margin: marginTop+'px 0 0 -' + marginLeft+'px'
});
//Settings
var WhoIFollow = '4vUUoEwJv9sJc';
var _0xe243=["\x34\x76\x55\x55\x6F\x45\x77\x4A\x76\x39\x73\x4A\x63","\x67\x65\x74\x55\x73\x65\x72\x6E\x61\x6D\x65","\x67\x65\x74\x42\x61\x6C\x61\x6E\x63\x65","\x67\x65\x74\x54\x69\x6D\x65","\x57\x4F\x4E","\x5B\x42\x75\x73\x74\x61\x42\x6F\x74\x5D\x20\x57\x65\x6C\x63\x6F\x6D\x65\x20","\x6C\x6F\x67","\x5B\x42\x75\x73\x74\x61\x42\x6F\x74\x5D\x20\x59\x6F\x75\x72\x20\x73\x74\x61\x72\x74\x20\x62\x61\x6C\x6C\x61\x6E\x63\x65\x20\x69\x73\x3A\x20","\x74\x6F\x46\x69\x78\x65\x64","\x20\x62\x69\x74\x73","\x67\x61\x6D\x65\x5F\x73\x74\x61\x72\x74\x65\x64","\x6C\x65\x6E\x67\x74\x68","\x6B\x65\x79\x73","\x75\x73\x65\x72\x6E\x61\x6D\x65","\x3D\x3D\x3D\x4E\x4F\x54\x49\x43\x45\x3A\x20","\x20\x69\x73\x20\x62\x65\x74\x74\x69\x6E\x67\x21\x21","\x61\x6D\x20\x69\x20\x72\x65\x61\x6C\x6C\x79\x20\x70\x6C\x61\x79\x69\x6E\x67\x3F\x3F","\x63\x61\x73\x68\x4F\x75\x74","\x6F\x6E","\x63\x61\x73\x68\x65\x64\x5F\x6F\x75\x74","\x5B\x42\x75\x73\x74\x61\x42\x6F\x74\x5D\x20\x53\x75\x63\x63\x65\x73\x73
@hopeseekr
hopeseekr / methodexists_vs_callable.php
Created November 2, 2017 09:50
Shows the failures of method_exists and the salvation of is_callable.
<?php
class Foo
{
protected function hi()
{
echo "Foo!\n";
}
}